diff --git a/README.md b/README.md index fc3cb1a..76bc1db 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ Adjacent points without stones are very important to the state of a point as wel ![Image of Game logic](public/game-logic.png) ### Game Records -For the moment, game records are modeled as a list where each move has the type `{player: , pos: {x: , y: }}`. +Game records are modeled as a list where each move has the type `{player: , pos: {x: , y: }, number: }`. The database `move` table contains additional information `number`, `game_record`, and `prior_move` in addition to a foreign key for a `game` row. `number` represents the move number in a game record (for now this corresponds to list position), `game_record` is a boolean representing whether the move was 'official' or is an alternate move used in study, and `prior_move` is a reference to another `move` row allowing for the construction of a tree model for the game (see [Expanding this representation](#expanding-this-representation), below.) @@ -47,7 +47,7 @@ There is a backend service that processes this list of moves into a current boar This is a customary representation of game records printed in Go literature. A frontend service processes the list of moves and plots each move onto a `` element drawn to resemble the grid lines on a board, with moves that are placed at prior points plotted on an additional `` element below. #### Expanding this representation -This representation will be expanded when support for alternate game paths is added. Alternate game paths will allow users to study completed games by playing out the consequences of moves not in the completed game. This feature will require a tree structure for the game record where moves on the main line are referred with a `main` property on each move node and alternate moves with any number of alternate references. +The list representation is expanded for alternate game paths with the addition of sub-lists in places where paths diverge. Alternate game paths allow users to study completed games by playing out the consequences of moves not in the completed game. Each move with a diverging path contains lists for each path, with the official path being the first. ### Caching multiple in-progress games ![Image of Game module in context](public/game-module.png) diff --git a/packages/server/services/Game.js b/packages/server/services/Game.js index b8b94e5..c52c101 100644 --- a/packages/server/services/Game.js +++ b/packages/server/services/Game.js @@ -184,7 +184,8 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => { if (gameRecord.length) { // play through all the moves const game = gameRecord.reduce( - (game, move) => game.makeMove(move), + (game, move) => + move.length ? game.makeMove(move[0]) : game.makeMove(move), Game({ gameData }).initGame() ); // ? why is this being wrapped? diff --git a/packages/server/test/Game.spec.js b/packages/server/test/Game.spec.js index 882ca68..4572b2a 100644 --- a/packages/server/test/Game.spec.js +++ b/packages/server/test/Game.spec.js @@ -608,14 +608,34 @@ 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 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 } }; + const firstMove = { + player: "black", + pos: { x: 4, y: 4 }, + id: 1, + prior: null, + }; + const secondMove = { + player: "white", + pos: { x: 16, y: 16 }, + id: 2, + prior: 1, + }; + const thirdMove = { player: "black", pos: { x: 16, y: 4 }, id: 3, prior: 2 }; + const fourthMove = { player: "white", pos: { x: 4, y: 16 }, id: 4, prior: 3 }; + const fifthMove = { player: "black", pos: { x: 10, y: 4 }, id: 5, prior: 4 }; + const sixthMove = { player: "white", pos: { x: 4, y: 10 }, id: 6, prior: 5 }; + const seventhMove = { + player: "black", + pos: { x: 10, y: 16 }, + id: 7, + prior: 6, + }; + const eighthMove = { + player: "white", + pos: { x: 16, y: 10 }, + id: 8, + prior: 7, + }; it("makeMove creates gameRecord item", (done) => { Game().initGame().makeMove(firstMove).gameRecord[0].should.eql(firstMove); @@ -728,11 +748,11 @@ describe("Game end logic", () => { it("two nonconsecutive passes continue game", (done) => { Game() .initGame() - .makeMove({ player: "black", pos: { x: 4, y: 4 } }) - .makeMove({ player: "white", pos: { x: 4, y: 5 } }) - .makeMove({ player: "black", pos: { x: 5, y: 3 } }) + .makeMove({ player: "black", pos: { x: 4, y: 4 }, id: 1, prior: null }) + .makeMove({ player: "white", pos: { x: 4, y: 5 }, id: 2, prior: 1 }) + .makeMove({ player: "black", pos: { x: 5, y: 3 }, id: 3, prior: 2 }) .submitPass("white") - .makeMove({ player: "black", pos: { x: 16, y: 16 } }) + .makeMove({ player: "black", pos: { x: 16, y: 16 }, id: 4, prior: 3 }) .submitPass("white") .getMeta() .should.eql({ @@ -740,12 +760,38 @@ describe("Game end logic", () => { pass: 1, turn: 1, gameRecord: [ - { player: "black", pos: { x: 4, y: 4 } }, - { player: "white", pos: { x: 4, y: 5 } }, - { player: "black", pos: { x: 5, y: 3 } }, - { player: "white", pos: { x: null, y: null } }, - { player: "black", pos: { x: 16, y: 16 } }, - { player: "white", pos: { x: null, y: null } }, + { + player: "black", + pos: { x: 4, y: 4 }, + id: 1, + prior: null, + }, + { + player: "white", + pos: { x: 4, y: 5 }, + id: 2, + prior: 1, + }, + { + player: "black", + pos: { x: 5, y: 3 }, + id: 3, + prior: 2, + }, + { + player: "white", + pos: { x: null, y: null }, + }, + { + player: "black", + pos: { x: 16, y: 16 }, + id: 4, + prior: 3, + }, + { + player: "white", + pos: { x: null, y: null }, + }, ], }); done(); diff --git a/packages/server/test/gameServices.spec.js b/packages/server/test/gameServices.spec.js index b50f4d6..0320af5 100644 --- a/packages/server/test/gameServices.spec.js +++ b/packages/server/test/gameServices.spec.js @@ -20,7 +20,7 @@ describe("game services", () => { it("games services places move", async () => { gameServices.initGame({ id: 1, handicap: 4 }); - const move = { player: "white", pos: { x: 6, y: 3 } }; + const move = { player: "white", pos: { x: 6, y: 3 }, id: 1, prior: null }; const afterMove = await gameServices.makeMove({ id: 1, move }); const afterMoveShould = { board: { ...fourHandicapBoard, "6-3": -1 },