|
@@ -1,17 +1,34 @@
|
|
|
// var Card = require('./Card.js');
|
|
|
+
|
|
|
const SHA256 = require('crypto-js/sha256');
|
|
|
-// TO-DO
|
|
|
+
|
|
|
+// TODO test
|
|
|
|
|
|
// 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
|
|
|
|
|
|
class GameMachine {
|
|
|
- constructor() {
|
|
|
+ constructor(setup) {
|
|
|
this.state = {
|
|
|
board: new Board(),
|
|
|
players: [],
|
|
|
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};
|
|
|
}
|
|
|
|
|
|
+ getMyTeam(token) {
|
|
|
+ return this.state.setup.id.indexOf(token);
|
|
|
+ }
|
|
|
+
|
|
|
getPlayerNumber(player) {
|
|
|
return this.state.players[0] === player ? 0 : 1;
|
|
|
// return this.state.players[0] === UserService.getUsername() ? 0 : 1;
|
|
@@ -50,7 +71,7 @@ class GameMachine {
|
|
|
}
|
|
|
|
|
|
hasFinished() {
|
|
|
- return this.state.stack.length >= 10;
|
|
|
+ return this.state.stacks.hashes.length >= 10;
|
|
|
}
|
|
|
|
|
|
getWinner() {
|
|
@@ -65,24 +86,26 @@ class GameMachine {
|
|
|
}
|
|
|
|
|
|
isMyTurn(player) {
|
|
|
- let moves = this.state.stack.length;
|
|
|
+ let moves = this.state.stacks.hashes.length;
|
|
|
return this.getPlayerNumber(player) === moves % 2 && moves < 10;
|
|
|
}
|
|
|
|
|
|
needFinalization() {
|
|
|
- return this.state.stack.length === 9;
|
|
|
+ return this.state.stacks.hashes.length === 9;
|
|
|
}
|
|
|
|
|
|
runMove(move) {
|
|
|
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');
|
|
|
return;
|
|
|
}
|
|
|
try {
|
|
|
move.verify(this.state);
|
|
|
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) {
|
|
|
throw e;
|
|
|
}
|
|
@@ -92,13 +115,11 @@ class GameMachine {
|
|
|
return this.state.board.owners[x];
|
|
|
}
|
|
|
|
|
|
- validateMatch(stack) {
|
|
|
- this.setPlayers()
|
|
|
+ runMatch(stack) {
|
|
|
for (let i = 0; i < 9; i++) {
|
|
|
this.runMove(stack[i]);
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
+ return this.getWinner();
|
|
|
}
|
|
|
}
|
|
|
|