Compare commits

..

No commits in common. "master" and "guest_account" have entirely different histories.

11 changed files with 12113 additions and 953 deletions

21
LICENSE
View file

@ -1,21 +0,0 @@
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.

12680
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -8500,9 +8500,9 @@
} }
}, },
"lodash": { "lodash": {
"version": "4.17.19", "version": "4.17.15",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==" "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
}, },
"lodash._reinterpolate": { "lodash._reinterpolate": {
"version": "3.0.0", "version": "3.0.0",

View file

@ -1,81 +1,49 @@
const winType = ["B+R", "B+", "B+T", "W+R", "W+", "W+T", "0", "Void", "?"]; const winType = [
'B+R', 'B+', 'B+T',
'W+R', 'W+', 'W+T',
'0', 'Void', '?'
]
const rankArray = [ const rankArray = [
"D9", 'D9', 'D8', 'D7', 'D6', 'D5', 'D4', 'D3', 'D2', 'D1',
"D8", 'K1', 'K2', 'K3', 'K4', 'K5', 'K6', 'K7', 'K8', 'K9', 'K10',
"D7", 'K11', 'K12', 'K13', 'K14', 'K15', 'K16', 'K17', 'K18', 'K19', 'K20',
"D6", 'K21', 'K22', 'K23', 'K24', 'K25', 'K26', 'K27', 'K28', 'K29', 'K30', 'UR'
"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,18 +1,17 @@
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,23 +70,17 @@ const findGameByRoom = async (roomId) => {
const insertGame = async (game) => {}; const insertGame = async (game) => {};
const endGame = async ({ id, winType, score, bCaptures, wCaptures }) => { const endGame = async ({ id }) => {
try { const game = await knex(game).where({ id: id }).update(
const game = await knex {
.from("game") win_type: winType,
.returning(gameDetailSelect) score: score,
.where({ id: id }) captures_black: capturesBlack,
.update({ captures_white: capturesWhite,
win_type: winType, }
score: score, // ["id"]
captures_black: bCaptures, );
captures_white: wCaptures, return game;
open: false,
});
return game;
} catch (e) {
return e;
}
}; };
module.exports = { module.exports = {

View file

@ -1,47 +1,36 @@
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", "prior_move", "placement") .select('player', 'point_x', 'point_y', 'number')
.orderBy("number") .orderBy('number')
.then((record) => .then(record => record.map(({player, point_x, point_y}) => ({player, pos: { x: point_x, y: point_y } }) ))
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({ .insert({ game: gameId, player, point_x: x, point_y: y, number, game_record: gameRecord, prior_move: priorMove })
game: gameId, .then(res => res);
player, }
point_x: x, catch (e) {
point_y: y, result = e
number, }
game_record: gameRecord, finally {
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

@ -1359,9 +1359,9 @@
} }
}, },
"lodash": { "lodash": {
"version": "4.17.19", "version": "4.17.15",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ==" "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
}, },
"lodash.includes": { "lodash.includes": {
"version": "4.3.0", "version": "4.3.0",

View file

@ -181,18 +181,13 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
this.kos = []; this.kos = [];
}, },
}; };
if (gameRecord.length) { if (gameRecord.length) {
// play through all the moves // play through all the moves
const game = gameRecord.reduce( return 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,
@ -244,58 +239,28 @@ const Game = ({ gameData = {}, gameRecord = [] } = {}) => {
}; };
}, },
checkMove: function ({ player, pos: { x, y } }) {
// if game is over
// TODO either change logic here or add additional method for handling moves off of record
if (this.pass > 1) {
return { ...this, success: false };
}
const point = this.boardState[`${x}-${y}`];
const isTurn =
(this.turn === 1 && player === "black") ||
(this.turn === -1 && player === "white");
if (isTurn) {
if (point.legal) {
return { ...this, success: true, move: { player, pos: { x, y } } };
}
}
return { ...this, success: false };
},
makeMove: function ({ player, pos: { x, y } }) { 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 this.submitPass(player);
let success = false;
let game = this; let game = this;
let success = false;
if (x === 0) return game.submitPass(player); const point = game.boardState[`${x}-${y}`];
const isTurn =
// if checkMove has not been run, determine legality (game.turn === 1 && player === "black") ||
if (!game.move) { (game.turn === -1 && player === "white");
game = game.checkMove({ player, pos: { x, y } }); if (isTurn) {
} if (point.legal) {
if ( game.pass = 0;
// if move is legal game.addToRecord({ player, pos: { x, y } });
// ? unclear if checking move values is beneficial to prevent race conditions if (this.kos.length) helper.clearKo.call(this);
game.move && point.makeMove(game);
game.move.player === player && game.turn *= -1;
game.move.pos.x === x && success = true;
game.move.pos.y }
) {
const point = game.boardState[`${x}-${y}`];
game.pass = 0;
// allows for recording of prior move on game record
game.addToRecord(game.move);
if (game.kos.length) helper.clearKo.call(game);
point.makeMove(game);
game.turn *= -1;
success = true;
} }
game.boardState = getBoardState(game); game.boardState = getBoardState(game);
// remove move attribute to prevent duplicate moves
delete game.move;
return { ...game, legalMoves: getLegalMoves(game), success }; return { ...game, legalMoves: getLegalMoves(game), success };
}, },
@ -424,14 +389,6 @@ 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

@ -7,33 +7,11 @@ const GameService = ({ moveQueries, gameQueries }) => {
}; };
const gamesInProgress = {}; const gamesInProgress = {};
const storeMove = (gameId) => async ({ player, pos: { x, y } }) => {
let move = { player, pos: { x, y } };
try {
if (moveQueries) {
const { id } = await moveQueries.addMove({
gameId,
player,
x,
y,
gameRecord: true,
priorMove: null,
});
move.id = id;
move.success = true;
}
} catch (e) {
console.log(e);
move.success = false;
} finally {
return move;
}
};
return { return {
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();
@ -54,7 +32,6 @@ const GameService = ({ moveQueries, gameQueries }) => {
return { message: "error restoring game" }; return { message: "error restoring game" };
} }
} }
gamesInProgress[id] = await gamesInProgress[id].checkMove(move);
gamesInProgress[id] = gamesInProgress[id].makeMove(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" };
@ -96,7 +73,6 @@ 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();
}, },
@ -132,19 +108,9 @@ 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) {
const result = await gameQueries.endGame({ // TODO add end game query
id,
winType,
score,
bCaptures,
wCaptures,
});
console.log(result);
} }
} catch (e) { } catch (e) {
console.log(e); console.log(e);

View file

@ -1,4 +1,4 @@
// TODO const someSocketLogic = require('./middleware/sockets/...'); // TODO const someSocketLogic = require('./middleware/socketssockets/...');
const socketIO = require("socket.io"); const socketIO = require("socket.io");
const io = socketIO({ cookie: false }); const io = socketIO({ cookie: false });
@ -24,23 +24,8 @@ 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 () => {
// TODO move this logic into game service
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({ await gameServices.initGame({ id: data.game.id, gameRecord });
id: data.game.id,
gameRecord,
gameData,
});
const { board, ...meta } = await gameServices.getDataForUI( const { board, ...meta } = await gameServices.getDataForUI(
data.game.id data.game.id
); );
@ -52,6 +37,7 @@ io.on("connection", async (socket) => {
socket.on("make_move", async (data) => { socket.on("make_move", async (data) => {
const { user, move, board, game, room } = data; const { user, move, board, game, room } = data;
const gameNsp = `game-${data.game.id}`; const gameNsp = `game-${data.game.id}`;
try { try {
const { board, message, ...meta } = await gameServices.makeMove({ const { board, message, ...meta } = await gameServices.makeMove({
id: data.game.id, id: data.game.id,