X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=www%2Fjs%2Fremoteglot.js;h=c0cba6a6120514406e02311e5a7f4307ec8761af;hb=8dc489f7af399b9e1236595a6bff2ad36b3a0a69;hp=5314a5681dbbbfbf7c589cd253b1179388988777;hpb=ddeb2eaf98778f23b624212fa100db79a8be8560;p=remoteglot diff --git a/www/js/remoteglot.js b/www/js/remoteglot.js index 5314a56..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; @@ -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} */ @@ -504,12 +520,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 +541,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 +584,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; @@ -656,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; @@ -734,6 +750,7 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { let ret = document.createDocumentFragment(); let in_tb = false; + 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 @@ -754,14 +771,10 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { } else if (toplay == 'B' && pv.length > 0) { ret.appendChild(document.createTextNode(move_num + '. … ')); } - for (let i = 0; i < pv.length; ++i) { - 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 + ');'; + for (; i < pv.length; ++i) { + let prefix = ''; if (splicepos === i) { - ret.appendChild(document.createTextNode('(TB: ')); + prefix = '(TB: '; in_tb = true; } @@ -771,18 +784,25 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) { } 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; } - 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) { @@ -866,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; @@ -1111,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']; @@ -1168,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'; } @@ -1226,7 +1240,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 = " "; + document.getElementById("searchstats").innerHTML = " "; document.getElementById("refutationlines").replaceChildren(); document.getElementById("whiteclock").replaceChildren(); document.getElementById("blackclock").replaceChildren(); @@ -1266,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']) { @@ -1285,6 +1297,9 @@ function update_board() { } else { document.getElementById("searchstats").textContent = ""; } + if (admin_password !== null) { + document.getElementById("searchstats").innerHTML += " | ADMIN MODE (if password is right)"; + } // Update the board itself. base_fen = data['position']['fen']; @@ -1294,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); @@ -1315,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])); @@ -1327,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]); @@ -1367,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(); @@ -1388,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; } @@ -1595,7 +1610,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) { @@ -1900,7 +1915,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. @@ -1984,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; } @@ -2076,12 +2092,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) { @@ -2106,13 +2122,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') { @@ -2221,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 && @@ -2466,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; }, @@ -2479,6 +2512,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(); @@ -2486,6 +2524,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();