HomeServer/lualib-src/Server-main/AllServer/GameServer/Lottery/Design.lua
2024-11-20 15:41:37 +08:00

425 lines
18 KiB
Lua

local skynet = require "skynet"
local oo = require "Class"
local log = require "Log"
local pb = require "pb"
local dataType = require "DataType"
local errorInfo = require "ErrorInfo"
local shop = require "Shop"
local Design = oo.class(shop)
Design.lotteryType = dataType.LotteryType_LongTime
Design.LotteryType_1= 1 --初级家具卡池
Design.LotteryType_2= 2 --高级家具卡池
Design.LotteryType_3= 3 --限时家具卡池
Design.TicketType_1= 1 --高级设计稿
Design.TicketType_2= 2 --初级设计稿
Design.TicketType_3= 3 --限时设计稿
Design.goldLotteryId = 0
Design.advancedLotteryId = 0
Design.limitedLotteryId = 0
Design.LotteryTimes_One = 1 --抽一次
Design.LotteryTimes_Ten = 2 --抽十次
--初始化数据
function Design:InitData( player , lotteryType )
end
--设计抽奖展示
function Design:Show( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SDesignShow", c2sData.data ))
local data = {}
if not player:IsUnlockSystem( dataType.UnlockSystem_DesignHouse ) then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
local design = player.gameData.design
local cfgRaffle = skynet.server.gameConfig.Raffle
data.limitedLotteryId = 0
for k, v in pairs(cfgRaffle) do
--目前初始和高级卡池不会变,只有限时卡池可能会根据时间来变
if self.LotteryType_1 == v.raffleType then
self.goldLotteryId = v.id
elseif self.LotteryType_2 == v.raffleType then
self.advancedLotteryId = v.id
end
if "" ~= v.startTime and "" ~= v.endTime then
local startTime = skynet.server.common:GetTime( v.startTime )
local endTime = skynet.server.common:GetTime( v.endTime )
if self.LotteryType_3 == v.raffleType and skynet.GetTime() >= startTime and skynet.GetTime() <= endTime then
self.limitedLotteryId = v.id
end
end
end
data.goldLotteryId = self.goldLotteryId
data.advancedLotteryId = self.advancedLotteryId
data.limitedLotteryId = self.limitedLotteryId
data.designCurrency = self:GetTicket( player )
data.basicFreeTime = design.basicFreeTime
data.advancedFreeTime = design.advancedFreeTime
data.isBuyStorePack8 = design.isBuyStorePack8
end
s2cData.cmd = pb.enum("MsgType","CMD_S2C_DesignShow")
s2cData.data = assert(pb.encode("S2CDesignShow", data))
end
--设计抽奖展示
function Design:Lottery( player , c2sData , s2cData )
c2sData.data = assert(pb.decode("C2SDesignLottery", c2sData.data ))
local data = {}
local lotteryType = c2sData.data.lotteryType
local lotteryTimes = c2sData.data.lotteryTimes
if not lotteryType or not lotteryTimes or lotteryType < self.LotteryType_1 or lotteryType > self.LotteryType_3 or lotteryTimes < self.LotteryTimes_One or lotteryTimes > self.LotteryTimes_Ten then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
elseif self.LotteryType_3 == lotteryType and 0 == self.limitedLotteryId then
s2cData.code = errorInfo.ErrorCode.ActivityClosed
elseif not player:IsUnlockSystem( dataType.UnlockSystem_DesignHouse ) then
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
else
local design = player.gameData.design
local moneyType,moneyCount = 0,0
local reduceCount = 0 --扣除券数量
if self.LotteryTimes_One == lotteryTimes then --抽一次
reduceCount = 1
local cfgSValue = skynet.server.gameConfig.SValue
if self.LotteryType_1 == lotteryType and skynet.GetTime() >= design.basicFreeTime then
reduceCount = 0
design.basicFreeTime = skynet.GetTime() + (cfgSValue.goldRaffleTypeFreeTime * 60 ) --1天后再领
end
if self.LotteryType_2 == lotteryType and skynet.GetTime() >= design.advancedFreeTime then
reduceCount = 0
design.advancedFreeTime = skynet.GetTime() + (cfgSValue.voluteRaffleTypeFreeTime * 60 ) --1天后再领
end
elseif self.LotteryTimes_Ten == lotteryTimes then --抽十次
reduceCount = 10
end
log.info(string.format("玩家 %d 设计间 抽奖类型 %d 抽奖次数 %d 扣除前 金币数量 %d 蜗壳数量 %d 券数量 %d %d %d", player.userId , lotteryType,reduceCount, player.gameData.coin , player.gameData.volute ,design.ticket1 , design.ticket2 , design.ticket3 ))
--开始计算扣除货币数量
local isEnoughMoney = false
local lotteryId = 0
if self.LotteryType_1 == lotteryType then --初级家具卡池
if design.ticket1 - reduceCount >= 0 then
design.ticket1 = design.ticket1 - reduceCount
isEnoughMoney = true
end
lotteryId = self.goldLotteryId
elseif self.LotteryType_2 == lotteryType then --高级家具卡池
if design.ticket2 - reduceCount >= 0 then
design.ticket2 = design.ticket2 - reduceCount
isEnoughMoney = true
end
lotteryId = self.advancedLotteryId
elseif self.LotteryType_3 == lotteryType then --限时家具卡池
if design.ticket3 - reduceCount >= 0 then
design.ticket3 = design.ticket3 - reduceCount
isEnoughMoney = true
end
lotteryId = self.limitedLotteryId
end
if not isEnoughMoney then
s2cData.code = errorInfo.ErrorCode.NoEnoughMoney
else
log.info(string.format("玩家 %d 设计间 扣除券数量 %d 扣除后 金币数量 %d 蜗壳数量 %d 券数量 %d %d %d", player.userId , reduceCount , player.gameData.coin , player.gameData.volute , design.ticket1 , design.ticket2 , design.ticket3 ))
local goods1 = self:GetFurniture( player , lotteryId , dataType.FurnitureQuality_Common ) --普通家具
local goods2 = self:GetFurniture( player , lotteryId , dataType.FurnitureQuality_Special ) --稀有家具
local goods3 = self:GetFurniture( player , lotteryId , dataType.FurnitureQuality_Rare ) --罕见家具
log.info(string.format("玩家 %d 设计间 商品数量 %d %d %d", player.userId , #goods1 , #goods2 , #goods3))
--开始抽奖
data.rewards = {}
local goodsType = 0
local goodsId = 0
if self.LotteryTimes_One == lotteryTimes then --抽一次
goodsType,goodsId = self:StartLottery( player , lotteryType , false , goods1 , goods2 , goods3 )
local goodsInfo = { goodsType = goodsType , goodsId = goodsId , goodsCount = 1 }
--local isNewGoods = not player:IsBuyGoods( goodsType , goodsId ) --不存在就是新的
local isNewGoods = not skynet.server.illustration:IsExistGoods( player , goodsType , goodsId ) --不存在就是新的
table.insert( data.rewards ,{ item = goodsInfo , isNew = isNewGoods } )
if self.LotteryType_1 == lotteryType then
skynet.server.msgTips:Reduce(player , 34)
elseif self.LotteryType_2 == lotteryType then
skynet.server.msgTips:Reduce(player , 31)
end
elseif self.LotteryTimes_Ten == lotteryTimes then --抽十次
local rand = math.random(1,10) --10连抽必出一个未拥有的罕见套间家具
for i = 1, 10, 1 do
if rand == i and ( self.LotteryType_2 == lotteryType or self.LotteryType_3 == lotteryType ) then --只有高级和限时卡池才有保底
goodsType,goodsId = self:StartLottery( player , lotteryType , true , goods1 , goods2 , goods3 )
local goodsInfo = { goodsType = goodsType , goodsId = goodsId , goodsCount = 1 }
--local isNewGoods = not player:IsBuyGoods( goodsType , goodsId ) --不存在就是新的
local isNewGoods = not skynet.server.illustration:IsExistGoods( player , goodsType , goodsId ) --不存在就是新的
table.insert( data.rewards ,{ item = goodsInfo , isNew = isNewGoods } )
else
goodsType,goodsId = self:StartLottery( player , lotteryType , false , goods1 , goods2 , goods3 )
local goodsInfo = { goodsType = goodsType , goodsId = goodsId , goodsCount = 1 }
--local isNewGoods = not player:IsBuyGoods( goodsType , goodsId ) --不存在就是新的
local isNewGoods = not skynet.server.illustration:IsExistGoods( player , goodsType , goodsId ) --不存在就是新的
table.insert( data.rewards ,{ item = goodsInfo , isNew = isNewGoods } )
end
if dataType.GoodsType_Decorate == goodsType then
self:DeleteAlreadyDec( goodsType , goodsId , goods1 , goods2 , goods3 )
end
end
skynet.server.achieveTask:Modify( player , 38 , 1)
end
--发放奖励
for k, v in pairs( data.rewards ) do
log.info(string.format("玩家 %d 设计间 发放奖励 商品类型 %d 商品ID %d 商品数量 %d", player.userId , v.item.goodsType , v.item.goodsId , v.item.goodsCount ))
skynet.server.bag:AddGoods( player , v.item.goodsType , v.item.goodsId , v.item.goodsCount )
end
end
end
data.lotteryType = lotteryType
data.designCurrency = self:GetTicket( player )
s2cData.cmd = pb.enum("MsgType","CMD_S2C_DesignLottery")
s2cData.data = assert(pb.encode("S2CDesignLottery", data))
end
--开始抽奖
function Design:StartLottery( player , lotteryType , isTenFloor , goods1 , goods2 , goods3 )
local cfgSValue = skynet.server.gameConfig.SValue
local design = player.gameData.design
local tenMustCount = cfgSValue.voluteRaffleRareNewGua --十连抽奖必出数量
--获取抽取概率
local rate = {} --1-普通 2-稀有 3-罕见
if self.LotteryType_1 == lotteryType then
rate[1],rate[2],rate[3] = cfgSValue.goldRaffleFurnitureRatio[2] * 100,cfgSValue.goldRaffleFurnitureRatio[1] * 100,0
skynet.server.passCheck:Modify( player , 9 , 1 )
elseif self.LotteryType_2 == lotteryType or self.LotteryType_3 == lotteryType then
if self.LotteryType_2 == lotteryType then
skynet.server.levelTask:Modify( player , 48 , 1 )
skynet.server.passCheck:Modify( player , 10 , 1 )
end
rate[1],rate[2],rate[3] = cfgSValue.voluteRaffleFurnitureRatio[3] * 100,cfgSValue.voluteRaffleFurnitureRatio[2] * 100,cfgSValue.voluteRaffleFurnitureRatio[1] * 100
end
rate[1],rate[2],rate[3] = math.floor(rate[1]),math.floor(rate[2]),math.floor(rate[3]) --取个整
local allRate = 0 --统计总概率
for k, v in pairs( rate ) do
allRate = allRate + v
end
local rand = math.random(1,allRate) --随机概率
local randIndex = 0 --对应概率索引
local allCount = 0
for k, v in pairs( rate ) do
allCount = allCount + v
if rand <= allCount then
randIndex = k
break
end
end
log.info(string.format("玩家 %d 设计间 概率区间【1, %d】 分别抽取概率 %d %d %d 随机值 %d 商品品质 %d", player.userId , allRate , rate[1],rate[2],rate[3] ,rand ,randIndex))
local goodsType = 0
local goodsId = 0
if isTenFloor then
--开启保底
local noBuyGoods = {} --把未购买的家具和装修选出来
for k, v in pairs( goods3 ) do
if v.isSuit and not player:IsBuyGoods( v.type , v.id ) then
table.insert( noBuyGoods , v )
end
end
--确实都是购买了的,那么只随机家具
if 0 == #noBuyGoods then
for k, v in pairs( goods3 ) do
--if v.isSuit and dataType.GoodsType_Furniture == v.type then
if v.isSuit then
table.insert( noBuyGoods , v )
end
end
end
goodsType,goodsId = self:GetRandOneGoods( noBuyGoods )
else
--不保底
if dataType.FurnitureQuality_Common == randIndex then
goodsType,goodsId = self:GetRandOneGoods( goods1 )
elseif dataType.FurnitureQuality_Special == randIndex then
goodsType,goodsId = self:GetRandOneGoods( goods2 )
elseif dataType.FurnitureQuality_Rare == randIndex then
goodsType,goodsId = self:GetRandOneGoods( goods3 )
end
end
design.curLotteryCount = design.curLotteryCount + 1
skynet.server.levelTask:Modify( player , 37 , 1 )
skynet.server.achieveTask:Modify( player , 37 , 1)
return goodsType,goodsId
end
--通行证充值
function Design:Pay( player , storeId )
if 8 == storeId then
local design = player.gameData.design
design.isBuyStorePack8 = true
end
--[[
if not design.isBuyStorePack8 then
local curCfgStorePack = nil
local cfgStorePack = skynet.server.gameConfig.StorePack
for k, v in pairs(cfgStorePack) do
if storeId == v.id then
local startTime = skynet.server.common:GetTime( v.startTime )
local endTime = skynet.server.common:GetTime( v.endTime )
if skynet.GetTime() >= startTime and skynet.GetTime() <= endTime then
curCfgStorePack = v --获取当前通行证ID
break
end
end
end
if curCfgStorePack then
local rewardId = curCfgStorePack.rewardId
player:GiveReward( rewardId )
design.isBuyStorePack8 = true
end
end
]]
end
--获取家具
function Design:GetFurniture( player , lotteryId , quality )
local cfgSValue = skynet.server.gameConfig.SValue
local goods = {} --家具和装修
local cfgFur = {}
local cfgDec = {}
local cfgRaffle = skynet.server.gameConfig:GetCurCfg( "Raffle" , lotteryId )
if dataType.FurnitureQuality_Common == quality then
cfgFur = cfgRaffle.ordinaryFurId
cfgDec = cfgRaffle.ordinaryDecId
elseif dataType.FurnitureQuality_Special == quality then
cfgFur = cfgRaffle.rarityFurId
cfgDec = cfgRaffle.rarityDecId
elseif dataType.FurnitureQuality_Rare == quality and 0 ~= self.limitedLotteryId then
cfgFur = cfgRaffle.rareFurId
cfgDec = cfgRaffle.rareDecId
end
--获取家具
for k, v in pairs( cfgFur ) do
table.insert( goods , { type = dataType.GoodsType_Furniture , id = v , isSuit = false })
end
--获取装修
for k, v in pairs( cfgDec ) do
if not player:IsBuyGoods( dataType.GoodsType_Decorate , v ) then
--未购买的装修才加入进来
table.insert( goods , { type = dataType.GoodsType_Decorate , id = v , isSuit = false })
end
end
--套装家具
local cfgAllFurniture = skynet.server.gameConfig.Furniture
for k1, v1 in pairs( cfgRaffle.suitId ) do
for k2, v2 in pairs(cfgAllFurniture) do
if v1 == v2.suitType and quality == v2.quality then
table.insert( goods , { type = dataType.GoodsType_Furniture , id = v2.id , isSuit = true })
end
end
end
--套装装修
local cfgAllDecoration = skynet.server.gameConfig.Decoration
for k1, v1 in pairs( cfgRaffle.suitId ) do
for k2, v2 in pairs(cfgAllDecoration) do
if v1 == v2.suitType and quality == v2.quality and not player:IsBuyGoods( dataType.GoodsType_Decorate , v2.id ) then
table.insert( goods , { type = dataType.GoodsType_Decorate , id = v2.id , isSuit = true })
end
end
end
return goods
end
--获取券信息
function Design:GetTicket( player )
local design = player.gameData.design
local data = {}
data.basicBluePrint = design.ticket1
data.advancedBluePrint = design.ticket2
data.limitedBluePrint = design.ticket3
return data
end
--获取不重复的商品
function Design:GetRandOneGoods( cfgAllGoods)
local randList = {}
local randIndex = nil
local newGoods = {}
local whileCount = 0
local goodsType = 0
local goodsId = 0
while true do
if 0 == #cfgAllGoods then
break
end
randIndex = math.random( 1, #cfgAllGoods)
if not randList[ randIndex ] then
--不存在新的列表就添加进去
goodsType = cfgAllGoods[ randIndex ].type
goodsId = cfgAllGoods[ randIndex ].id
randList[ randIndex ] = true
end
--找够了数据或者循环了1000次就退出
if 0 ~= goodsType or whileCount >= 1000 then
break
end
whileCount = whileCount + 1
end
return goodsType,goodsId
end
--删除已购买的装修
function Design:DeleteAlreadyDec( goodsType , goodsId , goods1 , goods2 , goods3 )
for k, v in pairs(goods1) do
if goodsType == v.type and goodsId == v.id then
table.remove( goods1 , k)
break
end
end
for k, v in pairs(goods2) do
if goodsType == v.type and goodsId == v.id then
table.remove( goods2 , k)
break
end
end
for k, v in pairs(goods3) do
if goodsType == v.type and goodsId == v.id then
table.remove( goods3 , k)
break
end
end
end
skynet.server.design = Design
return Design