feat(score): Added a scoring system to log 10 last victories

This commit is contained in:
gauvainboiche
2026-04-02 23:11:41 +02:00
parent bfdc836275
commit b11446cf56
7 changed files with 190 additions and 4 deletions
+35 -2
View File
@@ -134,6 +134,17 @@ export async function initGameSchema() {
await pool.query(`
ALTER TABLE grid_cells ADD COLUMN IF NOT EXISTS captured_by TEXT;
`);
// ── Round history (one row per completed seed epoch) ─────────────────────
await pool.query(`
CREATE TABLE IF NOT EXISTS round_history (
id SERIAL PRIMARY KEY,
world_seed TEXT NOT NULL,
ended_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
blue_score DOUBLE PRECISION NOT NULL DEFAULT 0,
red_score DOUBLE PRECISION NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS idx_round_history_ended_at ON round_history (ended_at DESC);
`);
}
// ── World-seed epoch ──────────────────────────────────────────────────────────
@@ -164,6 +175,11 @@ export async function ensureSeedEpoch() {
);
console.log(`[world] VP awarded to ${winner} for seed ${expiredSeed} (blue=${scores.blue.toFixed(3)}, red=${scores.red.toFixed(3)})`);
}
// Record round result in history regardless of whether scores are non-zero
await pool.query(
`INSERT INTO round_history (world_seed, blue_score, red_score) VALUES ($1, $2, $3)`,
[expiredSeed, scores.blue, scores.red]
);
} catch (e) {
console.error("[world] VP award error:", e);
}
@@ -347,10 +363,27 @@ export async function getDbCreatedAt() {
return rows[0]?.created_at ?? null;
}
// ── Round history ─────────────────────────────────────────────────────────────
export async function getRoundHistory(limit = 10) {
const { rows } = await pool.query(
`SELECT world_seed, ended_at, blue_score, red_score
FROM round_history
ORDER BY ended_at DESC
LIMIT $1`,
[limit]
);
return rows.map(r => ({
worldSeed: r.world_seed,
endedAt: r.ended_at,
blueScore: Number(r.blue_score),
redScore: Number(r.red_score),
}));
}
// ── Victory points ────────────────────────────────────────────────────────────
export async function getVictoryPoints() {
const { rows } = await pool.query(
export async function getVictoryPoints() { const { rows } = await pool.query(
`SELECT team, COUNT(*) AS cnt FROM victory_points GROUP BY team`
);
const result = { blue: 0, red: 0 };
+12
View File
@@ -32,6 +32,7 @@ import {
checkTeamVisibility,
insertTeamVisibility,
getTeamVisibleCells,
getRoundHistory,
} from "../db/gameDb.js";
import {
nextResetUtc,
@@ -553,4 +554,15 @@ router.get("/cell/attacks", async (req, res) => {
}
});
// GET /api/round-history
router.get("/round-history", async (_req, res) => {
try {
const history = await getRoundHistory(10);
res.json(history);
} catch (e) {
console.error(e);
res.status(500).json({ error: "database_error" });
}
});
export default router;