Dim Dim 6 лет назад
Родитель
Сommit
e6e883a2fb
2 измененных файлов с 37 добавлено и 18 удалено
  1. 33 12
      game/machine/GameMachine.js
  2. 4 6
      services/matchmaking/randomFree.js

+ 33 - 12
game/machine/GameMachine.js

@@ -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();
     }
 }
 

+ 4 - 6
services/matchmaking/randomFree.js

@@ -63,15 +63,13 @@ class randomFree {
                     p1.on('broadcast', broadcast);
                     p2.on('broadcast', broadcast);
 
-                    const gameOver = (matchStack) => {
+                    const gameOver = (stack) => {
                         // TODO this should be emitted ONLY if all 9 moves have been played
-                        let gameMachine = new GameMachine();
-                        let result;
-                        // result = gameService.lastMove(data);
                         //TODO verify move stack (compare with hashed cards)
+                        let gameMachine = new GameMachine(stack.setup);
+                        let winner = gameMachine.runMatch(stack.moves);
 
-
-                        nsp.in(channel).emit('result', result);
+                        nsp.in(channel).emit('winner', winner);
                     };
                     p1.once('gameOver', gameOver);
                     p2.once('gameOver', gameOver);