Private
Public Access
1
0

show left mines after end #2 && reduce network traffic && better active field checking method

This commit is contained in:
2016-11-19 12:09:05 +01:00
parent 25cc3910f5
commit 2cb38f2681
4 changed files with 154 additions and 126 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

View File

@@ -28,19 +28,25 @@ class MineSeeker extends React.Component {
return text; return text;
} }
/** game end control */ /** THE END */
makeGameEndIfItEnds(bluePoints, redPoints) { makeGameEndIfItEnds(bluePoints, redPoints) {
var redWins = redPoints > 2, var redWins = redPoints > 2,
blueWins = bluePoints > 2; blueWins = bluePoints > 2;
if (redWins || blueWins) { if (redWins || blueWins) {
this.refs.gridControl.state.sound.won.play();
this.refs.gridControl.setState({ this.refs.gridControl.setState({
overlay: true, overlay: true,
overlayTitle: (redWins ? 'Red' : 'Blue') + " wins the game!", overlayTitle: (redWins ? 'Red' : 'Blue') + " wins the game!",
overlaySubTitle: "Play again!" overlaySubTitle: "Play again!"
}); });
this.refs.gridControl.state.sound.won.play(); this.refs.gridControl.showLeftMines();
this.refs.gridControl.refs.userControl.setState({ activePlayer: false});
this.refs.gridControl.refs.userControl.refs.red.setState({ active: false });
this.refs.gridControl.refs.userControl.refs.blue.setState({ active: false });
} }
} }
@@ -107,7 +113,7 @@ class MineSeeker extends React.Component {
this.refs.gridControl.refs.userControl.setState({bombSelected: payload.data.bomb}); this.refs.gridControl.refs.userControl.setState({bombSelected: payload.data.bomb});
this.refs.gridControl.stepEvent(payload.data.coords); this.refs.gridControl.stepEvent(payload.data.coords);
/** End-game control */ /** THE END */
this.makeGameEndIfItEnds( this.makeGameEndIfItEnds(
this.refs.gridControl.refs.userControl.refs.blue.state.mines, this.refs.gridControl.refs.userControl.refs.blue.state.mines,
this.refs.gridControl.refs.userControl.refs.red.state.mines this.refs.gridControl.refs.userControl.refs.red.state.mines
@@ -159,6 +165,8 @@ class MineSeeker extends React.Component {
onClick(coords) { onClick(coords) {
var activePlayer = this.refs.gridControl.refs.userControl.state.activePlayer ? 'blue' : 'red'; var activePlayer = this.refs.gridControl.refs.userControl.state.activePlayer ? 'blue' : 'red';
/** if the clicked field is NEVER CLICKED */
if (this.refs.gridControl.checkFieldHasBeenNeverClicked(coords[0], coords[1])) {
/** Player step and it is the current player */ /** Player step and it is the current player */
if (activePlayer === this.refs.gridControl.state.webPlayer) { if (activePlayer === this.refs.gridControl.state.webPlayer) {
this.refs.gridControl.stepEvent(coords); this.refs.gridControl.stepEvent(coords);
@@ -175,7 +183,7 @@ class MineSeeker extends React.Component {
: 0 : 0
); );
/** End-game control */ /** THE END */
this.makeGameEndIfItEnds(bluePoints, redPoints); this.makeGameEndIfItEnds(bluePoints, redPoints);
this.state.session this.state.session
@@ -188,6 +196,7 @@ class MineSeeker extends React.Component {
}); });
} }
} }
}
render() { render() {
return ( return (

View File

@@ -47,6 +47,10 @@ class GridControl extends React.Component {
return typeof this.state.grid[row] !== 'undefined' && typeof this.state.grid[row][col] !== 'undefined' && this.state.grid[row][col] !== 'm'; return typeof this.state.grid[row] !== 'undefined' && typeof this.state.grid[row][col] !== 'undefined' && this.state.grid[row][col] !== 'm';
} }
checkFieldHasBeenNeverClicked(row, col) {
return this.state.updatedFieldCache.indexOf(this.refString(row, col)) < 0 && !this.refs[this.refString(row, col)].state.active;
}
getBombRadius(row, col) { getBombRadius(row, col) {
let isBombTargetCenter = row > 1 && row < this.state.grid.length - 2 && col > 1 && col < this.state.grid[row].length - 2; let isBombTargetCenter = row > 1 && row < this.state.grid.length - 2 && col > 1 && col < this.state.grid[row].length - 2;
@@ -146,6 +150,20 @@ class GridControl extends React.Component {
} }
} }
showLeftMines() {
for (let i = 0, j = this.state.grid.length; i < j; i++) {
for (let k = 0, l = this.state.grid[i].length; k < l; k++) {
let currentField = this.refs[this.refString(i, k)];
if (this.state.grid[i][k] === 'm' && this.checkFieldHasBeenNeverClicked(i, k)) {
currentField.setState({
currentImage: currentField.state.icons.root + currentField.state.icons.left
});
}
}
}
}
/** set __ACTIVE__ player in the UserControl !!!! */ /** set __ACTIVE__ player in the UserControl !!!! */
changePlayer(idx, max, currentObject) { changePlayer(idx, max, currentObject) {
var userControl = this.refs.userControl, var userControl = this.refs.userControl,
@@ -183,7 +201,7 @@ class GridControl extends React.Component {
active: true active: true
}); });
if (this.state.updatedFieldCache.indexOf(this.refString(row, col)) < 0) { if (this.checkFieldHasBeenNeverClicked(row, col)) {
this.state.updatedFieldCache.push(this.refString(row, col)); this.state.updatedFieldCache.push(this.refString(row, col));
} }
@@ -215,8 +233,6 @@ class GridControl extends React.Component {
activePlayer = userControl.state.activePlayer ? 'blue' : 'red', activePlayer = userControl.state.activePlayer ? 'blue' : 'red',
inactivePlayer = userControl.state.activePlayer ? 'red' : 'blue'; inactivePlayer = userControl.state.activePlayer ? 'red' : 'blue';
/** if the clicked field is NEVER CLICKED */
if (!gridFieldControl.state.active) {
/** update LAST CLICKED grid field */ /** update LAST CLICKED grid field */
if (!justOnFirstIteration) { if (!justOnFirstIteration) {
if (this.state.lastClicked[activePlayer] !== null) { if (this.state.lastClicked[activePlayer] !== null) {
@@ -279,7 +295,6 @@ class GridControl extends React.Component {
}); });
} }
} }
}
/** /**
* Show elems w/ conditions * Show elems w/ conditions
@@ -301,6 +316,8 @@ class GridControl extends React.Component {
* @param coords * @param coords
*/ */
stepEvent(coords) { stepEvent(coords) {
/** if the clicked field is NEVER CLICKED */
if (this.checkFieldHasBeenNeverClicked(coords[0], coords[1])) {
var activePlayer = this.refs.userControl.state.activePlayer ? 'blue' : 'red'; var activePlayer = this.refs.userControl.state.activePlayer ? 'blue' : 'red';
this.state.foundUserMineCache = 0; this.state.foundUserMineCache = 0;
@@ -348,6 +365,7 @@ class GridControl extends React.Component {
this.bombClear(); this.bombClear();
} }
} }
}
/** /**
* On Hover when you want to drop BOMB * On Hover when you want to drop BOMB

View File

@@ -27,7 +27,8 @@ class GridField extends React.Component {
lastRed: 'bg-last-red-outbg.png', lastRed: 'bg-last-red-outbg.png',
crosshair: 'bg-target-outbg.png', crosshair: 'bg-target-outbg.png',
crosshairBomb: 'bg-target-bomb-outbg.png' crosshairBomb: 'bg-target-bomb-outbg.png'
} },
left: 'bg-left-mine-outbg.png'
} }
}; };
} }