]> git.sesse.net Git - remoteglot/blobdiff - www/js/remoteglot.js
Remove the global move_num and toplay variables.
[remoteglot] / www / js / remoteglot.js
index e14fffa2b81394d6c63645182414e7ca1a142119..c0cba6a6120514406e02311e5a7f4307ec8761af 100644 (file)
@@ -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;
 
@@ -125,6 +119,9 @@ let reverse_dragging_from = null;
 /** @type {?number} @private */
 let unique = null;
 
+/** @type {?string} @private */
+let admin_password = null;
+
 /** @type {boolean} @private */
 let enable_sound = false;
 
@@ -353,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();
                }
        }
@@ -398,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}
  */
@@ -655,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;
@@ -869,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;
@@ -1114,15 +1133,6 @@ function update_board() {
                headline = 'Analysis';
        }
 
-       // Credits, where applicable. Note that we don't want the footer to change a lot
-       // when e.g. viewing history, so if any of these changed during the game,
-       // use the current one still.
-       if (current_data['using_lomonosov']) {
-               document.getElementById("lomonosov").style.display = null;
-       } else {
-               document.getElementById("lomonosov").style.display = 'none';
-       }
-
        // Credits: The engine name/version.
        if (current_data['engine'] && current_data['engine']['name'] !== null) {
                document.getElementById("engineid").textContent = current_data['engine']['name'];
@@ -1171,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';
                }
 
@@ -1269,8 +1280,6 @@ function update_board() {
        // The search stats.
        if (data['searchstats']) {
                document.getElementById("searchstats").textContent = data['searchstats'];
-       } else if (data['tablebase'] == 1) {
-               document.getElementById("searchstats").textContent = "Tablebase result";
        } else if (data['nodes'] && data['nps'] && data['depth']) {
                let stats = thousands(data['nodes']) + ' nodes, ' + thousands(data['nps']) + ' nodes/sec, depth ' + data['depth'] + ' ply';
                if (data['seldepth']) {
@@ -1288,6 +1297,9 @@ function update_board() {
        } else {
                document.getElementById("searchstats").textContent = "";
        }
+       if (admin_password !== null) {
+               document.getElementById("searchstats").innerHTML += " | <span style=\"color: red;\">ADMIN MODE (if password is right)</span>";
+       }
 
        // Update the board itself.
        base_fen = data['position']['fen'];
@@ -1297,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);
 
@@ -1318,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]));
@@ -1330,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]);
@@ -1370,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();
 
@@ -1391,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;
                        }
 
@@ -1987,7 +1999,8 @@ function update_displayed_line() {
 function set_board_position(new_fen) {
        board_is_animating = true;
        let old_fen = board.fen();
-       board.position(new_fen);
+       let animate = old_fen !== '8/8/8/8/8/8/8/';
+       board.position(new_fen, animate);
        if (board.fen() === old_fen) {
                board_is_animating = false;
        }
@@ -2224,6 +2237,18 @@ function onSnapEnd(source, target) {
                promotion: 'q' // NOTE: always promote to a queen for example simplicity
        });
 
+       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);
+               return;
+       }
+
        // Move ahead on the line we're on -- this includes history if we've
        // gone backwards.
        if (current_display_line &&
@@ -2469,6 +2494,11 @@ function init() {
                set_sound(false);
        }
 
+       let admin_match = window.location.href.match(/\?password=([a-zA-Z0-9_-]+)/);
+       if (admin_match !== null) {
+               admin_password = admin_match[1];
+       }
+
        // Create board.
        board = new window.ChessBoard('board', {
                onMoveEnd: function() { board_is_animating = false; },