Merge pull request #11 from sorrelbri/game_record_tree

Game Service plays through and ends finished games
This commit is contained in:
Sorrel 2020-07-01 15:06:06 -07:00 committed by GitHub
commit 2a84644e12
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 176 additions and 89 deletions

View file

@ -1,49 +1,81 @@
const winType = [ const winType = ["B+R", "B+", "B+T", "W+R", "W+", "W+T", "0", "Void", "?"];
'B+R', 'B+', 'B+T',
'W+R', 'W+', 'W+T',
'0', 'Void', '?'
]
const rankArray = [ const rankArray = [
'D9', 'D8', 'D7', 'D6', 'D5', 'D4', 'D3', 'D2', 'D1', "D9",
'K1', 'K2', 'K3', 'K4', 'K5', 'K6', 'K7', 'K8', 'K9', 'K10', "D8",
'K11', 'K12', 'K13', 'K14', 'K15', 'K16', 'K17', 'K18', 'K19', 'K20', "D7",
'K21', 'K22', 'K23', 'K24', 'K25', 'K26', 'K27', 'K28', 'K29', 'K30', 'UR' "D6",
] "D5",
"D4",
"D3",
"D2",
"D1",
"K1",
"K2",
"K3",
"K4",
"K5",
"K6",
"K7",
"K8",
"K9",
"K10",
"K11",
"K12",
"K13",
"K14",
"K15",
"K16",
"K17",
"K18",
"K19",
"K20",
"K21",
"K22",
"K23",
"K24",
"K25",
"K26",
"K27",
"K28",
"K29",
"K30",
"UR",
];
exports.up = function (knex) { exports.up = function (knex) {
return knex.schema.createTable("game", table => { return knex.schema.createTable("game", (table) => {
table.increments('id').primary(); table.increments("id").primary();
table.datetime('date'); table.datetime("date");
table.float('komi').default(6.5); table.float("komi").default(6.5);
table.integer('handicap').default(0); table.integer("handicap").default(0);
table.integer('board_size').default(19); table.integer("board_size").default(19);
table.boolean('open').default(true); table.boolean("open").default(true);
table.string('application'); table.string("application");
table.string('application_version'); table.string("application_version");
table.timestamps(true, true); table.timestamps(true, true);
table.string('player_black'); table.string("player_black");
table.string('player_white'); table.string("player_white");
table.enu('player_black_rank', rankArray).default('UR'); table.enu("player_black_rank", rankArray).default("UR");
table.enu('player_white_rank', rankArray).default('UR'); table.enu("player_white_rank", rankArray).default("UR");
table.string('event'); table.string("event");
table.string('name'); table.string("name");
table.string('description'); table.string("description");
table.integer('round'); table.integer("round");
table.enu('win_type', winType); table.enu("win_type", winType);
table.float('score'); table.float("score");
table.integer('captures_black'); table.integer("captures_black");
table.integer('captures_white'); table.integer("captures_white");
table.integer('user_black').references('id').inTable('user'); table.integer("user_black").references("id").inTable("user");
table.integer('user_white').references('id').inTable('user'); table.integer("user_white").references("id").inTable("user");
table.integer('room').references('id').inTable('room'); table.integer("room").references("id").inTable("room");
table.integer('time_setting').references('id').inTable('time_setting'); table.integer("time_setting").references("id").inTable("time_setting");
}) });
}; };
exports.down = knex => knex.schema.dropTableIfExists("game"); exports.down = (knex) => knex.schema.dropTableIfExists("game");

View file

@ -1,17 +1,18 @@
const players = ['white', 'black'] const players = ["white", "black"];
exports.up = knex => { exports.up = (knex) => {
return knex.schema.createTable("move", table => { return knex.schema.createTable("move", (table) => {
table.increments('id').primary(); table.increments("id").primary();
table.enu('player', players).notNullable(); table.enu("player", players).notNullable();
table.integer('point_x').notNullable(); table.integer("point_x").notNullable();
table.integer('point_y').notNullable(); table.integer("point_y").notNullable();
table.integer('number').notNullable(); table.integer("number").notNullable();
table.boolean('game_record').notNullable().default(true); table.boolean("game_record").notNullable().default(true);
table.boolean("placement").notNullable().default(false);
table.integer('game').references('id').inTable('game').notNullable(); table.integer("game").references("id").inTable("game").notNullable();
table.integer('prior_move').references('id').inTable('move'); table.integer("prior_move").references("id").inTable("move");
}); });
}; };
exports.down = knex => knex.schema.dropTableIfExists("move"); exports.down = (knex) => knex.schema.dropTableIfExists("move");

View file

@ -70,17 +70,23 @@ const findGameByRoom = async (roomId) => {
const insertGame = async (game) => {}; const insertGame = async (game) => {};
const endGame = async ({ id }) => { const endGame = async ({ id, winType, score, bCaptures, wCaptures }) => {
const game = await knex(game).where({ id: id }).update( try {
{ const game = await knex
.from("game")
.returning(gameDetailSelect)
.where({ id: id })
.update({
win_type: winType, win_type: winType,
score: score, score: score,
captures_black: capturesBlack, captures_black: bCaptures,
captures_white: capturesWhite, captures_white: wCaptures,
} open: false,
// ["id"] });
);
return game; return game;
} catch (e) {
return e;
}
}; };
module.exports = { module.exports = {

View file

@ -1,36 +1,47 @@
const knex = require('../db'); 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('player', 'point_x', 'point_y', 'number') .select("player", "point_x", "point_y", "number", "prior_move", "placement")
.orderBy('number') .orderBy("number")
.then(record => record.map(({player, point_x, point_y}) => ({player, pos: { x: point_x, y: point_y } }) )) .then((record) =>
record.map(({ player, point_x, point_y }) => ({
player,
pos: { x: point_x, y: point_y },
}))
);
// .then(res => res) // .then(res => res)
} };
// id: 1, player: 'black', point_x: 3, point_y: 3, number: 1, game_record: true, game: 1, prior_move: null // id: 1, player: 'black', point_x: 3, point_y: 3, number: 1, game_record: true, game: 1, prior_move: null
const addMove = async ({ gameId, player, x, y, gameRecord, priorMove }) => { const addMove = async ({ gameId, player, x, y, gameRecord, priorMove }) => {
// ! priorMove must be FK not move number
const number = priorMove + 1; const number = priorMove + 1;
let result; let result;
try { try {
result = await knex('move') result = await knex("move")
.returning('*') .returning("*")
.insert({ game: gameId, player, point_x: x, point_y: y, number, game_record: gameRecord, prior_move: priorMove }) .insert({
.then(res => res); game: gameId,
} player,
catch (e) { point_x: x,
result = e point_y: y,
} number,
finally { game_record: gameRecord,
prior_move: priorMove,
})
.then((res) => res);
} catch (e) {
result = e;
} finally {
console.log(result); console.log(result);
return result; return result;
} }
};
}
module.exports = { module.exports = {
findGameRecord, findGameRecord,
addMove addMove,
} };

View file

@ -181,13 +181,18 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
this.kos = []; this.kos = [];
}, },
}; };
if (gameRecord.length) { if (gameRecord.length) {
// play through all the moves // play through all the moves
return gameRecord.reduce( const game = gameRecord.reduce(
(game, move) => game.makeMove(move), (game, move) => game.makeMove(move),
Game({ gameData }).initGame() Game({ gameData }).initGame()
); );
// ? why is this being wrapped?
if (gameData && gameData.gameData && gameData.gameData.winner) {
const { winner, score } = gameData.gameData;
return game.manualEnd({ winner, score });
}
return game;
} }
return { return {
winner: gameData.winner || null, winner: gameData.winner || null,
@ -389,6 +394,14 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
this.playerState.bScore - (this.playerState.wScore + this.komi); this.playerState.bScore - (this.playerState.wScore + this.komi);
return { ...this, score, winner: score > 0 ? 1 : -1 }; return { ...this, score, winner: score > 0 ? 1 : -1 };
}, },
// for playing historic games
manualEnd: function ({ winner, score }) {
this.turn = 0;
this.winner = winner;
this.score = score;
return this;
},
}; };
}; };

View file

@ -11,7 +11,6 @@ const GameService = ({ moveQueries, gameQueries }) => {
initGame({ id, gameRecord = [], ...gameData }) { initGame({ id, gameRecord = [], ...gameData }) {
if (gamesInProgress[id]) return this.getDataForUI(id); if (gamesInProgress[id]) return this.getDataForUI(id);
if (gameRecord.length) { if (gameRecord.length) {
console.log("here");
gamesInProgress[id] = Game({ gameData, gameRecord }); gamesInProgress[id] = Game({ gameData, gameRecord });
} else { } else {
gamesInProgress[id] = Game({ gameData }).initGame(); gamesInProgress[id] = Game({ gameData }).initGame();
@ -73,6 +72,7 @@ const GameService = ({ moveQueries, gameQueries }) => {
}, },
resign: ({ id, player }) => { resign: ({ id, player }) => {
// add resign gamesQueries
return gamesInProgress[id].submitResign(player).getMeta(); return gamesInProgress[id].submitResign(player).getMeta();
}, },
@ -108,9 +108,19 @@ const GameService = ({ moveQueries, gameQueries }) => {
async endGame({ id }) { async endGame({ id }) {
gamesInProgress[id] = gamesInProgress[id].endGame(); gamesInProgress[id] = gamesInProgress[id].endGame();
const { winner, score, playerState } = gamesInProgress[id];
const { bCaptures, wCaptures } = playerState;
const winType = winner > 0 ? "B+" : "W+";
try { try {
if (gameQueries) { if (gameQueries) {
// TODO add end game query const result = await gameQueries.endGame({
id,
winType,
score,
bCaptures,
wCaptures,
});
console.log(result);
} }
} catch (e) { } catch (e) {
console.log(e); console.log(e);

View file

@ -24,8 +24,22 @@ io.on("connection", async (socket) => {
socket.on("connect_game", (data) => { socket.on("connect_game", (data) => {
const game = `game-${data.game.id}`; const game = `game-${data.game.id}`;
socket.join(game, async () => { socket.join(game, async () => {
const gameData = await gameQueries.findGameById(data.game.id);
const convertWinType = (winType) => {
if (winType.includes("B")) return 1;
if (winType.includes("W")) return -1;
if (winType.includes("0")) return "D";
return "?";
};
gameData.winner = gameData.win_type
? convertWinType(gameData.win_type)
: 0;
const gameRecord = await moveQueries.findGameRecord(data.game.id); const gameRecord = await moveQueries.findGameRecord(data.game.id);
await gameServices.initGame({ id: data.game.id, gameRecord }); await gameServices.initGame({
id: data.game.id,
gameRecord,
gameData,
});
const { board, ...meta } = await gameServices.getDataForUI( const { board, ...meta } = await gameServices.getDataForUI(
data.game.id data.game.id
); );