From a99426e03f3151991f449991561f250d17efd4a4 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Sat, 2 May 2020 22:04:22 -0700 Subject: [PATCH] Game.returnToMove(x) where x >= 0 returns game state at move x --- packages/server/services/Game.v2.js | 20 +++++++++----- packages/server/test/Game.v2.spec.js | 41 +++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/packages/server/services/Game.v2.js b/packages/server/services/Game.v2.js index 9232357..88d1f9d 100644 --- a/packages/server/services/Game.v2.js +++ b/packages/server/services/Game.v2.js @@ -238,14 +238,20 @@ const Game = ({gameData = {}, gameRecord = []} = {}) => { 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)] - }) + returnToMove: function(lastMove) { + const { komi, handicap, boardSize } = this; + if (lastMove === 0) { + return Game({ + gameData: { komi, handicap, boardSize } + }).initGame(); } + const length = this.gameRecord.length; + const index = lastMove < 0 ? length + lastMove : lastMove; + if (lastMove >= length && lastMove > 0) return this; + return Game({ + gameData: { komi, handicap, boardSize }, + gameRecord: [...this.gameRecord.slice(0, index)] + }); } } }; diff --git a/packages/server/test/Game.v2.spec.js b/packages/server/test/Game.v2.spec.js index c609a06..1f16d19 100644 --- a/packages/server/test/Game.v2.spec.js +++ b/packages/server/test/Game.v2.spec.js @@ -405,8 +405,14 @@ describe('capture logic: snapback, ko and playing in eyes', () => { }); describe('Game history functionality', () => { - const firstMove = { player: 'black', pos: { x: 4, y: 4 }}; - const secondMove = { player: 'white', pos: { x: 16, y: 16 }}; + const firstMove = { player: 'black', pos: { x: 4, y: 4 }}; + const secondMove = { player: 'white', pos: { x: 16, y: 16 }}; + const thirdMove = { player: 'black', pos: { x: 16, y: 4 } }; + const fourthMove = { player: 'white', pos: { x: 4, y: 16 }}; + const fifthMove = { player: 'black', pos: { x: 10, y: 4 } }; + const sixthMove = { player: 'white', pos: { x: 4, y: 10 }}; + const seventhMove = { player: 'black', pos: { x: 10, y: 16 } }; + const eighthMove = { player: 'white', pos: { x: 16, y: 10 }}; it('makeMove creates gameRecord item', done => { Game().initGame() @@ -423,16 +429,16 @@ describe('Game history functionality', () => { }); const rewoundGame = () => Game().initGame() - .makeMove(firstMove) - .makeMove(secondMove) - .makeMove({ player: 'black', pos: { x: 16, y: 4 } }) - .returnToMove(-1); + .makeMove(firstMove) + .makeMove(secondMove) + .makeMove(thirdMove) + .returnToMove(-1); it('Game.returnToMove returns new Game with gameRecord', done => { rewoundGame() .gameRecord.should.eql([ firstMove, secondMove ]) done(); - }) + }); it('Game.returnToMove returns new Game with new board state', done => { rewoundGame() @@ -442,6 +448,27 @@ describe('Game history functionality', () => { rewoundGame() .boardState['16-16'].stone.should.eql(-1); done(); + }); + + const resetGame = () => [ + firstMove, secondMove, thirdMove, fourthMove, fifthMove, sixthMove, seventhMove, eighthMove + ].reduce((game, move) => game.makeMove(move), Game().initGame()); + + it('Game.returnToMove(0) returns to init board state', done => { + const erasedGame = resetGame() + .returnToMove(0) + erasedGame.gameRecord.should.eql([]) + erasedGame.boardState['4-4'].stone.should.eql(0) + done(); + }); + + it('Game.returnToMove(5) returns to state after 5th move', done => { + const fifthMoveGame = resetGame() + .returnToMove(5); + fifthMoveGame.gameRecord.should.eql([firstMove, secondMove, thirdMove, fourthMove, fifthMove]); + fifthMoveGame.boardState['10-4'].stone.should.eql(1) + fifthMoveGame.boardState['4-10'].stone.should.eql(0) + done(); }) })