瀏覽代碼

Correct ownership on Game

Nikatlas 6 年之前
父節點
當前提交
6f36403a3f

+ 3 - 0
debug.log

@@ -2,3 +2,6 @@
 [1002/122225.068:ERROR:crash_report_database_win.cc(428)] unexpected header
 [1002/122240.056:ERROR:crash_report_database_win.cc(428)] unexpected header
 [1002/122619.337:ERROR:crash_report_database_win.cc(428)] unexpected header
+[1002/133139.195:ERROR:crash_report_database_win.cc(428)] unexpected header
+[1002/133205.655:ERROR:crash_report_database_win.cc(428)] unexpected header
+[1002/134049.134:ERROR:crash_report_database_win.cc(428)] unexpected header

+ 6 - 2
src/Game/game/machine/GameMachine.js

@@ -42,8 +42,12 @@ class GameMachine {
         // return this.state.players[0] === UserService.getUsername() ? 0 : 1;
     }
 
+    getPositionTeam(x) {
+        return this.state.players.indexOf(this.state.board.owners[x]);
+    }
+
     runMove(move) {
-        const spray = SHA256(JSON.stringify(move)).toString(); 
+        const spray = SHA256(JSON.stringify(move)).toString();
         if (this.state.stack.includes(spray)) {
             console.log('This move has been processed already');
             return;
@@ -51,7 +55,7 @@ class GameMachine {
         try {
             move.performMove(this.state.board);
             this.state.stack.push(spray);
-        } catch (e) { 
+        } catch (e) {
             throw e;
         }
     }

+ 8 - 7
src/Game/services/GameService.js

@@ -1,6 +1,6 @@
 import Game from '../game/';
 import SocketService from '../services/SocketService';
-import UserService from '../services/UserService';
+import UserService from './UserService';
 
 class GameService {
     constructor() {
@@ -21,22 +21,23 @@ class GameService {
         this.state.salts = game.cards.saltArray;
         this.state.setup = game.setup;
 
-        // this.GameMachine.setPlayers([game.setup.id.playerOne, game.setup.id.playerTwo]);
-        
-        
+        this.GameMachine.setPlayers(game.setup.id);
+
         if(this.onInit) this.onInit();
         SocketService.on('move', (data) => this.move(data));
         SocketService.on('result', (data) => this.end(data));
     }
 
+    getMyTeam() {
+        return this.state.setup.id.indexOf(UserService.getToken());
+    }
+
     move(data) {
         // Game Machine perform internal Move
         let {cardid, position, player} = data;
 
-        let playerNum = player === UserService.getUsername();
         const card = new Game.Card(cardid);
-        const move = new Game.GameMoves.PlaceMove(card, position, playerNum);
-
+        const move = new Game.GameMoves.PlaceMove(card, position, player);
         try{
             this.GameMachine.runMove(move);
         } catch (e) {

+ 9 - 2
src/Game/services/SocketService.js

@@ -1,21 +1,28 @@
 import Net from './Net.js';
 import openSocket from 'socket.io-client';
+
+import UserService from './UserService';
+
 class SocketService {
 
     constructor() {
         // check persistence - reconnect 
     }
-    
+
     openSocket(channel) {
         if(this.socket)
             this.socket.disconnect();
-        this.socket = openSocket('http://localhost:3555/' + channel);
+        this.socket = openSocket('http://localhost:3555/' + channel, { query: "token=" + UserService.getToken() });
         this.on = this.socket.on.bind(this.socket);
         this.once = this.socket.once.bind(this.socket);
         this.emit = this.socket.emit.bind(this.socket);
         this.close = this.socket.close.bind(this.socket);
     }
 
+    getId() {
+        return this.socket.id;
+    }
+
 
 }
 

+ 5 - 3
src/Game/views/buildings/Board.js

@@ -67,20 +67,22 @@ class BoardHandler extends GuiableContainer{
         this.addChild(this.score);
     }
 
-    sync = (board) => {
+    sync = (GameMachine) => {
+        let board = GameMachine.state.board;
         const owners = board.owners;
         const data = board.data;
         console.log(owners);
         this.holders.forEach((holder, index) => {
             if ( data[index] && !holder.isEmpty() ) {
-                holder.getCard().setTeam(owners[index]);
+                holder.getCard().setTeam(GameMachine.getPositionTeam(index));
             } else if ( data[index] ) {
                 const card = new Card({id: data[index].id});
                 this.addChild(card);
                 holder.occupy(card);
-                card.setTeam(owners[index]);
+                card.setTeam(GameMachine.getPositionTeam(index));
             }
         })
+        this.updateScore();
     }
 
     updateScore() {

+ 4 - 2
src/Game/views/buildings/Deck.js

@@ -5,6 +5,8 @@ import GuiableContainer from '../../../helpers/Guiable';
 // import Deck from '../../assets/deck';
 import CardHolder from '../base/CardHolder';
 
+import GameService from '../../services/GameService';
+
 import Card from '../base/Card';
 
 
@@ -69,12 +71,12 @@ class DeckHandler extends GuiableContainer{
         this.cards.forEach((item) => item.setTeam(team));
     }
 
-    sync(cards) {
+    sync(cards, team) {
         if(!cards)return;
         this.cards.forEach((holder, index) => {
             const card = new Card({id: cards[index]});
             this.addChild(card);
-            card.setTeam();
+            card.setTeam(team);
             card.attach(holder);
         });
     }

+ 5 - 3
src/Game/views/demo/Board.js

@@ -25,17 +25,19 @@ class BoardDemo extends PIXI.Container{
             const move = {
                 cardid: card.id,
                 position: position,
-                player: UserService.getUsername()
+                player: UserService.getToken()
             };
             SocketService.emit('broadcast', move);
         }
 
         GameService.onInit = () => {
-            this.deck.sync(GameService.state.cards);
+            console.log("GameService onInit : from './demo/Board.js'");
+            this.deck.sync(GameService.state.cards, GameService.getMyTeam());
         }
 
         GameService.onUpdate = () => {
-            this.board.sync(GameService.GameMachine.state.board);
+            console.log("GameService onUpdate : from './demo/Board.js'");
+            this.board.sync(GameService.GameMachine);
         }
     }
     

+ 1 - 3
src/Game/views/demo/BoardPlay.js

@@ -103,8 +103,6 @@ class BoardPlayDemo extends GuiableContainer{
         }));
     }
 
-
-
     saveState = () => {
         let state = GameMachine.save();
         alert(JSON.stringify({ state: state, freeSlots: this.freeSlots}));
@@ -127,7 +125,7 @@ class BoardPlayDemo extends GuiableContainer{
 
 
     sync() {
-
+        
     }
 
     _kill = () => {}