From bdeb9c9d867a9c44856e5a448a54445d6d1a4c73 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Sat, 2 May 2020 21:11:54 -0700 Subject: [PATCH] init Game.returnToMove --- packages/server/services/Game.v2.js | 15 +++++++++++++ packages/server/test/Game.v2.spec.js | 33 ++++++++++++++++++++++++++-- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/server/services/Game.v2.js b/packages/server/services/Game.v2.js index ba4b4c0..262f737 100644 --- a/packages/server/services/Game.v2.js +++ b/packages/server/services/Game.v2.js @@ -191,6 +191,10 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({ return this; }, + addToRecord: function(moveObject) { + this.gameRecord.push(moveObject); + }, + getMeta: function() { // cannot be chained // does not affect game object @@ -212,6 +216,7 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({ || ( game.turn === -1 && player === 'white' ); if (isTurn) { if (point.legal) { + game.addToRecord({ player, pos: { x, y } }); if (this.kos.length) this.clearKo(); point.makeMove(game); game.turn *= -1; @@ -226,6 +231,16 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => ({ const group = Symbol(`${point.pos.x}-${point.pos.y}`); this.groups[group] = { stones: new Set(), liberties: new Set()}; return { game: this, group }; + }, + + returnToMove: function(idx) { + if (idx < 0) { + const { komi, handicap, boardSize } = this; + return Game({ + gameData: { komi, handicap, boardSize }, + gameRecord: [...this.gameRecord.slice(0, this.gameRecord.length + idx)] + }) + } } }); diff --git a/packages/server/test/Game.v2.spec.js b/packages/server/test/Game.v2.spec.js index 01b95a7..f429d2a 100644 --- a/packages/server/test/Game.v2.spec.js +++ b/packages/server/test/Game.v2.spec.js @@ -348,11 +348,10 @@ describe('capture logic: snapback, ko and playing in eyes', () => { .makeMove({ player: 'white', pos: { x: 16, y: 16 } }) .makeMove({ player: 'black', pos: { x: 6, y: 4 } }) .makeMove({ player: 'white', pos: { x: 5, y: 5 } }) + .makeMove({ player: 'black', pos: { x: 5, y: 6 } }); it('snapback functions properly', done => { - console.log(snapbackGame().boardState['5-6']) snapbackGame() - .makeMove({ player: 'black', pos: { x: 5, y: 6 } }) .success.should.eql(true); done(); }); @@ -402,7 +401,37 @@ describe('capture logic: snapback, ko and playing in eyes', () => { .makeMove({ player: 'white', pos: { x: 4, y: 16 } }) .legalMoves['5-5'].should.eql('l'); done(); + }); +}); + +describe('Game history functionality', () => { + const firstMove = { player: 'black', pos: { x: 4, y: 4 }}; + const secondMove = { player: 'white', pos: { x: 16, y: 16 }}; + + it('makeMove creates gameRecord item', done => { + Game().initGame() + .makeMove(firstMove).gameRecord[0].should.eql(firstMove); + done(); + }); + + it('makeMove holds history', done => { + const game = Game().initGame() + .makeMove(firstMove).makeMove(secondMove); + game.gameRecord[0].should.eql(firstMove); + game.gameRecord[1].should.eql(secondMove) + done(); + }); + + it('Game.returnToMove returns new Game with gameRecord', done => { + Game().initGame() + .makeMove(firstMove) + .makeMove(secondMove) + .makeMove({ player: 'black', pos: { x: 16, y: 4 } }) + .returnToMove(-1) + .gameRecord.should.eql([ firstMove, secondMove ]) + done(); }) + // .boardState['16-4'].stone.should.eql(0) })