feat(ui): floating moves under board, chat full-height right, refined eval bar
Layout (after iterative feedback): - Two columns: board+eval (left, square) | chat full-height (right). - Moves row floats just under the board as a compact one-row strip with the controls (new game / settings) — replaces the old right sidebar. - Board sized imperatively via fitBoard + ResizeObserver; eval bar height matches the board exactly (no overflow into neighbours). Eval bar: - 36px wide (was 28); text fits without pill background. - System sans-serif, 10.5px / 700 weight, tabular-nums. - Text positioned on whichever side is winning (bottom-white-half when white ahead → dark text; top-black-half when black ahead → light). - Sign now correct: backend stores raw cp from side-to-move; frontend flips when FEN says "b" so the bar always reflects white POV. Replay: - evalByFen on the client gets backfilled from /api/state's new `evals` list (backend keeps state.eval_cache per game). Clicking past bubbles now restores the stockfish eval for that ply. - renderBoard clears stockfish arrow when no eval is known yet for the shown FEN, so the arrow no longer 'lags' onto the next position. Auto-focus: - Latest played move is highlighted in both the chat bubble list and the moves strip when in live mode (viewPly === null). - scrollIntoView keeps the active bubble/SAN visible after each move.
This commit is contained in:
+107
-48
@@ -9,6 +9,7 @@ const STATE = {
|
||||
settings: null,
|
||||
bindings: null, // {white: {agent, model, ...}, black: {...}}
|
||||
viewPly: null, // null = live; otherwise integer ply (1-based)
|
||||
evalByFen: new Map(), // fen → eval payload (cp, mate, best_uci, pv) — from white's POV
|
||||
};
|
||||
window._STATE = STATE; // for debugging
|
||||
|
||||
@@ -20,6 +21,27 @@ const cg = Chessground($("board"), {
|
||||
drawable: { enabled: true, visible: true },
|
||||
});
|
||||
|
||||
// ---------- responsive board sizing ----------
|
||||
function fitBoard() {
|
||||
const row = $("board-row");
|
||||
const evalCol = $("eval-col");
|
||||
const board = $("board");
|
||||
if (!row || !board || !evalCol) return;
|
||||
// 10px = gap between eval-col and board in #board-row (CSS).
|
||||
const w = row.clientWidth - evalCol.offsetWidth - 10;
|
||||
const h = row.clientHeight;
|
||||
const side = Math.max(120, Math.floor(Math.min(w, h)));
|
||||
board.style.width = side + "px";
|
||||
board.style.height = side + "px";
|
||||
evalCol.style.height = side + "px";
|
||||
// Let chessground recompute internal layout.
|
||||
if (cg && cg.redrawAll) cg.redrawAll();
|
||||
}
|
||||
const _ro = new ResizeObserver(fitBoard);
|
||||
_ro.observe($("board-row"));
|
||||
window.addEventListener("resize", fitBoard);
|
||||
requestAnimationFrame(fitBoard);
|
||||
|
||||
// ---------- agent labels ----------
|
||||
function agentOfSide(side) {
|
||||
return STATE.bindings?.[side]?.agent || "?";
|
||||
@@ -31,27 +53,34 @@ function agentDisplay(side) {
|
||||
}
|
||||
|
||||
// ---------- rendering ----------
|
||||
function currentFen() {
|
||||
if (!STATE.game) return "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
|
||||
const moves = (STATE.game.history || []).filter(h => h.san);
|
||||
if (STATE.viewPly === null) return STATE.game.fen;
|
||||
const c = new Chess();
|
||||
for (let i = 0; i < STATE.viewPly; i++) c.move(moves[i].san);
|
||||
return c.fen();
|
||||
}
|
||||
|
||||
function renderBoard() {
|
||||
if (!STATE.game) return;
|
||||
let fen = STATE.game.fen;
|
||||
let lastMove = undefined;
|
||||
const moves = (STATE.game.history || []).filter(h => h.san);
|
||||
if (STATE.viewPly !== null && STATE.viewPly >= 0) {
|
||||
const c = new Chess();
|
||||
for (let i = 0; i < STATE.viewPly; i++) c.move(moves[i].san);
|
||||
fen = c.fen();
|
||||
if (STATE.viewPly > 0) {
|
||||
const m = moves[STATE.viewPly - 1];
|
||||
const uci = m.uci || "";
|
||||
if (uci.length >= 4) lastMove = [uci.slice(0, 2), uci.slice(2, 4)];
|
||||
}
|
||||
} else if (moves.length) {
|
||||
const m = moves[moves.length - 1];
|
||||
const uci = m.uci || "";
|
||||
const fen = currentFen();
|
||||
let lastMove = undefined;
|
||||
const idx = STATE.viewPly === null ? moves.length - 1 : STATE.viewPly - 1;
|
||||
if (idx >= 0 && moves[idx]) {
|
||||
const uci = moves[idx].uci || "";
|
||||
if (uci.length >= 4) lastMove = [uci.slice(0, 2), uci.slice(2, 4)];
|
||||
}
|
||||
cg.set({ fen, lastMove });
|
||||
// Pass shapes via set() to atomically replace last move + arrow.
|
||||
// Arrow comes from stored eval for this FEN, if any.
|
||||
const e = STATE.evalByFen.get(fen);
|
||||
const shapes = (e && e.best_uci && e.best_uci.length >= 4)
|
||||
? [{ orig: e.best_uci.slice(0, 2), dest: e.best_uci.slice(2, 4), brush: "green" }]
|
||||
: [];
|
||||
cg.set({ fen, lastMove, drawable: { autoShapes: shapes } });
|
||||
$("live-btn").hidden = STATE.viewPly === null;
|
||||
applyEvalForFen(fen);
|
||||
}
|
||||
|
||||
function renderChat() {
|
||||
@@ -60,13 +89,15 @@ function renderChat() {
|
||||
const moves = (STATE.game?.history || []).filter(h => h.san);
|
||||
const counter = $("chat-counter");
|
||||
counter.textContent = moves.length ? `${moves.length} moves` : "";
|
||||
const activePly = STATE.viewPly === null ? moves.length : STATE.viewPly;
|
||||
|
||||
let activeDiv = null;
|
||||
moves.forEach((h, i) => {
|
||||
const ply = i + 1;
|
||||
const side = h.side; // "white" | "black"
|
||||
const side = h.side;
|
||||
const div = document.createElement("div");
|
||||
div.className = `bubble bubble-${side}`;
|
||||
if (STATE.viewPly === ply) div.classList.add("active");
|
||||
if (ply === activePly) { div.classList.add("active"); activeDiv = div; }
|
||||
const moveNum = Math.floor(i / 2) + 1;
|
||||
const moveLabel = side === "white" ? `${moveNum}.` : `${moveNum}...`;
|
||||
const meta = document.createElement("div");
|
||||
@@ -86,14 +117,21 @@ function renderChat() {
|
||||
list.appendChild(div);
|
||||
});
|
||||
|
||||
// auto-scroll to bottom if at live
|
||||
if (STATE.viewPly === null) list.scrollTop = list.scrollHeight;
|
||||
// Scroll the active bubble into view (smooth at live tail, instant elsewhere).
|
||||
if (activeDiv) {
|
||||
requestAnimationFrame(() => activeDiv.scrollIntoView({
|
||||
behavior: "smooth", block: "nearest",
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
function renderHistory() {
|
||||
const list = $("history-list");
|
||||
list.innerHTML = "";
|
||||
const moves = (STATE.game?.history || []).filter(h => h.san);
|
||||
const activePly = STATE.viewPly === null ? moves.length : STATE.viewPly;
|
||||
let activeLi = null;
|
||||
|
||||
let row = 0;
|
||||
for (let i = 0; i < moves.length; i += 2) {
|
||||
row++;
|
||||
@@ -104,18 +142,23 @@ function renderHistory() {
|
||||
|
||||
const w = document.createElement("li");
|
||||
w.textContent = moves[i]?.san || "";
|
||||
if (STATE.viewPly === i + 1) w.classList.add("active");
|
||||
if (i + 1 === activePly) { w.classList.add("active"); activeLi = w; }
|
||||
w.addEventListener("click", () => setView(i + 1));
|
||||
list.appendChild(w);
|
||||
|
||||
const b = document.createElement("li");
|
||||
b.textContent = moves[i + 1]?.san || "";
|
||||
if (moves[i + 1]) {
|
||||
if (STATE.viewPly === i + 2) b.classList.add("active");
|
||||
if (i + 2 === activePly) { b.classList.add("active"); activeLi = b; }
|
||||
b.addEventListener("click", () => setView(i + 2));
|
||||
}
|
||||
list.appendChild(b);
|
||||
}
|
||||
if (activeLi) {
|
||||
requestAnimationFrame(() => activeLi.scrollIntoView({
|
||||
behavior: "smooth", block: "nearest",
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
function renderStatus() {
|
||||
@@ -129,32 +172,53 @@ function renderStatus() {
|
||||
}
|
||||
}
|
||||
|
||||
function applyEval(e) {
|
||||
const cp = e.cp, mate = e.mate;
|
||||
const col = $("eval-col"), bar = $("eval-fill"), txt = $("eval-text");
|
||||
// pct = % of the bar occupied by WHITE (bottom-anchored).
|
||||
// 50 = even; >50 = white ahead; <50 = black ahead.
|
||||
function storeEval(e) {
|
||||
if (!e || !e.fen) return;
|
||||
// Normalize cp/mate to WHITE's POV. Stockfish reports from side-to-move.
|
||||
// If FEN says black to move, flip the sign.
|
||||
const sideToMove = e.fen.split(" ")[1] || "w";
|
||||
const flip = sideToMove === "b";
|
||||
const norm = {
|
||||
cp: e.cp == null ? null : (flip ? -e.cp : e.cp),
|
||||
mate: e.mate == null ? null : (flip ? -e.mate : e.mate),
|
||||
best_uci: e.best_uci || null,
|
||||
pv: e.pv || [],
|
||||
fen: e.fen,
|
||||
};
|
||||
STATE.evalByFen.set(e.fen, norm);
|
||||
}
|
||||
|
||||
function applyEvalForFen(fen) {
|
||||
const e = STATE.evalByFen.get(fen);
|
||||
const col = $("eval-col"), bar = $("eval-fill"), txt = $("eval-text"), pv = $("sf-pv");
|
||||
if (!e) {
|
||||
txt.textContent = "—";
|
||||
pv.textContent = "";
|
||||
bar.style.height = "50%";
|
||||
col.classList.remove("advantage-black");
|
||||
return;
|
||||
}
|
||||
let pct = 50;
|
||||
if (mate != null) {
|
||||
pct = mate > 0 ? 100 : 0;
|
||||
txt.textContent = (mate > 0 ? "+M" : "-M") + Math.abs(mate);
|
||||
} else if (cp != null) {
|
||||
// map cp to percentage: ±800cp = full bar. Capped.
|
||||
pct = Math.max(2, Math.min(98, 50 + cp / 16));
|
||||
const v = (cp / 100);
|
||||
if (e.mate != null) {
|
||||
pct = e.mate > 0 ? 100 : 0;
|
||||
txt.textContent = (e.mate > 0 ? "+M" : "-M") + Math.abs(e.mate);
|
||||
} else if (e.cp != null) {
|
||||
pct = Math.max(2, Math.min(98, 50 + e.cp / 16));
|
||||
const v = (e.cp / 100);
|
||||
txt.textContent = (v > 0 ? "+" : "") + v.toFixed(2);
|
||||
} else {
|
||||
txt.textContent = "—";
|
||||
}
|
||||
bar.style.height = `${pct}%`;
|
||||
col.classList.toggle("advantage-black", pct < 50);
|
||||
if (e.best_uci && e.best_uci.length >= 4) {
|
||||
const from = e.best_uci.slice(0, 2), to = e.best_uci.slice(2, 4);
|
||||
cg.setShapes([{ orig: from, dest: to, brush: "green" }]);
|
||||
}
|
||||
if (e.pv && e.pv.length) {
|
||||
$("sf-pv").textContent = "pv: " + e.pv.slice(0, 6).join(" ");
|
||||
}
|
||||
col.classList.toggle("advantage-white", pct > 50);
|
||||
pv.textContent = e.pv && e.pv.length ? "pv: " + e.pv.slice(0, 6).join(" ") : "";
|
||||
}
|
||||
|
||||
function applyEval(e) {
|
||||
storeEval(e);
|
||||
// If we're currently looking at this fen, refresh the visuals.
|
||||
if (currentFen() === e.fen) renderBoard();
|
||||
}
|
||||
|
||||
function setView(ply) {
|
||||
@@ -167,20 +231,15 @@ function goLive() {
|
||||
renderBoard(); renderChat(); renderHistory();
|
||||
}
|
||||
|
||||
function log(line) {
|
||||
const t = $("log-text");
|
||||
t.textContent += line + "\n";
|
||||
// trim to last 200 lines
|
||||
const lines = t.textContent.split("\n");
|
||||
if (lines.length > 200) t.textContent = lines.slice(-200).join("\n");
|
||||
t.scrollTop = t.scrollHeight;
|
||||
}
|
||||
function log(_line) { /* log strip removed; keep no-op to preserve callsites */ }
|
||||
|
||||
function applySnapshot(s) {
|
||||
STATE.game = s.game;
|
||||
STATE.settings = s.settings;
|
||||
STATE.bindings = s.bindings;
|
||||
STATE.viewPly = null;
|
||||
STATE.evalByFen.clear();
|
||||
for (const e of (s.evals || [])) storeEval(e);
|
||||
renderBoard(); renderChat(); renderHistory(); renderStatus();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user