feat(menu): unified home screen + ai-vs-ai/play split + human mode

Routes:
- GET /        → menu.html (new home/menu).
- GET /ai-vs-ai → old index.html (AI-vs-AI live game).
- GET /play    → same index.html; app.js detects the human side from
  bindings and enables chessground move input on that side only.
- GET /games   → unchanged.

Backend:
- coordinator: 'human' is now an allowed agent type alongside claude/codex.
  start_new_game accepts overrides[side].agent = 'human'. _send_first /
  _send_turn / watchdog skip human sides.
- POST /api/human-move: humans submit moves from the browser (UCI/SAN
  + optional comment); rejected if it's not their turn or side isn't human.
- /api/options now also returns 'all_agents' = AI agents + 'human' for the
  human-vs-AI configurator.
- pane-agent.sh: when binding agent is 'human', the pane just prints a
  note and sleeps — no claude/codex is launched for that side.

Frontend menu (new public/menu.{html,css,js}):
- 'underground tournament' aesthetic: Major Mono Display title, JetBrains
  Mono body (Google Fonts), warm gold (#c0a060) / crimson (#c04040)
  accents, subtle film-grain overlay.
- Three big mode cards: 01 AI×AI, 02 HUMAN×AI, 03 GAMES·STATS.
- Inline configurator below the cards: WHITE / VS / BLACK columns with
  agent / model / persona selects, persona description preview, swap +
  randomize tools, abort-current warning that links to /api/abort-game.
- Start button routes to /ai-vs-ai or /play depending on whether a human
  side was picked. Enter submits.
- HUD strip with tmux / stockfish / games-played counters.

app.js changes:
- Chessground constructed with viewOnly toggling; renderBoard rebuilds
  movable.color + dests = legal moves at the live FEN from chess.js for
  the human's turn only; board is read-only when scrubbing history,
  during the AI's turn, or in AI-vs-AI.
- humanSide() helper reads bindings; orientation auto-flips when the
  human plays black.
This commit is contained in:
ai-chess
2026-05-15 01:10:47 +03:00
parent 42f23c71cc
commit ab38cda1bd
7 changed files with 889 additions and 13 deletions
+260
View File
@@ -0,0 +1,260 @@
const $ = (id) => document.getElementById(id);
const STATE = {
options: null,
mode: null, // "ai_vs_ai" | "human_vs_ai" | null
inProgress: false,
};
const SIDES = ["white", "black"];
function elSelect(side, name) {
return document.querySelector(`fieldset[data-side="${side}"] select[name="${name}"]`);
}
function elField(side, when) {
return document.querySelectorAll(`fieldset[data-side="${side}"] [data-when="${when}"]`);
}
// ---------- option loaders ----------
function fillAgentSelect(sel, agents) {
sel.innerHTML = "";
for (const a of agents) {
const o = document.createElement("option");
o.value = a;
o.textContent = a === "human" ? "you (human)" : a;
sel.appendChild(o);
}
}
function fillModelSelect(side) {
const agent = elSelect(side, "agent").value;
const sel = elSelect(side, "model");
sel.innerHTML = "";
if (agent === "human") return;
for (const m of (STATE.options.models[agent] || [])) {
const o = document.createElement("option");
o.value = m; o.textContent = m;
sel.appendChild(o);
}
}
function fillPromptSelect(side) {
const sel = elSelect(side, "prompt");
sel.innerHTML = "";
for (const p of STATE.options.prompts) {
const o = document.createElement("option");
o.value = p.id; o.textContent = p.label; o.dataset.desc = p.description;
sel.appendChild(o);
}
if (STATE.options.defaults?.prompt) sel.value = STATE.options.defaults.prompt;
updatePersonaDesc(side);
}
function updatePersonaDesc(side) {
const sel = elSelect(side, "prompt");
const target = document.querySelector(`fieldset[data-side="${side}"] .persona-desc`);
if (!sel || !target) return;
target.textContent = sel.options[sel.selectedIndex]?.dataset?.desc || "";
}
function refreshSide(side) {
const agent = elSelect(side, "agent").value;
fillModelSelect(side);
fillPromptSelect(side);
for (const el of elField(side, "ai")) el.hidden = (agent === "human");
for (const el of elField(side, "human")) el.hidden = (agent !== "human");
}
// ---------- mode selection ----------
function setMode(mode) {
STATE.mode = mode;
document.querySelectorAll(".mode").forEach((m) => {
m.setAttribute("aria-pressed", m.dataset.mode === mode ? "true" : "false");
});
const setup = $("setup");
setup.hidden = false;
setup.dataset.mode = mode;
$("setup-title").textContent =
mode === "ai_vs_ai" ? "configure the fight" : "you vs the silicon hustler";
// Set defaults per mode
const whiteAgentSel = elSelect("white", "agent");
const blackAgentSel = elSelect("black", "agent");
if (mode === "ai_vs_ai") {
fillAgentSelect(whiteAgentSel, STATE.options.agents);
fillAgentSelect(blackAgentSel, STATE.options.agents);
whiteAgentSel.value = STATE.options.agents[0];
blackAgentSel.value = STATE.options.agents[1] || STATE.options.agents[0];
} else if (mode === "human_vs_ai") {
// Default: human plays white, an AI plays black.
fillAgentSelect(whiteAgentSel, STATE.options.all_agents);
fillAgentSelect(blackAgentSel, STATE.options.all_agents);
whiteAgentSel.value = "human";
blackAgentSel.value = STATE.options.agents[0];
}
refreshSide("white");
refreshSide("black");
// scroll to setup
setTimeout(() => setup.scrollIntoView({ behavior: "smooth", block: "start" }), 60);
refreshGate();
}
// ---------- gating ----------
async function loadInProgress() {
try {
const s = await (await fetch("/api/state")).json();
STATE.inProgress = !!(s.game && !s.game.status?.result);
} catch (e) {
STATE.inProgress = false;
}
}
function refreshGate() {
$("setup-warning").hidden = !STATE.inProgress;
$("start-btn").disabled = STATE.inProgress;
}
// ---------- HUD ----------
async function refreshHud() {
try {
const s = await (await fetch("/api/state")).json();
$("hud-sf").textContent = `depth ${s.settings?.stockfish_depth ?? "?"}`;
if (s.game && !s.game.status?.result) {
$("hud-tmux").textContent = "session ‘chess’ running";
} else {
$("hud-tmux").textContent = "idle";
}
} catch (e) { /* ignore */ }
try {
// games count is allowed only when no active game.
const r = await fetch("/api/stats");
if (r.ok) {
const j = await r.json();
$("hud-games").textContent = String(j.total_games);
} else {
$("hud-games").textContent = "—";
}
} catch (e) {
$("hud-games").textContent = "—";
}
}
// ---------- start ----------
function readOverrides() {
const overrides = { white: {}, black: {} };
for (const side of SIDES) {
const agent = elSelect(side, "agent").value;
overrides[side].agent = agent;
if (agent !== "human") {
overrides[side].model = elSelect(side, "model").value;
overrides[side].prompt = elSelect(side, "prompt").value;
}
}
return overrides;
}
async function start() {
const overrides = readOverrides();
const r = await fetch("/api/new-game", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ overrides }),
});
if (r.status === 409) {
STATE.inProgress = true;
refreshGate();
return;
}
if (!r.ok) {
const detail = await r.text();
alert("Failed to start: " + detail);
return;
}
// Kick off the first prompt for AI side(s)
fetch("/api/dispatch-first", { method: "POST" }).catch(() => {});
// Route to the right gameplay page
const isHumanGame = (
overrides.white.agent === "human" || overrides.black.agent === "human"
);
location.href = isHumanGame ? "/play" : "/ai-vs-ai";
}
// ---------- wiring ----------
async function init() {
STATE.options = await (await fetch("/api/options")).json();
await loadInProgress();
refreshHud();
setInterval(refreshHud, 5000);
// Mode picker
document.querySelectorAll(".mode[data-mode]").forEach((el) => {
el.addEventListener("click", () => setMode(el.dataset.mode));
});
// Side change handlers
for (const side of SIDES) {
elSelect(side, "agent").addEventListener("change", () => refreshSide(side));
elSelect(side, "prompt").addEventListener("change", () => updatePersonaDesc(side));
}
// Swap & randomize
$("btn-swap").addEventListener("click", () => {
for (const name of ["agent", "model", "prompt"]) {
const w = elSelect("white", name);
const b = elSelect("black", name);
if (!w || !b) continue;
const tmp = w.value; w.value = b.value; b.value = tmp;
}
refreshSide("white"); refreshSide("black");
});
$("btn-random").addEventListener("click", () => {
const pool = (STATE.mode === "human_vs_ai")
? [...STATE.options.agents] // randomize only the AI agent (keep human side)
: [...STATE.options.agents];
pool.sort(() => Math.random() - 0.5);
if (STATE.mode === "ai_vs_ai") {
elSelect("white", "agent").value = pool[0];
elSelect("black", "agent").value = pool[1] || pool[0];
} else {
// Find which side is currently AI and randomize that one
for (const side of SIDES) {
if (elSelect(side, "agent").value !== "human") {
elSelect(side, "agent").value = pool[Math.floor(Math.random() * pool.length)];
}
}
}
refreshSide("white"); refreshSide("black");
// Random model + prompt for AI sides
for (const side of SIDES) {
const agent = elSelect(side, "agent").value;
if (agent === "human") continue;
const models = STATE.options.models[agent];
elSelect(side, "model").value = models[Math.floor(Math.random() * models.length)];
const prompts = STATE.options.prompts.map(p => p.id);
elSelect(side, "prompt").value = prompts[Math.floor(Math.random() * prompts.length)];
updatePersonaDesc(side);
}
});
// Back link → close setup
$("back-link").addEventListener("click", (e) => {
e.preventDefault();
$("setup").hidden = true;
STATE.mode = null;
document.querySelectorAll(".mode").forEach((m) => m.setAttribute("aria-pressed", "false"));
});
// Abort current
$("abort-current").addEventListener("click", async () => {
await fetch("/api/abort-game", { method: "POST" });
setTimeout(async () => { await loadInProgress(); refreshGate(); refreshHud(); }, 200);
});
// Start
$("start-btn").addEventListener("click", start);
// Enter starts the game when setup is open and not blocked
window.addEventListener("keydown", (e) => {
if (e.key === "Enter" && !$("setup").hidden && !$("start-btn").disabled) {
e.preventDefault(); start();
}
});
}
init();