X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=www%2Fjs%2Fremoteglot.js;h=6c220f43cac9dbb2462c12678f02f18304c64025;hb=deed21c21899c068017e4a9e149aaec592252447;hp=d6659f8e588cd8ee11ed6b3e9f321a78caee9f04;hpb=f78cdecc6646f28638dfd86a3a8ef27d11238aea;p=remoteglot diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index d6659f8..6c220f4 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; @@ -76,6 +76,18 @@ 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. + * + * @type {Array.<{ + * score: Array, + * depth: string, + * pv: Array., + * move: string + * }>} + * @private */ let refutation_lines = []; @@ -86,6 +98,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; @@ -157,15 +175,14 @@ 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. * "score" is also evaluated at this point. */ +var DisplayLine; /** All PVs that we currently know of. * @@ -200,7 +217,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 +242,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; @@ -397,12 +414,10 @@ function redraw_arrows() { /** @param {!string} fen * @return {!string} * - * Return whose side it is to play (W or B), given a FEN. - * This is in uppercase due to historical reasons, which we - * should get rid of. + * Return whose side it is to play (w or b), given a FEN. */ function find_toplay(fen) { - return fen.split(' ')[1].toUpperCase(); + return fen.split(' ')[1]; } /** @param {!string} fen @@ -689,7 +704,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 @@ -698,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 }); @@ -723,21 +736,21 @@ 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; if (start_display_move_num > 0) { pv = pv.slice(start_display_move_num); let to_add = start_display_move_num; - if (toplay === 'B') { + if (toplay === 'b') { ++move_num; - toplay = 'W'; + toplay = 'w'; --to_add; } if (to_add % 2 == 1) { - toplay = 'B'; + toplay = 'b'; --to_add; } move_num += to_add / 2; @@ -751,7 +764,7 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { 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 + // 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. ret.appendChild(document.createTextNode('(')); @@ -766,7 +779,7 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { ++i; } move_num += i / 2; - } else if (toplay == 'B' && pv.length > 0) { + } else if (toplay == 'b' && pv.length > 0) { ret.appendChild(document.createTextNode(move_num + '. … ')); } for (; i < pv.length; ++i) { @@ -776,10 +789,10 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { in_tb = true; } - if (toplay == 'B' && i == 0) { + if (toplay == 'b' && i == 0) { ++move_num; - toplay = 'W'; - } else if (toplay == 'W') { + toplay = 'w'; + } else if (toplay == 'w') { if (i > opt_limit && !opt_showlast) { if (in_tb) { prefix += ')'; @@ -789,10 +802,10 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { } prefix += ' ' + move_num + '. '; ++move_num; - toplay = 'B'; + toplay = 'b'; } else { prefix += ' '; - toplay = 'W'; + toplay = 'w'; } ret.appendChild(document.createTextNode(prefix)); @@ -853,7 +866,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) { @@ -884,7 +897,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; } @@ -921,7 +934,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; @@ -950,7 +963,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); } @@ -1178,18 +1191,18 @@ function update_board() { // Find the previous move. let previous_move_num, previous_toplay; let fen = data['position']['fen']; - if (find_toplay(fen) === 'B') { + if (find_toplay(fen) === 'b') { previous_move_num = find_move_num(fen); - previous_toplay = 'W'; + previous_toplay = 'w'; } else { previous_move_num = find_move_num(fen) - 1; - previous_toplay = 'B'; + previous_toplay = 'b'; } last_move = format_move_with_number( data['position']['last_move'], previous_move_num, - previous_toplay == 'W'); + previous_toplay == 'w'); headline += ' after ' + last_move; } else { last_move = null; @@ -1241,6 +1254,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(); @@ -1327,7 +1341,7 @@ function update_board() { hiddenboard.move(data['pv'][i + 1]); // To keep continuity. } - let alt_moves = find_nonstupid_moves(data, 30, toplay === 'B'); + let alt_moves = find_nonstupid_moves(data, 30, toplay === 'b'); for (let i = 1; i < alt_moves.length && i < 3; ++i) { hiddenboard = new Chess(base_fen); let move = patch_move(hiddenboard.move(alt_moves[i])); @@ -1339,7 +1353,7 @@ function update_board() { // See if all semi-reasonable moves have only one possible response. if (data['pv'].length >= 2) { - let nonstupid_moves = find_nonstupid_moves(data, 300, toplay === 'B'); + let nonstupid_moves = find_nonstupid_moves(data, 300, toplay === 'b'); let hiddenboard = new Chess(base_fen); hiddenboard.move(data['pv'][0]); let response = hiddenboard.move(data['pv'][1]); @@ -1379,7 +1393,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. @@ -1400,7 +1417,7 @@ function update_sparkline(data) { if (first_move_num !== undefined) { let fen = data['position']['fen']; let last_move_num = find_move_num(fen) * 2 - 3; - if (find_toplay(fen) === 'B') { + if (find_toplay(fen) === 'b') { ++last_move_num; } @@ -1622,7 +1639,7 @@ function update_clock() { } /** - * @param {Number} remaining_ms + * @param {number} remaining_ms * @param {boolean} show_seconds */ function format_clock(remaining_ms, show_seconds) { @@ -1648,7 +1665,7 @@ function format_clock(remaining_ms, show_seconds) { } /** - * @param {Number} x + * @param {number} x */ function format_2d(x) { if (x >= 10) { @@ -1660,7 +1677,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) { @@ -1676,7 +1693,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) { @@ -1688,7 +1705,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] || @@ -1742,6 +1759,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. @@ -2067,6 +2085,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(); } @@ -2220,6 +2239,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; @@ -2234,14 +2275,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; }