Commit e459a5fc804fd15a045a6b4036fcc8cbf75bd8e9

Authored by suhongyang
1 parent ae20365b

战斗回合检查同步以及回合数据缓存

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