diff --git a/public/app.js b/public/app.js new file mode 100644 index 0000000..f3cfd3d --- /dev/null +++ b/public/app.js @@ -0,0 +1,210 @@ +import { Chess } from "/static/vendor/chess.min.js"; +import { Chessground } from "/static/vendor/chessground.min.js"; + +const Terminal = window.Terminal; +const FitAddon = window.FitAddon; + +const wsUrl = `${location.protocol === "https:" ? "wss" : "ws"}://${location.host}/ws`; +const $ = (id) => document.getElementById(id); + +const STATE = { + game: null, + settings: null, + bindings: null, + viewPly: null, // null = live +}; + +const cg = Chessground($("board"), { + viewOnly: true, + fen: "start", + orientation: "white", + movable: { free: false, color: undefined, dests: new Map() }, + drawable: { enabled: true, visible: true }, +}); + +const terms = { + white: makeTerm(), + black: makeTerm(), +}; +let termsAttached = false; + +function makeTerm() { + const t = new Terminal({ + convertEol: true, fontSize: 12, cursorBlink: false, + theme: { background: "#1a1a1a", foreground: "#e7e5e4" }, + }); + const fit = new FitAddon.FitAddon(); + t.loadAddon(fit); + return { term: t, fit }; +} + +function attachTerms() { + if (termsAttached) return; + if (!STATE.bindings || !STATE.bindings.white) return; + const codexSide = STATE.bindings.white.agent === "codex" ? "white" : "black"; + const claudeSide = codexSide === "white" ? "black" : "white"; + terms[codexSide].term.open($("term-codex")); + terms[claudeSide].term.open($("term-claude")); + setTimeout(() => { + try { terms.white.fit.fit(); } catch (e) {} + try { terms.black.fit.fit(); } catch (e) {} + }, 50); + + $("agent-codex").classList.toggle("theme-light", codexSide === "white"); + $("agent-codex").classList.toggle("theme-dark", codexSide === "black"); + $("agent-claude").classList.toggle("theme-light", claudeSide === "white"); + $("agent-claude").classList.toggle("theme-dark", claudeSide === "black"); + + $("agent-codex").querySelector('[data-bind="codex-side"]').textContent = codexSide; + $("agent-claude").querySelector('[data-bind="claude-side"]').textContent = claudeSide; + termsAttached = true; +} + +function renderBoard() { + if (!STATE.game) return; + let fen = STATE.game.fen; + if (STATE.viewPly !== null) { + const c = new Chess(); + const moves = STATE.game.history.filter(h => h.san); + for (let i = 0; i < STATE.viewPly; i++) c.move(moves[i].san); + fen = c.fen(); + } + cg.set({ fen, lastMove: undefined }); +} + +function renderHistory() { + const list = $("history-list"); + list.innerHTML = ""; + const moves = (STATE.game?.history || []).filter(h => h.san); + moves.forEach((h, i) => { + const li = document.createElement("li"); + li.value = Math.floor(i / 2) + 1; + li.textContent = `${h.san}${h.comment ? " «" + h.comment + "»" : ""}`; + li.classList.toggle("active", + STATE.viewPly === null ? i === moves.length - 1 : i === STATE.viewPly - 1); + li.addEventListener("click", () => { + STATE.viewPly = i + 1; + renderBoard(); renderHistory(); + }); + list.appendChild(li); + }); +} + +function renderStatus() { + const g = STATE.game; + if (!g) { $("status-line").textContent = "no game"; return; } + const result = g.status?.result; + $("status-line").textContent = result + ? `Game over: ${result} (${g.status.termination})` + : `${g.side_to_move} to move`; +} + +function applyEval(e) { + const cp = e.cp; + const mate = e.mate; + const bar = $("eval-fill"); const txt = $("eval-text"); + let pct = 50; + if (mate != null) { + pct = mate > 0 ? 100 : 0; + txt.textContent = `#${mate}`; + } else if (cp != null) { + pct = Math.max(0, Math.min(100, 50 + cp / 20)); + txt.textContent = (cp / 100).toFixed(2); + } + bar.style.bottom = `${100 - pct}%`; + if (e.best_uci && e.best_uci.length >= 4) { + const from = e.best_uci.slice(0, 2); + const to = e.best_uci.slice(2, 4); + cg.setShapes([{ orig: from, dest: to, brush: "green" }]); + } + if (e.pv && e.pv.length) { + $("sf-lines").textContent = "pv: " + e.pv.slice(0, 6).join(" "); + } +} + +function log(line) { + const t = $("log-text"); + t.textContent += line + "\n"; + t.scrollTop = t.scrollHeight; +} + +function applySnapshot(s) { + STATE.game = s.game; + STATE.settings = s.settings; + STATE.bindings = s.bindings; + STATE.viewPly = null; + if (STATE.bindings && STATE.bindings.white) attachTerms(); + renderBoard(); renderHistory(); renderStatus(); +} + +function connect() { + const ws = new WebSocket(wsUrl); + ws.addEventListener("message", (m) => { + const ev = JSON.parse(m.data); + if (ev.type === "snapshot") return applySnapshot(ev); + if (ev.type === "new_game") { + STATE.game = ev.game; + STATE.viewPly = null; + renderBoard(); renderHistory(); renderStatus(); + log(`new game: ${ev.game?.id || "?"}`); + return; + } + if (ev.type === "move") { + if (!STATE.game) return; + STATE.game.history = ev.history; + STATE.game.fen = ev.fen; + STATE.game.status = ev.status; + if (!ev.status?.result) { + STATE.game.side_to_move = ev.side === "white" ? "black" : "white"; + } + STATE.viewPly = null; + renderBoard(); renderHistory(); renderStatus(); + log(`${ev.side} ${ev.san || ev.uci || "?"}${ev.comment ? " «" + ev.comment + "»" : ""}`); + return; + } + if (ev.type === "eval") return applyEval(ev); + if (ev.type === "game_over") { log(`game over: ${ev.result} (${ev.termination})`); return; } + if (ev.type === "term_chunk") { + const t = terms[ev.side]; + if (!t) return; + t.term.reset(); + t.term.write(ev.data); + return; + } + if (ev.type === "settings") { STATE.settings = ev.settings; return; } + log(`event: ${ev.type}`); + }); + ws.addEventListener("close", () => setTimeout(connect, 1000)); + ws.addEventListener("error", () => {}); +} +connect(); + +$("new-game").addEventListener("click", async () => { + await fetch("/api/new-game", { method: "POST" }); +}); + +const dialog = $("settings-dialog"); +$("settings-btn").addEventListener("click", async () => { + const s = STATE.settings || (await (await fetch("/api/settings")).json()); + dialog.querySelector('[name="allow_draw_resign"]').checked = s.allow_draw_resign; + dialog.querySelector('[name="allow_illegal_moves"]').checked = s.allow_illegal_moves; + dialog.querySelector('[name="theme_mode"]').value = s.theme_mode; + dialog.querySelector('[name="stockfish_depth"]').value = s.stockfish_depth; + dialog.showModal(); +}); +$("settings-save").addEventListener("click", async (e) => { + e.preventDefault(); + const form = $("settings-form"); + const patch = { + allow_draw_resign: form.allow_draw_resign.checked, + allow_illegal_moves: form.allow_illegal_moves.checked, + theme_mode: form.theme_mode.value, + stockfish_depth: parseInt(form.stockfish_depth.value, 10), + }; + await fetch("/api/settings", { + method: "POST", + headers: {"content-type": "application/json"}, + body: JSON.stringify(patch), + }); + dialog.close(); +});