From 8dc489f7af399b9e1236595a6bff2ad36b3a0a69 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Wed, 27 Dec 2023 08:42:59 +0100 Subject: [PATCH] Remove the global move_num and toplay variables. These are redundant from the FEN, and we would like to fix so that we can use a different FEN in some cases, so remove the (semi-)hidden dependency on the board state. --- Position.pm | 2 ++ www/js/remoteglot.js | 57 ++++++++++++++++++++++++++++---------------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/Position.pm b/Position.pm index 3e39e46..9e9fe29 100644 --- a/Position.pm +++ b/Position.pm @@ -191,6 +191,8 @@ sub fen { sub to_json_hash { my $pos = shift; my $json = { %$pos, fen => $pos->fen() }; + delete $json->{'toplay'}; + delete $json->{'move_num'}; delete $json->{'board'}; delete $json->{'prettyprint_cache'}; delete $json->{'tbprobe_cache'}; diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 51a470f..c0cba6a 100644 --- a/www/js/remoteglot.js +++ b/www/js/remoteglot.js @@ -8,7 +8,7 @@ * @type {Number} * @const * @private */ -let SCRIPT_VERSION = 2021021300; +let SCRIPT_VERSION = 2023122700; /** * The current backend URL. @@ -86,12 +86,6 @@ let refutation_lines = []; */ let hash_refutation_lines = null; -/** @type {!number} @private */ -let move_num = 1; - -/** @type {!string} @private */ -let toplay = 'W'; - /** @type {number} @private */ let ims = 0; @@ -356,8 +350,7 @@ function possibly_play_sound(old_data, new_data) { if (ding && ding.play) { if (old_data['position'] && old_data['position']['fen'] && new_data['position'] && new_data['position']['fen'] && - (old_data['position']['fen'] !== new_data['position']['fen'] || - old_data['position']['move_num'] !== new_data['position']['move_num'])) { + old_data['position']['fen'] !== new_data['position']['fen']) { ding.play(); } } @@ -401,6 +394,26 @@ 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. + */ +function find_toplay(fen) { + return fen.split(' ')[1].toUpperCase(); +} + +/** @param {!string} fen + * @return {!number} + * + * Return the move clock, starting from 1. See also find_toplay(). + */ +function find_move_num(fen) { + return parseInt(fen.split(' ')[5]); +} + /** @param {!number} x * @return {!number} */ @@ -658,7 +671,8 @@ function find_nonstupid_moves(data, margin, invert) { moves.push(move); } } - moves = moves.sort(function(a, b) { return compare_by_score(data['refutation_lines'], data['position']['toplay'] === 'B', a, b) }); + let toplay = find_toplay(data['position']['fen']); + moves = moves.sort(function(a, b) { return compare_by_score(data['refutation_lines'], toplay, a, b) }); moves.unshift(data['pv'][0]); return moves; @@ -872,6 +886,8 @@ function update_refutation_lines() { moves.push(move); } + let move_num = find_move_num(base_fen); + let toplay = find_toplay(base_fen); let invert = (toplay === 'B'); if (current_display_line && current_display_move % 2 == 0 && !current_display_line_is_history) { invert = !invert; @@ -1165,11 +1181,12 @@ function update_board() { } else if (data['position']['last_move'] !== 'none') { // Find the previous move. let previous_move_num, previous_toplay; - if (data['position']['toplay'] == 'B') { - previous_move_num = data['position']['move_num']; + let fen = data['position']['fen']; + if (find_toplay(fen) === 'B') { + previous_move_num = find_move_num(fen); previous_toplay = 'W'; } else { - previous_move_num = data['position']['move_num'] - 1; + previous_move_num = find_move_num(fen) - 1; previous_toplay = 'B'; } @@ -1292,10 +1309,11 @@ function update_board() { document.getElementById("pvtitle").textContent = "PV:"; let scores = [{ first_move: -1, score: data['score'] }]; - document.getElementById("pv").replaceChildren(add_pv(data['position']['fen'], data['pv'], data['position']['move_num'], data['position']['toplay'], scores, 0)); + document.getElementById("pv").replaceChildren(add_pv(data['position']['fen'], data['pv'], scores, 0)); // Update the PV arrow. clear_arrows(); + let toplay = find_toplay(data['position']['fen']); if (data['pv'].length >= 1) { let hiddenboard = new Chess(base_fen); @@ -1313,7 +1331,7 @@ function update_board() { hiddenboard.move(data['pv'][i + 1]); // To keep continuity. } - let alt_moves = find_nonstupid_moves(data, 30, data['position']['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])); @@ -1325,7 +1343,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, data['position']['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]); @@ -1365,8 +1383,6 @@ function update_board() { // Update the refutation lines. base_fen = data['position']['fen']; - move_num = parseInt(data['position']['move_num']); - toplay = data['position']['toplay']; refutation_lines = hash_refutation_lines || data['refutation_lines']; update_refutation_lines(); @@ -1386,8 +1402,9 @@ function update_sparkline(data) { } } if (first_move_num !== undefined) { - let last_move_num = data['position']['move_num'] * 2 - 3; - if (data['position']['toplay'] === 'B') { + let fen = data['position']['fen']; + let last_move_num = find_move_num(fen) * 2 - 3; + if (find_toplay(fen) === 'B') { ++last_move_num; } -- 2.39.2