store winner side only, remove redundant reference

This commit is contained in:
Nathaniel Tampus
2023-04-01 20:59:25 +08:00
parent 8d19c88c83
commit a8bfe0abdc
2 changed files with 5 additions and 11 deletions

View File

@@ -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",

View File

@@ -8,17 +8,12 @@ export const activeGames: Array<Game> = [];
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,