|
@@ -1,17 +1,34 @@
|
|
// var Card = require('./Card.js');
|
|
// var Card = require('./Card.js');
|
|
|
|
+
|
|
const SHA256 = require('crypto-js/sha256');
|
|
const SHA256 = require('crypto-js/sha256');
|
|
-// TO-DO
|
|
|
|
|
|
+
|
|
|
|
+// TODO test
|
|
|
|
|
|
// Need to create an initialization move or something to get verified!
|
|
// Need to create an initialization move or something to get verified!
|
|
// Players can have their names placed on state.players on init, showing turns and colors
|
|
// Players can have their names placed on state.players on init, showing turns and colors
|
|
|
|
|
|
class GameMachine {
|
|
class GameMachine {
|
|
- constructor() {
|
|
|
|
|
|
+ constructor(setup) {
|
|
this.state = {
|
|
this.state = {
|
|
board: new Board(),
|
|
board: new Board(),
|
|
players: [],
|
|
players: [],
|
|
hash: '0123456789',
|
|
hash: '0123456789',
|
|
- stack: []
|
|
|
|
|
|
+ setup: setup,
|
|
|
|
+ stacks: {
|
|
|
|
+ moves: [],
|
|
|
|
+ hashes: []
|
|
|
|
+ }
|
|
|
|
+ };
|
|
|
|
+ this.setPlayers(setup.id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ getStack() {
|
|
|
|
+ return {
|
|
|
|
+ setup: this.state.setup,
|
|
|
|
+ stack: {
|
|
|
|
+ moves: this.state.stacks.moves.map((move) => move.export()),
|
|
|
|
+ hashes: this.state.stacks.hashes
|
|
|
|
+ }
|
|
};
|
|
};
|
|
}
|
|
}
|
|
|
|
|
|
@@ -40,6 +57,10 @@ class GameMachine {
|
|
this.state = { ...this.state, players};
|
|
this.state = { ...this.state, players};
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ getMyTeam(token) {
|
|
|
|
+ return this.state.setup.id.indexOf(token);
|
|
|
|
+ }
|
|
|
|
+
|
|
getPlayerNumber(player) {
|
|
getPlayerNumber(player) {
|
|
return this.state.players[0] === player ? 0 : 1;
|
|
return this.state.players[0] === player ? 0 : 1;
|
|
// return this.state.players[0] === UserService.getUsername() ? 0 : 1;
|
|
// return this.state.players[0] === UserService.getUsername() ? 0 : 1;
|
|
@@ -50,7 +71,7 @@ class GameMachine {
|
|
}
|
|
}
|
|
|
|
|
|
hasFinished() {
|
|
hasFinished() {
|
|
- return this.state.stack.length >= 10;
|
|
|
|
|
|
+ return this.state.stacks.hashes.length >= 10;
|
|
}
|
|
}
|
|
|
|
|
|
getWinner() {
|
|
getWinner() {
|
|
@@ -65,24 +86,26 @@ class GameMachine {
|
|
}
|
|
}
|
|
|
|
|
|
isMyTurn(player) {
|
|
isMyTurn(player) {
|
|
- let moves = this.state.stack.length;
|
|
|
|
|
|
+ let moves = this.state.stacks.hashes.length;
|
|
return this.getPlayerNumber(player) === moves % 2 && moves < 10;
|
|
return this.getPlayerNumber(player) === moves % 2 && moves < 10;
|
|
}
|
|
}
|
|
|
|
|
|
needFinalization() {
|
|
needFinalization() {
|
|
- return this.state.stack.length === 9;
|
|
|
|
|
|
+ return this.state.stacks.hashes.length === 9;
|
|
}
|
|
}
|
|
|
|
|
|
runMove(move) {
|
|
runMove(move) {
|
|
const spray = SHA256(JSON.stringify(move)).toString();
|
|
const spray = SHA256(JSON.stringify(move)).toString();
|
|
- if (this.state.stack.includes(spray)) {
|
|
|
|
|
|
+ if (this.state.stacks.hashes.includes(spray)) {
|
|
console.log('This move has been processed already');
|
|
console.log('This move has been processed already');
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
try {
|
|
try {
|
|
move.verify(this.state);
|
|
move.verify(this.state);
|
|
move.performMove(this.state.board);
|
|
move.performMove(this.state.board);
|
|
- this.state.stack.push(spray);
|
|
|
|
|
|
+ this.state.stacks.moves.push(move);
|
|
|
|
+ this.state.stacks.hashes.push(spray);
|
|
|
|
+ console.log(this.state.stacks);
|
|
} catch (e) {
|
|
} catch (e) {
|
|
throw e;
|
|
throw e;
|
|
}
|
|
}
|
|
@@ -92,13 +115,11 @@ class GameMachine {
|
|
return this.state.board.owners[x];
|
|
return this.state.board.owners[x];
|
|
}
|
|
}
|
|
|
|
|
|
- validateMatch(stack) {
|
|
|
|
- this.setPlayers()
|
|
|
|
|
|
+ runMatch(stack) {
|
|
for (let i = 0; i < 9; i++) {
|
|
for (let i = 0; i < 9; i++) {
|
|
this.runMove(stack[i]);
|
|
this.runMove(stack[i]);
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|
|
|
|
+ return this.getWinner();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|