153 lines
5.4 KiB
Lua
153 lines
5.4 KiB
Lua
|
|
local skynet = require "skynet"
|
||
|
|
local oo = require "Class"
|
||
|
|
local pb = require "pb"
|
||
|
|
local log = require "Log"
|
||
|
|
local errorInfo = require "ErrorInfo"
|
||
|
|
local dataType = require "DataType"
|
||
|
|
local GiftBox = oo.class()
|
||
|
|
|
||
|
|
GiftBox.Status_NoGet = 1 --无法领取
|
||
|
|
GiftBox.Status_CanGet = 2 --能领取
|
||
|
|
GiftBox.Status_AlreadyGet = 3 --已经领取
|
||
|
|
|
||
|
|
function GiftBox:Init()
|
||
|
|
end
|
||
|
|
|
||
|
|
--礼盒展示
|
||
|
|
function GiftBox:Show( player , c2sData , s2cData )
|
||
|
|
c2sData.data = assert(pb.decode("C2SGiftBoxShow", c2sData.data ))
|
||
|
|
local data = {}
|
||
|
|
data.adCount = player.gameData.todayGain.adCount
|
||
|
|
data.boxInfo = {}
|
||
|
|
|
||
|
|
for k, v in pairs( player.gameData.giftBox ) do
|
||
|
|
table.insert( data.boxInfo , { id = v.id , status = v.status })
|
||
|
|
end
|
||
|
|
|
||
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_GiftBoxShow")
|
||
|
|
s2cData.data = assert(pb.encode("S2CGiftBoxShow", data))
|
||
|
|
end
|
||
|
|
|
||
|
|
--礼盒领奖
|
||
|
|
function GiftBox:Prize( player , c2sData , s2cData )
|
||
|
|
c2sData.data = assert(pb.decode("C2SGiftBoxPrize", c2sData.data ))
|
||
|
|
local id = c2sData.data.id
|
||
|
|
local data = {}
|
||
|
|
data.id = id
|
||
|
|
data.boxInfo = {}
|
||
|
|
data.award = {}
|
||
|
|
|
||
|
|
if not id then
|
||
|
|
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
||
|
|
else
|
||
|
|
local isSuc = false
|
||
|
|
for k, v in pairs( player.gameData.giftBox ) do
|
||
|
|
if id == v.id and self.Status_CanGet == v.status then
|
||
|
|
isSuc = self:GivePrize( player , id , data)
|
||
|
|
v.status = self.Status_AlreadyGet
|
||
|
|
end
|
||
|
|
table.insert( data.boxInfo , { id = v.id , status = v.status })
|
||
|
|
end
|
||
|
|
|
||
|
|
if not isSuc then
|
||
|
|
s2cData.code = errorInfo.ErrorCode.GetGoodsFailed
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
s2cData.cmd = pb.enum("MsgType","CMD_S2C_GiftBoxPrize")
|
||
|
|
s2cData.data = assert(pb.encode("S2CGiftBoxPrize", data))
|
||
|
|
end
|
||
|
|
|
||
|
|
--检查有不有新的礼包
|
||
|
|
function GiftBox:CheckReward( player )
|
||
|
|
local cfgSValue= skynet.server.gameConfig.SValue
|
||
|
|
local adCount =player.gameData.todayGain.adCount
|
||
|
|
|
||
|
|
for k, v in pairs( player.gameData.giftBox ) do
|
||
|
|
--当前观看的广告数量大于指定礼包的数量就可以领取
|
||
|
|
if self.Status_NoGet == v.status and adCount >= cfgSValue.adTimes_PayBack[ v.id ] then
|
||
|
|
v.status = self.Status_CanGet
|
||
|
|
log.info(string.format("玩家 %d 礼盒 可获得礼包ID %d " , player.basicInfo.userID , v.id ))
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
skynet.server.msgTips:Reset( player , 2 )
|
||
|
|
for k, v in pairs( player.gameData.giftBox ) do
|
||
|
|
if self.Status_CanGet == v.status then
|
||
|
|
skynet.server.msgTips:Add( player , 2 )
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
--发奖
|
||
|
|
function GiftBox:GivePrize( player , id , data )
|
||
|
|
local cfgGift= skynet.server.gameConfig.Gift
|
||
|
|
local level = player.gameData.level
|
||
|
|
|
||
|
|
local furniture = {}
|
||
|
|
for k1, v1 in pairs( cfgGift ) do
|
||
|
|
if id == v1.id then
|
||
|
|
--奖金
|
||
|
|
player:MoneyChange( dataType.MoneyType_Map , v1.diamond )
|
||
|
|
log.info(string.format("玩家 %d 礼盒 获得礼包ID %d 四叶草 %d" , player.basicInfo.userID , v1.id , v1.diamond ))
|
||
|
|
|
||
|
|
--根据规则找相应的礼包
|
||
|
|
local allGoods = {}
|
||
|
|
for k2, v2 in pairs( v1.furnitureType ) do
|
||
|
|
local cfgFurniture = {}
|
||
|
|
for k3, v3 in pairs( skynet.server.gameConfig.Furniture ) do
|
||
|
|
if v2 == v3.subType then
|
||
|
|
table.insert( cfgFurniture , v3)
|
||
|
|
end
|
||
|
|
end
|
||
|
|
for k3, v3 in pairs( v1.purchaseType) do
|
||
|
|
for k4, v4 in pairs( cfgFurniture ) do
|
||
|
|
if v4.purchaseType == v3 and v4.level <= level + 1 then
|
||
|
|
table.insert( allGoods , v4)
|
||
|
|
--log.info(string.format("玩家 %d 礼盒 第一档找到礼包ID %d 家具 %d" , player.basicInfo.userID , v1.id , v4.id ))
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
--[[
|
||
|
|
if #allGoods < v1.number then
|
||
|
|
for k2, v2 in pairs( v1.furnitureType ) do
|
||
|
|
local cfgGoods = skynet.server.gameConfig:GetCurFurnitureCfg( v2 )
|
||
|
|
for k3, v3 in pairs( v1.purchaseType) do
|
||
|
|
if cfgGoods and cfgGoods.purchaseType == v3 and cfgGoods.level <= level + 1 then
|
||
|
|
table.insert( allGoods , cfgGoods)
|
||
|
|
log.info(string.format("玩家 %d 礼盒 第二档找到礼包ID %d 家具 %d" , player.basicInfo.userID , v1.id , v2 ))
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
end
|
||
|
|
]]
|
||
|
|
|
||
|
|
--随机数量
|
||
|
|
for i = 1, v1.number, 1 do
|
||
|
|
local rand = math.random(1, #allGoods)
|
||
|
|
local curGoods = allGoods[ rand ]
|
||
|
|
table.insert( data.award , { type = pb.enum("EnumGoodsType","Furniture") , id = curGoods.id , count = 1})
|
||
|
|
skynet.server.bag:AddGoods( player , dataType.GoodsType_Furniture , curGoods.id , 1 )
|
||
|
|
log.info(string.format("玩家 %d 礼盒 获得礼包ID %d 家具 %d" , player.basicInfo.userID , v1.id , curGoods.id ))
|
||
|
|
end
|
||
|
|
return true
|
||
|
|
end
|
||
|
|
end
|
||
|
|
|
||
|
|
return false
|
||
|
|
end
|
||
|
|
|
||
|
|
--获取礼包信息
|
||
|
|
function GiftBox:Get( player , giftId )
|
||
|
|
for k, v in pairs( player.gameData.giftBox ) do
|
||
|
|
if giftId == v.id then
|
||
|
|
return v
|
||
|
|
end
|
||
|
|
end
|
||
|
|
return nil
|
||
|
|
end
|
||
|
|
|
||
|
|
skynet.server.giftBox = GiftBox
|
||
|
|
return GiftBox
|