From 384bb077c785366a93be90ec5f6e5db08f6c0692 Mon Sep 17 00:00:00 2001 From: zhouahaihai Date: Fri, 22 Feb 2019 14:08:24 +0800 Subject: [PATCH] 挂机 --- src/ProtocolCode.lua | 6 ++++++ src/actions/AdvAction.lua | 4 ++-- src/actions/HangAction.lua | 164 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/adv/AdvBattle.lua | 2 +- src/models/Role.lua | 14 ++++++++++++++ 5 files changed, 187 insertions(+), 3 deletions(-) create mode 100644 src/actions/HangAction.lua diff --git a/src/ProtocolCode.lua b/src/ProtocolCode.lua index 271fb1b..24c3fa2 100644 --- a/src/ProtocolCode.lua +++ b/src/ProtocolCode.lua @@ -44,6 +44,12 @@ actionCodes = { Hero_loveTaskRpc = 213, Hero_changeSkinRpc = 214, + Hang_startRpc = 251, + Hang_checkRpc = 252, + Hang_startBattleRpc = 253, + Hang_endBattleRpc = 254, + Hang_roleFormatRpc = 255, + } rpcResponseBegin = 10000 diff --git a/src/actions/AdvAction.lua b/src/actions/AdvAction.lua index 06d35f4..006b2a7 100644 --- a/src/actions/AdvAction.lua +++ b/src/actions/AdvAction.lua @@ -35,14 +35,14 @@ function _M.roleFormatRpc(agent , data) local role = agent.role local msg = MsgPack.unpack(data) local advTeam = role:getProperty("advTeam") - for slot, heroId in ipairs(msg.heros) do + for slot, heroId in pairs(msg.heros) do if not role.heros[heroId] then return end end table.clear(advTeam) advTeam.heros = {} - for slot, heroId in ipairs(msg.heros) do + for slot, heroId in pairs(msg.heros) do advTeam.heros[slot] = heroId end role:updateProperty({field = "advTeam", value = advTeam}) diff --git a/src/actions/HangAction.lua b/src/actions/HangAction.lua new file mode 100644 index 0000000..3f759ac --- /dev/null +++ b/src/actions/HangAction.lua @@ -0,0 +1,164 @@ +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 nowTime = skynet.timex() + + local coinCount = math.floor((nowTime - hangInfo.coinTime) / 5) + hangInfo.coinTime = hangInfo.coinTime + coinCount * 5 + + local itemCount = math.floor((nowTime - hangInfo.itemTime) / 60) + hangInfo.itemTime = hangInfo.itemTime + itemCount * 60 + + 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 + + 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)] + items[cur[1]] = (items[cur[1]] or 0) + cur[2] + end + + if coinCount > 0 or itemCount > 0 then + role:updateProperty({field = "hangBag", value = items}) + role:updateProperty({field = "hangInfo", value = hangInfo}) + 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 end + + local hangPass = role:getProperty("hangPass") + if carbonData.prepose ~= "" and carbonData.prepose ~= 0 and not hangPass[carbonData.prepose] then return end + + checkReward(role) + + local hangInfo = role:getProperty("hangInfo") + table.clear(hangInfo) + hangInfo.carbonId = carbonId + local nowTime = skynet.timex() + hangInfo.coinTime = nowTime + hangInfo.itemTime = nowTime + if not hangPass[carbonId] then + hangInfo.bossTime = nowTime + 100 + end + role:updateProperty({field = "hangInfo", value = hangInfo}) + + SendPacket(actionCodes.Hang_startRpc, '') + return true +end + +-- 每隔1分钟检查一次 +function _M.checkRpc(agent, data) + local role = agent.role + -- local msg = MsgPack.unpack(data) + checkReward(role) + SendPacket(actionCodes.Hang_checkRpc, MsgPack.pack({})) + return true +end + +function _M.startBattleRpc(agent, data) + local role = agent.role + local msg = MsgPack.unpack(data) + local hangInfo = role:getProperty("hangInfo") + if msg.carbonId ~= hangInfo.carbonId then return end + local hangPass = role:getProperty("hangPass") + if hangPass[hangInfo.carbonId] then return end + local key = tostring(math.random()) + hangInfo.key = key + local nowTime = skynet.timex() + hangInfo.bossTime = nowTime + 100 + role:updateProperty({field = "hangInfo", value = hangInfo}) + 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 ~= hangInfo.key then return end + if msg.carbonId ~= hangInfo.carbonId then return end + local hangPass = role:getProperty("hangPass") + if hangPass[hangInfo.carbonId] then return end + local reward + if msg.starNum and msg.starNum > 0 then --win + hangPass[hangInfo.carbonId] = 1 + role:updateProperty({field = "hangPass", value = hangPass}) + hangInfo.bossTime = nil + + -- reward + reward = {} + local items = role:getProperty("hangBag") + local carbonData = csvdb["idle_battleCsv"][carbonId] + items[ItemId.Gold] = (items[ItemId.Gold] or 0) + carbonData.money_clear + items[ItemId.Exp] = (items[ItemId.Exp] or 0) + carbonData.exp_clear + reward[ItemId.Gold] = carbonData.money_clear + reward[ItemId.Exp] = carbonData.exp_clear + for itemId, count in pairs(carbonData.item_clear:toNumMap()) do + items[itemId] = (items[itemId] or 0) + count + reward[itemId] = count + end + end + hangInfo.key = nil + role:updateProperty({field = "hangInfo", value = hangInfo}) + SendPacket(actionCodes.Hang_endBattleRpc, MsgPack.pack({ + starNum = msg.starNum, + reward = reward, + })) + return true +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 + end + end + table.clear(hangTeam) + hangTeam.heros = {} + for slot, heroId in pairs(msg.heros) do + hangTeam.heros[slot] = heroId + end + hangTeam.leader = msg.leader + + role:updateProperty({field = "hangTeam", value = hangTeam}) + SendPacket(actionCodes.Hang_roleFormatRpc, '') + return true +end + +return _M \ No newline at end of file diff --git a/src/adv/AdvBattle.lua b/src/adv/AdvBattle.lua index a01984e..9284596 100644 --- a/src/adv/AdvBattle.lua +++ b/src/adv/AdvBattle.lua @@ -38,7 +38,7 @@ function Battle:initPlayer() end end player.hp = hp - player.atk = player.hp * 0.1 --todo 系数是临时的 + player.atk = tonumber(string.format("%0.0f", player.hp * 0.1)) --todo 系数是临时的 player.miss = 0 player.hit = 100 self.adv.advTeam.player = player diff --git a/src/models/Role.lua b/src/models/Role.lua index 03975b0..6fbcb25 100644 --- a/src/models/Role.lua +++ b/src/models/Role.lua @@ -42,6 +42,13 @@ Role.schema = { advInfo = {"table", {}}, -- 冒险关卡信息 advTeam = {"table", {}}, -- 冒险玩家队伍信息 + --挂机相关 + hangPass = {"table", {}}, -- 挂机通过的最大关卡 + hangTeam = {"table", {}}, -- 挂机队伍 + hangInfo = {"table", {}}, -- 当前挂机信息 + hangBag = {"table", {}}, -- 背包 + hangBagLimit = {"number", 10}, --背包上限 + } @@ -144,10 +151,17 @@ function Role:data() reDiamond = self:getProperty("reDiamond"), items = self:getProperty("items"):toNumMap(), loveStatus = self:getProperty("loveStatus"):toNumMap(), + advPass = self:getProperty("advPass"), advInfo = self:getProperty("advInfo"), advItems = self:getProperty("advItems"):toNumMap(), advTeam = self:getProperty("advTeam"), + + hangPass = self:getProperty("hangPass"), + hangTeam = self:getProperty("hangTeam"), + hangInfo = self:getProperty("hangInfo"), + hangBag = self:getProperty("hangBag"), + hangBagLimit = self:getProperty("hangBagLimit"), } end -- libgit2 0.21.2