add started & ended timestamps to game table
This commit is contained in:
@@ -21,6 +21,8 @@ export const INIT_TABLES = /* sql */ `
|
|||||||
white_id INT REFERENCES "user",
|
white_id INT REFERENCES "user",
|
||||||
white_name VARCHAR(32),
|
white_name VARCHAR(32),
|
||||||
black_id INT REFERENCES "user",
|
black_id INT REFERENCES "user",
|
||||||
black_name VARCHAR(32)
|
black_name VARCHAR(32),
|
||||||
|
started_at TIMESTAMP NOT NULL,
|
||||||
|
ended_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
);
|
);
|
||||||
`;
|
`;
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ import type { Game, User } from "@chessu/types";
|
|||||||
|
|
||||||
export const activeGames: Array<Game> = [];
|
export const activeGames: Array<Game> = [];
|
||||||
|
|
||||||
// todo: join user and game relationship
|
|
||||||
|
|
||||||
export const save = async (game: Game) => {
|
export const save = async (game: Game) => {
|
||||||
try {
|
try {
|
||||||
const white: User = {};
|
const white: User = {};
|
||||||
@@ -20,7 +18,7 @@ export const save = async (game: Game) => {
|
|||||||
black.id = game.black?.id;
|
black.id = game.black?.id;
|
||||||
}
|
}
|
||||||
const res = await db.query(
|
const res = await db.query(
|
||||||
`INSERT INTO "game"(winner, end_reason, pgn, white_id, white_name, black_id, black_name) VALUES($1, $2, $3, $4, $5, $6, $7) RETURNING *`,
|
`INSERT INTO "game"(winner, end_reason, pgn, white_id, white_name, black_id, black_name, started_at) VALUES($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *`,
|
||||||
[
|
[
|
||||||
game.winner || null,
|
game.winner || null,
|
||||||
game.endReason || null,
|
game.endReason || null,
|
||||||
@@ -28,7 +26,8 @@ export const save = async (game: Game) => {
|
|||||||
white.id || null,
|
white.id || null,
|
||||||
white.name || null,
|
white.name || null,
|
||||||
black.id || null,
|
black.id || null,
|
||||||
black.name || null
|
black.name || null,
|
||||||
|
new Date(game.startedAt as number)
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
if (black.id || white.id) {
|
if (black.id || white.id) {
|
||||||
@@ -65,7 +64,9 @@ export const save = async (game: Game) => {
|
|||||||
black: {
|
black: {
|
||||||
id: res.rows[0].black_id || undefined,
|
id: res.rows[0].black_id || undefined,
|
||||||
name: res.rows[0].black_name || undefined
|
name: res.rows[0].black_name || undefined
|
||||||
}
|
},
|
||||||
|
startedAt: res.rows[0].started_at.getTime(),
|
||||||
|
endedAt: res.rows[0].ended_at?.getTime() || undefined
|
||||||
} as Game;
|
} as Game;
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
@@ -76,7 +77,7 @@ export const save = async (game: Game) => {
|
|||||||
export const findById = async (id: number) => {
|
export const findById = async (id: number) => {
|
||||||
try {
|
try {
|
||||||
const res = await db.query(
|
const res = await db.query(
|
||||||
`SELECT game.id, game.winner, game.end_reason, game.pgn, white_user.id AS white_id, COALESCE(white_user.name, game.white_name) AS white_name, black_user.id AS black_id, COALESCE(black_user.name, game.black_name) AS black_name FROM game LEFT JOIN "user" white_user ON white_user.id = game.white_id LEFT JOIN "user" black_user ON black_user.id = game.black_id WHERE game.id=$1`,
|
`SELECT game.id, game.winner, game.end_reason, game.pgn, white_user.id AS white_id, COALESCE(white_user.name, game.white_name) AS white_name, black_user.id AS black_id, started_at, ended_at, COALESCE(black_user.name, game.black_name) AS black_name FROM game LEFT JOIN "user" white_user ON white_user.id = game.white_id LEFT JOIN "user" black_user ON black_user.id = game.black_id WHERE game.id=$1`,
|
||||||
[id]
|
[id]
|
||||||
);
|
);
|
||||||
if (res.rowCount) {
|
if (res.rowCount) {
|
||||||
@@ -86,7 +87,9 @@ export const findById = async (id: number) => {
|
|||||||
endReason: res.rows[0].end_reason,
|
endReason: res.rows[0].end_reason,
|
||||||
pgn: res.rows[0].pgn,
|
pgn: res.rows[0].pgn,
|
||||||
white: { id: res.rows[0].white_id || undefined, name: res.rows[0].white_name },
|
white: { id: res.rows[0].white_id || undefined, name: res.rows[0].white_name },
|
||||||
black: { id: res.rows[0].black_id || undefined, name: res.rows[0].black_name }
|
black: { id: res.rows[0].black_id || undefined, name: res.rows[0].black_name },
|
||||||
|
startedAt: res.rows[0].started_at.getTime(),
|
||||||
|
endedAt: res.rows[0].ended_at?.getTime() || undefined
|
||||||
} as Game;
|
} as Game;
|
||||||
} else return null;
|
} else return null;
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
@@ -102,7 +105,7 @@ export const findByUserId = async (id: number, limit = 10) => {
|
|||||||
try {
|
try {
|
||||||
// TODO: pagination
|
// TODO: pagination
|
||||||
const res = await db.query(
|
const res = await db.query(
|
||||||
`SELECT game.id, game.winner, game.end_reason, game.pgn, white_user.id AS white_id, COALESCE(white_user.name, game.white_name) AS white_name, black_user.id AS black_id, COALESCE(black_user.name, game.black_name) AS black_name FROM game LEFT JOIN "user" white_user ON white_user.id = game.white_id LEFT JOIN "user" black_user ON black_user.id = game.black_id WHERE white_user.id=$1 OR black_user.id=$1 ORDER BY id DESC LIMIT $2`,
|
`SELECT game.id, game.winner, game.end_reason, game.pgn, white_user.id AS white_id, COALESCE(white_user.name, game.white_name) AS white_name, black_user.id AS black_id, started_at, ended_at, COALESCE(black_user.name, game.black_name) AS black_name FROM game LEFT JOIN "user" white_user ON white_user.id = game.white_id LEFT JOIN "user" black_user ON black_user.id = game.black_id WHERE white_user.id=$1 OR black_user.id=$1 ORDER BY id DESC LIMIT $2`,
|
||||||
[id, limit]
|
[id, limit]
|
||||||
);
|
);
|
||||||
return res.rows.map((r) => {
|
return res.rows.map((r) => {
|
||||||
@@ -112,7 +115,9 @@ export const findByUserId = async (id: number, limit = 10) => {
|
|||||||
endReason: r.end_reason,
|
endReason: r.end_reason,
|
||||||
pgn: r.pgn,
|
pgn: r.pgn,
|
||||||
white: { id: r.white_id || undefined, name: r.white_name },
|
white: { id: r.white_id || undefined, name: r.white_name },
|
||||||
black: { id: r.black_id || undefined, name: r.black_name }
|
black: { id: r.black_id || undefined, name: r.black_name },
|
||||||
|
startedAt: r.started_at.getTime(),
|
||||||
|
endedAt: r.ended_at?.getTime() || undefined
|
||||||
} as Game;
|
} as Game;
|
||||||
});
|
});
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
@@ -131,7 +136,9 @@ export const update = async (id: number, data: string) => {
|
|||||||
endReason: res.rows[0].end_reason,
|
endReason: res.rows[0].end_reason,
|
||||||
pgn: res.rows[0].pgn,
|
pgn: res.rows[0].pgn,
|
||||||
white: { id: res.rows[0].white_id, name: res.rows[0].white_name },
|
white: { id: res.rows[0].white_id, name: res.rows[0].white_name },
|
||||||
black: { id: res.rows[0].black_id, name: res.rows[0].black_name }
|
black: { id: res.rows[0].black_id, name: res.rows[0].black_name },
|
||||||
|
startedAt: res.rows[0].started_at.getTime(),
|
||||||
|
endedAt: res.rows[0].ended_at?.getTime() || undefined
|
||||||
} as Game;
|
} as Game;
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
@@ -148,7 +155,9 @@ export const remove = async (id: number) => {
|
|||||||
endReason: res.rows[0].end_reason,
|
endReason: res.rows[0].end_reason,
|
||||||
pgn: res.rows[0].pgn,
|
pgn: res.rows[0].pgn,
|
||||||
white: { id: res.rows[0].white_id, name: res.rows[0].white_name },
|
white: { id: res.rows[0].white_id, name: res.rows[0].white_name },
|
||||||
black: { id: res.rows[0].black_id, name: res.rows[0].black_name }
|
black: { id: res.rows[0].black_id, name: res.rows[0].black_name },
|
||||||
|
startedAt: res.rows[0].started_at.getTime(),
|
||||||
|
endedAt: res.rows[0].ended_at?.getTime() || undefined
|
||||||
} as Game;
|
} as Game;
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
console.log(err);
|
console.log(err);
|
||||||
|
|||||||
@@ -231,6 +231,7 @@ export async function joinAsPlayer(this: Socket) {
|
|||||||
name: this.request.session.user.name,
|
name: this.request.session.user.name,
|
||||||
side: "white"
|
side: "white"
|
||||||
});
|
});
|
||||||
|
game.startedAt = Date.now();
|
||||||
} else if (!game.black) {
|
} else if (!game.black) {
|
||||||
const sessionUser = {
|
const sessionUser = {
|
||||||
id: this.request.session.user.id,
|
id: this.request.session.user.id,
|
||||||
@@ -243,6 +244,7 @@ export async function joinAsPlayer(this: Socket) {
|
|||||||
name: this.request.session.user.name,
|
name: this.request.session.user.name,
|
||||||
side: "black"
|
side: "black"
|
||||||
});
|
});
|
||||||
|
game.startedAt = Date.now();
|
||||||
} else {
|
} else {
|
||||||
console.log("joinAsPlayer: attempted to join a game with already 2 players");
|
console.log("joinAsPlayer: attempted to join a game with already 2 players");
|
||||||
}
|
}
|
||||||
|
|||||||
2
types/index.d.ts
vendored
2
types/index.d.ts
vendored
@@ -10,6 +10,8 @@ export interface Game {
|
|||||||
unlisted?: boolean;
|
unlisted?: boolean;
|
||||||
timeout?: number;
|
timeout?: number;
|
||||||
observers?: User[];
|
observers?: User[];
|
||||||
|
startedAt?: number;
|
||||||
|
endedAt?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface User {
|
export interface User {
|
||||||
|
|||||||
Reference in New Issue
Block a user