]> git.sesse.net Git - remoteglot/blobdiff - www/js/remoteglot.js
Remove redundant parameters to add_pv().
[remoteglot] / www / js / remoteglot.js
index 54fc90b46afc5d5e818ddb87bd9a7cfaa41b33b6..d6659f8e588cd8ee11ed6b3e9f321a78caee9f04 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;
@@ -672,19 +689,17 @@ function thousands(x) {
 /**
  * @param {!string} start_fen
  * @param {Array.<string>} pv
- * @param {number} move_num
- * @param {!string} toplay
  * @param {Array<{ first_move: integer, score: Object }>} scores
  * @param {number} start_display_move_num
  * @param {number=} opt_limit
  * @param {boolean=} opt_showlast
  */
-function add_pv(start_fen, pv, move_num, toplay, scores, start_display_move_num, opt_limit, opt_showlast) {
+function add_pv(start_fen, pv, scores, start_display_move_num, opt_limit, opt_showlast) {
        display_lines.push({
                start_fen: start_fen,
                pv: pv,
-               move_num: parseInt(move_num),
-               toplay: toplay,
+               move_num: find_move_num(start_fen),
+               toplay: find_toplay(start_fen),
                scores: scores,
                start_display_move_num: start_display_move_num
        });
@@ -869,7 +884,7 @@ function update_refutation_lines() {
                moves.push(move);
        }
 
-       let invert = (toplay === 'B');
+       let invert = (find_toplay(base_fen) === 'B');
        if (current_display_line && current_display_move % 2 == 0 && !current_display_line_is_history) {
                invert = !invert;
        }
@@ -906,7 +921,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'] ]), move_num, toplay, scores, start_display_move_num));
+                       pv_td.append(add_pv(base_fen, base_line.concat([ line['move'] ]), scores, start_display_move_num));
 
                        tbl.append(tr);
                        continue;
@@ -935,7 +950,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']), move_num, toplay, scores, start_display_move_num, 10));
+               pv_td.append(add_pv(base_fen, base_line.concat(line['pv']), scores, start_display_move_num, 10));
 
                tbl.append(tr);
        }
@@ -1087,8 +1102,8 @@ function update_board() {
        // unconditionally taken from current_data (we're not interested in
        // historic history).
        if (current_data['position']['history']) {
-               let start = (current_data['position'] && current_data['position']['start_fen']) ? current_data['position']['start_fen'] : 'start';
-               add_pv(start, current_data['position']['history'], 1, 'W', null, 0, 8, true);
+               let start = (current_data['position'] && current_data['position']['start_fen']) ? current_data['position']['start_fen'] : 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
+               add_pv(start, current_data['position']['history'], null, 0, 8, true);
        } else {
                display_lines.push(null);
        }
@@ -1162,11 +1177,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';
                }
 
@@ -1277,6 +1293,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'];
@@ -1286,10 +1305,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);
 
@@ -1307,7 +1327,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]));
@@ -1319,7 +1339,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]);
@@ -1359,8 +1379,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();
 
@@ -1380,8 +1398,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;
                        }
 
@@ -2214,6 +2233,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 &&
@@ -2459,6 +2490,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; },