Browse Source

Match moves run

Dim Dim 6 years ago
parent
commit
e6e883a2fb
2 changed files with 37 additions and 18 deletions
  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');
 // 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();
     }
     }
 }
 }
 
 

+ 4 - 6
services/matchmaking/randomFree.js

@@ -63,15 +63,13 @@ class randomFree {
                     p1.on('broadcast', broadcast);
                     p1.on('broadcast', broadcast);
                     p2.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
                         // 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)
                         //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);
                     p1.once('gameOver', gameOver);
                     p2.once('gameOver', gameOver);
                     p2.once('gameOver', gameOver);