From b2779e155a09b1a3fcc2e132aaafbbbe4884ed49 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 09:35:56 +0100 Subject: [PATCH 01/16] Preparatory refactoring. --- www/js/remoteglot.js | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 870703c..7135953 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -2218,6 +2218,28 @@ function onDrop(source, target) { if (move === null) return 'snapback'; } +/** + * If we are in admin mode, send this move to the backend. + * + * @param {string} fen + * @param {string} move + */ +function send_chosen_move(fen, move) { + if (admin_password !== null) { + let history = current_analysis_data['position']['history']; + let url = '/manual-override.pl'; + url += '?fen=' + encodeURIComponent(fen); + url += '&history=' + encodeURIComponent(JSON.stringify(history)); + url += '&move=' + encodeURIComponent(move); + url += '&player_w=' + encodeURIComponent(current_analysis_data['position']['player_w']); + url += '&player_b=' + encodeURIComponent(current_analysis_data['position']['player_b']); + url += '&password=' + encodeURIComponent(admin_password); + + console.log(fen, history); + fetch(url); // Ignore the result. + } +} + function onSnapEnd(source, target) { if (source === target && recommended_move !== null) { source = recommended_move.from; @@ -2232,14 +2254,7 @@ function onSnapEnd(source, target) { }); if (admin_password !== null) { - let url = '/manual-override.pl'; - url += '?fen=' + encodeURIComponent(display_fen); - url += '&history=' + encodeURIComponent(JSON.stringify(current_analysis_data['position']['history'])); - url += '&move=' + encodeURIComponent(move.san); - url += '&player_w=' + encodeURIComponent(current_analysis_data['position']['player_w']); - url += '&player_b=' + encodeURIComponent(current_analysis_data['position']['player_b']); - url += '&password=' + encodeURIComponent(admin_password); - fetch(url); + send_chosen_move(display_fen, move.san); return; } -- 2.39.2 From ae8f2127842079342869c1f818fe0812f0abba9a Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 09:50:25 +0100 Subject: [PATCH 02/16] Potentially fix a move-number error in refutation lines. Or possibly just mess up everything. This is mainly because I have too many patches in trying to fix up admin mode, and none of them work, and I need to clear out a bit :-) --- www/js/remoteglot.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 7135953..039db0e 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -76,6 +76,10 @@ 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. + * + * TODO: This is a mess, and we should probably just let update_refutation_lines() + * decide anew every time instead of storing the state here in multiple global + * variables. */ let refutation_lines = []; @@ -86,6 +90,12 @@ let refutation_lines = []; */ let hash_refutation_lines = null; +/** + * What FEN refutation_lines is relative to. Will usually be base_fen, + * but if another move gets loaded while we are looking, they can diverge. + */ +let refutation_lines_base_fen = null; + /** @type {number} @private */ let ims = 0; @@ -851,7 +861,7 @@ window['collapse_history'] = collapse_history; * Also recreates the global "display_lines". */ function update_refutation_lines() { - if (base_fen === null) { + if (refutation_lines_base_fen === null) { return; } if (display_lines.length > 2) { @@ -882,7 +892,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 +929,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 +958,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); } @@ -1239,6 +1249,7 @@ function update_board() { document.getElementById("whiteclock").replaceChildren(); document.getElementById("blackclock").replaceChildren(); refutation_lines = []; + refutation_lines_base_fen = null; update_refutation_lines(); clear_arrows(); update_displayed_line(); @@ -1377,7 +1388,10 @@ function update_board() { // Update the refutation lines. base_fen = data['position']['fen']; - refutation_lines = hash_refutation_lines || data['refutation_lines']; + if (hash_refutation_lines === null) { + refutation_lines = data['refutation_lines']; + refutation_lines_base_fen = base_fen; + } update_refutation_lines(); // Update the sparkline last, since its size depends on how everything else reflowed. @@ -1740,6 +1754,7 @@ function show_line(line_num, move_num) { current_display_line = null; current_display_move = null; hash_refutation_lines = null; + refutation_lines_base_fen = base_fen; if (displayed_analysis_data) { // TODO: Support exiting to history position if we are in an // analysis line of a history position. @@ -2065,6 +2080,7 @@ function show_explore_hash_results(data, fen) { } current_hash_display_timer = null; hash_refutation_lines = data['lines']; + refutation_lines_base_fen = fen; update_board(); } -- 2.39.2 From 190f64548b562d0086e98445153e6ec96a47a275 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 10:09:49 +0100 Subject: [PATCH 03/16] Fix some random type annotations. --- www/js/remoteglot.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 039db0e..5947591 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; @@ -80,6 +80,14 @@ let occupied_by_arrows = []; * TODO: This is a mess, and we should probably just let update_refutation_lines() * decide anew every time instead of storing the state here in multiple global * variables. + * + * @type {Array.<{ + * score: Array, + * depth: string, + * pv: Array., + * move: string + * }>} + * @private */ let refutation_lines = []; @@ -171,11 +179,12 @@ let display_fen = null; * 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. * "score" is also evaluated at this point. */ +var DisplayLine; /** All PVs that we currently know of. * @@ -210,7 +219,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; @@ -235,7 +244,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; @@ -697,7 +706,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 @@ -1634,7 +1643,7 @@ function update_clock() { } /** - * @param {Number} remaining_ms + * @param {number} remaining_ms * @param {boolean} show_seconds */ function format_clock(remaining_ms, show_seconds) { @@ -1660,7 +1669,7 @@ function format_clock(remaining_ms, show_seconds) { } /** - * @param {Number} x + * @param {number} x */ function format_2d(x) { if (x >= 10) { @@ -1672,7 +1681,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) { @@ -1688,7 +1697,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) { @@ -1700,7 +1709,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] || -- 2.39.2 From ce0761dfe536522fb3570f145b0307d19869b893 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 10:11:04 +0100 Subject: [PATCH 04/16] Chess.js: Remove unused keep_headers. --- www/js/chess.js | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/www/js/chess.js b/www/js/chess.js index b9d4d53..4598758 100644 --- a/www/js/chess.js +++ b/www/js/chess.js @@ -128,11 +128,7 @@ var Chess = function(fen) { load(fen); } - function clear(keep_headers) { - if (typeof keep_headers === 'undefined') { - keep_headers = false; - } - + function clear() { board = new Array(128); kings = {w: EMPTY, b: EMPTY}; turn = WHITE; @@ -141,7 +137,7 @@ var Chess = function(fen) { half_moves = 0; move_number = 1; history = []; - if (!keep_headers) header = {}; + header = {}; update_setup(generate_fen()); } @@ -149,11 +145,7 @@ var Chess = function(fen) { load(DEFAULT_POSITION); } - function load(fen, keep_headers) { - if (typeof keep_headers === 'undefined') { - keep_headers = false; - } - + function load(fen) { var tokens = fen.split(/\s+/); var position = tokens[0]; var square = 0; @@ -162,7 +154,7 @@ var Chess = function(fen) { return false; } - clear(keep_headers); + clear(); for (var i = 0; i < position.length; i++) { var piece = position.charAt(i); -- 2.39.2 From deed21c21899c068017e4a9e149aaec592252447 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 10:51:41 +0100 Subject: [PATCH 05/16] Remove a small redundancy in DisplayLine. --- www/js/remoteglot.js | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 5947591..6c220f4 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -175,8 +175,6 @@ 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 * }} @@ -715,8 +713,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 }); @@ -740,8 +736,8 @@ 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; + let move_num = find_move_num(display_line.start_fen); + let toplay = find_toplay(display_line.start_fen); // Truncate PV at the start if needed. let start_display_move_num = display_line.start_display_move_num; -- 2.39.2 From b8bab144633660d73362f242bd6a334d9cdfc88b Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 20:51:41 +0100 Subject: [PATCH 06/16] Remove unused entry point set_truncate_history(). --- www/js/remoteglot.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 6c220f4..34b413f 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -1741,15 +1741,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 -- 2.39.2 From a4adf36837b43e693b288cc700fcd72bbba78cb3 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 22:56:12 +0100 Subject: [PATCH 07/16] Clean up the refutation line choice logic a bit. --- www/js/remoteglot.js | 47 ++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 24 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 34b413f..cf5fba7 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -75,34 +75,29 @@ 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). * - * TODO: This is a mess, and we should probably just let update_refutation_lines() - * decide anew every time instead of storing the state here in multiple global - * variables. - * - * @type {Array.<{ + * @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 refutation_lines is relative to. Will usually be base_fen, - * but if another move gets loaded while we are looking, they can diverge. + * What FEN hash_refutation_lines is relative to. */ -let refutation_lines_base_fen = null; +let hash_refutation_lines_base_fen = null; /** @type {number} @private */ let ims = 0; @@ -861,12 +856,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 (refutation_lines_base_fen === null) { + const [refutation_lines, refutation_lines_base_fen] = choose_displayed_refutation_lines(); + if (!refutation_lines) { return; } if (display_lines.length > 2) { @@ -1253,8 +1259,6 @@ function update_board() { document.getElementById("refutationlines").replaceChildren(); document.getElementById("whiteclock").replaceChildren(); document.getElementById("blackclock").replaceChildren(); - refutation_lines = []; - refutation_lines_base_fen = null; update_refutation_lines(); clear_arrows(); update_displayed_line(); @@ -1391,12 +1395,7 @@ function update_board() { } } - // Update the refutation lines. base_fen = data['position']['fen']; - if (hash_refutation_lines === null) { - refutation_lines = data['refutation_lines']; - refutation_lines_base_fen = base_fen; - } update_refutation_lines(); // Update the sparkline last, since its size depends on how everything else reflowed. @@ -1750,7 +1749,6 @@ function show_line(line_num, move_num) { current_display_line = null; current_display_move = null; hash_refutation_lines = null; - refutation_lines_base_fen = base_fen; if (displayed_analysis_data) { // TODO: Support exiting to history position if we are in an // analysis line of a history position. @@ -2076,7 +2074,7 @@ function show_explore_hash_results(data, fen) { } current_hash_display_timer = null; hash_refutation_lines = data['lines']; - refutation_lines_base_fen = fen; + hash_refutation_lines_base_fen = fen; update_board(); } @@ -2188,6 +2186,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']) { -- 2.39.2 From 79966cb518085499da9ea884d9d99dd74d2f9514 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 28 Dec 2023 09:05:14 +0100 Subject: [PATCH 08/16] Make the print_pv() logic hopefully slightly easier to understand. Using half-move logic instead of trying to keep track of white/black manually seems to be a good idea. No guarantees I didn't add extra bugs, though. --- www/js/remoteglot.js | 74 +++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 43 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index cf5fba7..2e66c82 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -733,35 +733,14 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { let pv = display_line.pv; let move_num = find_move_num(display_line.start_fen); let toplay = find_toplay(display_line.start_fen); - - // 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 = move_num * 2 + (toplay === 'w' ? 0 : 1); // From two, for simplicity. 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'; @@ -769,25 +748,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 += ')'; @@ -795,12 +786,9 @@ 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)); -- 2.39.2 From f1ec5afd1d8e6d99ca439df48f3d6a8a966cbf6b Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 28 Dec 2023 09:16:07 +0100 Subject: [PATCH 09/16] Comment DisplayLine.scores better. --- www/js/remoteglot.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 2e66c82..2a15b3e 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -175,7 +175,10 @@ let display_fen = null; * }} * * "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; -- 2.39.2 From b9fd5677c24d668a989e30d30deb5fb453df81db Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 28 Dec 2023 10:10:41 +0100 Subject: [PATCH 10/16] Factor out find_halfmove_num() into its own function. --- www/js/remoteglot.js | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 2a15b3e..69f0134 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -427,6 +427,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} */ @@ -734,9 +745,7 @@ 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 = find_move_num(display_line.start_fen); - let toplay = find_toplay(display_line.start_fen); - let halfmove_num = move_num * 2 + (toplay === 'w' ? 0 : 1); // From two, for simplicity. + let halfmove_num = find_halfmove_num(display_line.start_fen) + 2; // From two, for simplicity. let ret = document.createDocumentFragment(); -- 2.39.2 From 6bc75bbe9c7b761ebce2844cf204c14c12bf5bf9 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Thu, 28 Dec 2023 15:06:54 +0100 Subject: [PATCH 11/16] More fixes for relative move numbering after the recent refactoring. --- www/js/remoteglot.js | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 69f0134..ceeff7b 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -174,7 +174,9 @@ let display_fen = null; * start_display_move_num: number * }} * - * "start_display_move_num" is the (half-)move number to start displaying the PV at. + * "start_display_move_num" is the (half-)move number to start displaying the PV at, + * i.e., the index into pv. + * * "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 @@ -199,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; /** @@ -746,6 +752,7 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { let display_line = display_lines[line_num]; let pv = display_line.pv; 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(); @@ -806,9 +813,9 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { 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) { @@ -1758,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); @@ -1925,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'); } @@ -2289,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; } } -- 2.39.2 From 5a1f6c832883367ca4925443c63ed2aefb6514fd Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 30 Dec 2023 10:56:33 +0100 Subject: [PATCH 12/16] Add an undo button to the admin interface. --- www/js/remoteglot.js | 25 ++++++++++++++++++++++++- www/manual-override.pl | 25 +++++++++++++++++-------- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index ceeff7b..d36bf9e 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -1319,7 +1319,7 @@ function update_board() { document.getElementById("searchstats").textContent = ""; } if (admin_password !== null) { - document.getElementById("searchstats").innerHTML += " | ADMIN MODE (if password is right)"; + document.getElementById("searchstats").innerHTML += " | ADMIN MODE (if password is right) | Undo move"; } // Update the board itself. @@ -2258,6 +2258,29 @@ function send_chosen_move(fen, move) { } } +function undo_move() { + if (admin_password !== null) { + let history = current_analysis_data['position']['history']; + history = history.slice(0, history.length - 1); + + let position = current_analysis_data['position']['start_fen']; + let hiddenboard = chess_from(position, history, history.length); + let fen = hiddenboard.fen(); + + let url = '/manual-override.pl'; + url += '?fen=' + encodeURIComponent(fen); + url += '&history=' + encodeURIComponent(JSON.stringify(history)); + url += '&move=null'; + url += '&player_w=' + encodeURIComponent(current_analysis_data['position']['player_w']); + url += '&player_b=' + encodeURIComponent(current_analysis_data['position']['player_b']); + url += '&password=' + encodeURIComponent(admin_password); + + console.log(fen, history); + fetch(url); // Ignore the result. + } +} +window['undo_move'] = undo_move; + function onSnapEnd(source, target) { if (source === target && recommended_move !== null) { source = recommended_move.from; diff --git a/www/manual-override.pl b/www/manual-override.pl index 1333352..23f571f 100755 --- a/www/manual-override.pl +++ b/www/manual-override.pl @@ -20,14 +20,23 @@ if (!defined($password) || $password ne $remoteglotconf::adminpass) { exit; } -$dbh->do('INSERT INTO game_extensions ( fen, history, player_w, player_b, ts, next_move ) VALUES ( ?, ?, ?, ?, CURRENT_TIMESTAMP, ? )', - undef, - Encode::decode_utf8($cgi->param('fen')), - Encode::decode_utf8($cgi->param('history')), - Encode::decode_utf8($cgi->param('player_w')), - Encode::decode_utf8($cgi->param('player_b')), - Encode::decode_utf8($cgi->param('move'))); -system("sudo", "touch", $remoteglotconf::target); +if ($cgi->param('move') eq 'null') { + $dbh->do('DELETE FROM game_extensions WHERE fen=? AND history=? AND player_w=? AND player_b=?', + undef, + Encode::decode_utf8($cgi->param('fen')), + Encode::decode_utf8($cgi->param('history')), + Encode::decode_utf8($cgi->param('player_w')), + Encode::decode_utf8($cgi->param('player_b'))); +} else { + $dbh->do('INSERT INTO game_extensions ( fen, history, player_w, player_b, ts, next_move ) VALUES ( ?, ?, ?, ?, CURRENT_TIMESTAMP, ? )', + undef, + Encode::decode_utf8($cgi->param('fen')), + Encode::decode_utf8($cgi->param('history')), + Encode::decode_utf8($cgi->param('player_w')), + Encode::decode_utf8($cgi->param('player_b')), + Encode::decode_utf8($cgi->param('move'))); +} +system("sudo", "/usr/bin/touch", $remoteglotconf::target); print CGI->header(-type=>'text/plain; charset=utf-8'); print "OK\n"; -- 2.39.2 From 8189ee4740066938b9f640bf0847aa707bcbfc96 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Sat, 30 Dec 2023 11:59:23 +0100 Subject: [PATCH 13/16] Update the Closure compiler (no big changes). --- build.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/build.sh b/build.sh index c970b13..75141e4 100755 --- a/build.sh +++ b/build.sh @@ -1,12 +1,12 @@ #! /bin/sh -# Download http://dl.google.com/closure-compiler/compiler-latest.zip -# and unzip it in closure/ before running this script. +# Download the latest .jar from +# https://mvnrepository.com/artifact/com.google.javascript/closure-compiler/v20231112 +# (adjust the URL for the version you want) before running this script. umask 022 -java -jar closure-compiler-v20221004.jar \ - --language_in ECMASCRIPT_2018 \ +java -jar closure-compiler-v20231112.jar \ --compilation_level SIMPLE \ --js_output_file=www/js/remoteglot.min.js \ www/js/chessboard-0.3.0.js \ -- 2.39.2 From 3ca44b02eb1e3ffc751ad046ffa832d5ffe95591 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 11:24:26 +0100 Subject: [PATCH 14/16] Update the list of manual move extensions even if the FEN did not change. This makes it easier to see how far behind the feed is. --- remoteglot.pl | 1 + 1 file changed, 1 insertion(+) diff --git a/remoteglot.pl b/remoteglot.pl index 663b2d5..08e03ea 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -268,6 +268,7 @@ sub handle_position { for my $key ('white_clock', 'black_clock', 'white_clock_target', 'black_clock_target') { $pos_calculating->{$key} //= $pos->{$key}; } + $pos_calculating->{'extra_moves'} = $pos->{'extra_moves'}; return; } -- 2.39.2 From 04d3e4d93513d9a060b82eff5e45d68294afa932 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 11:29:41 +0100 Subject: [PATCH 15/16] Allow polling more often. --- config.pm | 3 +++ remoteglot.pl | 4 ++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/config.pm b/config.pm index 1bd40e5..073ab92 100644 --- a/config.pm +++ b/config.pm @@ -77,6 +77,9 @@ our $dbipass = undef; # For manual moves made from the web interface. our $adminpass = undef; +# How often to poll the target, in seconds. +our $poll_frequency = 1.0; + eval { my $config_filename = $ENV{'REMOTEGLOT_CONFIG'} // 'config.local.pm'; require $config_filename; diff --git a/remoteglot.pl b/remoteglot.pl index 08e03ea..f7b0456 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -142,7 +142,7 @@ sub fetch_pgn { }; if ($@) { warn "$url: $@"; - $http_timer = AnyEvent->timer(after => 1.0, cb => sub { + $http_timer = AnyEvent->timer(after => $remoteglotconf::poll_frequency, cb => sub { fetch_pgn($url); }); } @@ -252,7 +252,7 @@ sub handle_pgn { } } - $http_timer = AnyEvent->timer(after => 1.0, cb => sub { + $http_timer = AnyEvent->timer(after => $remoteglotconf::poll_frequency, cb => sub { fetch_pgn($url); }); } -- 2.39.2 From 7518f69658b2a65d742ca401afda30e944f37172 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 7 Feb 2024 17:47:21 +0100 Subject: [PATCH 16/16] Handle streaming PGNs, like from Lichess (although this might break non-streaming ones). --- remoteglot.pl | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/remoteglot.pl b/remoteglot.pl index f7b0456..b5db356 100755 --- a/remoteglot.pl +++ b/remoteglot.pl @@ -89,7 +89,10 @@ print "Chess engine ready.\n"; if (defined($remoteglotconf::target)) { if ($remoteglotconf::target =~ /^(?:\/|https?:)/) { - fetch_pgn($remoteglotconf::target); + my $target = $remoteglotconf::target; + # Convenience. + $target =~ s#https://lichess.org/broadcast/.*/([^/]+)?$#https://lichess.org/api/stream/broadcast/round/$1.pgn#; + fetch_pgn($target); } } @@ -147,9 +150,14 @@ sub fetch_pgn { }); } } else { - AnyEvent::HTTP::http_get($url, sub { - handle_pgn(@_, $url); - }); + my $buffer = ''; + AnyEvent::HTTP::http_get($url, + on_body => sub { + handle_partial_pgn(@_, \$buffer, $url); + }, + sub { + end_pgn(@_, \$buffer, $url); + }); } } @@ -157,14 +165,32 @@ my ($last_pgn_white, $last_pgn_black); my @last_pgn_uci_moves = (); my $pgn_hysteresis_counter = 0; -sub handle_pgn { - my ($body, $header, $url) = @_; +sub handle_partial_pgn { + my ($body, $header, $buffer, $url) = @_; if ($stop_pgn_fetch) { $stop_pgn_fetch = 0; $http_timer = undef; - return; + return 0; } + $$buffer .= $body; + while ($$buffer =~ s/^\s*(.*)\n\n\n//s) { + handle_pgn($1, $url); + } + return 1; +} + +sub end_pgn { + my ($body, $header, $buffer, $url) = @_; + handle_pgn($$buffer, $url); + $$buffer = ""; + $http_timer = AnyEvent->timer(after => $remoteglotconf::poll_frequency, cb => sub { + fetch_pgn($url); + }); +} + +sub handle_pgn { + my ($body, $url) = @_; my $pgn = Chess::PGN::Parse->new(undef, $body); if (!defined($pgn)) { -- 2.39.2