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" ( CREATE TABLE IF NOT EXISTS "game" (
id SERIAL PRIMARY KEY, id SERIAL PRIMARY KEY,
code VARCHAR(8) UNIQUE NOT NULL, code VARCHAR(8) UNIQUE NOT NULL,
winner_id INT REFERENCES "user", winner VARCHAR(5),
reason VARCHAR(16), end_reason VARCHAR(16),
pgn TEXT, pgn TEXT,
host_id INT REFERENCES "user",
white_id INT REFERENCES "user", white_id INT REFERENCES "user",
white_guest_name VARCHAR(32), white_guest_name VARCHAR(32),
black_id INT REFERENCES "user", black_id INT REFERENCES "user",

View File

@@ -8,17 +8,12 @@ export const activeGames: Array<Game> = [];
export const create = async (game: Game) => { export const create = async (game: Game) => {
try { try {
const res = await db.query( 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.code || null,
game.winner === "draw" game.winner || null,
? null game.endReason || null,
: game.winner === "white"
? game.white?.id
: game.black?.id,
game.reason || null,
game.pgn, game.pgn,
game.host?.id || null,
game.white?.id || null, game.white?.id || null,
game.white?.name || null, game.white?.name || null,
game.black?.id || null, game.black?.id || null,