Add global match history and ending timestamps

This commit is contained in:
2026-09-20 21:44:36 -04:00
parent fc681c56c9
commit 3c8b5b2e40
8 changed files with 68 additions and 14 deletions
+8 -5
View File
@@ -1,10 +1,13 @@
import { db } from '../db/index.js';
import type { ServerMatch } from '../../match-history.js';
export function serverMatchHistory(server: string, limit = 25): { matches: ServerMatch[]; total: number } {
export function serverMatchHistory(server?: string, limit = 25): { matches: ServerMatch[]; total: number } {
const handle = db();
const total = (handle.prepare('SELECT COUNT(*) AS total FROM leaderboard_matches WHERE server = ?').get(server) as { total: number }).total;
const matches = handle.prepare(`SELECT m.id, m.map_name AS map, m.started_at AS startedAt, m.status,
const where = server === undefined ? '' : 'WHERE m.server = ?';
const parameters = server === undefined ? [] : [server];
const total = (handle.prepare(`SELECT COUNT(*) AS total FROM leaderboard_matches m ${where}`).get(...parameters) as { total: number }).total;
const matches = handle.prepare(`SELECT m.id, m.server, m.map_name AS map, m.started_at AS startedAt, m.status,
CASE WHEN m.status = 'completed' THEN m.ended_at END AS endedAt,
m.start_players AS startPlayers,
(SELECT COUNT(*) FROM leaderboard_match_players p WHERE p.match_id = m.id) AS uniquePlayers,
CASE WHEN m.status = 'completed' THEN
@@ -13,7 +16,7 @@ export function serverMatchHistory(server: string, limit = 25): { matches: Serve
CASE WHEN m.status = 'completed' AND m.start_observed = 1 THEN
MAX(0, ROUND((julianday(m.ended_at) - julianday(m.started_at)) * 86400000)) END AS durationMs,
CASE WHEN m.status = 'completed' THEN m.highest_round END AS finalRound
FROM leaderboard_matches m WHERE m.server = ? ORDER BY m.id DESC LIMIT ?`)
.all(server, limit) as ServerMatch[];
FROM leaderboard_matches m ${where} ORDER BY m.started_at DESC, m.id DESC LIMIT ?`)
.all(...parameters, limit) as ServerMatch[];
return { matches, total };
}