From 21dd169dfa7276cb9298c9fbd7cf6e2ce4f285d8 Mon Sep 17 00:00:00 2001 From: ai-chess Date: Fri, 15 May 2026 00:52:40 +0300 Subject: [PATCH] feat(ui): thinking timer replaces pv text MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Compact 'agent_name думает m:ss' indicator with a pulsing dot, tabular nums, lives in the same slot the stockfish PV used to occupy. Resets on new_game (white starts thinking), flips to the opponent on every move, clears on game_over / when scrubbing history. Tickers tick 1 Hz. Drops the now-unused PV stringification in applyEvalForFen. --- public/app.js | 58 +++++++++++++++++++++++++++++++++++++++++++---- public/index.html | 2 +- public/style.css | 30 +++++++++++++++++++++++- 3 files changed, 83 insertions(+), 7 deletions(-) diff --git a/public/app.js b/public/app.js index 4ef44a9..77ce9c1 100644 --- a/public/app.js +++ b/public/app.js @@ -200,12 +200,11 @@ function storeEval(e) { function applyEvalForFen(fen) { const e = STATE.evalByFen.get(fenKey(fen)); - const col = $("eval-col"), bar = $("eval-fill"), txt = $("eval-text"), pv = $("sf-pv"); + const col = $("eval-col"), bar = $("eval-fill"), txt = $("eval-text"); if (!e) { txt.textContent = "—"; - pv.textContent = ""; bar.style.height = "50%"; - col.classList.remove("advantage-black"); + col.classList.remove("advantage-black", "advantage-white"); return; } let pct = 50; @@ -222,9 +221,44 @@ function applyEvalForFen(fen) { bar.style.height = `${pct}%`; col.classList.toggle("advantage-black", pct < 50); col.classList.toggle("advantage-white", pct > 50); - pv.textContent = e.pv && e.pv.length ? "pv: " + e.pv.slice(0, 6).join(" ") : ""; } +// ---------- thinking timer ---------- +const THINK = { + startedAt: null, // ms timestamp when current side started thinking + side: null, // "white" | "black" | null + lastMs: { white: 0, black: 0 }, +}; + +function fmtMmSs(ms) { + if (ms == null || ms < 0) return "0:00"; + const s = Math.floor(ms / 1000); + return `${Math.floor(s / 60)}:${(s % 60).toString().padStart(2, "0")}`; +} + +function renderThinking() { + const el = $("think-meter"); + if (!el) return; + // No live thinking while viewing history or after game-over. + const live = STATE.viewPly === null && THINK.side && !STATE.game?.status?.result; + if (!live) { + // Show last completed move's think time when in live + game-over, + // otherwise stay quiet. + if (STATE.game?.status?.result) { + el.innerHTML = ""; + } else if (STATE.viewPly !== null) { + el.innerHTML = ""; + } else { + el.innerHTML = ""; + } + return; + } + const elapsed = Date.now() - (THINK.startedAt || Date.now()); + const who = agentDisplay(THINK.side); + el.innerHTML = `${who} думает ${fmtMmSs(elapsed)}`; +} +setInterval(renderThinking, 1000); + function applyEval(e) { storeEval(e); // If we're currently looking at this fen, refresh the visuals. @@ -262,7 +296,12 @@ function connect() { if (ev.type === "new_game") { STATE.game = ev.game; STATE.viewPly = null; + THINK.startedAt = Date.now(); + THINK.side = "white"; + THINK.lastMs = { white: 0, black: 0 }; renderBoard(); renderChat(); renderHistory(); renderStatus(); + refreshGamesLink(); + renderThinking(); log(`new game: ${ev.game?.id || "?"}`); return; } @@ -274,8 +313,14 @@ function connect() { if (!ev.status?.result) { STATE.game.side_to_move = ev.side === "white" ? "black" : "white"; } + // think timer: mover finished, opponent starts + if (THINK.startedAt != null && THINK.side === ev.side) { + THINK.lastMs[ev.side] = Date.now() - THINK.startedAt; + } + THINK.startedAt = ev.status?.result ? null : Date.now(); + THINK.side = ev.status?.result ? null : (ev.side === "white" ? "black" : "white"); // stay live unless user clicked back - renderBoard(); renderChat(); renderHistory(); renderStatus(); + renderBoard(); renderChat(); renderHistory(); renderStatus(); renderThinking(); log(`${ev.side} ${ev.san} «${ev.comment || ""}»`); return; } @@ -285,7 +330,10 @@ function connect() { STATE.game.status.result = ev.result; STATE.game.status.termination = ev.termination; } + THINK.startedAt = null; + THINK.side = null; renderStatus(); + renderThinking(); refreshGamesLink(); // unlock the setup dialog if it was open and waiting try { refreshSetupGate(); } catch (e) {} diff --git a/public/index.html b/public/index.html index 4266c4c..5c47900 100644 --- a/public/index.html +++ b/public/index.html @@ -20,7 +20,7 @@