From a8bfe0abdc02c78f5a9522dbf360b372a9829870 Mon Sep 17 00:00:00 2001 From: Nathaniel Tampus Date: Sat, 1 Apr 2023 20:59:25 +0800 Subject: [PATCH] store winner side only, remove redundant reference --- server/src/db/index.ts | 5 ++--- server/src/db/models/game.model.ts | 11 +++-------- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/server/src/db/index.ts b/server/src/db/index.ts index 1ea73e5..4b03376 100644 --- a/server/src/db/index.ts +++ b/server/src/db/index.ts @@ -13,10 +13,9 @@ 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), + winner VARCHAR(5), + end_reason VARCHAR(16), pgn TEXT, - host_id INT REFERENCES "user", white_id INT REFERENCES "user", white_guest_name VARCHAR(32), black_id INT REFERENCES "user", diff --git a/server/src/db/models/game.model.ts b/server/src/db/models/game.model.ts index 5286be6..e3d516d 100644 --- a/server/src/db/models/game.model.ts +++ b/server/src/db/models/game.model.ts @@ -8,17 +8,12 @@ export const activeGames: Array = []; export const create = async (game: Game) => { try { const res = await db.query( - `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 *`, + `INSERT INTO "game"(code, winner, end_reason, pgn, 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.winner || null, + game.endReason || null, game.pgn, - game.host?.id || null, game.white?.id || null, game.white?.name || null, game.black?.id || null,