feat(menu): in-progress game card replaces mode picker

When /api/state shows a live game, the home page now hides the mode
cards + setup form and shows an 'active-card' instead: ROUND IN
PROGRESS header with pulsing crimson dot, a diagonal 'LIVE' stamp,
white/black seat panels (agent / model / persona), VS divider with
'who's thinking' status, a gold WATCH LIVE button that routes to
/ai-vs-ai (or /play if either side is human), and a crimson ABORT
CURRENT ROUND escape hatch. menu.js polls every 3s so the meta strip
(move count, current thinker) stays fresh and the picker comes back as
soon as the game ends.

CSS housekeeping:
- Global [hidden] { display: none !important } — without it CSS rules
  like .modes { display: grid } override the HTML hidden attribute and
  the picker bled through the active-card.
- New tokens reuse the existing palette: gold for the watch CTA + meta,
  crimson for the LIVE stamp + abort, dashed rule + bone tones for
  seat borders. No new fonts or colors introduced — matches the
  Major Mono Display / JetBrains Mono tournament-bulletin style.
This commit is contained in:
ai-chess
2026-05-15 04:10:46 +03:00
parent 2038ec95c9
commit e23a07f07b
3 changed files with 249 additions and 3 deletions
+77 -2
View File
@@ -4,6 +4,8 @@ const STATE = {
options: null,
mode: null, // "ai_vs_ai" | "human_vs_ai" | null
inProgress: false,
game: null, // current game payload (only set when in progress)
bindings: null,
};
const SIDES = ["white", "black"];
@@ -102,8 +104,12 @@ async function loadInProgress() {
try {
const s = await (await fetch("/api/state")).json();
STATE.inProgress = !!(s.game && !s.game.status?.result);
STATE.game = STATE.inProgress ? s.game : null;
STATE.bindings = STATE.inProgress ? s.bindings : null;
} catch (e) {
STATE.inProgress = false;
STATE.game = null;
STATE.bindings = null;
}
}
function refreshGate() {
@@ -111,6 +117,54 @@ function refreshGate() {
$("start-btn").disabled = STATE.inProgress;
}
// ---------- active game card ----------
function renderActiveCard() {
const card = $("active-card");
const modes = $("modes");
const setup = $("setup");
if (!STATE.inProgress) {
card.hidden = true;
modes.hidden = false;
return;
}
// Hide picker + setup, show the live card.
card.hidden = false;
modes.hidden = true;
setup.hidden = true;
const g = STATE.game || {};
const b = STATE.bindings || {};
const promptLabel = (id) => {
if (!id) return "";
const p = (STATE.options?.prompts || []).find(x => x.id === id);
return p ? p.label : id;
};
// Pull prompt out of game.agents (the dict committed by start_new_game) —
// bindings may not carry it.
const wA = g.agents?.white || {};
const bA = g.agents?.black || {};
$("active-white-agent").textContent = (wA.agent || "?");
$("active-white-model").textContent = wA.agent === "human" ? "controlled by you" : (wA.model || "?");
$("active-white-persona").textContent = wA.agent === "human" ? "" : promptLabel(wA.prompt);
$("active-black-agent").textContent = (bA.agent || "?");
$("active-black-model").textContent = bA.agent === "human" ? "controlled by you" : (bA.model || "?");
$("active-black-persona").textContent = bA.agent === "human" ? "" : promptLabel(bA.prompt);
// current state strip
const moves = (g.history || []).filter(h => h.san).length;
$("active-meta").textContent = `move ${Math.ceil(moves/2) + (g.side_to_move === "white" ? 0 : 0)} · ${moves} half-moves · ${g.id ? g.id.slice(0,10) : ""}…`;
const sideRu = g.side_to_move === "white" ? "белые" : "чёрные";
const moverAgent = (g.side_to_move === "white" ? wA.agent : bA.agent) || "?";
$("active-turn").textContent = `${moverAgent} (${sideRu}) thinks…`;
// Pick the right destination for watch
const watchHref = (wA.agent === "human" || bA.agent === "human") ? "/play" : "/ai-vs-ai";
$("watch-btn").setAttribute("href", watchHref);
}
// ---------- HUD ----------
async function refreshHud() {
try {
@@ -180,8 +234,20 @@ async function start() {
async function init() {
STATE.options = await (await fetch("/api/options")).json();
await loadInProgress();
renderActiveCard();
refreshHud();
setInterval(refreshHud, 5000);
// Poll the active-card payload so the meta strip ('move N · whose turn')
// updates without a page reload. Skipped while no game is live.
setInterval(async () => {
const before = STATE.inProgress;
await loadInProgress();
renderActiveCard();
// Game just ended? Unblock the picker.
if (before && !STATE.inProgress) {
$("modes").hidden = false;
}
}, 3000);
// Mode picker
document.querySelectorAll(".mode[data-mode]").forEach((el) => {
@@ -241,10 +307,19 @@ async function init() {
document.querySelectorAll(".mode").forEach((m) => m.setAttribute("aria-pressed", "false"));
});
// Abort current
// Abort current (inside the setup dialog warning)
$("abort-current").addEventListener("click", async () => {
await fetch("/api/abort-game", { method: "POST" });
setTimeout(async () => { await loadInProgress(); refreshGate(); refreshHud(); }, 200);
setTimeout(async () => {
await loadInProgress(); refreshGate(); refreshHud(); renderActiveCard();
}, 200);
});
// Abort current from the active-card (visible only when a game is live)
$("active-abort").addEventListener("click", async () => {
if (!confirm("Прервать текущую партию?")) return;
await fetch("/api/abort-game", { method: "POST" });
setTimeout(async () => { await loadInProgress(); renderActiveCard(); refreshHud(); }, 200);
});
// Start