local skynet = require "skynet" local oo = require "Class" local log = require "Log" local pb = require "pb" local errorInfo = require "ErrorInfo" local dataType = require "DataType" local AD = oo.class() AD.WatchType_No = 1 --不可观看 AD.WatchType_Can = 2 --可观看 function AD:Init() end function AD:LoginInitData( player ) -- --初始化广告数据 -- if next(player.gameData.adCountInfo) == nil then -- local adTypeMax = pb.enum("EnumADType", "ADTypeMax" ) -- for i = 1,adTypeMax-1,1 do -- player.gameData.adCountInfo[i] = {} -- player.gameData.adCountInfo[i].adCount = 0 -- player.gameData.adCountInfo[i].nextRefreshTime = 0 -- player.gameData.adCountInfo[i].state = self.WatchType_Can -- end -- end --初始化广告数据,检查广告数据 local adTypeMax = pb.enum("EnumADType", "ADTypeMax" ) for i = 1,adTypeMax-1,1 do if player.gameData.adCountInfo[i]== nil then player.gameData.adCountInfo[i] = {} player.gameData.adCountInfo[i].adCount = 0 player.gameData.adCountInfo[i].nextRefreshTime = 0 player.gameData.adCountInfo[i].state = self.WatchType_Can end end end --礼盒展示 function AD:Watch( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SADWatch", c2sData.data )) local data = {} data.adInfo = {} local adType = c2sData.data.adType if not adType then s2cData.code = errorInfo.ErrorCode.ErrRequestParam else if self:CanWatch(player,adType) then data = self:Update( player , adType , false ) else s2cData.code = errorInfo.ErrorCode.CanNotWatchAD end end s2cData.cmd = pb.enum("MsgType","CMD_S2C_ADWatch") s2cData.data = assert(pb.encode("S2CADWatch", data)) end --获取所有广告 function AD:GetAllAD( player ) local data = {} local adCountInfo = player.gameData.adCountInfo if adCountInfo == nil then --兼容老玩家广告数据 local adTypeMax = pb.enum("EnumADType", "ADTypeMax" ) for i = 1,adTypeMax-1,1 do if i <= pb.enum("EnumADType", "FreeGainCoin" ) then player.gameData.adCountInfo[i] = {} player.gameData.adCountInfo[i].adCount = player.gameData.adCounts[i] player.gameData.adCountInfo[i].nextRefreshTime = 0 player.gameData.adCountInfo[i].state = self.WatchType_Can else player.gameData.adCountInfo[i] = {} player.gameData.adCountInfo[i].adCount = 0 player.gameData.adCountInfo[i].nextRefreshTime = 0 player.gameData.adCountInfo[i].state = self.WatchType_Can end end end for k , v in pairs(adCountInfo) do table.insert( data , { adType = k , adCount = v.adCount , adState = v.state , adRefreshTime = v.nextRefreshTime } ) end return data end function AD:CanWatch(player , adType) local curAdId = pb.enum("EnumADType", adType ) local adCountInfo = player.gameData.adCountInfo[curAdId] if adCountInfo.state == self.WatchType_Can then return true elseif adCountInfo.state == self.WatchType_No and skynet.GetTime() >= adCountInfo.nextRefreshTime then adCountInfo.state = self.WatchType_Can return true else return false end end --广告更新(其它看广告地方也可以调 , isSendMsg 其它地方调用要,可以不填,要填就填true ) function AD:Update( player , adType , isSendMsg ) isSendMsg = (nil == isSendMsg) and true or false local curAdId = pb.enum("EnumADType", adType ) local adCountInfo = player.gameData.adCountInfo[curAdId] local cfgSValue = skynet.server.gameConfig:GetPlayerAllCfg(player, "SValue") --更新广告对应数据 if curAdId == pb.enum("EnumADType", " FreeGainCoin" ) then --邮箱不限制 adCountInfo.adCount = adCountInfo.adCount + 1 adCountInfo.nextRefreshTime = 0 else adCountInfo.state = self.WatchType_No adCountInfo.adCount = adCountInfo.adCount + 1 adCountInfo.nextRefreshTime = skynet.GetTime() + cfgSValue.adRefreshCountdown * 60 end -- player.gameData.adCount = player.gameData.adCount + 1 --组装广告数据 local data = {} data.adInfo = {} data.adInfo.adType = curAdId data.adInfo.adCount = adCountInfo.adCount data.adInfo.adState = adCountInfo.state data.adInfo.adRefreshTime = adCountInfo.nextRefreshTime --将最新数量发给客户端 if isSendMsg then skynet.server.gameServer:SendMsgToUser( player.userId , "CMD_S2C_ADWatch" , data ) end --更新广告数量 log.debug(string.format("玩家 %d 广告观看 广告类型 %s" , player.userId , adType )) return data end --支付广告券 function AD:PayADTicket( player , adType , count ) count = count or 1 if skynet.server.bag:RemoveGoods(player , dataType.GoodsType_Prop, 17 , count ) then self:Update( player , adType ) return true end return false end skynet.server.ad = AD return AD