From 10ed00ebc0698de53312702567f8b8a8cb1586f7 Mon Sep 17 00:00:00 2001 From: Nathaniel Tampus Date: Sat, 25 Mar 2023 19:17:49 +0800 Subject: [PATCH] update game model & types --- server/src/db/index.ts | 8 +++++- server/src/db/models/game.model.ts | 41 ++++++++++++++++++++++++++---- types/index.d.ts | 6 ++++- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/server/src/db/index.ts b/server/src/db/index.ts index c8262ef..1ea73e5 100644 --- a/server/src/db/index.ts +++ b/server/src/db/index.ts @@ -12,8 +12,14 @@ export const INIT_TABLES = ` ); CREATE TABLE IF NOT EXISTS "game" ( id SERIAL PRIMARY KEY, + code VARCHAR(8) UNIQUE NOT NULL, + winner_id INT REFERENCES "user", + reason VARCHAR(16), pgn TEXT, + host_id INT REFERENCES "user", white_id INT REFERENCES "user", - black_id INT REFERENCES "user" + white_guest_name VARCHAR(32), + black_id INT REFERENCES "user", + black_guest_name VARCHAR(32) ); `; diff --git a/server/src/db/models/game.model.ts b/server/src/db/models/game.model.ts index d31d90b..5286be6 100644 --- a/server/src/db/models/game.model.ts +++ b/server/src/db/models/game.model.ts @@ -8,15 +8,37 @@ export const activeGames: Array = []; export const create = async (game: Game) => { try { const res = await db.query( - `INSERT INTO "game"(pgn, white_id, black_id, winner) VALUES($1, $2, $3, $4) RETURNING *`, - [game.pgn || null, game.white?.id || null, game.black?.id || null, game.winner || null] + `INSERT INTO "game"(code, winner_id, reason, pgn, host_id, white_id, white_guest_name, black_id, black_guest_name) VALUES($1, $2, $3, $4, $5, $6, $7, $8, $9) RETURNING *`, + [ + game.code || null, + game.winner === "draw" + ? null + : game.winner === "white" + ? game.white?.id + : game.black?.id, + game.reason || null, + game.pgn, + game.host?.id || null, + game.white?.id || null, + game.white?.name || null, + game.black?.id || null, + game.black?.name || null + ] ); return { id: res.rows[0].id, + code: res.rows[0].code, + winner: + res.rows[0].winner_id === res.rows[0].white_id + ? "white" + : res.rows[0].winner_id === res.rows[0].black_id + ? "black" + : "draw", + reason: res.rows[0].reason, pgn: res.rows[0].pgn, - white: { id: res.rows[0].white_id }, - black: { id: res.rows[0].black_id }, - winner: res.rows[0].winner + host: { id: res.rows[0].host_id }, + white: { id: res.rows[0].white_id, name: res.rows[0].white_guest_name }, + black: { id: res.rows[0].black_id, name: res.rows[0].black_guest_name } } as Game; } catch (err: unknown) { console.log(err); @@ -80,3 +102,12 @@ export const remove = async (id: number) => { return null; } }; + +const GameModel = { + create, + find, + update, + remove +}; + +export default GameModel; diff --git a/types/index.d.ts b/types/index.d.ts index 25dd30c..26a626d 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -4,6 +4,7 @@ export interface Game { white?: User; black?: User; winner?: "white" | "black" | "draw"; + reason?: string; host?: User; code?: string; unlisted?: boolean; @@ -15,5 +16,8 @@ export interface User { id?: number | string; // string for guest IDs name?: string | null; email?: string; - connected?: boolean; // mainly for players, not spectators + + // mainly for players, not spectators + connected?: boolean; + disconnectedOn?: number; }