]> git.sesse.net Git - remoteglot/blobdiff - www/js/remoteglot.js
Add a very crude function for manual overrides.
[remoteglot] / www / js / remoteglot.js
index 5d19116ee1927ebc82a989e0aab19a0dc646cf5b..51a470f502c59af3788255da7c719bab1694b336 100644 (file)
@@ -125,6 +125,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;
 
@@ -504,12 +507,11 @@ function position_arrow(arrow) {
                return;
        }
 
-       let board_width = parseInt(document.querySelector(".board-b72b1").style.width, 10);
-       let zoom_factor = board_width / 400.0;
-       let line_width = arrow.line_width * zoom_factor;
-       let arrow_size = arrow.arrow_size * zoom_factor;
+       // We always draw as if the board is 400x400, the viewBox will adjust that for us
+       let line_width = arrow.line_width;
+       let arrow_size = arrow.arrow_size;
 
-       let square_width = board_width / 8;
+       let square_width = 400 / 8;
        let from_y, to_y, from_x, to_x;
        if (board.orientation() === 'black') {
                from_y = (arrow.from_row + 0.5)*square_width;
@@ -526,10 +528,9 @@ function position_arrow(arrow) {
        let SVG_NS = "http://www.w3.org/2000/svg";
        let XHTML_NS = "http://www.w3.org/1999/xhtml";
        let svg = document.createElementNS(SVG_NS, "svg");
-       svg.setAttribute("width", board_width);
-       svg.setAttribute("height", board_width);
-       svg.setAttribute("style", "position: absolute");
-       svg.setAttribute("position", "absolute");
+       svg.setAttribute("width", "100%");
+       svg.setAttribute("height", "100%");
+       svg.setAttribute("viewBox", "0 0 400 400");
        svg.setAttribute("version", "1.1");
        svg.setAttribute("class", "c1");
        svg.setAttribute("xmlns", XHTML_NS);
@@ -570,8 +571,9 @@ function position_arrow(arrow) {
        head.setAttribute("fill", arrow.fg_color);
        svg.appendChild(head);
 
-       svg.style.top = '2px';  /* Border for .board-b72b1. */
-       svg.style.left = '2px';
+       svg.style.position = 'absolute';
+       svg.style.top = '0px';  /* Border for .board-b72b1. */
+       svg.style.left = '0px';
        svg.style.pointerEvents = 'none';
        document.getElementById('board').appendChild(svg);
        arrow.svg = svg;
@@ -733,16 +735,16 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
        }
 
        let ret = document.createDocumentFragment();
-       let i = 0;
        let in_tb = false;
-       let link = document.createElement('a');
-       link.className = 'move';
+       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.
                ret.appendChild(document.createTextNode('('));
+               let link = document.createElement('a');
+               link.className = 'move';
                link.href = 'javascript:collapse_history(false)';
                link.textContent = '…';
                ret.appendChild(link);
@@ -754,48 +756,39 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
                move_num += i / 2;
        } else if (toplay == 'B' && pv.length > 0) {
                ret.appendChild(document.createTextNode(move_num + '. … '));
-               let move = "";
-               if (splicepos === 0) {
-                       ret.appendChild(document.createTextNode('(TB: '));
-                       in_tb = true;
-               }
-               link.setAttribute('id', 'automove' + line_num + '-0');
-               link.href = 'javascript:show_line(' + line_num + ', ' + 0 + ');';
-               link.textContent = pv[0];
-               ret.appendChild(link);
-               toplay = 'W';
-               ++i;
-               ++move_num;
        }
-       for ( ; i < pv.length; ++i) {
-               link = document.createElement('a');
-               link.className = 'move';
-               link.setAttribute('id', 'automove' + line_num + '-' + i);
-               link.textContent = pv[i];
-               link.href = 'javascript:show_line(' + line_num + ', ' + i + ');';
+       for (; i < pv.length; ++i) {
+               let prefix = '';
                if (splicepos === i) {
-                       ret.appendChild(document.createTextNode('(TB: '));
+                       prefix = '(TB: ';
                        in_tb = true;
                }
 
-               if (toplay == 'W') {
+               if (toplay == 'B' && i == 0) {
+                       ++move_num;
+                       toplay = 'W';
+               } else if (toplay == 'W') {
                        if (i > opt_limit && !opt_showlast) {
                                if (in_tb) {
-                                       ret.appendChild(document.createTextNode(')'));
+                                       prefix += ')';
                                }
-                               ret.appendChild(document.createTextNode(' (…)'));
+                               ret.appendChild(document.createTextNode(prefix + ' (…)'));
                                return ret;
                        }
-                       if (ret != '') {
-                               ret.appendChild(document.createTextNode(' '));
-                       }
-                       ret.appendChild(document.createTextNode(move_num + '. '));
+                       prefix += ' ' + move_num + '. ';
                        ++move_num;
                        toplay = 'B';
                } else {
-                       ret.appendChild(document.createTextNode(' '));
+                       prefix += ' ';
                        toplay = 'W';
                }
+               ret.appendChild(document.createTextNode(prefix));
+
+               let link = document.createElement('a');
+               link.className = 'move';
+               link.setAttribute('id', 'automove' + line_num + '-' + i);
+               link.textContent = pv[i];
+               link.href = 'javascript:show_line(' + line_num + ', ' + i + ');';
                ret.appendChild(link);
        }
        if (in_tb) {
@@ -1124,15 +1117,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'];
@@ -1239,7 +1223,7 @@ function update_board() {
                document.getElementById("score").textContent = "No analysis for this move";
                document.getElementById("pvtitle").textContent = "PV:";
                document.getElementById("pv").replaceChildren();
-               document.getElementById("searchstats").textContent = "&nbsp;";
+               document.getElementById("searchstats").innerHTML = "&nbsp;";
                document.getElementById("refutationlines").replaceChildren();
                document.getElementById("whiteclock").replaceChildren();
                document.getElementById("blackclock").replaceChildren();
@@ -1279,8 +1263,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']) {
@@ -1298,6 +1280,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'];
@@ -1608,7 +1593,7 @@ function update_clock() {
        // This matches what DGT clocks do.
        let show_seconds = (white_clock_ms < 60 * 20 * 1000 || black_clock_ms < 60 * 20 * 1000);
 
-       if (color) {
+       if (color && remaining_ms > 0) {
                // See when the clock will change next, and update right after that.
                let next_update_ms;
                if (show_seconds) {
@@ -1913,7 +1898,7 @@ function update_move_highlight() {
                        document.getElementById("pvtitle").textContent = "Exploring:";
                        current_display_line.start_display_move_num = 0;
                        display_lines.push(current_display_line);
-                       document.getElementById("pv").append(print_pv(display_lines.length - 1, null));  // FIXME
+                       document.getElementById("pv").replaceChildren(print_pv(display_lines.length - 1, null));  // FIXME
                        display_line_num = display_lines.length - 1;
 
                        // Clear out the PV, so it's not selected by anything later.
@@ -1997,7 +1982,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;
        }
@@ -2089,12 +2075,12 @@ function onDragStart(source, piece, position, orientation) {
 }
 
 function mousedownSquare(e) {
-       if (!e.target || !e.target.closest('.square-55d63')) {
+       if (!e.target || !e.target.getAttribute('data-square')) {
                return;
        }
 
        reverse_dragging_from = null;
-       let square = e.target.closest('.square-55d63').getAttribute('data-square');
+       let square = e.target.getAttribute('data-square');
 
        let pseudogame = new Chess(display_fen);
        if (pseudogame.game_over() === true) {
@@ -2119,13 +2105,13 @@ function mousedownSquare(e) {
 }
 
 function mouseupSquare(e) {
-       if (!e.target || !e.target.closest('.square-55d63')) {
+       if (!e.target || !e.target.getAttribute('data-square')) {
                return;
        }
        if (reverse_dragging_from === null) {
                return;
        }
-       let source = e.target.closest('.square-55d63').getAttribute('data-square');
+       let source = e.target.getAttribute('data-square');
        let target = reverse_dragging_from;
        reverse_dragging_from = null;
        if (onDrop(source, target) !== 'snapback') {
@@ -2234,6 +2220,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 &&
@@ -2479,6 +2477,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; },
@@ -2492,6 +2495,11 @@ function init() {
        document.getElementById("board").addEventListener('mousedown', mousedownSquare);
        document.getElementById("board").addEventListener('mouseup', mouseupSquare);
 
+       if (window['inline_json']) {
+               let j = window['inline_json'];
+               process_update_response(j['data'], { 'get': (h) => j['headers'][h] });
+               delete window['inline_json'];
+       }
        request_update();
        window.addEventListener('resize', function() {
                board.resize();
@@ -2499,6 +2507,7 @@ function init() {
                update_board_highlight();
                redraw_arrows();
        });
+       new ResizeObserver(() => update_sparkline(displayed_analysis_data || current_analysis_data)).observe(document.getElementById('scoresparkcontainer'));
        window.addEventListener('keyup', function(event) {
                if (event.which == 39) {  // Left arrow.
                        next_move();