refactor make move to serve game meta data with game record

This commit is contained in:
Sorrel Bri 2020-01-30 23:35:39 -08:00 committed by sorrelbri
parent 76f29cf9d3
commit 23cd485cbb
3 changed files with 25 additions and 7 deletions

View file

@ -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) => {

View file

@ -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) => {

View file

@ -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'
};
};
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
}