From 4c99df0b407c27b6235686183681a2b42b8b16e2 Mon Sep 17 00:00:00 2001 From: Lang Date: Sun, 20 Nov 2016 11:53:41 +0100 Subject: [PATCH] bugfix points problem in the end #16 --- .../Resources/public/js/mine-seeker/app.js | 51 +++++++++++-------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/Mine/SeekerBundle/Resources/public/js/mine-seeker/app.js b/src/Mine/SeekerBundle/Resources/public/js/mine-seeker/app.js index 7f8e58c..7cfc704 100644 --- a/src/Mine/SeekerBundle/Resources/public/js/mine-seeker/app.js +++ b/src/Mine/SeekerBundle/Resources/public/js/mine-seeker/app.js @@ -28,6 +28,27 @@ class MineSeeker extends React.Component { return text; } + /** STEP */ + makePointsCalcAndStep(coords) { + let users = this.refs.gridControl.refs.userControl, + activePlayer = users.state.activePlayer ? 'blue' : 'red', + inactivePlayer = users.state.activePlayer ? 'red' : 'blue', + redPoints = activePlayer === 'red' + ? users.refs[activePlayer].state.mines + : users.refs[inactivePlayer].state.mines, + bluePoints = activePlayer === 'blue' + ? users.refs[activePlayer].state.mines + : users.refs[inactivePlayer].state.mines; + + this.refs.gridControl.stepEvent(coords); + + let mineCache = this.refs.gridControl.state.foundUserMineCache; + redPoints += activePlayer === 'red' ? mineCache : 0; + bluePoints += activePlayer === 'blue' ? mineCache : 0; + + return {red: bluePoints, blue: redPoints}; + } + /** THE END */ makeGameEndIfItEnds(bluePoints, redPoints) { var redWins = redPoints > 2, @@ -129,13 +150,12 @@ class MineSeeker extends React.Component { console.warn('Opponent stepped: Auto-Step process'); this.refs.gridControl.refs.userControl.setState({bombSelected: payload.data.bomb}); - this.refs.gridControl.stepEvent(payload.data.coords); + + /** STEP */ + let points = this.makePointsCalcAndStep(payload.data.coords); /** THE END */ - this.makeGameEndIfItEnds( - this.refs.gridControl.refs.userControl.refs.blue.state.mines, - this.refs.gridControl.refs.userControl.refs.red.state.mines - ); + this.makeGameEndIfItEnds(points.blue, points.red); } } else { @@ -202,30 +222,19 @@ class MineSeeker extends React.Component { if (this.refs.gridControl.checkFieldHasBeenNeverClicked(coords[0], coords[1])) { /** Player step and it is the current player */ if (activePlayer === this.refs.gridControl.state.webPlayer) { - this.refs.gridControl.stepEvent(coords); - - var mineCache = this.refs.gridControl.state.foundUserMineCache, - redPoints = this.refs.gridControl.refs.userControl.refs.red.state.mines + ( - this.refs.gridControl.refs.userControl.refs.red.state.active - ? mineCache - : 0 - ), - bluePoints = this.refs.gridControl.refs.userControl.refs.blue.state.mines + ( - this.refs.gridControl.refs.userControl.refs.blue.state.active - ? mineCache - : 0 - ); + /** STEP */ + let points = this.makePointsCalcAndStep(coords); /** THE END */ - this.makeGameEndIfItEnds(bluePoints, redPoints); + this.makeGameEndIfItEnds(points.blue, points.red); this.state.session .publish(this.state.channel, { 'coords': coords, 'player': activePlayer, 'bomb': this.refs.gridControl.refs.userControl.state.bombSelected, - 'redPoints': redPoints, - 'bluePoints': bluePoints + 'redPoints': points.red, + 'bluePoints': points.blue }); } }