X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=www%2Fjs%2Fremoteglot.js;h=ceeff7b3c94de6c10ff6f013d837b414c500227f;hb=6bc75bbe9c7b761ebce2844cf204c14c12bf5bf9;hp=7135953fb2298b579393d2a2668a28279fb59436;hpb=b2779e155a09b1a3fcc2e132aaafbbbe4884ed49;p=remoteglot diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 7135953..ceeff7b 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -5,7 +5,7 @@ * Version of this script. If the server returns a version larger than * this, it is a sign we should reload to upgrade ourselves. * - * @type {Number} + * @type {number} * @const * @private */ let SCRIPT_VERSION = 2023122700; @@ -75,17 +75,30 @@ let occupied_by_arrows = []; /** Currently displayed refutation lines (on-screen). * Can either come from the current_analysis_data, displayed_analysis_data, - * or hash_refutation_lines. + * or hash_refutation_lines (choose_displayed_refutation_lines() chooses which one). + * + * @typedef {{ + * score: Array, + * depth: string, + * pv: Array., + * move: string + * }} + * @private */ -let refutation_lines = []; +var RefutationLine; /** Refutation lines from current hash probe. - * * If non-null, will override refutation lines from the base position. - * Note that these are relative to display_fen, not base_fen. + * + * @type {Array.} */ let hash_refutation_lines = null; +/** + * What FEN hash_refutation_lines is relative to. + */ +let hash_refutation_lines_base_fen = null; + /** @type {number} @private */ let ims = 0; @@ -157,15 +170,19 @@ let display_fen = null; /** @typedef {{ * start_fen: string, * pv: Array., - * move_num: number, - * toplay: string, * scores: Array<{first_move: number, score: Object}>, * start_display_move_num: number - * }} DisplayLine + * }} + * + * "start_display_move_num" is the (half-)move number to start displaying the PV at, + * i.e., the index into pv. * - * "start_display_move_num" is the (half-)move number to start displaying the PV at. - * "score" is also evaluated at this point. + * "score" is also evaluated at this point. scores can be empty and is frequently + * sparse; it's generally only really useful for history (we obviously don't know + * much about how the score will * move during e.g. a PV, except that a mate/TB + * counter might go down). */ +var DisplayLine; /** All PVs that we currently know of. * @@ -184,7 +201,11 @@ let current_display_line = null; /** @type {boolean} @private */ let current_display_line_is_history = false; -/** @type {?number} @private */ +/** @type {?number} @private + * + * The highlighted/used/shown move in current_display_line.pv, in terms of absolute index + * (not relative to e.g. the start FEN). + */ let current_display_move = null; /** @@ -200,7 +221,7 @@ let current_analysis_xhr = null; * The current timer to fire off a request to get main analysis (not history), * if any, so that we can abort it. * - * @type {?Number} + * @type {?number} * @private */ let current_analysis_request_timer = null; @@ -225,7 +246,7 @@ let current_hash_xhr = null; * The current timer to display hash probe information (it could be waiting on the * board to stop animating), if any, so that we can abort it. * - * @type {?Number} + * @type {?number} * @private */ let current_hash_display_timer = null; @@ -412,6 +433,17 @@ function find_move_num(fen) { return parseInt(fen.split(' ')[5]); } +/** @param {!string} fen + * @return {!number} + * + * Return the half-move clock, starting from 0 (and never resetting). + */ +function find_halfmove_num(fen) { + let move_num = find_move_num(fen); + let toplay = find_toplay(fen); + return (move_num - 1) * 2 + (toplay === 'w' ? 0 : 1); +} + /** @param {!number} x * @return {!number} */ @@ -687,7 +719,7 @@ function thousands(x) { /** * @param {!string} start_fen * @param {Array.} pv - * @param {Array<{ first_move: integer, score: Object }>} scores + * @param {Array<{ first_move: number, score: Object }>} scores * @param {number} start_display_move_num * @param {number=} opt_limit * @param {boolean=} opt_showlast @@ -696,8 +728,6 @@ function add_pv(start_fen, pv, scores, start_display_move_num, opt_limit, opt_sh display_lines.push({ start_fen: start_fen, pv: pv, - move_num: find_move_num(start_fen), - toplay: find_toplay(start_fen), scores: scores, start_display_move_num: start_display_move_num }); @@ -721,37 +751,15 @@ function add_pv(start_fen, pv, scores, start_display_move_num, opt_limit, opt_sh function print_pv(line_num, splicepos, opt_limit, opt_showlast) { let display_line = display_lines[line_num]; let pv = display_line.pv; - let move_num = display_line.move_num; - let toplay = display_line.toplay; - - // Truncate PV at the start if needed. - let start_display_move_num = display_line.start_display_move_num; - if (start_display_move_num > 0) { - pv = pv.slice(start_display_move_num); - let to_add = start_display_move_num; - if (toplay === 'b') { - ++move_num; - toplay = 'w'; - --to_add; - } - if (to_add % 2 == 1) { - toplay = 'b'; - --to_add; - } - move_num += to_add / 2; - if (splicepos !== null && splicepos > 0) { - --splicepos; - } - } + let halfmove_num = find_halfmove_num(display_line.start_fen) + 2; // From two, for simplicity. + let start_halfmove_num = halfmove_num; let ret = document.createDocumentFragment(); - let in_tb = false; - let i = 0; - if (opt_limit && opt_showlast && pv.length > opt_limit) { - // Truncate the PV at the beginning (instead of at the end). - // We assume here that toplay is 'w'. We also assume that if - // opt_showlast is set, then it is the history, and thus, - // the UI should be to expand the history. + + // Truncate PV at the start if needed. + let to_skip = display_line.start_display_move_num; + if (opt_limit && opt_showlast && pv.length - to_skip > opt_limit) { + // Explicit (UI-visible) truncation from the start, for the history. ret.appendChild(document.createTextNode('(')); let link = document.createElement('a'); link.className = 'move'; @@ -759,25 +767,37 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { link.textContent = '…'; ret.appendChild(link); ret.appendChild(document.createTextNode(') ')); - i = pv.length - opt_limit; - if (i % 2 == 1) { - ++i; + to_skip = pv.length - opt_limit; + to_skip += to_skip % 2; // Make sure it starts on a white move. + } + if (to_skip > 0) { + pv = pv.slice(to_skip); + halfmove_num += to_skip; + if (splicepos !== null) { + splicepos -= to_skip; + if (splicepos < 0) { + splicepos = 0; + } } - move_num += i / 2; - } else if (toplay == 'b' && pv.length > 0) { - ret.appendChild(document.createTextNode(move_num + '. … ')); } - for (; i < pv.length; ++i) { + + // The initial move number needs to go before any (TB: …) marker. + if (halfmove_num % 2 == 1) { + // Black move. + ret.appendChild(document.createTextNode((halfmove_num - 1) / 2 + '. … ')); + } else { + // White move. + ret.appendChild(document.createTextNode(halfmove_num / 2 + '. ')); + } + let in_tb = false; + for (let i = 0; i < pv.length; ++i, ++halfmove_num) { let prefix = ''; if (splicepos === i) { - prefix = '(TB: '; + prefix = '(TB:'; in_tb = true; } - if (toplay == 'b' && i == 0) { - ++move_num; - toplay = 'w'; - } else if (toplay == 'w') { + if (halfmove_num % 2 == 0 && i != 0) { if (i > opt_limit && !opt_showlast) { if (in_tb) { prefix += ')'; @@ -785,20 +805,17 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { ret.appendChild(document.createTextNode(prefix + ' (…)')); return ret; } - prefix += ' ' + move_num + '. '; - ++move_num; - toplay = 'b'; + prefix += ' ' + (halfmove_num / 2) + '. '; } else { prefix += ' '; - toplay = 'w'; } ret.appendChild(document.createTextNode(prefix)); let link = document.createElement('a'); link.className = 'move'; - link.setAttribute('id', 'automove' + line_num + '-' + i); + link.setAttribute('id', 'automove' + line_num + '-' + (halfmove_num - start_halfmove_num)); link.textContent = pv[i]; - link.href = 'javascript:show_line(' + line_num + ', ' + i + ');'; + link.href = 'javascript:show_line(' + line_num + ', ' + (halfmove_num - start_halfmove_num) + ');'; ret.appendChild(link); } if (in_tb) { @@ -846,12 +863,23 @@ function collapse_history(truncate_history) { } window['collapse_history'] = collapse_history; -/** Update the HTML display of multi-PV from the global "refutation_lines". +function choose_displayed_refutation_lines() { + if (hash_refutation_lines) { + // If we're in hash exploration, that takes precedence. + return [hash_refutation_lines, hash_refutation_lines_base_fen]; + } else { + let data = displayed_analysis_data || current_analysis_data; + return [data['refutation_lines'], data['position']['fen']]; + } +} + +/** Update the HTML display of multi-PV. * * Also recreates the global "display_lines". */ function update_refutation_lines() { - if (base_fen === null) { + const [refutation_lines, refutation_lines_base_fen] = choose_displayed_refutation_lines(); + if (!refutation_lines) { return; } if (display_lines.length > 2) { @@ -882,7 +910,7 @@ function update_refutation_lines() { moves.push(move); } - let invert = (find_toplay(base_fen) === 'b'); + let invert = (find_toplay(refutation_lines_base_fen) === 'b'); if (current_display_line && current_display_move % 2 == 0 && !current_display_line_is_history) { invert = !invert; } @@ -919,7 +947,7 @@ function update_refutation_lines() { let pv_td = document.createElement("td"); tr.appendChild(pv_td); pv_td.classList.add("pv"); - pv_td.append(add_pv(base_fen, base_line.concat([ line['move'] ]), scores, start_display_move_num)); + pv_td.append(add_pv(refutation_lines_base_fen, base_line.concat([ line['move'] ]), scores, start_display_move_num)); tbl.append(tr); continue; @@ -948,7 +976,7 @@ function update_refutation_lines() { let pv_td = document.createElement("td"); tr.appendChild(pv_td); pv_td.classList.add("pv"); - pv_td.append(add_pv(base_fen, base_line.concat(line['pv']), scores, start_display_move_num, 10)); + pv_td.append(add_pv(refutation_lines_base_fen, base_line.concat(line['pv']), scores, start_display_move_num, 10)); tbl.append(tr); } @@ -1238,7 +1266,6 @@ function update_board() { document.getElementById("refutationlines").replaceChildren(); document.getElementById("whiteclock").replaceChildren(); document.getElementById("blackclock").replaceChildren(); - refutation_lines = []; update_refutation_lines(); clear_arrows(); update_displayed_line(); @@ -1375,9 +1402,7 @@ function update_board() { } } - // Update the refutation lines. base_fen = data['position']['fen']; - refutation_lines = hash_refutation_lines || data['refutation_lines']; update_refutation_lines(); // Update the sparkline last, since its size depends on how everything else reflowed. @@ -1620,7 +1645,7 @@ function update_clock() { } /** - * @param {Number} remaining_ms + * @param {number} remaining_ms * @param {boolean} show_seconds */ function format_clock(remaining_ms, show_seconds) { @@ -1646,7 +1671,7 @@ function format_clock(remaining_ms, show_seconds) { } /** - * @param {Number} x + * @param {number} x */ function format_2d(x) { if (x >= 10) { @@ -1658,7 +1683,7 @@ function format_2d(x) { /** * @param {string} move - * @param {Number} move_num Move number of this move. + * @param {number} move_num Move number of this move. * @param {boolean} white_to_play Whether white is to play this move. */ function format_move_with_number(move, move_num, white_to_play) { @@ -1674,7 +1699,7 @@ function format_move_with_number(move, move_num, white_to_play) { /** * @param {string} move - * @param {Number} halfmove_num Half-move number that is to be played, + * @param {number} halfmove_num Half-move number that is to be played, * starting from 0. */ function format_halfmove_with_number(move, halfmove_num) { @@ -1686,7 +1711,7 @@ function format_halfmove_with_number(move, halfmove_num) { /** * @param {Object} data - * @param {Number} halfmove_num + * @param {number} halfmove_num */ function format_tooltip(data, halfmove_num) { if (data['score_history'][halfmove_num + 1] || @@ -1722,15 +1747,6 @@ function format_tooltip(data, halfmove_num) { } } -/** - * @param {boolean} truncate_history - */ -function set_truncate_history(truncate_history) { - truncate_display_history = truncate_history; - update_refutation_lines(); -} -window['set_truncate_history'] = set_truncate_history; - /** * @param {number} line_num * @param {number} move_num @@ -1749,7 +1765,7 @@ function show_line(line_num, move_num) { return; } else { current_display_line = {...display_lines[line_num]}; // Shallow clone. - current_display_move = move_num + current_display_line.start_display_move_num; + current_display_move = move_num; } current_display_line_is_history = (line_num == 0); @@ -1916,7 +1932,7 @@ function update_move_highlight() { display_lines[1].pv = []; } - highlighted_move = document.getElementById("automove" + display_line_num + "-" + (current_display_move - current_display_line.start_display_move_num)); + highlighted_move = document.getElementById("automove" + display_line_num + "-" + current_display_move); if (highlighted_move !== null) { highlighted_move.classList.add('highlight'); } @@ -2065,6 +2081,7 @@ function show_explore_hash_results(data, fen) { } current_hash_display_timer = null; hash_refutation_lines = data['lines']; + hash_refutation_lines_base_fen = fen; update_board(); } @@ -2176,6 +2193,7 @@ function get_best_move(game, source, target, invert) { let best_move = null; let best_move_score = null; + let refutation_lines = choose_displayed_refutation_lines()[0]; for (let move in refutation_lines) { let line = refutation_lines[move]; if (!line['score']) { @@ -2278,7 +2296,7 @@ function onSnapEnd(source, target) { } let line = display_lines[i]; if (line.pv[line.start_display_move_num] === move.san) { - show_line(i, 0); + show_line(i, line.start_display_move_num); return; } }