Commit e459a5fc804fd15a045a6b4036fcc8cbf75bd8e9
1 parent
ae20365b
战斗回合检查同步以及回合数据缓存
Showing
3 changed files
with
30 additions
and
7 deletions
Show diff stats
src/actions/AdvAction.lua
... | ... | @@ -102,8 +102,11 @@ function _M.nextTurnRpc(agent, data) |
102 | 102 | local role = agent.role |
103 | 103 | local msg = MsgPack.unpack(data) |
104 | 104 | local adv = role:getAdvData() |
105 | - local status = adv:nextBattleTurn() | |
106 | - if not status then return end | |
105 | + local status = adv:nextBattleTurn(msg.turn) | |
106 | + if not status then -- 数据不同步则发送缓存数据 | |
107 | + SendPacket(actionCodes.Adv_nextTurnRpc, MsgPack.pack({events = adv.tempBackEvents})) | |
108 | + return true | |
109 | + end | |
107 | 110 | SendPacket(actionCodes.Adv_nextTurnRpc, MsgPack.pack({events = adv:popBackEvents()})) |
108 | 111 | return true |
109 | 112 | end | ... | ... |
src/adv/Adv.lua
... | ... | @@ -442,6 +442,7 @@ function Adv:ctor(owner) |
442 | 442 | self.advTeam = self.owner:getProperty("advTeam") --这个变量置空使用 table.clear |
443 | 443 | self:clear() |
444 | 444 | self.backEvents = {} --发给客户端的事件组 |
445 | + self.tempBackEvents = {} --发给客户端的事件组(缓存) | |
445 | 446 | end |
446 | 447 | |
447 | 448 | -- 清空自己组织的数据 |
... | ... | @@ -899,9 +900,9 @@ function Adv:clickBlock(roomId, blockId, params) |
899 | 900 | end |
900 | 901 | |
901 | 902 | --继续战斗 |
902 | -function Adv:nextBattleTurn() | |
903 | +function Adv:nextBattleTurn(turn) | |
903 | 904 | local enemy = self.battle.enemy |
904 | - if not enemy then | |
905 | + if not enemy or self.battle:checkTurn(turn) then | |
905 | 906 | return |
906 | 907 | end |
907 | 908 | local roomId, blockId = self.battle:getRBByEnemyId(enemy.id) |
... | ... | @@ -1120,6 +1121,8 @@ end |
1120 | 1121 | |
1121 | 1122 | function Adv:popBackEvents() |
1122 | 1123 | local events = self.backEvents |
1124 | + -- TODO 缓存数据需要分类,防止发错,暂时只有战斗,可以不分 | |
1125 | + self.tempBackEvents = events | |
1123 | 1126 | self.backEvents = {} |
1124 | 1127 | return events |
1125 | 1128 | end | ... | ... |
src/adv/AdvBattle.lua
... | ... | @@ -8,6 +8,7 @@ function Battle:ctor(adv) |
8 | 8 | self.isNewPlayer = false |
9 | 9 | self.enemy = nil |
10 | 10 | self.enemys = {} --怪 |
11 | + self.tempData = {} -- 临时回合数据 | |
11 | 12 | self:initPlayer() |
12 | 13 | self:initEnemys() |
13 | 14 | self:initAfter() |
... | ... | @@ -112,7 +113,9 @@ end |
112 | 113 | |
113 | 114 | --战斗开始 |
114 | 115 | function Battle:battleBegin(roomId, blockId) |
116 | + self.tempData = {} -- 清理上次战斗数据 | |
115 | 117 | if self.enemy then |
118 | + self.player:reset(self.adv.advTeam.player) | |
116 | 119 | self.enemy:reset(self.adv.rooms[self.enemy.roomId].blocks[self.enemy.blockId].event.enemy) |
117 | 120 | end |
118 | 121 | local enemy = self:getEnemy(roomId, blockId) |
... | ... | @@ -124,10 +127,16 @@ function Battle:battleBegin(roomId, blockId) |
124 | 127 | end |
125 | 128 | end |
126 | 129 | |
130 | +-- 检查回合数同步 | |
131 | +function Battle:checkTurn(turn) | |
132 | + if self.tempData[turn] then | |
133 | + return true | |
134 | + end | |
135 | +end | |
136 | + | |
127 | 137 | -- 战斗内一回合 |
128 | 138 | function Battle:doBattleTurn() |
129 | 139 | local enemy = self.enemy |
130 | - if not enemy then return end | |
131 | 140 | -- 玩家先出手 |
132 | 141 | self.adv:backAtk(nil, enemy.id) |
133 | 142 | self:doPlayerTurn(self.player, enemy) |
... | ... | @@ -188,13 +197,21 @@ function Battle:afterRound() |
188 | 197 | else |
189 | 198 | self.player:hurt(self.player.hpMax / 10, nil, {hurtType = 4}) |
190 | 199 | end |
191 | - | |
192 | - | |
193 | 200 | if self.player.isDead then |
194 | 201 | self.adv:over(false) |
195 | 202 | end |
196 | 203 | end |
197 | 204 | |
205 | +-- 写入临时数据 | |
206 | +function Battle:getTempDB() | |
207 | + local turnDB = {} | |
208 | + if self.enemy then | |
209 | + turnDB.enemy = self.enemy:getDB() | |
210 | + end | |
211 | + turnDB.player = self.player:getDB() | |
212 | + table.insert(self.tempData, turnDB) | |
213 | +end | |
214 | + | |
198 | 215 | --写入数据 |
199 | 216 | function Battle:getDB() |
200 | 217 | if not self.enemy then | ... | ... |