feat(ui): /games page with replay

This commit is contained in:
ai-chess
2026-05-14 23:28:22 +03:00
parent 62d66c5ab6
commit d5d5756b18
2 changed files with 103 additions and 0 deletions
+37
View File
@@ -0,0 +1,37 @@
<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8" />
<title>ai-chess — games</title>
<link rel="stylesheet" href="/static/vendor/chessground.base.css" />
<link rel="stylesheet" href="/static/vendor/chessground.brown.css" />
<link rel="stylesheet" href="/static/vendor/chessground.cburnett.css" />
<link rel="stylesheet" href="/static/style.css" />
</head>
<body>
<main style="padding: 16px;">
<h1>Games</h1>
<a href="/">← back to live</a>
<table id="games-table" style="width: 100%; margin-top: 16px;">
<thead><tr><th>id</th><th>white</th><th>black</th><th>result</th><th>termination</th><th>moves</th><th>pgn</th></tr></thead>
<tbody></tbody>
</table>
<hr />
<div id="replay-wrap" style="display: none;">
<h3 id="replay-title"></h3>
<div style="display: grid; grid-template-columns: 360px 1fr; gap: 16px;">
<div id="replay-board" style="width: 360px; aspect-ratio: 1/1;"></div>
<div>
<div>
<button id="prev">←</button>
<button id="next">→</button>
<span id="ply">ply 0</span>
</div>
<ol id="replay-history" style="max-height: 60vh; overflow: auto;"></ol>
</div>
</div>
</div>
</main>
<script src="/static/games.js" type="module"></script>
</body>
</html>
+66
View File
@@ -0,0 +1,66 @@
import { Chess } from "/static/vendor/chess.min.js";
import { Chessground } from "/static/vendor/chessground.min.js";
const $ = (id) => document.getElementById(id);
const tbody = document.querySelector("#games-table tbody");
let cg = null, chess = null, history = [], ply = 0;
function renderReplay() {
chess = new Chess();
for (let i = 0; i < ply; i++) chess.move(history[i].san);
cg.set({ fen: chess.fen() });
$("ply").textContent = `ply ${ply}/${history.length}`;
const ol = $("replay-history");
ol.innerHTML = "";
history.forEach((h, i) => {
const li = document.createElement("li");
li.textContent = h.san + (h.comment ? " «" + h.comment + "»" : "");
if (i === ply - 1) li.style.background = "#444";
ol.appendChild(li);
});
}
async function openReplay(id) {
const data = await (await fetch(`/api/games/${id}`)).json();
const meta = data.meta;
const w = meta.agents?.white || {};
const b = meta.agents?.black || {};
$("replay-title").textContent =
`${id} — ${w.agent || "?"}/${w.model || "?"} vs ${b.agent || "?"}/${b.model || "?"} → ${meta.result || "*"}`;
chess = new Chess();
chess.load_pgn(data.pgn);
// chess.js 0.13.4: history({verbose:true}) returns [{san, ...}, ...]
history = chess.history({ verbose: true }).map(m => ({ san: m.san, comment: null }));
ply = 0;
if (!cg) cg = Chessground($("replay-board"), { viewOnly: true, fen: "start" });
$("replay-wrap").style.display = "block";
renderReplay();
}
async function main() {
const games = await (await fetch("/api/games")).json();
for (const g of games) {
const w = g.agents?.white || {};
const b = g.agents?.black || {};
const tr = document.createElement("tr");
tr.innerHTML = `
<td><a href="#" data-id="${g.id}">${(g.id || "?").slice(0, 10)}…</a></td>
<td>${w.agent || "?"}/${w.model || "?"}</td>
<td>${b.agent || "?"}/${b.model || "?"}</td>
<td>${g.result || "*"}</td>
<td>${g.termination || ""}</td>
<td>${g.moves_count ?? 0}</td>
<td><a href="/api/games/${g.id}/pgn">pgn</a></td>
`;
tr.querySelector("a[data-id]").addEventListener("click", (e) => {
e.preventDefault();
openReplay(g.id);
});
tbody.appendChild(tr);
}
}
main();
$("prev").addEventListener("click", () => { if (ply > 0) { ply--; renderReplay(); } });
$("next").addEventListener("click", () => { if (ply < history.length) { ply++; renderReplay(); } });