From 63cd9ae16267ff447784f4008e90fea6ac8d534b Mon Sep 17 00:00:00 2001 From: Guangcong Luo Date: Fri, 6 Nov 2015 06:08:42 -0500 Subject: [PATCH] Refactor battle.switchSides The details of maintaining the current battle state while switching sides is now handled in battle.js rather than client-battle.js, to help the replay player support it more easily. --- js/battle.js | 21 +++++++++++++++++++-- js/client-battle.js | 20 ++------------------ 2 files changed, 21 insertions(+), 20 deletions(-) diff --git a/js/battle.js b/js/battle.js index 5d9560ee8..dfb97457e 100644 --- a/js/battle.js +++ b/js/battle.js @@ -2536,8 +2536,25 @@ var Battle = (function () { } this.activityWait(effectElem); }; - Battle.prototype.switchSides = function () { - this.sidesSwitched = !this.sidesSwitched; + Battle.prototype.switchSides = function (replay) { + if (replay) { + this.reset(true); + this.setSidesSwitched(!this.sidesSwitched); + this.play(); + } else if (this.done) { + this.reset(true); + this.setSidesSwitched(!this.sidesSwitched); + this.fastForwardTo(-1); + } else { + var turn = this.turn; + this.reset(true); + this.setSidesSwitched(!this.sidesSwitched); + if (turn) this.fastForwardTo(turn); + this.play(true); + } + }; + Battle.prototype.setSidesSwitched = function (sidesSwitched) { + this.sidesSwitched = sidesSwitched; if (this.sidesSwitched) { this.mySide = this.p2; this.yourSide = this.p1; diff --git a/js/client-battle.js b/js/client-battle.js index d5ee7c258..1d4f92595 100644 --- a/js/client-battle.js +++ b/js/client-battle.js @@ -669,13 +669,7 @@ if (!sideData.id) return; this.side = sideData.id; if (this.battle.sidesSwitched !== !!(this.side === 'p2')) { - this.battle.reset(true); - this.battle.switchSides(); - if (midBattle) { - this.battle.fastForwardTo(-1); - } else { - this.battle.play(); - } + this.battle.switchSides(!midBattle); this.$chat = this.$chatFrame.find('.inner'); } }, @@ -716,17 +710,7 @@ this.send('/savereplay'); }, switchSides: function () { - if (this.battle.done) { - this.battle.reset(true); - this.battle.switchSides(); - this.battle.fastForwardTo(-1); - } else { - var turn = this.battle.turn; - this.battle.reset(true); - this.battle.switchSides(); - if (turn) this.battle.fastForwardTo(turn); - this.battle.play(true); - } + this.battle.switchSides(); }, instantReplay: function () { this.hideTooltip();