From e459a5fc804fd15a045a6b4036fcc8cbf75bd8e9 Mon Sep 17 00:00:00 2001 From: suhongyang <1609423485@qq.com> Date: Tue, 9 Jul 2019 16:32:03 +0800 Subject: [PATCH] 战斗回合检查同步以及回合数据缓存 --- src/actions/AdvAction.lua | 7 +++++-- src/adv/Adv.lua | 7 +++++-- src/adv/AdvBattle.lua | 23 ++++++++++++++++++++--- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/actions/AdvAction.lua b/src/actions/AdvAction.lua index eb5c534..911716c 100644 --- a/src/actions/AdvAction.lua +++ b/src/actions/AdvAction.lua @@ -102,8 +102,11 @@ function _M.nextTurnRpc(agent, data) local role = agent.role local msg = MsgPack.unpack(data) local adv = role:getAdvData() - local status = adv:nextBattleTurn() - if not status then return end + local status = adv:nextBattleTurn(msg.turn) + if not status then -- 数据不同步则发送缓存数据 + SendPacket(actionCodes.Adv_nextTurnRpc, MsgPack.pack({events = adv.tempBackEvents})) + return true + end SendPacket(actionCodes.Adv_nextTurnRpc, MsgPack.pack({events = adv:popBackEvents()})) return true end diff --git a/src/adv/Adv.lua b/src/adv/Adv.lua index 1c75c9d..a43357a 100644 --- a/src/adv/Adv.lua +++ b/src/adv/Adv.lua @@ -442,6 +442,7 @@ function Adv:ctor(owner) self.advTeam = self.owner:getProperty("advTeam") --这个变量置空使用 table.clear self:clear() self.backEvents = {} --发给客户端的事件组 + self.tempBackEvents = {} --发给客户端的事件组(缓存) end -- 清空自己组织的数据 @@ -899,9 +900,9 @@ function Adv:clickBlock(roomId, blockId, params) end --继续战斗 -function Adv:nextBattleTurn() +function Adv:nextBattleTurn(turn) local enemy = self.battle.enemy - if not enemy then + if not enemy or self.battle:checkTurn(turn) then return end local roomId, blockId = self.battle:getRBByEnemyId(enemy.id) @@ -1120,6 +1121,8 @@ end function Adv:popBackEvents() local events = self.backEvents + -- TODO 缓存数据需要分类,防止发错,暂时只有战斗,可以不分 + self.tempBackEvents = events self.backEvents = {} return events end diff --git a/src/adv/AdvBattle.lua b/src/adv/AdvBattle.lua index 92614a4..b1340e3 100644 --- a/src/adv/AdvBattle.lua +++ b/src/adv/AdvBattle.lua @@ -8,6 +8,7 @@ function Battle:ctor(adv) self.isNewPlayer = false self.enemy = nil self.enemys = {} --怪 + self.tempData = {} -- 临时回合数据 self:initPlayer() self:initEnemys() self:initAfter() @@ -112,7 +113,9 @@ end --战斗开始 function Battle:battleBegin(roomId, blockId) + self.tempData = {} -- 清理上次战斗数据 if self.enemy then + self.player:reset(self.adv.advTeam.player) self.enemy:reset(self.adv.rooms[self.enemy.roomId].blocks[self.enemy.blockId].event.enemy) end local enemy = self:getEnemy(roomId, blockId) @@ -124,10 +127,16 @@ function Battle:battleBegin(roomId, blockId) end end +-- 检查回合数同步 +function Battle:checkTurn(turn) + if self.tempData[turn] then + return true + end +end + -- 战斗内一回合 function Battle:doBattleTurn() local enemy = self.enemy - if not enemy then return end -- 玩家先出手 self.adv:backAtk(nil, enemy.id) self:doPlayerTurn(self.player, enemy) @@ -188,13 +197,21 @@ function Battle:afterRound() else self.player:hurt(self.player.hpMax / 10, nil, {hurtType = 4}) end - - if self.player.isDead then self.adv:over(false) end end +-- 写入临时数据 +function Battle:getTempDB() + local turnDB = {} + if self.enemy then + turnDB.enemy = self.enemy:getDB() + end + turnDB.player = self.player:getDB() + table.insert(self.tempData, turnDB) +end + --写入数据 function Battle:getDB() if not self.enemy then -- libgit2 0.21.2