From 4336666eef81751bf7cfbdb474f1a8b79ea8668d Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Thu, 30 Jan 2020 23:35:39 -0800 Subject: [PATCH] refactor make move to serve game meta data with game record --- server/services/Game.js | 8 ++++++-- server/services/gameServices.js | 5 ++--- server/test/gameServices.spec.js | 19 +++++++++++++++++-- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/server/services/Game.js b/server/services/Game.js index 3f1250f..bb4ebe6 100644 --- a/server/services/Game.js +++ b/server/services/Game.js @@ -94,6 +94,10 @@ class Game { }, {}) } + getMeta = () => { + return { winner: this.winner, turn: this.turn, pass: this.pass, playerState: this.playerState, gameRecord: this.gameRecord } + } + findPointFromIdx = (arr) => { return this.boardState.find( point => point.pos[0] === arr[0] && point.pos[1] === arr[1] ); } @@ -108,9 +112,9 @@ class Game { point.stone = this.turn; point.joinGroup(this); clearCaptures(this); - this.gameRecord.push(`${STONES_DATA[this.turn]}: ${point.pos}`) + this.gameRecord.push(move) this.turn*= -1; - return this.getBoardState(); + return { board: this.getBoardState(), meta: this.getMeta()}; } clickBoard = (evt) => { diff --git a/server/services/gameServices.js b/server/services/gameServices.js index 341da1a..ccdf29c 100644 --- a/server/services/gameServices.js +++ b/server/services/gameServices.js @@ -12,9 +12,8 @@ const initGame = (game) => { } const makeMove = (game, move) => { - let meta = {}; - const board = gamesInProgress[game.id].makeMove(move); - return {board, meta} + const newState = gamesInProgress[game.id].makeMove(move); + return {...newState} } const getBoard = (gameId) => { diff --git a/server/test/gameServices.spec.js b/server/test/gameServices.spec.js index 852b02a..db513c5 100644 --- a/server/test/gameServices.spec.js +++ b/server/test/gameServices.spec.js @@ -12,7 +12,7 @@ describe('game services', () => { it('games services places move', done => { gameServices.initGame({id: 1, handicap: 4}) const afterMoveOne = gameServices.makeMove({id: 1}, {player: 'white', pos: { X:6, Y:3 }}); - const afterMoveOneShould = { board:{ ...fourHandicapBoard, '6-3': -1}, meta: {} }; + const afterMoveOneShould = { board:{ ...fourHandicapBoard, '6-3': -1}, meta: moveOneMeta }; afterMoveOne.should.eql(afterMoveOneShould); done(); }); @@ -51,4 +51,19 @@ const fourHandicapBoard = { '17-1': 'l','17-2': 'l','17-3': 'l','17-4': 'l','17-5': 'l','17-6': 'l','17-7': 'l','17-8': 'l','17-9': 'l','17-10': 'l','17-11': 'l','17-12': 'l','17-13': 'l','17-14': 'l','17-15': 'l','17-16': 'l','17-17': 'l','17-18': 'l','17-19': 'l', '18-1': 'l','18-2': 'l','18-3': 'l','18-4': 'l','18-5': 'l','18-6': 'l','18-7': 'l','18-8': 'l','18-9': 'l','18-10': 'l','18-11': 'l','18-12': 'l','18-13': 'l','18-14': 'l','18-15': 'l','18-16': 'l','18-17': 'l','18-18': 'l','18-19': 'l', '19-1': 'l','19-2': 'l','19-3': 'l','19-4': 'l','19-5': 'l','19-6': 'l','19-7': 'l','19-8': 'l','19-9': 'l','19-10': 'l','19-11': 'l','19-12': 'l','19-13': 'l','19-14': 'l','19-15': 'l','19-16': 'l','19-17': 'l','19-18': 'l','19-19': 'l' -}; \ No newline at end of file +}; + +const moveOneMeta = { + gameRecord: [ + {player: 'white', pos: { X:6, Y:3 }} + ], + pass: 0, + playerState: { + bCaptures: 0, + bScore: 0, + wCaptures: 0, + wScore: 0 + }, + turn: 1, + winner: null +} \ No newline at end of file