local ipairs = ipairs local table = table local math = math local next = next local string = string local redisproxy = redisproxy local MsgPack = MsgPack local getRandomName = getRandomName local mcast_util = mcast_util local string_format = string.format local tonumber = tonumber local require = require local table_insert = table.insert local tconcat = table.concat local table_unpack = table.unpack local _M = {} local function checkReward(role) local hangInfo = role:getProperty("hangInfo") if not hangInfo.carbonId or not hangInfo.coinTime or not hangInfo.itemTime then return end local carbonData = csvdb["idle_battleCsv"][hangInfo.carbonId] local nowCoinTime = math.min(skynet.timex(), hangInfo.endCoinTime or 0) local nowItemTime = math.min(skynet.timex(), hangInfo.endItemTime or 0) local coinCount = math.max(0, math.floor((nowCoinTime - hangInfo.coinTime) / globalCsv.idle_money_produce_cd)) hangInfo.coinTime = hangInfo.coinTime + coinCount * globalCsv.idle_money_produce_cd local itemCount = math.max(0, math.floor((nowItemTime - hangInfo.itemTime) / globalCsv.idle_item_produce_cd)) hangInfo.itemTime = hangInfo.itemTime + itemCount * globalCsv.idle_item_produce_cd local items = role:getProperty("hangBag") items[ItemId.Gold] = (items[ItemId.Gold] or 0) + coinCount * carbonData.money items[ItemId.Exp] = (items[ItemId.Exp] or 0) + coinCount * carbonData.exp items[ItemId.PlayerExp] = (items[ItemId.PlayerExp] or 0) + coinCount * carbonData.playerExp local pool = {} for _, temp in pairs(carbonData.item:toArray()) do table.insert(pool, temp:toArray(true, "=")) end local curTypeCount = 0 for id, _ in pairs(items) do if id ~= ItemId.Gold and id ~= ItemId.Exp and id ~= ItemId.PlayerExp then curTypeCount = curTypeCount + 1 end end for i = 1, itemCount do local cur = pool[math.randWeight(pool, 3)] if items[cur[1]] or curTypeCount < role:getProperty("hangBagLimit") then if not items[cur[1]] then curTypeCount = curTypeCount + 1 end items[cur[1]] = (items[cur[1]] or 0) + cur[2] end end if coinCount > 0 or itemCount > 0 then return true end end --开始一个新的关卡 function _M.startRpc( agent, data ) local role = agent.role local msg = MsgPack.unpack(data) local carbonId = msg.carbonId local carbonData = csvdb["idle_battleCsv"][carbonId] if not carbonData then return 1 end if math.floor(carbonId / 10000) ~= 1 then if not role:isFuncUnlock(FuncUnlock.DifficultHang) then return end end local hangPass = role:getProperty("hangPass") for _, preCarbonId in ipairs(carbonData.prepose:toArray(true, "=")) do if not hangPass[preCarbonId] then return 2 end end if checkReward(role) then role:updateProperty({field = "hangBag", value = role:getProperty("hangBag")}) end local hangInfo = role:getProperty("hangInfo") local isNew = not hangInfo.carbonId hangInfo.carbonId = carbonId local nowTime = skynet.timex() hangInfo.coinTime = nowTime hangInfo.itemTime = nowTime if isNew then hangInfo.endCoinTime = nowTime + globalCsv.idle_producetime_max hangInfo.endItemTime = nowTime + globalCsv.idle_producetime_max end if not hangPass[carbonId] then hangInfo.bossTime = nowTime + carbonData.idle_time else hangInfo.bossTime = nil end role:updateProperty({field = "hangInfo", value = hangInfo}) role:log("hang_action", {desc = "startHang", int1 = carbonId}) SendPacket(actionCodes.Hang_startRpc, '') return true end -- 每隔1分钟检查一次 function _M.checkRpc(agent, data) local role = agent.role -- local msg = MsgPack.unpack(data) if checkReward(role) then role:updateProperty({field = "hangBag", value = role:getProperty("hangBag")}) role:updateProperty({field = "hangInfo", value = role:getProperty("hangInfo")}) end SendPacket(actionCodes.Hang_checkRpc, MsgPack.pack({})) return true end local _BattleKey = nil function _M.startBattleRpc(agent, data) local role = agent.role local msg = MsgPack.unpack(data) local carbonId = msg.carbonId local curData = csvdb["idle_battleCsv"][carbonId] if not curData then return 1 end local hangInfo = role:getProperty("hangInfo") if curData.main ~= 1 then if carbonId ~= hangInfo.carbonId then return 2 end end local hangPass = role:getProperty("hangPass") if hangPass[carbonId] then return 3 end _BattleKey = tostring(math.random()) role:checkTaskEnter("HangBattle", {id = carbonId}) SendPacket(actionCodes.Hang_startBattleRpc, MsgPack.pack({key = key})) return true end function _M.endBattleRpc(agent, data) local role = agent.role local msg = MsgPack.unpack(data) local hangInfo = role:getProperty("hangInfo") if not msg.key or msg.key ~= _BattleKey then return 1 end local carbonId = msg.carbonId local carbonData = csvdb["idle_battleCsv"][carbonId] if not carbonData then return 2 end if carbonData.main ~= 1 then if carbonId ~= hangInfo.carbonId then return 3 end end local hangPass = role:getProperty("hangPass") if hangPass[carbonId] then return 4 end local reward if msg.starNum and msg.starNum > 0 then --win hangPass[carbonId] = 1 role:updateProperty({field = "hangPass", value = hangPass}) if carbonData.main ~= 1 then hangInfo.bossTime = nil end -- reward reward = {} reward[ItemId.Gold] = carbonData.money_clear reward[ItemId.Exp] = carbonData.exp_clear reward[ItemId.PlayerExp] = carbonData.playerExp_clear for itemId, count in pairs(carbonData.item_clear:toNumMap()) do reward[itemId] = count end reward = role:award(reward, {log = {desc = "hangBattle", int1 = carbonId}}) role:checkTaskEnter("HangPass", {id = carbonId}) end role:updateProperty({field = "hangInfo", value = hangInfo}) role:log("hang_action", {desc = "hangBattle", short1 = msg.starNum > 0 and 1 or 0, int1 = carbonId}) SendPacket(actionCodes.Hang_endBattleRpc, MsgPack.pack({ starNum = msg.starNum, reward = reward, })) return true end local function checkLeader(heros, leader) if not leader then return end for slot, heroId in pairs(heros) do if heroId == leader then return true end end end function _M.roleFormatRpc(agent , data) local role = agent.role local msg = MsgPack.unpack(data) local hangTeam = role:getProperty("hangTeam") for slot, heroId in pairs(msg.heros) do if not role.heros[heroId] then return 1 end end local supports = {} for slot, support in pairs(msg.supports or {}) do if slot ~= 1 and slot ~= 2 then return 2 end local level = role.dinerData:getProperty("dishTree"):getv(support, 0) if level <= 0 then return 3 end supports[slot] = support end if not checkLeader(msg.heros, msg.leader) then return 4 end table.clear(hangTeam) hangTeam.heros = {} for slot, heroId in pairs(msg.heros) do hangTeam.heros[slot] = heroId end hangTeam.leader = msg.leader hangTeam.supports = supports role:saveHangTeam(hangTeam) SendPacket(actionCodes.Hang_roleFormatRpc, '') return true end function _M.getRewardRpc(agent , data) local role = agent.role checkReward(role) local items = role:getProperty("hangBag") if not next(items) then return end local reward = role:award(items, {log = {desc = "hangReward"}}) table.clear(items) local hangInfo = role:getProperty("hangInfo") local nowTime = skynet.timex() hangInfo.endItemTime = nowTime + globalCsv.idle_producetime_max hangInfo.endCoinTime = nowTime + globalCsv.idle_producetime_max hangInfo.coinTime = nowTime hangInfo.itemTime = nowTime role:updateProperty({field = "hangBag", value = items}) role:updateProperty({field = "hangInfo", value = hangInfo}) role:checkTaskEnter("HangGet") if reward[ItemId.Gold] then role:checkTaskEnter("HangGetGold", {count = reward[ItemId.Gold]}) end SendPacket(actionCodes.Hang_getRewardRpc, MsgPack.pack({ reward = reward })) return true end -- function _M.getRewardItemRpc(agent , data) -- local role = agent.role -- checkReward(role) -- local items = role:getProperty("hangBag") -- if not next(items) then return end -- local notNeed = {[ItemId.Gold] = 1, [ItemId.Exp] = 1, [ItemId.PlayerExp] = 1} -- local reward = {} -- for itemId , count in pairs(items) do -- if not notNeed[itemId] then -- reward[itemId] = count -- items[itemId] = nil -- end -- end -- if not next(reward) then return end -- local reward = role:award(reward) -- local hangInfo = role:getProperty("hangInfo") -- local nowTime = skynet.timex() -- hangInfo.endItemTime = nowTime + globalCsv.idle_producetime_max -- hangInfo.itemTime = nowTime -- role:updateProperty({field = "hangBag", value = items}) -- role:updateProperty({field = "hangInfo", value = hangInfo}) -- SendPacket(actionCodes.Hang_getRewardItemRpc, MsgPack.pack({ -- reward = reward -- })) -- return true -- end -- function _M.getRewardCoinRpc(agent , data) -- local role = agent.role -- checkReward(role) -- local items = role:getProperty("hangBag") -- if not next(items) then return end -- local need = {[ItemId.Gold] = 1, [ItemId.Exp] = 1, [ItemId.PlayerExp] = 1} -- local reward = {} -- for itemId , count in pairs(items) do -- if need[itemId] then -- reward[itemId] = count -- items[itemId] = nil -- end -- end -- local reward = role:award(reward) -- local hangInfo = role:getProperty("hangInfo") -- local nowTime = skynet.timex() -- hangInfo.endCoinTime = nowTime + globalCsv.idle_producetime_max -- hangInfo.coinTime = nowTime -- role:updateProperty({field = "hangBag", value = items}) -- role:updateProperty({field = "hangInfo", value = hangInfo}) -- SendPacket(actionCodes.Hang_getRewardCoinRpc, MsgPack.pack({ -- reward = reward -- })) -- return true -- end function _M.quickRpc(agent , data) local role = agent.role local hangInfo = role:getProperty("hangInfo") if not hangInfo.carbonId then return end local carbonData = csvdb["idle_battleCsv"][hangInfo.carbonId] local curCount = role.dailyData:getProperty("hangQC") + 1 local costs = globalCsv.idle_quickproduce_cost:toArray(true, "=") if not costs[curCount] then return end if costs[curCount] > 0 then if not role:checkItemEnough({[ItemId.Diamond] = costs[curCount]}) then return end role:costItems({[ItemId.Diamond] = costs[curCount]}, {log = {desc = "quickHang", int1 = hangInfo.carbonId}}) end role.dailyData:updateProperty({field = "hangQC", value = curCount}) local time = globalCsv.idle_quickproduce_time local reward = {} local coinCount = math.floor(time / globalCsv.idle_money_produce_cd) local itemCount = math.floor(time / globalCsv.idle_item_produce_cd) reward[ItemId.Gold] = (reward[ItemId.Gold] or 0) + coinCount * carbonData.money reward[ItemId.Exp] = (reward[ItemId.Exp] or 0) + coinCount * carbonData.exp local pool = {} for _, temp in pairs(carbonData.item:toArray()) do table.insert(pool, temp:toArray(true, "=")) end for i = 1, itemCount do local cur = pool[math.randWeight(pool, 3)] reward[cur[1]] = (reward[cur[1]] or 0) + cur[2] end reward = role:award(reward, {log = {desc = "quickHang", int1 = hangInfo.carbonId}}) if reward[ItemId.Gold] then role:checkTaskEnter("HangGetGold", {count = reward[ItemId.Gold]}) end role:checkTaskEnter("HangQuick") SendPacket(actionCodes.Hang_quickRpc, MsgPack.pack({ reward = reward })) return true end function _M.bonusFormatRpc(agent , data) local role = agent.role local msg = MsgPack.unpack(data) local bTeam = role:getProperty("bTeam") for slot, heroId in pairs(msg.heros) do if not role.heros[heroId] then return end end local supports = {} for slot, support in pairs(msg.supports) do if slot ~= 1 and slot ~= 2 then return end local level = role.dinerData:getProperty("dishTree"):getv(support, 0) if level <= 0 then return end supports[slot] = support end if not checkLeader(msg.heros, msg.leader) then return end table.clear(bTeam) bTeam.heros = {} for slot, heroId in pairs(msg.heros) do bTeam.heros[slot] = heroId end bTeam.leader = msg.leader bTeam.supports = supports role:updateProperty({field = "bTeam", value = bTeam}) SendPacket(actionCodes.Hang_bonusFormatRpc, '') return true end function _M.buyBonusCountRpc(agent, data) local role = agent.role local msg = MsgPack.unpack(data) local count = msg.count local btype = msg.type if not TimeReset["DailyBattle" .. btype] then return end local bonusC = role.dailyData:getProperty("bonusC") bonusC[btype] = bonusC[btype] or {c = 0, b = 0} local lastCount = globalCsv.bonus_daily_buy_count - bonusC[btype]["b"] if math.illegalNum(count, 1, lastCount) then return 1 end if not role:checkItemEnough({[ItemId.Diamond] = globalCsv.bonus_buy_cost * count}) then return 2 end role:costItems({[ItemId.Diamond] = globalCsv.bonus_buy_cost * count}, {log = {desc = "buyBonusCount", short1 = btype, int1 = count}}) bonusC[btype]["b"] = bonusC[btype]["b"] + count bonusC[btype]["c"] = bonusC[btype]["c"] - count role.dailyData:updateProperty({field = "bonusC", value = bonusC}) SendPacket(actionCodes.Hang_buyBonusCountRpc, '') return true end function _M.startBonusBattleRpc(agent, data) local role = agent.role local msg = MsgPack.unpack(data) local id = msg.id if not role:isFuncUnlock(FuncUnlock.BonusBattle) then return end local bonusData = csvdb["bonus_battleCsv"][id] if not role:isTimeResetOpen(TimeReset["DailyBattle" .. bonusData.type]) then return end if not bonusData then return 1 end local hangPass = role:getProperty("hangPass") if not hangPass[bonusData.unlock] then return 2 end if not next(role:getProperty("bTeam")) then return 3 end local bonusC = role.dailyData:getProperty("bonusC") bonusC[bonusData.type] = bonusC[bonusData.type] or {c = 0, b = 0} if globalCsv.bonus_daily_count - bonusC[bonusData.type]["c"] <= 0 then return 4 end bonusC[bonusData.type]["c"] = bonusC[bonusData.type]["c"] + 1 role.dailyData:updateProperty({field = "bonusC", value = bonusC}) role.__bonusBattleCache = { key = tostring(math.random()), id = id, } SendPacket(actionCodes.Hang_startBonusBattleRpc, MsgPack.pack({key = role.__bonusBattleCache.key})) return true end function _M.endBonusBattleRpc(agent, data) local role = agent.role local msg = MsgPack.unpack(data) local id = msg.id local key = msg.key local starNum = msg.starNum if not role.__bonusBattleCache then return 1 end if role.__bonusBattleCache.id ~= id or role.__bonusBattleCache.key ~= key then return 2 end local bonusData = csvdb["bonus_battleCsv"][id] local reward if starNum and starNum > 0 then reward = role:award(bonusData.reward, {log = {desc = "bonusBattle", int1 = id}}) role:checkTaskEnter("BonusPass", {id = id}) end role:log("hang_action", {desc = "bonusBattle", short1 = msg.starNum > 0 and 1 or 0, int1 = id}) SendPacket(actionCodes.Hang_endBonusBattleRpc, MsgPack.pack({ starNum = starNum, reward = reward})) return true end return _M