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 activity = require "Activity" local json =require "json" local ActivityMatchStuff = oo.class() ActivityMatchStuff.ActivityType = dataType.ActivityType_MatchStuff --构造函数 function ActivityMatchStuff:Init() end -- 每次登录需要进行修改的数据 function ActivityMatchStuff:LoginInitData(player) --log.debug("好物匹配 登录初始化") -- 重置红点 skynet.server.msgTips:Reset(player, 104) skynet.server.msgTips:Reset(player, 105) --获取玩家数据 检查活动是否结束,并结算银星币 if not next(player.gameData.matchStuffMap) then return end --获取开启的活动 local activityId, startTime, endTime, id = activity:GetActivityInfo(player, ActivityMatchStuff.ActivityType) --循环配置 --没有开启的活动处理银星币结算 for key, matchStuff in pairs(player.gameData.matchStuffMap) do if (key ~= activityId and activityId == 0) and not matchStuff.isSettle then self:settle(player,matchStuff) return end end end function ActivityMatchStuff:settle(player,matchStuff) -- 查询道具是否足够-道具id 是26 银星币 这里写死以往规则 local goodsCount=skynet.server.bag:GetGoodsCount(player,dataType.GoodsType_Prop, 26) if goodsCount<=0 then log.debug(string.format("银星币无需结算,玩家:%s,银星币数量:%d",player.userId,skynet.server.bag:GetGoodsCount(player,dataType.GoodsType_Prop, 26))) matchStuff.isSettle = true return end local cfgSValue = skynet.server.gameConfig:GetAllCfg( "SValue") --计算可以获取的金币数量 local coinCount= goodsCount * tonumber(cfgSValue.silverCoinIntegralCoin) if coinCount0 and player.gameData.matchStuffMap[activityId]~=nil and player.gameData.matchStuffMap[activityId].StoreInfo[cfMatchStuffStore.id]~=nil and player.gameData.matchStuffMap[activityId].StoreInfo[cfMatchStuffStore.id].BuyCount>=cfMatchStuffStore.limitExchange then goto continue end -- 查询道具是否足够-道具id 是26 银星币 这里写死以往规则 if skynet.server.bag:GetGoodsCount(player,dataType.GoodsType_Prop, 26) thisLevel and (nextLevel > value or nextLevel == 0 or nextLevel == thisLevel) then nextLevel = value end end return nextLevel end --获取配置信息 function ActivityMatchStuff:GetMatchStuffConfig(player,activityId) local cfMatchStuff=nil --获取该活动的默认初始关卡 local cfMatchStuffs=skynet.server.gameConfig:GetPlayerAllCfg(player, "MatchStuff") if cfMatchStuffs==nil then --log.debug("cfMatchStuff is nil") return cfMatchStuff end for index, value in pairs(cfMatchStuffs) do if value.id==activityId then cfMatchStuff=value break end end return cfMatchStuff end --获取显示信息 function ActivityMatchStuff:Show( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SActivityMatchStuffShow", c2sData.data )) s2cData.cmd = pb.enum("MsgType","CMD_S2C_ActivityMatchStuffShow") local data = {} data.magnifyingGlassNum = 0 data.curLevelId = 0 data.haveWatchAD = 0 --不存在开启的活动 local activityId=self:CheckData(player) --活动未开启 if activityId==0 then --log.debug("activityId==0") s2cData.code = errorInfo.ErrorCode.ActivityClosed return end if player.gameData.matchStuffMap[activityId]~=nil then --处理错误数据 if player.gameData.matchStuffMap[activityId].magnifierDaily < 0 then player.gameData.matchStuffMap[activityId].magnifierDaily = 0 end data.magnifyingGlassNum = player.gameData.matchStuffMap[activityId].magnifierDaily data.curLevelId = player.gameData.matchStuffMap[activityId].levelId data.haveWatchAD = player.gameData.matchStuffMap[activityId].magnifierAd data.curLevelisFinish=player.gameData.matchStuffMap[activityId].LevelisFinish end s2cData.data = assert(pb.encode("S2CActivityMatchStuffShow", data)) end --获取商店显示信息 function ActivityMatchStuff:ShopShow( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SActivityMatchStuffShopShow", c2sData.data )) s2cData.cmd = pb.enum("MsgType","CMD_S2C_ActivityMatchStuffShopShow") local data = {} data.shopGoodsInfos = {} --不存在开启的活动 local activityId=self:CheckData(player) --活动未开启 if activityId==0 then s2cData.code = errorInfo.ErrorCode.ActivityClosed return end --获取该活动的默认初始关卡 local cfMatchStuffStore=skynet.server.gameConfig:GetPlayerAllCfg(player, "MatchStuffStore") if cfMatchStuffStore==nil then return end local cfgMatchStuff=self:GetMatchStuffConfig(player,activityId) if cfgMatchStuff==nil then return end --循环获取商城配置 for k,cofg in pairs(cfMatchStuffStore) do local buyCount=0 --是否存在玩家数据 if player.gameData.matchStuffMap[activityId]~=nil and player.gameData.matchStuffMap[activityId].StoreInfo[cofg.id]~=nil then buyCount=player.gameData.matchStuffMap[activityId].StoreInfo[cofg.id].BuyCount end local shopGoodsInfos={} shopGoodsInfos.id=cofg.id shopGoodsInfos.buyCount=buyCount shopGoodsInfos.todayBuyCount=0 local index=0 for tmpIndex, value in ipairs(cfgMatchStuff.silverStoreContent) do if value == cofg.id then index=tmpIndex end end --添加到列表 data.shopGoodsInfos[index]=shopGoodsInfos end s2cData.data = assert(pb.encode("S2CActivityMatchStuffShopShow", data)) end --是否可以挑战 function ActivityMatchStuff:CanChallenge( player ) --活动未开启 local activityId=self:CheckData(player) if activityId == 0 then return false end --放大镜不足 if player.gameData.matchStuffMap[activityId].magnifierDaily<=0 then return false end --已全部挑战 if player.gameData.matchStuffMap[activityId].LevelisFinish then return false end return true end --商品购买 function ActivityMatchStuff:ShopBuy( player , c2sData , s2cData ) c2sData.data = assert(pb.decode("C2SActivityMatchStuffShopBuy", c2sData.data )) s2cData.cmd = pb.enum("MsgType","CMD_S2C_ActivityMatchStuffShopBuy") --参数 local goodsId = c2sData.data.goodsId --返回信息 local data = {} data.buyShopGoodsInfo = {} --不存在开启的活动 local activityId=self:CheckData(player) --活动未开启 if activityId==0 then s2cData.code = errorInfo.ErrorCode.ActivityClosed --log.debug("errorInfo.ErrorCode.ActivityClosed") return end --获取该活动的默认初始关卡 local cfMatchStuffStore=skynet.server.gameConfig:GetPlayerAllCfg(player, "MatchStuffStore") if cfMatchStuffStore==nil then --log.debug("cfMatchStuffStore==nil ") return end local storeCofg=nil --循环获取商城配置 for index, cofg in pairs(cfMatchStuffStore) do if cofg.id==goodsId then storeCofg=cofg break end end --配置不存在 if storeCofg==nil then s2cData.code = errorInfo.ErrorCode.NoExistShopType --log.debug("storeCofg==nil ") return end --判断是否限购 if storeCofg.limitExchange>0 and player.gameData.matchStuffMap[activityId]~=nil and player.gameData.matchStuffMap[activityId].StoreInfo[goodsId]~=nil and player.gameData.matchStuffMap[activityId].StoreInfo[goodsId].BuyCount>=storeCofg.limitExchange then s2cData.code = errorInfo.ErrorCode.StoreMaxLimit --log.debug("判断是否限购") return end -- 查询道具是否足够-道具id 是26 银星币 这里写死以往规则 if skynet.server.bag:GetGoodsCount(player,dataType.GoodsType_Prop, 26)=storeCofg.silverCoinPrice then,playerPrice:%d, Price:%d",skynet.server.bag:GetGoodsCount(player,dataType.GoodsType_Prop, 26),storeCofg.silverCoinPrice)) return end --扣除道具--道具id 是26 银星币 这里写死以往规则 skynet.server.bag:RemoveGoods(player,dataType.GoodsType_Prop, 26,storeCofg.silverCoinPrice) --发放奖励 local eventId = pb.enum("EnumMoneyChangeEventID","EventID_114") player:GiveReward(storeCofg.rewardId ,eventId,1) --添加购买次数 if player.gameData.matchStuffMap[activityId].StoreInfo[goodsId]==nil then player.gameData.matchStuffMap[activityId].StoreInfo[goodsId]={ BuyCount=0 } end player.gameData.matchStuffMap[activityId].StoreInfo[goodsId].BuyCount =player.gameData.matchStuffMap[activityId].StoreInfo[goodsId].BuyCount + 1 --返回购买结果 data.buyShopGoodsInfo={id=storeCofg.id,buyCount=player.gameData.matchStuffMap[activityId].StoreInfo[goodsId].BuyCount,todayBuyCount=0} --处理红点 if not self:IsCanBuy(player) then skynet.server.msgTips:ReduceAll(player, 105) end s2cData.data = assert(pb.encode("S2CActivityMatchStuffShopBuy", data)) end --游戏开始 function ActivityMatchStuff:GameStart(player,c2sData,s2cData) c2sData.data = assert(pb.decode("C2SActivityMatchStuffGameStart", c2sData.data )) s2cData.cmd = pb.enum("MsgType","CMD_S2C_ActivityMatchStuffGameStart") --参数 local curLevelId = c2sData.data.curLevelId --返回信息 local data = {} --不存在开启的活动 local activityId=self:CheckData(player) --活动未开启 if activityId==0 then s2cData.code = errorInfo.ErrorCode.ActivityClosed return end local matchStuff=player.gameData.matchStuffMap[activityId] --获取当前可以挑战的关卡 if curLevelId~=matchStuff.levelId then s2cData.code = errorInfo.ErrorCode.ThisNodeNotFight return end --添加挑战状态 matchStuff.isFightingLevel = true --log.debug("好物匹配 玩家开始游戏 !") s2cData.data = assert(pb.encode("S2CActivityMatchStuffGameStart", data)) end --游戏结束 function ActivityMatchStuff:GameOver(player,c2sData,s2cData) c2sData.data = assert(pb.decode("C2SActivityMatchStuffGameOver", c2sData.data )) s2cData.cmd = pb.enum("MsgType","CMD_S2C_ActivityMatchStuffGameOver") --参数 local isFinish = c2sData.data.isFinish local extraRewards=c2sData.data.extraRewards --返回信息 local data = {} --不存在开启的活动 local activityId=self:CheckData(player) --活动未开启 if activityId==0 then s2cData.code = errorInfo.ErrorCode.ActivityClosed return end local matchStuff=player.gameData.matchStuffMap[activityId] --消耗资源是否足够 if matchStuff.magnifierDaily <= 0 then s2cData.code = errorInfo.ErrorCode.NoEnoughMoney return end --是否可以结束挑战 if not matchStuff.isFightingLevel then s2cData.code = errorInfo.ErrorCode.AlreadyFinish return end --更新缓存 if isFinish then --获取配置 local matchStuffCfg={} local matchStuffCfgs= skynet.server.gameConfig:GetPlayerAllCfg(player, "MatchStuff") for index,data in pairs(matchStuffCfgs) do if data.id==activityId then matchStuffCfg=data end end if matchStuffCfg==nil then s2cData.code = errorInfo.ErrorCode.NoGoodsID return end --当前节点 local levelId=player.gameData.matchStuffMap[activityId].levelId --获取节点配置 local cfgMatchStuffLevel=nil local cfgMatchStuffLevels=skynet.server.gameConfig:GetPlayerAllCfg(player, "MatchStuffLevel") for index,data in pairs(cfgMatchStuffLevels) do if data.level==levelId then cfgMatchStuffLevel=data end end if cfgMatchStuffLevel==nil then s2cData.code = errorInfo.ErrorCode.NoGoodsID return end --发奖节点奖励 local eventId = pb.enum("EnumMoneyChangeEventID","EventID_115") player:GiveReward(cfgMatchStuffLevel.levelReward ,eventId,1) --获取下一个挑战节点 local nextLevelId= GetNextLevel(matchStuffCfg.levelContent,levelId) if nextLevelId==levelId then --log.debug(string.format("玩家 % d 挑战节点 %d 已完成",player.id,levelId)) player.gameData.matchStuffMap[activityId].LevelisFinish=true end --log.debug(string.format("nextLevelId:%d",nextLevelId)) player.gameData.matchStuffMap[activityId].levelId = nextLevelId -- 发放额外奖励 if #matchStuffCfg.timeExchangeSilverCoin==2 then local totalCoin=matchStuffCfg.timeExchangeSilverCoin[2]*extraRewards skynet.server.bag:AddGoods(player,dataType.GoodsType_Prop, 26,totalCoin) end end --消耗放大镜 player.gameData.matchStuffMap[activityId].magnifierDaily =player.gameData.matchStuffMap[activityId].magnifierDaily -1 data.magnifyingGlassNum = player.gameData.matchStuffMap[activityId].magnifierDaily data.curLevelId = player.gameData.matchStuffMap[activityId].levelId data.curLevelisFinish = player.gameData.matchStuffMap[activityId].LevelisFinish --处理红点 if self:CanChallenge(player) then skynet.server.msgTips:ReduceAll(player, 104) end --处理商城红点 if self:IsCanBuy(player) then skynet.server.msgTips:Add(player, 105) end --处理当前挑战关卡 matchStuff.isFightingLevel = false s2cData.data = assert(pb.encode("S2CActivityMatchStuffGameOver", data)) end --购买放大镜 function ActivityMatchStuff:MagnifyingGlassSupplement(player,c2sData,s2cData) c2sData.data = assert(pb.decode("C2SActivityMatchStuffMagnifyingGlassSupplement", c2sData.data )) s2cData.cmd = pb.enum("MsgType","CMD_S2C_ActivityMatchStuffMagnifyingGlassSupplement") --参数 local methodType = c2sData.data.methodType --返回信息 local data = {} --不存在开启的活动 local activityId=self:CheckData(player) --活动未开启 if activityId==0 then s2cData.code = errorInfo.ErrorCode.ActivityClosed return end --获取配置 local matchStuffCfg={} local matchStuffCfgs= skynet.server.gameConfig:GetPlayerAllCfg(player, "MatchStuff") for index,data in pairs(matchStuffCfgs) do if data.id==activityId then matchStuffCfg=data end end if matchStuffCfg==nil then s2cData.code = errorInfo.ErrorCode.NoGoodsID return end --1-蜗壳币 if methodType == dataType.AccelerateType_Volute then -- 消耗玩家蜗壳币 local eventId = pb.enum("EnumMoneyChangeEventID","EventID_116") if player:MoneyChange(dataType.MoneyType_Volute, -matchStuffCfg.magnifierPrice, eventId) then -- -- 使用蜗壳币进行刷新 修改相关数据 -- player.gameData.todayGain.updateUseCount = player.gameData.todayGain.updateUseCount + 1 -- if player.gameData.todayGain.updateUseCount == cfgSValue.triggerUpdatePack[1] then -- skynet.server.store:TriggerPack(player, skynet.server.store.TriggerPack_Update) -- end else s2cData.code = errorInfo.ErrorCode.NoEnoughMoney return end elseif methodType == dataType.AccelerateType_WatchAD then -- 4-观看广告 --判断进入观看次数 if player.gameData.matchStuffMap[activityId].magnifierAd==0 then --log.debug(string.format("观看次数已用完,activityId:%d,已看次数:%d,总次数:%d",activityId,matchStuffCfg.magnifierAd-player.gameData.matchStuffMap[activityId].magnifierAd,matchStuffCfg.magnifierAd)) s2cData.code = errorInfo.ErrorCode.OPFailed return end player.gameData.matchStuffMap[activityId].magnifierAd= player.gameData.matchStuffMap[activityId].magnifierAd - 1 else--参数不支持 s2cData.code = errorInfo.ErrorCode.ErrRequestParam return end --添加放大镜 player.gameData.matchStuffMap[activityId].magnifierDaily =player.gameData.matchStuffMap[activityId].magnifierDaily +1 if self:CanChallenge(player) then --处理红点 skynet.server.msgTips:Add(player, 104) end --日志 --log.debug(string.format("玩家 %d 购买放大镜 1 购买类型" , player.userId , methodType)) data.magnifyingGlassNum= player.gameData.matchStuffMap[activityId].magnifierDaily s2cData.data = assert(pb.encode("S2CActivityMatchStuffMagnifyingGlassSupplement", data)) end skynet.server.activityMatchStuff = ActivityMatchStuff return ActivityMatchStuff