Compare commits
8 commits
game_recor
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
08745443ca | ||
|
177d3a4c42 | ||
|
1d804694b7 | ||
|
a40253a4fd | ||
|
1c470ffec0 | ||
|
27cf281670 | ||
|
0fad98a1d0 | ||
|
14a94be59e |
11 changed files with 755 additions and 12111 deletions
21
LICENSE
Normal file
21
LICENSE
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) 2021 Sorrel
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
|
@ -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)
|
![Image of Game logic](public/game-logic.png)
|
||||||
|
|
||||||
### Game Records
|
### Game Records
|
||||||
Game records are modeled as a list where each move has the type `{player: <string: player color>, pos: {x: <integer>, y: <integer>}, number: <integer>}`.
|
For the moment, game records are modeled as a list where each move has the type `{player: <string: player color>, pos: {x: <integer>, y: <integer>}}`.
|
||||||
|
|
||||||
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.)
|
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 `<canvas>` element drawn to resemble the grid lines on a board, with moves that are placed at prior points plotted on an additional `<canvas>` element below.
|
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 `<canvas>` element drawn to resemble the grid lines on a board, with moves that are placed at prior points plotted on an additional `<canvas>` element below.
|
||||||
|
|
||||||
#### Expanding this representation
|
#### Expanding this representation
|
||||||
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.
|
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.
|
||||||
|
|
||||||
### Caching multiple in-progress games
|
### Caching multiple in-progress games
|
||||||
![Image of Game module in context](public/game-module.png)
|
![Image of Game module in context](public/game-module.png)
|
||||||
|
|
12680
package-lock.json
generated
12680
package-lock.json
generated
File diff suppressed because it is too large
Load diff
6
packages/play-node-go/package-lock.json
generated
6
packages/play-node-go/package-lock.json
generated
|
@ -8500,9 +8500,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"lodash": {
|
"lodash": {
|
||||||
"version": "4.17.15",
|
"version": "4.17.19",
|
||||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
|
||||||
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
|
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ=="
|
||||||
},
|
},
|
||||||
"lodash._reinterpolate": {
|
"lodash._reinterpolate": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
|
|
|
@ -48,9 +48,6 @@ const Point = (props) => {
|
||||||
};
|
};
|
||||||
return dispatch(action);
|
return dispatch(action);
|
||||||
}
|
}
|
||||||
const priorMove = meta.gameRecord.length
|
|
||||||
? meta.gameRecord[meta.gameRecord.length - 1].id
|
|
||||||
: null;
|
|
||||||
const action = {
|
const action = {
|
||||||
type: "SOCKET",
|
type: "SOCKET",
|
||||||
message: "MAKE_MOVE",
|
message: "MAKE_MOVE",
|
||||||
|
@ -59,7 +56,7 @@ const Point = (props) => {
|
||||||
game,
|
game,
|
||||||
room: game.room,
|
room: game.room,
|
||||||
board: {},
|
board: {},
|
||||||
move: { player: turn, pos: { x: posX, y: posY }, priorMove },
|
move: { player: turn, pos: { x: posX, y: posY } },
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
dispatch(action);
|
dispatch(action);
|
||||||
|
|
|
@ -3,22 +3,12 @@ const knex = require("../db");
|
||||||
const findGameRecord = async (gameId) => {
|
const findGameRecord = async (gameId) => {
|
||||||
return await knex("move")
|
return await knex("move")
|
||||||
.where({ game: gameId, game_record: true })
|
.where({ game: gameId, game_record: true })
|
||||||
.select(
|
.select("player", "point_x", "point_y", "number", "prior_move", "placement")
|
||||||
"player",
|
|
||||||
"point_x",
|
|
||||||
"point_y",
|
|
||||||
"number",
|
|
||||||
"prior_move",
|
|
||||||
"placement",
|
|
||||||
"id"
|
|
||||||
)
|
|
||||||
.orderBy("number")
|
.orderBy("number")
|
||||||
.then((record) =>
|
.then((record) =>
|
||||||
record.map(({ player, point_x, point_y, id, prior_move }) => ({
|
record.map(({ player, point_x, point_y }) => ({
|
||||||
player,
|
player,
|
||||||
pos: { x: point_x, y: point_y },
|
pos: { x: point_x, y: point_y },
|
||||||
id,
|
|
||||||
prior: prior_move,
|
|
||||||
}))
|
}))
|
||||||
);
|
);
|
||||||
// .then(res => res)
|
// .then(res => res)
|
||||||
|
@ -42,10 +32,11 @@ const addMove = async ({ gameId, player, x, y, gameRecord, priorMove }) => {
|
||||||
game_record: gameRecord,
|
game_record: gameRecord,
|
||||||
prior_move: priorMove,
|
prior_move: priorMove,
|
||||||
})
|
})
|
||||||
.then((res) => res[0]);
|
.then((res) => res);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
result = e;
|
result = e;
|
||||||
} finally {
|
} finally {
|
||||||
|
console.log(result);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
6
packages/server/package-lock.json
generated
6
packages/server/package-lock.json
generated
|
@ -1359,9 +1359,9 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"lodash": {
|
"lodash": {
|
||||||
"version": "4.17.15",
|
"version": "4.17.19",
|
||||||
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
|
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
|
||||||
"integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
|
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ=="
|
||||||
},
|
},
|
||||||
"lodash.includes": {
|
"lodash.includes": {
|
||||||
"version": "4.3.0",
|
"version": "4.3.0",
|
||||||
|
|
|
@ -184,8 +184,7 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
|
||||||
if (gameRecord.length) {
|
if (gameRecord.length) {
|
||||||
// play through all the moves
|
// play through all the moves
|
||||||
const game = gameRecord.reduce(
|
const game = gameRecord.reduce(
|
||||||
(game, move) =>
|
(game, move) => game.makeMove(move),
|
||||||
move.length ? game.makeMove(move[0]) : game.makeMove(move),
|
|
||||||
Game({ gameData }).initGame()
|
Game({ gameData }).initGame()
|
||||||
);
|
);
|
||||||
// ? why is this being wrapped?
|
// ? why is this being wrapped?
|
||||||
|
@ -263,15 +262,16 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
|
||||||
return { ...this, success: false };
|
return { ...this, success: false };
|
||||||
},
|
},
|
||||||
|
|
||||||
makeMove: function ({ player, pos: { x, y }, id, prior }) {
|
makeMove: function ({ player, pos: { x, y } }) {
|
||||||
if (this.pass > 1) {
|
if (this.pass > 1) {
|
||||||
return { ...this, success: false };
|
return { ...this, success: false };
|
||||||
}
|
}
|
||||||
if (x === 0) return game.submitPass(player);
|
|
||||||
|
|
||||||
let success = false;
|
let success = false;
|
||||||
let game = this;
|
let game = this;
|
||||||
|
|
||||||
|
if (x === 0) return game.submitPass(player);
|
||||||
|
|
||||||
// if checkMove has not been run, determine legality
|
// if checkMove has not been run, determine legality
|
||||||
if (!game.move) {
|
if (!game.move) {
|
||||||
game = game.checkMove({ player, pos: { x, y } });
|
game = game.checkMove({ player, pos: { x, y } });
|
||||||
|
@ -287,7 +287,7 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
|
||||||
const point = game.boardState[`${x}-${y}`];
|
const point = game.boardState[`${x}-${y}`];
|
||||||
game.pass = 0;
|
game.pass = 0;
|
||||||
// allows for recording of prior move on game record
|
// allows for recording of prior move on game record
|
||||||
game.addToRecord({ player, pos: { x, y }, id, prior });
|
game.addToRecord(game.move);
|
||||||
if (game.kos.length) helper.clearKo.call(game);
|
if (game.kos.length) helper.clearKo.call(game);
|
||||||
point.makeMove(game);
|
point.makeMove(game);
|
||||||
game.turn *= -1;
|
game.turn *= -1;
|
||||||
|
|
|
@ -7,28 +7,22 @@ const GameService = ({ moveQueries, gameQueries }) => {
|
||||||
};
|
};
|
||||||
const gamesInProgress = {};
|
const gamesInProgress = {};
|
||||||
|
|
||||||
const storeMove = (gameId) => async ({
|
const storeMove = (gameId) => async ({ player, pos: { x, y } }) => {
|
||||||
player,
|
let move = { player, pos: { x, y } };
|
||||||
pos: { x, y },
|
|
||||||
priorMove,
|
|
||||||
}) => {
|
|
||||||
let move = { player, pos: { x, y }, priorMove };
|
|
||||||
try {
|
try {
|
||||||
if (moveQueries) {
|
if (moveQueries) {
|
||||||
const moveDbResult = await moveQueries.addMove({
|
const { id } = await moveQueries.addMove({
|
||||||
gameId,
|
gameId,
|
||||||
player,
|
player,
|
||||||
x,
|
x,
|
||||||
y,
|
y,
|
||||||
gameRecord: true,
|
gameRecord: true,
|
||||||
priorMove,
|
priorMove: null,
|
||||||
});
|
});
|
||||||
move.id = moveDbResult.id;
|
move.id = id;
|
||||||
move.prior = moveDbResult.prior_move;
|
|
||||||
move.success = true;
|
move.success = true;
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log("Exception ------------");
|
|
||||||
console.log(e);
|
console.log(e);
|
||||||
move.success = false;
|
move.success = false;
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -61,14 +55,23 @@ const GameService = ({ moveQueries, gameQueries }) => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
gamesInProgress[id] = await gamesInProgress[id].checkMove(move);
|
gamesInProgress[id] = await gamesInProgress[id].checkMove(move);
|
||||||
|
gamesInProgress[id] = gamesInProgress[id].makeMove(move);
|
||||||
if (gamesInProgress[id].success === false)
|
if (gamesInProgress[id].success === false)
|
||||||
return { message: "illegal move" };
|
return { message: "illegal move" };
|
||||||
try {
|
try {
|
||||||
if (moveQueries) {
|
if (moveQueries) {
|
||||||
// todo change prior move
|
const priorMove = gamesInProgress[id].gameRecord.length;
|
||||||
move = await storeMove(id)(move);
|
const moveInsert = {
|
||||||
|
gameId: id,
|
||||||
|
player: move.player,
|
||||||
|
x: move.pos.x,
|
||||||
|
y: move.pos.y,
|
||||||
|
gameRecord: true,
|
||||||
|
priorMove,
|
||||||
|
};
|
||||||
|
let moveDbResult;
|
||||||
|
moveDbResult = await moveQueries.addMove(moveInsert);
|
||||||
}
|
}
|
||||||
gamesInProgress[id] = gamesInProgress[id].makeMove(move);
|
|
||||||
} catch {
|
} catch {
|
||||||
gamesInProgress[id].returnToMove(-1);
|
gamesInProgress[id].returnToMove(-1);
|
||||||
} finally {
|
} finally {
|
||||||
|
|
|
@ -608,34 +608,14 @@ describe("capture logic: snapback, ko and playing in eyes", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("Game history functionality", () => {
|
describe("Game history functionality", () => {
|
||||||
const firstMove = {
|
const firstMove = { player: "black", pos: { x: 4, y: 4 } };
|
||||||
player: "black",
|
const secondMove = { player: "white", pos: { x: 16, y: 16 } };
|
||||||
pos: { x: 4, y: 4 },
|
const thirdMove = { player: "black", pos: { x: 16, y: 4 } };
|
||||||
id: 1,
|
const fourthMove = { player: "white", pos: { x: 4, y: 16 } };
|
||||||
prior: null,
|
const fifthMove = { player: "black", pos: { x: 10, y: 4 } };
|
||||||
};
|
const sixthMove = { player: "white", pos: { x: 4, y: 10 } };
|
||||||
const secondMove = {
|
const seventhMove = { player: "black", pos: { x: 10, y: 16 } };
|
||||||
player: "white",
|
const eighthMove = { player: "white", pos: { x: 16, y: 10 } };
|
||||||
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) => {
|
it("makeMove creates gameRecord item", (done) => {
|
||||||
Game().initGame().makeMove(firstMove).gameRecord[0].should.eql(firstMove);
|
Game().initGame().makeMove(firstMove).gameRecord[0].should.eql(firstMove);
|
||||||
|
@ -748,11 +728,11 @@ describe("Game end logic", () => {
|
||||||
it("two nonconsecutive passes continue game", (done) => {
|
it("two nonconsecutive passes continue game", (done) => {
|
||||||
Game()
|
Game()
|
||||||
.initGame()
|
.initGame()
|
||||||
.makeMove({ player: "black", pos: { x: 4, y: 4 }, id: 1, prior: null })
|
.makeMove({ player: "black", pos: { x: 4, y: 4 } })
|
||||||
.makeMove({ player: "white", pos: { x: 4, y: 5 }, id: 2, prior: 1 })
|
.makeMove({ player: "white", pos: { x: 4, y: 5 } })
|
||||||
.makeMove({ player: "black", pos: { x: 5, y: 3 }, id: 3, prior: 2 })
|
.makeMove({ player: "black", pos: { x: 5, y: 3 } })
|
||||||
.submitPass("white")
|
.submitPass("white")
|
||||||
.makeMove({ player: "black", pos: { x: 16, y: 16 }, id: 4, prior: 3 })
|
.makeMove({ player: "black", pos: { x: 16, y: 16 } })
|
||||||
.submitPass("white")
|
.submitPass("white")
|
||||||
.getMeta()
|
.getMeta()
|
||||||
.should.eql({
|
.should.eql({
|
||||||
|
@ -760,38 +740,12 @@ describe("Game end logic", () => {
|
||||||
pass: 1,
|
pass: 1,
|
||||||
turn: 1,
|
turn: 1,
|
||||||
gameRecord: [
|
gameRecord: [
|
||||||
{
|
{ player: "black", pos: { x: 4, y: 4 } },
|
||||||
player: "black",
|
{ player: "white", pos: { x: 4, y: 5 } },
|
||||||
pos: { x: 4, y: 4 },
|
{ player: "black", pos: { x: 5, y: 3 } },
|
||||||
id: 1,
|
{ player: "white", pos: { x: null, y: null } },
|
||||||
prior: null,
|
{ player: "black", pos: { x: 16, y: 16 } },
|
||||||
},
|
{ player: "white", pos: { x: null, y: 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();
|
done();
|
||||||
|
|
|
@ -20,7 +20,7 @@ describe("game services", () => {
|
||||||
|
|
||||||
it("games services places move", async () => {
|
it("games services places move", async () => {
|
||||||
gameServices.initGame({ id: 1, handicap: 4 });
|
gameServices.initGame({ id: 1, handicap: 4 });
|
||||||
const move = { player: "white", pos: { x: 6, y: 3 }, id: 1, prior: null };
|
const move = { player: "white", pos: { x: 6, y: 3 } };
|
||||||
const afterMove = await gameServices.makeMove({ id: 1, move });
|
const afterMove = await gameServices.makeMove({ id: 1, move });
|
||||||
const afterMoveShould = {
|
const afterMoveShould = {
|
||||||
board: { ...fourHandicapBoard, "6-3": -1 },
|
board: { ...fourHandicapBoard, "6-3": -1 },
|
||||||
|
|
Loading…
Reference in a new issue