5 * Version of this script. If the server returns a version larger than
6 * this, it is a sign we should reload to upgrade ourselves.
11 let SCRIPT_VERSION = 2023122700;
14 * The current backend URL.
19 let backend_url = "/analysis.pl";
20 let backend_hash_url = "/hash";
22 /** @type {window.ChessBoard} @private */
25 /** @type {boolean} @private */
26 let board_is_animating = false;
29 * The most recent analysis data we have from the server
30 * (about the most recent position).
34 let current_analysis_data = null;
37 * If we are displaying previous analysis or from hash, this is non-null,
38 * and will override most of current_analysis_data.
43 let displayed_analysis_data = null;
46 * Games currently in progress, if any.
58 let current_games = null;
73 /** @type {Array.<Array.<boolean>>} */
74 let occupied_by_arrows = [];
76 /** Currently displayed refutation lines (on-screen).
77 * Can either come from the current_analysis_data, displayed_analysis_data,
78 * or hash_refutation_lines.
80 let refutation_lines = [];
82 /** Refutation lines from current hash probe.
84 * If non-null, will override refutation lines from the base position.
85 * Note that these are relative to display_fen, not base_fen.
87 let hash_refutation_lines = null;
89 /** @type {number} @private */
92 /** @type {boolean} @private */
93 let truncate_display_history = true;
95 /** @type {!string|undefined} @private */
96 let highlight_from = undefined;
98 /** @type {!string|undefined} @private */
99 let highlight_to = undefined;
101 /** The HTML object of the move currently being highlighted (in red).
104 let highlighted_move = null;
106 /** Currently suggested/recommended move when dragging.
107 * @type {?{from: !string, to: !string}}
110 let recommended_move = null;
112 /** If reverse-dragging (dragging from the destination square to the
113 * source square), the destination square.
117 let reverse_dragging_from = null;
119 /** @type {?number} @private */
122 /** @type {?string} @private */
123 let admin_password = null;
125 /** @type {boolean} @private */
126 let enable_sound = false;
128 /** @type {!number} @private */
132 * Our best estimate of how many milliseconds we need to add to
133 * new Date() to get the true UTC time. Calibrated against the
139 let client_clock_offset_ms = null;
141 let clock_timer = null;
143 /** The current position being analyzed, represented as a FEN string.
144 * Note that this is not necessarily the same as display_fen.
150 /** The current position on the board, represented as a FEN string.
151 * Note that board.fen() does not contain e.g. who is to play.
155 let display_fen = null;
159 * pv: Array.<string>,
162 * scores: Array<{first_move: number, score: Object}>,
163 * start_display_move_num: number
166 * "start_display_move_num" is the (half-)move number to start displaying the PV at.
167 * "score" is also evaluated at this point.
170 /** All PVs that we currently know of.
172 * Element 0 is history (or null if no history).
173 * Element 1 is current main PV, or explored line if nowhere else on the screen.
174 * All remaining elements are refutation lines (multi-PV).
176 * @type {Array.<DisplayLine>}
179 let display_lines = [];
181 /** @type {?DisplayLine} @private */
182 let current_display_line = null;
184 /** @type {boolean} @private */
185 let current_display_line_is_history = false;
187 /** @type {?number} @private */
188 let current_display_move = null;
191 * The current backend request to get main analysis (not history), if any,
192 * so that we can abort it.
194 * @type {?AbortController}
197 let current_analysis_xhr = null;
200 * The current timer to fire off a request to get main analysis (not history),
201 * if any, so that we can abort it.
206 let current_analysis_request_timer = null;
209 * The current backend request to get historic data, if any.
211 * @type {?AbortController}
214 let current_historic_xhr = null;
217 * The current backend request to get hash probes, if any, so that we can abort it.
219 * @type {?AbortController}
222 let current_hash_xhr = null;
225 * The current timer to display hash probe information (it could be waiting on the
226 * board to stop animating), if any, so that we can abort it.
231 let current_hash_display_timer = null;
233 function supports_html5_storage() {
235 return 'localStorage' in window && window['localStorage'] !== null;
241 // Make the unique token persistent so people refreshing the page won't count twice.
242 // Of course, you can never fully protect against people deliberately wanting to spam.
243 function get_unique() {
244 let use_local_storage = supports_html5_storage();
245 if (use_local_storage && window['localStorage']['unique']) {
246 return window['localStorage']['unique'];
248 let unique = Math.random();
249 if (use_local_storage) {
250 window['localStorage']['unique'] = unique;
255 function request_update() {
256 current_analysis_request_timer = null;
258 let handle_err = () => {
259 // Backend error or similar. Wait ten seconds, then try again.
260 current_analysis_request_timer = setTimeout(function() { request_update(); }, 10000);
263 current_analysis_xhr = new AbortController();
264 const signal = current_analysis_xhr.signal;
265 fetch(backend_url + "?ims=" + ims + "&unique=" + unique, { signal })
266 .then((response) => response.json().then(data => ({ok: response.ok, headers: response.headers, json: data}))) // ick
273 if (delay_ms === 0) {
274 process_update_response(obj.json, obj.headers);
276 setTimeout(function() { process_update_response(obj.json, obj.headers); }, delay_ms);
280 if (!backend_url.match(/history/)) {
282 current_analysis_request_timer = setTimeout(function() { request_update(); }, timeout);
286 if (err.name === 'AbortError') {
287 // Aborted because we are switching backends. Abandon and don't retry,
288 // because another one is already started for us.
296 document.body.style.opacity = null;
301 function process_update_response(data, headers) {
302 sync_server_clock(headers.get('Date'));
303 ims = headers.get('X-RGLM');
304 let num_viewers = headers.get('X-RGNV');
306 if (Array.isArray(data)) {
307 new_data = JSON.parse(JSON.stringify(current_analysis_data));
308 JSON_delta.patch(new_data, data);
313 let minimum_version = headers.get('X-RGMV');
314 if (minimum_version && minimum_version > SCRIPT_VERSION) {
315 // Upgrade to latest version with a force-reload.
316 location.reload(true);
319 // Verify that the PV makes sense.
321 if (new_data['pv']) {
322 let hiddenboard = new Chess(new_data['position']['fen']);
323 for (let i = 0; i < new_data['pv'].length; ++i) {
324 if (hiddenboard.move(new_data['pv'][i]) === null) {
332 possibly_play_sound(current_analysis_data, new_data);
333 current_analysis_data = new_data;
335 update_num_viewers(num_viewers);
337 console.log("Received invalid update, waiting five seconds and trying again.");
338 setTimeout(function() { location.reload(true); }, 5000);
342 function possibly_play_sound(old_data, new_data) {
346 if (old_data === null) {
349 let ding = document.getElementById('ding');
350 if (ding && ding.play) {
351 if (old_data['position'] && old_data['position']['fen'] &&
352 new_data['position'] && new_data['position']['fen'] &&
353 old_data['position']['fen'] !== new_data['position']['fen']) {
360 * @type {!string} server_date_string
362 function sync_server_clock(server_date_string) {
363 let server_time_ms = new Date(server_date_string).getTime();
364 let client_time_ms = new Date().getTime();
365 let estimated_offset_ms = server_time_ms - client_time_ms;
367 // In order not to let the noise move us too much back and forth
368 // (the server only has one-second resolution anyway), we only
369 // change an existing skew if we are at least five seconds off.
370 if (client_clock_offset_ms === null ||
371 Math.abs(estimated_offset_ms - client_clock_offset_ms) > 5000) {
372 client_clock_offset_ms = estimated_offset_ms;
376 function clear_arrows() {
377 for (let i = 0; i < arrows.length; ++i) {
379 arrows[i].svg.remove();
380 delete arrows[i].svg;
385 occupied_by_arrows = [];
386 for (let y = 0; y < 8; ++y) {
387 occupied_by_arrows.push([false, false, false, false, false, false, false, false]);
391 function redraw_arrows() {
392 for (let i = 0; i < arrows.length; ++i) {
393 position_arrow(arrows[i]);
397 /** @param {!string} fen
400 * Return whose side it is to play (W or B), given a FEN.
401 * This is in uppercase due to historical reasons, which we
404 function find_toplay(fen) {
405 return fen.split(' ')[1].toUpperCase();
408 /** @param {!string} fen
411 * Return the move clock, starting from 1. See also find_toplay().
413 function find_move_num(fen) {
414 return parseInt(fen.split(' ')[5]);
417 /** @param {!number} x
430 /** See if drawing this arrow on the board would cause unduly amount of confusion.
431 * @param {!string} from The square the arrow is from (e.g. e4).
432 * @param {!string} to The square the arrow is to (e.g. e4).
435 function interfering_arrow(from, to) {
436 let from_col = from.charCodeAt(0) - "a1".charCodeAt(0);
437 let from_row = from.charCodeAt(1) - "a1".charCodeAt(1);
438 let to_col = to.charCodeAt(0) - "a1".charCodeAt(0);
439 let to_row = to.charCodeAt(1) - "a1".charCodeAt(1);
441 occupied_by_arrows[from_row][from_col] = true;
443 // Knight move: Just check that we haven't been at the destination before.
444 if ((Math.abs(to_col - from_col) == 2 && Math.abs(to_row - from_row) == 1) ||
445 (Math.abs(to_col - from_col) == 1 && Math.abs(to_row - from_row) == 2)) {
446 return occupied_by_arrows[to_row][to_col];
449 // Sliding piece: Check if anything except the from-square is seen before.
450 let dx = sign(to_col - from_col);
451 let dy = sign(to_row - from_row);
457 if (occupied_by_arrows[y][x]) {
460 occupied_by_arrows[y][x] = true;
461 } while (x != to_col || y != to_row);
466 /** Find a point along the coordinate system given by the given line,
467 * <t> units forward from the start of the line, <u> units to the right of it.
468 * @param {!number} x1
469 * @param {!number} x2
470 * @param {!number} y1
471 * @param {!number} y2
474 * @return {!string} The point in "x y" form, suitable for SVG paths.
476 function point_from_start(x1, y1, x2, y2, t, u) {
480 let norm = 1.0 / Math.sqrt(dx * dx + dy * dy);
484 let x = x1 + dx * t + dy * u;
485 let y = y1 + dy * t - dx * u;
489 /** Find a point along the coordinate system given by the given line,
490 * <t> units forward from the end of the line, <u> units to the right of it.
491 * @param {!number} x1
492 * @param {!number} x2
493 * @param {!number} y1
494 * @param {!number} y2
497 * @return {!string} The point in "x y" form, suitable for SVG paths.
499 function point_from_end(x1, y1, x2, y2, t, u) {
503 let norm = 1.0 / Math.sqrt(dx * dx + dy * dy);
507 let x = x2 + dx * t + dy * u;
508 let y = y2 + dy * t - dx * u;
512 function position_arrow(arrow) {
514 if (arrow.svg.parentElement) {
515 arrow.svg.parentElement.removeChild(arrow.svg);
519 if (current_display_line !== null && !current_display_line_is_history) {
523 // We always draw as if the board is 400x400, the viewBox will adjust that for us
524 let line_width = arrow.line_width;
525 let arrow_size = arrow.arrow_size;
527 let square_width = 400 / 8;
528 let from_y, to_y, from_x, to_x;
529 if (board.orientation() === 'black') {
530 from_y = (arrow.from_row + 0.5)*square_width;
531 to_y = (arrow.to_row + 0.5)*square_width;
532 from_x = (7 - arrow.from_col + 0.5)*square_width;
533 to_x = (7 - arrow.to_col + 0.5)*square_width;
535 from_y = (7 - arrow.from_row + 0.5)*square_width;
536 to_y = (7 - arrow.to_row + 0.5)*square_width;
537 from_x = (arrow.from_col + 0.5)*square_width;
538 to_x = (arrow.to_col + 0.5)*square_width;
541 let SVG_NS = "http://www.w3.org/2000/svg";
542 let XHTML_NS = "http://www.w3.org/1999/xhtml";
543 let svg = document.createElementNS(SVG_NS, "svg");
544 svg.setAttribute("width", "100%");
545 svg.setAttribute("height", "100%");
546 svg.setAttribute("viewBox", "0 0 400 400");
547 svg.setAttribute("version", "1.1");
548 svg.setAttribute("class", "c1");
549 svg.setAttribute("xmlns", XHTML_NS);
557 let outline = document.createElementNS(SVG_NS, "path");
558 outline.setAttribute("d", "M " + point_from_start(x1, y1, x2, y2, arrow_size / 2, 0) + " L " + point_from_end(x1, y1, x2, y2, -arrow_size / 2, 0));
559 outline.setAttribute("xmlns", XHTML_NS);
560 outline.setAttribute("stroke", "#666");
561 outline.setAttribute("stroke-width", line_width + 2);
562 outline.setAttribute("fill", "none");
563 svg.appendChild(outline);
565 let path = document.createElementNS(SVG_NS, "path");
566 path.setAttribute("d", "M " + point_from_start(x1, y1, x2, y2, arrow_size / 2, 0) + " L " + point_from_end(x1, y1, x2, y2, -arrow_size / 2, 0));
567 path.setAttribute("xmlns", XHTML_NS);
568 path.setAttribute("stroke", arrow.fg_color);
569 path.setAttribute("stroke-width", line_width);
570 path.setAttribute("fill", "none");
571 svg.appendChild(path);
573 // Then the arrow head.
574 let head = document.createElementNS(SVG_NS, "path");
575 head.setAttribute("d",
576 "M " + point_from_end(x1, y1, x2, y2, 0, 0) +
577 " L " + point_from_end(x1, y1, x2, y2, -arrow_size, -arrow_size / 2) +
578 " L " + point_from_end(x1, y1, x2, y2, -arrow_size * .623, 0.0) +
579 " L " + point_from_end(x1, y1, x2, y2, -arrow_size, arrow_size / 2) +
580 " L " + point_from_end(x1, y1, x2, y2, 0, 0));
581 head.setAttribute("xmlns", XHTML_NS);
582 head.setAttribute("stroke", "#000");
583 head.setAttribute("stroke-width", "1");
584 head.setAttribute("fill", arrow.fg_color);
585 svg.appendChild(head);
587 svg.style.position = 'absolute';
588 svg.style.top = '0px'; /* Border for .board-b72b1. */
589 svg.style.left = '0px';
590 svg.style.pointerEvents = 'none';
591 document.getElementById('board').appendChild(svg);
596 * @param {!string} from_square
597 * @param {!string} to_square
598 * @param {!string} fg_color
599 * @param {number} line_width
600 * @param {number} arrow_size
602 function create_arrow(from_square, to_square, fg_color, line_width, arrow_size) {
603 let from_col = from_square.charCodeAt(0) - "a1".charCodeAt(0);
604 let from_row = from_square.charCodeAt(1) - "a1".charCodeAt(1);
605 let to_col = to_square.charCodeAt(0) - "a1".charCodeAt(0);
606 let to_row = to_square.charCodeAt(1) - "a1".charCodeAt(1);
614 line_width: line_width,
615 arrow_size: arrow_size,
619 position_arrow(arrow);
623 function compare_by_score(refutation_lines, invert, a, b) {
624 let sa = compute_score_sort_key(refutation_lines[b]['score'], refutation_lines[b]['depth'], invert);
625 let sb = compute_score_sort_key(refutation_lines[a]['score'], refutation_lines[a]['depth'], invert);
630 * Fake multi-PV using the refutation lines. Find all “relevant” moves,
631 * sorted by quality, descending.
633 * @param {!Object} data
634 * @param {number} margin The maximum number of centipawns worse than the
635 * best move can be and still be included.
636 * @param {boolean} invert Whether black is to play.
637 * @return {Array.<string>} The FEN representation (e.g. Ne4) of all
638 * moves, in score order.
640 function find_nonstupid_moves(data, margin, invert) {
641 // First of all, if there are any moves that are more than 0.5 ahead of
642 // the primary move, the refutation lines are probably bunk, so just
644 let best_score = undefined;
645 let pv_score = undefined;
646 for (let move in data['refutation_lines']) {
647 let line = data['refutation_lines'][move];
648 let score = compute_score_sort_key(line['score'], line['depth'], invert);
649 if (move == data['pv'][0]) {
652 if (best_score === undefined || score > best_score) {
655 if (line['depth'] < 8) {
660 if (best_score - pv_score > 50) {
664 // Now find all moves that are within “margin” of the best score.
665 // The PV move will always be first.
667 for (let move in data['refutation_lines']) {
668 let line = data['refutation_lines'][move];
669 let score = compute_score_sort_key(line['score'], line['depth'], invert);
670 if (move != data['pv'][0] && best_score - score <= margin) {
674 let toplay = find_toplay(data['position']['fen']);
675 moves = moves.sort(function(a, b) { return compare_by_score(data['refutation_lines'], toplay, a, b) });
676 moves.unshift(data['pv'][0]);
685 function thousands(x) {
686 return String(x).split('').reverse().join('').replace(/(\d{3}\B)/g, '$1,').split('').reverse().join('');
690 * @param {!string} start_fen
691 * @param {Array.<string>} pv
692 * @param {Array<{ first_move: integer, score: Object }>} scores
693 * @param {number} start_display_move_num
694 * @param {number=} opt_limit
695 * @param {boolean=} opt_showlast
697 function add_pv(start_fen, pv, scores, start_display_move_num, opt_limit, opt_showlast) {
699 start_fen: start_fen,
701 move_num: find_move_num(start_fen),
702 toplay: find_toplay(start_fen),
704 start_display_move_num: start_display_move_num
706 let splicepos = null;
707 if (scores !== null && scores.length >= 1 &&
708 scores[scores.length - 1].score !== undefined &&
709 scores[scores.length - 1].score !== null &&
710 (scores[scores.length - 1].score[0] === 'T' ||
711 scores[scores.length - 1].score[0] === 't')) {
712 splicepos = scores[scores.length - 1].score[1];
714 return print_pv(display_lines.length - 1, splicepos, opt_limit, opt_showlast);
718 * @param {number} line_num
719 * @param {?number} splicepos If non-null, where the tablebase-spliced portion of the TB starts.
720 * @param {number=} opt_limit If set, show at most this number of moves.
721 * @param {boolean=} opt_showlast If limit is set, show the last moves instead of the first ones.
723 function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
724 let display_line = display_lines[line_num];
725 let pv = display_line.pv;
726 let move_num = display_line.move_num;
727 let toplay = display_line.toplay;
729 // Truncate PV at the start if needed.
730 let start_display_move_num = display_line.start_display_move_num;
731 if (start_display_move_num > 0) {
732 pv = pv.slice(start_display_move_num);
733 let to_add = start_display_move_num;
734 if (toplay === 'B') {
739 if (to_add % 2 == 1) {
743 move_num += to_add / 2;
744 if (splicepos !== null && splicepos > 0) {
749 let ret = document.createDocumentFragment();
752 if (opt_limit && opt_showlast && pv.length > opt_limit) {
753 // Truncate the PV at the beginning (instead of at the end).
754 // We assume here that toplay is 'W'. We also assume that if
755 // opt_showlast is set, then it is the history, and thus,
756 // the UI should be to expand the history.
757 ret.appendChild(document.createTextNode('('));
758 let link = document.createElement('a');
759 link.className = 'move';
760 link.href = 'javascript:collapse_history(false)';
761 link.textContent = '…';
762 ret.appendChild(link);
763 ret.appendChild(document.createTextNode(') '));
764 i = pv.length - opt_limit;
769 } else if (toplay == 'B' && pv.length > 0) {
770 ret.appendChild(document.createTextNode(move_num + '. … '));
772 for (; i < pv.length; ++i) {
774 if (splicepos === i) {
779 if (toplay == 'B' && i == 0) {
782 } else if (toplay == 'W') {
783 if (i > opt_limit && !opt_showlast) {
787 ret.appendChild(document.createTextNode(prefix + ' (…)'));
790 prefix += ' ' + move_num + '. ';
797 ret.appendChild(document.createTextNode(prefix));
799 let link = document.createElement('a');
800 link.className = 'move';
801 link.setAttribute('id', 'automove' + line_num + '-' + i);
802 link.textContent = pv[i];
803 link.href = 'javascript:show_line(' + line_num + ', ' + i + ');';
804 ret.appendChild(link);
807 ret.appendChild(document.createTextNode(')'));
812 /** Update the highlighted to/from squares on the board.
813 * Based on the global "highlight_from" and "highlight_to" letiables.
815 function update_board_highlight() {
816 document.getElementById("board").querySelectorAll('.square-55d63').forEach((square) => square.classList.remove('nonuglyhighlight'));
817 if ((current_display_line === null || current_display_line_is_history) &&
818 highlight_from !== undefined && highlight_to !== undefined) {
819 document.getElementById("board").querySelector('.square-' + highlight_from).classList.add('nonuglyhighlight');
820 document.getElementById("board").querySelector('.square-' + highlight_to).classList.add('nonuglyhighlight');
824 function update_history() {
825 let history = document.getElementById('history');
826 if (display_lines[0] === null || display_lines[0].pv.length == 0) {
827 history.textContent = 'No history';
828 } else if (truncate_display_history) {
829 history.replaceChildren(print_pv(0, null, 8, true));
831 history.textContent = '(';
832 let link = document.createElement('a');
833 link.className = 'move';
834 link.href = 'javascript:collapse_history(true)';
835 link.textContent = 'collapse';
836 history.appendChild(link);
837 history.appendChild(document.createTextNode(') '));
838 history.append(print_pv(0, null));
843 * @param {!boolean} truncate_history
845 function collapse_history(truncate_history) {
846 truncate_display_history = truncate_history;
849 window['collapse_history'] = collapse_history;
851 /** Update the HTML display of multi-PV from the global "refutation_lines".
853 * Also recreates the global "display_lines".
855 function update_refutation_lines() {
856 if (base_fen === null) {
859 if (display_lines.length > 2) {
860 // Truncate so that only the history and PV is left.
861 display_lines = [ display_lines[0], display_lines[1] ];
863 let tbl = document.getElementById("refutationlines");
864 tbl.replaceChildren();
866 if (display_lines.length < 2) {
867 // Update the move highlight, as we've rewritten all the HTML.
868 update_move_highlight();
872 // Find out where the lines start from.
874 let base_scores = display_lines[1].scores;
875 let start_display_move_num = 0;
876 if (hash_refutation_lines) {
877 base_line = current_display_line.pv.slice(0, current_display_move + 1);
878 base_scores = current_display_line.scores;
879 start_display_move_num = base_line.length;
883 for (let move in refutation_lines) {
887 let invert = (find_toplay(base_fen) === 'B');
888 if (current_display_line && current_display_move % 2 == 0 && !current_display_line_is_history) {
891 moves = moves.sort(function(a, b) { return compare_by_score(refutation_lines, invert, a, b) });
892 for (let i = 0; i < moves.length; ++i) {
893 let line = refutation_lines[moves[i]];
895 let tr = document.createElement("tr");
897 let move_td = document.createElement("td");
898 tr.appendChild(move_td);
899 move_td.classList.add("move");
901 let scores = base_scores.concat([{ first_move: start_display_move_num, score: line['score'] }]);
903 if (line['pv'].length == 0) {
904 // Not found, so just make a one-move PV.
905 let link = document.createElement('a');
906 link.className = 'move';
907 link.href = 'javascript:show_line(' + display_lines.length + ', ' + 0 + ')';
908 link.textContent = line['move'];
909 move_td.replaceChildren(link);
911 let score_td = document.createElement("td");
912 score_td.classList.add("score");
913 score_td.textContent = "—";
914 tr.appendChild(score_td);
916 let depth_td = document.createElement("td");
917 tr.appendChild(depth_td);
918 depth_td.classList.add("depth");
919 depth_td.textContent = "—";
921 let pv_td = document.createElement("td");
922 tr.appendChild(pv_td);
923 pv_td.classList.add("pv");
924 pv_td.append(add_pv(base_fen, base_line.concat([ line['move'] ]), scores, start_display_move_num));
930 let move_link = document.createElement("a");
931 move_link.classList.add("move");
932 move_link.setAttribute("href", "javascript:show_line(" + display_lines.length + ", 0)");
933 move_link.textContent = line['move'];
934 move_td.appendChild(move_link);
936 let score_td = document.createElement("td");
937 tr.appendChild(score_td);
938 score_td.classList.add("score");
939 score_td.textContent = format_short_score(line['score']);
941 let depth_td = document.createElement("td");
942 tr.appendChild(depth_td);
943 depth_td.classList.add("depth");
944 if (line['depth'] && line['depth'] >= 0) {
945 depth_td.textContent = "d" + line['depth'];
947 depth_td.textContent = "—";
950 let pv_td = document.createElement("td");
951 tr.appendChild(pv_td);
952 pv_td.classList.add("pv");
953 pv_td.append(add_pv(base_fen, base_line.concat(line['pv']), scores, start_display_move_num, 10));
958 // Update the move highlight, as we've rewritten all the HTML.
959 update_move_highlight();
963 * Create a Chess.js board object, containing the given position plus the given moves,
964 * up to the given limit.
966 * @param {?string} fen
967 * @param {Array.<string>} moves
968 * @param {number} last_move
970 function chess_from(fen, moves, last_move) {
971 let hiddenboard = new Chess();
972 if (fen !== null && fen !== undefined) {
973 hiddenboard.load(fen);
975 for (let i = 0; i <= last_move; ++i) {
976 if (moves[i] === '0-0') {
977 hiddenboard.move('O-O');
978 } else if (moves[i] === '0-0-0') {
979 hiddenboard.move('O-O-O');
981 hiddenboard.move(moves[i]);
987 function update_game_list(games) {
988 document.getElementById("games").textContent = "";
989 if (games === null) {
993 let games_div = document.getElementById('games');
994 for (let game_num = 0; game_num < games.length; ++game_num) {
995 let game = games[game_num];
996 let game_span = document.createElement("span");
997 game_span.setAttribute("class", "game");
999 let game_name = document.createTextNode(game['name']);
1000 if (game['url'] === backend_url) {
1002 game_span.appendChild(game_name);
1004 if (current_analysis_data && current_analysis_data['position']) {
1006 if (current_analysis_data['position']['result']) {
1007 score = " (" + current_analysis_data['position']['result'] + ")";
1009 score = " (" + format_short_score(current_analysis_data['score']) + ")";
1011 game_span.appendChild(document.createTextNode(score));
1015 let game_a = document.createElement("a");
1016 game_a.setAttribute("href", "#" + game['id']);
1017 game_a.appendChild(game_name);
1018 game_span.appendChild(game_a);
1021 if (game['result']) {
1022 score = " (" + game['result'] + ")";
1024 score = " (" + format_short_score(game['score']) + ")";
1026 game_span.appendChild(document.createTextNode(score));
1029 games_div.appendChild(game_span);
1034 * Try to find a running game that matches with the current hash,
1035 * and switch to it if we're not already displaying it.
1037 function possibly_switch_game_from_hash() {
1038 let history_match = window.location.hash.match(/^#history=([a-zA-Z0-9_-]+)/);
1039 if (history_match !== null) {
1040 let game_id = history_match[1];
1042 url: '/history/' + game_id + '.json',
1044 id: 'history=' + game_id
1046 switch_backend(fake_game);
1050 if (current_games === null) {
1054 let hash = window.location.hash.replace(/^#/,'');
1055 for (let i = 0; i < current_games.length; ++i) {
1056 if (current_games[i]['id'] === hash) {
1057 if (backend_url !== current_games[i]['url']) {
1058 switch_backend(current_games[i]);
1066 * If this is a Chess960 castling which doesn't move the king,
1067 * move the rook instead.
1069 function patch_move(move) {
1070 if (move === null) return null;
1071 if (move.from !== move.to) return move;
1073 let f = move.rook_sq & 15;
1074 let r = move.rook_sq >> 4;
1075 let from = ('abcdefgh'.substring(f,f+1) + '87654321'.substring(r,r+1));
1078 if (move.to === 'g1') {
1080 } else if (move.to === 'g8') {
1082 } else if (move.to === 'b1') {
1084 } else if (move.to === 'b8') {
1088 return { from: from, to: to };
1091 /** Update all the HTML on the page, based on current global state.
1093 function update_board() {
1094 document.body.style.opacity = null;
1096 let data = displayed_analysis_data || current_analysis_data;
1097 let current_data = current_analysis_data; // Convenience alias.
1101 // Print the history. This is pretty much the only thing that's
1102 // unconditionally taken from current_data (we're not interested in
1103 // historic history).
1104 if (current_data['position']['history']) {
1105 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';
1106 add_pv(start, current_data['position']['history'], null, 0, 8, true);
1108 display_lines.push(null);
1112 // Games currently in progress, if any.
1113 if (current_data['games']) {
1114 current_games = current_data['games'];
1115 possibly_switch_game_from_hash();
1117 current_games = null;
1119 update_game_list(current_games);
1121 // The headline. Names are always fetched from current_data;
1122 // the rest can depend a bit.
1125 current_data['position']['player_w'] && current_data['position']['player_b']) {
1126 headline = current_data['position']['player_w'] + '–' +
1127 current_data['position']['player_b'] + ', analysis';
1129 headline = 'Analysis';
1132 // Credits: The engine name/version.
1133 if (current_data['engine'] && current_data['engine']['name'] !== null) {
1134 document.getElementById("engineid").textContent = current_data['engine']['name'];
1137 // Credits: The engine URL.
1138 if (current_data['engine'] && current_data['engine']['url']) {
1139 document.getElementById("engineid").setAttribute("href", current_data['engine']['url']);
1141 document.getElementById("engineid").removeAttribute("href");
1144 // Credits: Engine details.
1145 if (current_data['engine'] && current_data['engine']['details']) {
1146 document.getElementById("enginedetails").textContent = " (" + current_data['engine']['details'] + ")";
1148 document.getElementById("enginedetails").textContent = "";
1151 // Credits: Move source, possibly with URL.
1152 if (current_data['move_source'] && current_data['move_source_url']) {
1153 document.getElementById("movesource").textContent = "Moves provided by ";
1154 let movesource_a = document.createElement("a");
1155 movesource_a.setAttribute("href", current_data['move_source_url']);
1156 let movesource_text = document.createTextNode(current_data['move_source']);
1157 movesource_a.appendChild(movesource_text);
1158 let movesource_period = document.createTextNode(".");
1159 document.getElementById("movesource").appendChild(movesource_a);
1160 document.getElementById("movesource").appendChild(movesource_period);
1161 } else if (current_data['move_source']) {
1162 document.getElementById("movesource").textContent = "Moves provided by " + current_data['move_source'] + ".";
1164 document.getElementById("movesource").textContent = "";
1168 if (displayed_analysis_data) {
1169 // Displaying some non-current position, pick out the last move
1170 // from the history. This will work even if the fetch failed.
1171 if (current_display_move !== -1) {
1172 last_move = format_halfmove_with_number(
1173 current_display_line.pv[current_display_move],
1174 current_display_move);
1175 headline += ' after ' + last_move;
1177 } else if (data['position']['last_move'] !== 'none') {
1178 // Find the previous move.
1179 let previous_move_num, previous_toplay;
1180 let fen = data['position']['fen'];
1181 if (find_toplay(fen) === 'B') {
1182 previous_move_num = find_move_num(fen);
1183 previous_toplay = 'W';
1185 previous_move_num = find_move_num(fen) - 1;
1186 previous_toplay = 'B';
1189 last_move = format_move_with_number(
1190 data['position']['last_move'],
1192 previous_toplay == 'W');
1193 headline += ' after ' + last_move;
1197 document.getElementById("headline").textContent = headline;
1199 // The <title> contains a very brief headline.
1200 let title_elems = [];
1201 if (data['position'] && data['position']['result']) {
1202 title_elems.push(data['position']['result']);
1203 } else if (data['score']) {
1204 title_elems.push(format_short_score(data['score']));
1206 if (last_move !== null) {
1207 title_elems.push(last_move);
1210 if (title_elems.length != 0) {
1211 document.title = '(' + title_elems.join(', ') + ') analysis.sesse.net';
1213 document.title = 'analysis.sesse.net';
1216 // The last move (shown by highlighting the from and to squares).
1217 if (data['position'] && data['position']['last_move_uci']) {
1218 highlight_from = data['position']['last_move_uci'].substr(0, 2);
1219 highlight_to = data['position']['last_move_uci'].substr(2, 2);
1220 } else if (current_display_line_is_history && current_display_line && current_display_move >= 0) {
1221 // We don't have historic analysis for this position, but we
1222 // can reconstruct what the last move was by just replaying
1224 let position = (data['position'] && data['position']['start_fen']) ? data['position']['start_fen'] : null;
1225 let hiddenboard = chess_from(position, current_display_line.pv, current_display_move);
1226 let moves = hiddenboard.history({ verbose: true });
1227 last_move = moves.pop();
1228 highlight_from = last_move.from;
1229 highlight_to = last_move.to;
1231 highlight_from = highlight_to = undefined;
1233 update_board_highlight();
1235 if (data['failed']) {
1236 document.getElementById("score").textContent = "No analysis for this move";
1237 document.getElementById("pvtitle").textContent = "PV:";
1238 document.getElementById("pv").replaceChildren();
1239 document.getElementById("searchstats").innerHTML = " ";
1240 document.getElementById("refutationlines").replaceChildren();
1241 document.getElementById("whiteclock").replaceChildren();
1242 document.getElementById("blackclock").replaceChildren();
1243 refutation_lines = [];
1244 update_refutation_lines();
1246 update_displayed_line();
1247 update_move_highlight();
1251 if (clock_timer !== null) {
1252 clearTimeout(clock_timer);
1257 if (current_display_line && !current_display_line_is_history) {
1259 if (current_display_line.scores && current_display_line.scores.length > 0) {
1260 for (let i = 0; i < current_display_line.scores.length; ++i) {
1261 if (current_display_move < current_display_line.scores[i].first_move) {
1264 score = current_display_line.scores[i].score;
1268 document.getElementById("score").textContent = format_long_score(score);
1270 document.getElementById("score").textContent = "No score for this line";
1272 } else if (data['score']) {
1273 document.getElementById("score").textContent = format_long_score(data['score']);
1276 // The search stats.
1277 if (data['searchstats']) {
1278 document.getElementById("searchstats").textContent = data['searchstats'];
1279 } else if (data['nodes'] && data['nps'] && data['depth']) {
1280 let stats = thousands(data['nodes']) + ' nodes, ' + thousands(data['nps']) + ' nodes/sec, depth ' + data['depth'] + ' ply';
1281 if (data['seldepth']) {
1282 stats += ' (' + data['seldepth'] + ' selective)';
1284 if (data['tbhits'] && data['tbhits'] > 0) {
1285 if (data['tbhits'] == 1) {
1286 stats += ', one Syzygy hit';
1288 stats += ', ' + thousands(data['tbhits']) + ' Syzygy hits';
1292 document.getElementById("searchstats").textContent = stats;
1294 document.getElementById("searchstats").textContent = "";
1296 if (admin_password !== null) {
1297 document.getElementById("searchstats").innerHTML += " | <span style=\"color: red;\">ADMIN MODE (if password is right)</span>";
1300 // Update the board itself.
1301 base_fen = data['position']['fen'];
1302 update_displayed_line();
1305 document.getElementById("pvtitle").textContent = "PV:";
1307 let scores = [{ first_move: -1, score: data['score'] }];
1308 document.getElementById("pv").replaceChildren(add_pv(data['position']['fen'], data['pv'], scores, 0));
1310 // Update the PV arrow.
1312 let toplay = find_toplay(data['position']['fen']);
1313 if (data['pv'].length >= 1) {
1314 let hiddenboard = new Chess(base_fen);
1316 // draw a continuation arrow as long as it's the same piece
1318 for (let i = 0; i < data['pv'].length; i += 2) {
1319 let move = patch_move(hiddenboard.move(data['pv'][i]));
1321 if ((i >= 2 && move.from != last_to) ||
1322 interfering_arrow(move.from, move.to)) {
1325 create_arrow(move.from, move.to, '#f66', 6, 20);
1327 hiddenboard.move(data['pv'][i + 1]); // To keep continuity.
1330 let alt_moves = find_nonstupid_moves(data, 30, toplay === 'B');
1331 for (let i = 1; i < alt_moves.length && i < 3; ++i) {
1332 hiddenboard = new Chess(base_fen);
1333 let move = patch_move(hiddenboard.move(alt_moves[i]));
1334 if (move !== null) {
1335 create_arrow(move.from, move.to, '#f66', 1, 10);
1340 // See if all semi-reasonable moves have only one possible response.
1341 if (data['pv'].length >= 2) {
1342 let nonstupid_moves = find_nonstupid_moves(data, 300, toplay === 'B');
1343 let hiddenboard = new Chess(base_fen);
1344 hiddenboard.move(data['pv'][0]);
1345 let response = hiddenboard.move(data['pv'][1]);
1346 for (let i = 0; i < nonstupid_moves.length; ++i) {
1347 if (nonstupid_moves[i] == data['pv'][0]) {
1348 // ignore the PV move for refutation lines.
1351 if (!data['refutation_lines'] ||
1352 !data['refutation_lines'][nonstupid_moves[i]] ||
1353 !data['refutation_lines'][nonstupid_moves[i]]['pv'] ||
1354 data['refutation_lines'][nonstupid_moves[i]]['pv'].length < 2) {
1355 // Incomplete PV, abort.
1356 response = undefined;
1359 let line = data['refutation_lines'][nonstupid_moves[i]];
1360 hiddenboard = new Chess(base_fen);
1361 hiddenboard.move(line['pv'][0]);
1362 let this_response = hiddenboard.move(line['pv'][1]);
1363 if (this_response === null) {
1364 console.log("BUG: ", i);
1366 console.log(line['pv']);
1368 if (response.from !== this_response.from || response.to !== this_response.to) {
1369 // Different response depending on lines, abort.
1370 response = undefined;
1375 if (nonstupid_moves.length > 0 && response !== undefined) {
1376 create_arrow(response.from, response.to, '#66f', 6, 20);
1380 // Update the refutation lines.
1381 base_fen = data['position']['fen'];
1382 refutation_lines = hash_refutation_lines || data['refutation_lines'];
1383 update_refutation_lines();
1385 // Update the sparkline last, since its size depends on how everything else reflowed.
1386 update_sparkline(data);
1389 function update_sparkline(data) {
1390 let scorespark = document.getElementById('scoresparkcontainer');
1391 scorespark.textContent = '';
1392 if (data && data['score_history']) {
1393 let first_move_num = undefined;
1394 for (let halfmove_num in data['score_history']) {
1395 halfmove_num = parseInt(halfmove_num);
1396 if (first_move_num === undefined || halfmove_num < first_move_num) {
1397 first_move_num = halfmove_num;
1400 if (first_move_num !== undefined) {
1401 let fen = data['position']['fen'];
1402 let last_move_num = find_move_num(fen) * 2 - 3;
1403 if (find_toplay(fen) === 'B') {
1407 // Possibly truncate some moves if we don't have enough width.
1408 let max_moves = Math.floor(scorespark.getBoundingClientRect().width / 5) - 3;
1409 if (last_move_num - first_move_num > max_moves) {
1410 first_move_num = last_move_num - max_moves;
1415 let last_score = null;
1417 for (let halfmove_num = first_move_num; halfmove_num <= last_move_num; ++halfmove_num) {
1418 if (data['score_history'][halfmove_num]) {
1419 let score = compute_plot_score(data['score_history'][halfmove_num]);
1421 if (score < min_score) min_score = score;
1422 if (score > max_score) max_score = score;
1424 scores.push(last_score);
1426 if (data['score']) {
1427 let score = compute_plot_score(data['score']);
1429 if (score < min_score) min_score = score;
1430 if (score > max_score) max_score = score;
1432 if (max_score - min_score < 100) {
1433 if (Math.abs(max_score) >= Math.abs(min_score)) {
1434 max_score = min_score + 100;
1436 min_score = max_score - 100;
1440 const h = scorespark.getBoundingClientRect().height;
1442 let base_y = h - h * min_score / (min_score - max_score);
1443 for (let i = 0; i < scores.length; ++i) {
1444 let rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
1445 //rect.setAttributeNS(null, 'stroke', "#000000");
1446 rect.setAttributeNS(null, 'x', i * 5)
1447 rect.setAttributeNS(null, 'width', 4);
1448 let extent = scores[i] * h / (max_score - min_score);
1449 if (extent > 0 && extent < 1) {
1451 } else if (extent < 0 && extent > -1) {
1456 if (scores[i] === 0) {
1457 color = [0.5, 0.5, 0.5];
1458 rect.setAttributeNS(null, 'y', base_y);
1459 rect.setAttributeNS(null, 'height', 1);
1460 } else if (scores[i] > 0) {
1461 color = [0.2, 0.4, 0.8];
1462 rect.setAttributeNS(null, 'y', base_y - extent);
1463 rect.setAttributeNS(null, 'height', extent + 1);
1465 color = [1.0, 0.267, 0.267];
1466 rect.setAttributeNS(null, 'y', base_y);
1467 rect.setAttributeNS(null, 'height', -extent + 1);
1469 let hlcolor = [color[0], color[1], color[2]];
1470 if (scores[i] !== 0) {
1471 hlcolor[0] = Math.min(hlcolor[0] * 1.4, 1.0);
1472 hlcolor[1] = Math.min(hlcolor[1] * 1.4, 1.0);
1473 hlcolor[2] = Math.min(hlcolor[2] * 1.4, 1.0);
1475 rect.style.fill = 'rgb(' + color[0]*100.0 + '%, ' + color[1]*100.0 + '%, ' + color[2]*100.0 + '%)';
1477 // score_history contains the Nth _position_, but format_tooltip
1478 // wants to format the Nth _move_; thus the -1.
1479 const tooltip = format_tooltip(data, i + first_move_num - 1);
1480 rect.addEventListener('mouseenter', (e) => draw_hover(e, hlcolor, tooltip));
1481 rect.addEventListener('mousemove', (e) => draw_hover(e, hlcolor, tooltip));
1482 rect.addEventListener('mouseleave', (e) => hide_hover(e, color));
1483 rect.addEventListener('click', (e) => { hide_hover(e, color); show_line(0, i + first_move_num - 1) });
1484 scorespark.appendChild(rect);
1490 function draw_hover(e, color, tooltip) {
1491 e.target.style.fill = 'rgb(' + color[0]*100.0 + '%, ' + color[1]*100.0 + '%, ' + color[2]*100.0 + '%)';
1493 let hover = document.getElementById('sparklinehover');
1494 hover.textContent = tooltip;
1495 hover.style.display = 'initial';
1497 let left = Math.max(e.pageX + 10, window.pageXOffset);
1498 let top = Math.max(e.pageY - hover.getBoundingClientRect().height, window.pageYOffset);
1499 left = Math.min(left, window.pageXOffset + document.documentElement.clientWidth - hover.getBoundingClientRect().width);
1501 hover.style.left = left + 'px';
1502 hover.style.top = top + 'px';
1505 function hide_hover(e, color) {
1506 e.target.style.fill = 'rgb(' + color[0]*100.0 + '%, ' + color[1]*100.0 + '%, ' + color[2]*100.0 + '%)';
1507 document.getElementById('sparklinehover').style.display = 'none';
1511 * @param {number} num_viewers
1513 function update_num_viewers(num_viewers) {
1515 if (num_viewers === null) {
1517 } else if (num_viewers == 1) {
1518 text = "You are the only current viewer";
1520 text = num_viewers + " current viewers";
1522 if (display_fen !== null) {
1523 let counter = Math.floor(display_fen.split(" ")[4] / 2);
1524 if (counter >= 20) {
1525 text = text.replace("current ", "");
1526 text += " | 50-move rule: " + counter;
1529 document.getElementById("numviewers").textContent = text;
1532 function update_clock() {
1535 let data = displayed_analysis_data || current_analysis_data;
1538 if (data['position']) {
1539 let result = data['position']['result'];
1540 if (result === '1-0') {
1541 document.getElementById("whiteclock").textContent = "1";
1542 document.getElementById("blackclock").textContent = "0";
1543 document.getElementById("whiteclock").classList.remove("running-clock");
1544 document.getElementById("blackclock").classList.remove("running-clock");
1547 if (result === '1/2-1/2') {
1548 document.getElementById("whiteclock").textContent = "1/2";
1549 document.getElementById("blackclock").textContent = "1/2";
1550 document.getElementById("whiteclock").classList.remove("running-clock");
1551 document.getElementById("blackclock").classList.remove("running-clock");
1554 if (result === '0-1') {
1555 document.getElementById("whiteclock").textContent = "0";
1556 document.getElementById("blackclock").textContent = "1";
1557 document.getElementById("whiteclock").classList.remove("running-clock");
1558 document.getElementById("blackclock").classList.remove("running-clock");
1563 let white_clock_ms = null;
1564 let black_clock_ms = null;
1567 if (data['position'] &&
1568 data['position']['white_clock'] &&
1569 data['position']['black_clock']) {
1570 white_clock_ms = data['position']['white_clock'] * 1000;
1571 black_clock_ms = data['position']['black_clock'] * 1000;
1574 // Dynamic clock (only one, obviously).
1576 if (data['position']['white_clock_target']) {
1578 document.getElementById("whiteclock").classList.add("running-clock");
1579 document.getElementById("blackclock").classList.remove("running-clock");
1580 } else if (data['position']['black_clock_target']) {
1582 document.getElementById("whiteclock").classList.remove("running-clock");
1583 document.getElementById("blackclock").classList.add("running-clock");
1585 document.getElementById("whiteclock").classList.remove("running-clock");
1586 document.getElementById("blackclock").classList.remove("running-clock");
1590 let now = new Date().getTime() + client_clock_offset_ms;
1591 remaining_ms = data['position'][color + '_clock_target'] * 1000 - now;
1592 if (color === "white") {
1593 white_clock_ms = remaining_ms;
1595 black_clock_ms = remaining_ms;
1599 if (white_clock_ms === null || black_clock_ms === null) {
1600 document.getElementById("whiteclock").replaceChildren();
1601 document.getElementById("blackclock").replaceChildren();
1605 // If either player has twenty minutes or less left, add the second counters.
1606 // This matches what DGT clocks do.
1607 let show_seconds = (white_clock_ms < 60 * 20 * 1000 || black_clock_ms < 60 * 20 * 1000);
1609 if (color && remaining_ms > 0) {
1610 // See when the clock will change next, and update right after that.
1613 next_update_ms = remaining_ms % 1000 + 100;
1615 next_update_ms = remaining_ms % 60000 + 100;
1617 clock_timer = setTimeout(update_clock, next_update_ms);
1620 document.getElementById("whiteclock").textContent = format_clock(white_clock_ms, show_seconds);
1621 document.getElementById("blackclock").textContent = format_clock(black_clock_ms, show_seconds);
1625 * @param {Number} remaining_ms
1626 * @param {boolean} show_seconds
1628 function format_clock(remaining_ms, show_seconds) {
1629 if (remaining_ms <= 0) {
1637 let remaining = Math.floor(remaining_ms / 1000);
1638 let seconds = remaining % 60;
1639 remaining = (remaining - seconds) / 60;
1640 let minutes = remaining % 60;
1641 remaining = (remaining - minutes) / 60;
1642 let hours = remaining;
1644 return format_2d(hours) + ":" + format_2d(minutes) + ":" + format_2d(seconds);
1646 return format_2d(hours) + ":" + format_2d(minutes);
1653 function format_2d(x) {
1662 * @param {string} move
1663 * @param {Number} move_num Move number of this move.
1664 * @param {boolean} white_to_play Whether white is to play this move.
1666 function format_move_with_number(move, move_num, white_to_play) {
1668 if (white_to_play) {
1669 ret = move_num + '. ';
1671 ret = move_num + '… ';
1678 * @param {string} move
1679 * @param {Number} halfmove_num Half-move number that is to be played,
1682 function format_halfmove_with_number(move, halfmove_num) {
1683 return format_move_with_number(
1685 Math.floor(halfmove_num / 2) + 1,
1686 halfmove_num % 2 == 0);
1690 * @param {Object} data
1691 * @param {Number} halfmove_num
1693 function format_tooltip(data, halfmove_num) {
1694 if (data['score_history'][halfmove_num + 1] ||
1695 (halfmove_num + 1) === data['position']['history'].length) {
1696 // Position is in the history, or it is the current position
1697 // (which is implicitly tacked onto the history).
1700 if ((halfmove_num + 1) === data['position']['history'].length) {
1701 move = data['position']['last_move'];
1702 short_score = format_short_score(data['score']);
1704 move = data['position']['history'][halfmove_num];
1705 short_score = format_short_score(data['score_history'][halfmove_num + 1]);
1707 if (halfmove_num === -1) {
1708 return "Start position: " + short_score;
1710 let move_with_number = format_halfmove_with_number(move, halfmove_num);
1711 return "After " + move_with_number + ": " + short_score;
1714 for (let i = halfmove_num; i --> -1; ) {
1715 if (data['score_history'][i]) {
1716 let move = data['position']['history'][i];
1718 return "[Analysis kept from start position]";
1720 return "[Analysis kept from " + format_halfmove_with_number(move, i) + "]";
1728 * @param {boolean} truncate_history
1730 function set_truncate_history(truncate_history) {
1731 truncate_display_history = truncate_history;
1732 update_refutation_lines();
1734 window['set_truncate_history'] = set_truncate_history;
1737 * @param {number} line_num
1738 * @param {number} move_num
1740 function show_line(line_num, move_num) {
1741 if (line_num == -1) {
1742 current_display_line = null;
1743 current_display_move = null;
1744 hash_refutation_lines = null;
1745 if (displayed_analysis_data) {
1746 // TODO: Support exiting to history position if we are in an
1747 // analysis line of a history position.
1748 displayed_analysis_data = null;
1753 current_display_line = {...display_lines[line_num]}; // Shallow clone.
1754 current_display_move = move_num + current_display_line.start_display_move_num;
1756 current_display_line_is_history = (line_num == 0);
1758 update_historic_analysis();
1759 update_displayed_line();
1760 update_board_highlight();
1761 update_move_highlight();
1764 window['show_line'] = show_line;
1766 function prev_move() {
1767 if (current_display_line &&
1768 current_display_move >= current_display_line.start_display_move_num) {
1769 --current_display_move;
1771 update_historic_analysis();
1772 update_displayed_line();
1773 update_move_highlight();
1775 window['prev_move'] = prev_move;
1777 function next_move() {
1778 if (current_display_line &&
1779 current_display_move < current_display_line.pv.length - 1) {
1780 ++current_display_move;
1782 update_historic_analysis();
1783 update_displayed_line();
1784 update_move_highlight();
1786 window['next_move'] = next_move;
1788 function next_game() {
1789 if (current_games === null) {
1793 // Try to find the game we are currently looking at.
1794 for (let game_num = 0; game_num < current_games.length; ++game_num) {
1795 let game = current_games[game_num];
1796 if (game['url'] === backend_url) {
1797 let next_game_num = (game_num + 1) % current_games.length;
1798 switch_backend(current_games[next_game_num]);
1803 // Couldn't find it; give up.
1806 function update_historic_analysis() {
1807 if (!current_display_line_is_history) {
1810 if (current_display_move == current_display_line.pv.length - 1) {
1811 displayed_analysis_data = null;
1815 // Fetch old analysis for this line if it exists.
1816 let hiddenboard = chess_from(current_display_line.start_fen, current_display_line.pv, current_display_move);
1817 let filename = "/history/move" + (current_display_move + 1) + "-" +
1818 hiddenboard.fen().replace(/ /g, '_').replace(/\//g, '-') + ".json";
1820 let handle_err = () => {
1821 displayed_analysis_data = {'failed': true};
1825 current_historic_xhr = new AbortController();
1826 const signal = current_analysis_xhr.signal;
1827 fetch(filename, { signal })
1828 .then((response) => response.json().then(data => ({ok: response.ok, json: data}))) // ick
1834 displayed_analysis_data = obj.json;
1838 if (err.name === 'AbortError') {
1839 // Aborted because we are switching backends. Abandon and don't retry,
1840 // because another one is already started for us.
1849 * @param {string} fen
1851 function update_imbalance(fen) {
1852 let imbalance = {'k': 0, 'q': 0, 'r': 0, 'b': 0, 'n': 0, 'p': 0};
1853 for (const c of fen) {
1858 if (c != c.toUpperCase()) {
1860 } else if (c != c.toLowerCase()) {
1861 ++imbalance[c.toLowerCase()];
1865 let white_imbalance = document.getElementById('whiteimbalance');
1866 let black_imbalance = document.getElementById('blackimbalance');
1867 white_imbalance.textContent = '';
1868 black_imbalance.textContent = '';
1869 for (let piece in imbalance) {
1870 for (let i = 0; i < imbalance[piece]; ++i) {
1871 let i1 = document.createElement('img');
1872 i1.src = svg_pieces['w' + piece.toUpperCase()];
1873 i1.setAttribute('alt', piece.toUpperCase());
1874 i1.classList.add('imbalance-piece');
1875 white_imbalance.appendChild(i1);
1877 let i2 = document.createElement('img');
1878 i2.src = svg_pieces['b' + piece.toUpperCase()];
1879 i2.setAttribute('alt', piece.toUpperCase());
1880 i2.classList.add('imbalance-inverted-piece');
1881 white_imbalance.appendChild(i2);
1883 for (let i = 0; i < -imbalance[piece]; ++i) {
1884 let i1 = document.createElement('img');
1885 i1.src = svg_pieces['b' + piece.toUpperCase()];
1886 i1.setAttribute('alt', piece.toUpperCase());
1887 i1.classList.add('imbalance-piece');
1888 black_imbalance.appendChild(i1);
1890 let i2 = document.createElement('img');
1891 i2.src = svg_pieces['w' + piece.toUpperCase()];
1892 i2.setAttribute('alt', piece.toUpperCase());
1893 i2.classList.add('imbalance-inverted-piece');
1894 black_imbalance.appendChild(i2);
1899 /** Mark the currently selected move in red.
1900 * Also replaces the PV with the current displayed line if it's not shown
1901 * anywhere else on the screen.
1903 function update_move_highlight() {
1904 if (highlighted_move !== null) {
1905 highlighted_move.classList.remove('highlight');
1907 if (current_display_line) {
1908 let display_line_num = find_display_line_matching_num();
1909 if (display_line_num === null) {
1910 // Replace the PV with the (complete) line.
1911 document.getElementById("pvtitle").textContent = "Exploring:";
1912 current_display_line.start_display_move_num = 0;
1913 display_lines.push(current_display_line);
1914 document.getElementById("pv").replaceChildren(print_pv(display_lines.length - 1, null)); // FIXME
1915 display_line_num = display_lines.length - 1;
1917 // Clear out the PV, so it's not selected by anything later.
1918 display_lines[1].pv = [];
1921 highlighted_move = document.getElementById("automove" + display_line_num + "-" + (current_display_move - current_display_line.start_display_move_num));
1922 if (highlighted_move !== null) {
1923 highlighted_move.classList.add('highlight');
1929 * See if the current displayed line is identical to any of the ones
1930 * we have on screen. (It might not be if e.g. the analysis reloaded
1931 * since we started looking.)
1935 function find_display_line_matching_num() {
1936 for (let i = 0; i < display_lines.length; ++i) {
1937 let line = display_lines[i];
1938 if (line.start_display_move_num > 0) continue;
1939 if (current_display_line.start_fen !== line.start_fen) continue;
1940 if (current_display_line.pv.length !== line.pv.length) continue;
1942 for (let j = 0; j < line.pv.length; ++j) {
1943 if (current_display_line.pv[j] !== line.pv[j]) {
1955 /** Update the board based on the currently displayed line.
1957 * TODO: This should really be called only whenever something changes,
1958 * instead of all the time.
1960 function update_displayed_line() {
1961 if (current_display_line === null) {
1962 document.getElementById("linenav").style.display = 'none';
1963 document.getElementById("linemsg").style.display = 'revert';
1964 display_fen = base_fen;
1965 set_board_position(base_fen);
1966 update_imbalance(base_fen);
1970 document.getElementById("linenav").style.display = 'revert';
1971 document.getElementById("linemsg").style.display = 'none';
1973 if (current_display_move <= 0) {
1974 document.getElementById("prevmove").textContent = "Previous";
1976 document.getElementById("prevmove").innerHTML = "<a href=\"javascript:prev_move();\">Previous</a></span>";
1978 if (current_display_move == current_display_line.pv.length - 1) {
1979 document.getElementById("nextmove").textContent = "Next";
1981 document.getElementById("nextmove").innerHTML = "<a href=\"javascript:next_move();\">Next</a></span>";
1984 let hiddenboard = chess_from(current_display_line.start_fen, current_display_line.pv, current_display_move);
1985 set_board_position(hiddenboard.fen());
1986 if (display_fen !== hiddenboard.fen() && !current_display_line_is_history) {
1987 // Fire off a hash request, since we're now off the main position
1988 // and it just changed.
1989 explore_hash(hiddenboard.fen());
1991 display_fen = hiddenboard.fen();
1992 update_imbalance(hiddenboard.fen());
1995 function set_board_position(new_fen) {
1996 board_is_animating = true;
1997 let old_fen = board.fen();
1998 let animate = old_fen !== '8/8/8/8/8/8/8/';
1999 board.position(new_fen, animate);
2000 if (board.fen() === old_fen) {
2001 board_is_animating = false;
2006 * @param {boolean} param_enable_sound
2008 function set_sound(param_enable_sound) {
2009 enable_sound = param_enable_sound;
2011 document.getElementById("soundon").innerHTML = "<strong>On</strong>";
2012 document.getElementById("soundoff").innerHTML = "<a href=\"javascript:set_sound(false)\">Off</a>";
2014 // Seemingly at least Firefox prefers MP3 over Opus; tell it otherwise,
2015 // and also preload the file since the user has selected audio.
2016 let ding = document.getElementById('ding');
2017 if (ding && ding.canPlayType && ding.canPlayType('audio/ogg; codecs="opus"') === 'probably') {
2018 ding.src = 'ding.opus';
2022 document.getElementById("soundon").innerHTML = "<a href=\"javascript:set_sound(true)\">On</a>";
2023 document.getElementById("soundoff").innerHTML = "<strong>Off</strong>";
2025 if (supports_html5_storage()) {
2026 window['localStorage']['enable_sound'] = enable_sound ? 1 : 0;
2029 window['set_sound'] = set_sound;
2031 /** Send off a hash probe request to the backend.
2032 * @param {string} fen
2034 function explore_hash(fen) {
2035 // If we already have a backend response going, abort it.
2036 if (current_hash_xhr) {
2037 current_hash_xhr.abort();
2039 if (current_hash_display_timer) {
2040 clearTimeout(current_hash_display_timer);
2041 current_hash_display_timer = null;
2043 document.getElementById("refutationlines").replaceChildren();
2045 current_hash_xhr = new AbortController();
2046 const signal = current_analysis_xhr.signal;
2047 fetch(backend_hash_url + "?fen=" + fen, { signal })
2048 .then((response) => response.json())
2049 .then((data) => { show_explore_hash_results(data, fen); })
2051 // Truncate the lines, since we already cleared the display.
2052 display_lines = [ display_lines[0], display_lines[1] ];
2053 update_move_highlight();
2057 /** Process the JSON response from a hash probe request.
2058 * @param {!Object} data
2059 * @param {string} fen
2061 function show_explore_hash_results(data, fen) {
2062 if (board_is_animating) {
2063 // Updating while the animation is still going causes
2064 // the animation to jerk. This is pretty crude, but it will do.
2065 current_hash_display_timer = setTimeout(function() { show_explore_hash_results(data, fen); }, 100);
2068 current_hash_display_timer = null;
2069 hash_refutation_lines = data['lines'];
2073 // almost all of this stuff comes from the chessboard.js example page
2074 function onDragStart(source, piece, position, orientation) {
2075 let pseudogame = new Chess(display_fen);
2076 if (pseudogame.game_over() === true ||
2077 (pseudogame.turn() === 'w' && piece.search(/^b/) !== -1) ||
2078 (pseudogame.turn() === 'b' && piece.search(/^w/) !== -1)) {
2082 recommended_move = get_best_move(pseudogame, source, null, pseudogame.turn() === 'b');
2083 if (recommended_move) {
2084 let squareEl = document.querySelector('#board .square-' + recommended_move.to);
2085 squareEl.classList.add('highlight1-32417');
2090 function mousedownSquare(e) {
2091 if (!e.target || !e.target.getAttribute('data-square')) {
2095 reverse_dragging_from = null;
2096 let square = e.target.getAttribute('data-square');
2098 let pseudogame = new Chess(display_fen);
2099 if (pseudogame.game_over() === true) {
2103 // If the square is empty, or has a piece of the side not to move,
2104 // we handle it. If not, normal piece dragging will take it.
2105 let position = board.position();
2106 if (!position.hasOwnProperty(square) ||
2107 (pseudogame.turn() === 'w' && position[square].search(/^b/) !== -1) ||
2108 (pseudogame.turn() === 'b' && position[square].search(/^w/) !== -1)) {
2109 reverse_dragging_from = square;
2110 recommended_move = get_best_move(pseudogame, null, square, pseudogame.turn() === 'b');
2111 if (recommended_move) {
2112 let squareEl = document.querySelector('#board .square-' + recommended_move.from);
2113 squareEl.classList.add('highlight1-32417');
2114 squareEl = document.querySelector('#board .square-' + recommended_move.to);
2115 squareEl.classList.add('highlight1-32417');
2120 function mouseupSquare(e) {
2121 if (!e.target || !e.target.getAttribute('data-square')) {
2124 if (reverse_dragging_from === null) {
2127 let source = e.target.getAttribute('data-square');
2128 let target = reverse_dragging_from;
2129 reverse_dragging_from = null;
2130 if (onDrop(source, target) !== 'snapback') {
2131 onSnapEnd(source, target);
2133 document.getElementById("board").querySelectorAll('.square-55d63.highlight1-32417').forEach((square) => {
2134 square.classList.remove('highlight1-32417');
2138 function get_best_move(game, source, target, invert) {
2139 let moves = game.moves({ verbose: true });
2140 if (source !== null) {
2141 moves = moves.filter(function(move) { return move.from == source; });
2143 if (target !== null) {
2144 moves = moves.filter(function(move) { return move.to == target; });
2146 if (moves.length == 0) {
2149 if (moves.length == 1) {
2153 // More than one move. Use the display lines (if we have them)
2154 // to disambiguate; otherwise, we have no information.
2156 for (let i = 0; i < moves.length; ++i) {
2157 move_hash[moves[i].san] = moves[i];
2160 // See if we're already exploring some line.
2161 if (current_display_line &&
2162 current_display_move < current_display_line.pv.length - 1) {
2163 let first_move = current_display_line.pv[current_display_move + 1];
2164 if (move_hash[first_move]) {
2165 return move_hash[first_move];
2169 // History and PV take priority over the display lines.
2170 for (let i = 0; i < 2; ++i) {
2171 let line = display_lines[i];
2172 let first_move = line.pv[line.start_display_move_num];
2173 if (move_hash[first_move]) {
2174 return move_hash[first_move];
2178 let best_move = null;
2179 let best_move_score = null;
2181 for (let move in refutation_lines) {
2182 let line = refutation_lines[move];
2183 if (!line['score']) {
2186 let first_move = line['pv'][0];
2187 if (move_hash[first_move]) {
2188 let score = compute_score_sort_key(line['score'], line['depth'], invert);
2189 if (best_move_score === null || score > best_move_score) {
2190 best_move = move_hash[first_move];
2191 best_move_score = score;
2198 function onDrop(source, target) {
2199 if (source === target) {
2200 if (recommended_move === null) {
2203 // Accept the move. It will be changed in onSnapEnd.
2207 // Suggestion not asked for.
2208 recommended_move = null;
2211 // see if the move is legal
2212 let pseudogame = new Chess(display_fen);
2213 let move = pseudogame.move({
2216 promotion: 'q' // NOTE: always promote to a queen for example simplicity
2220 if (move === null) return 'snapback';
2223 function onSnapEnd(source, target) {
2224 if (source === target && recommended_move !== null) {
2225 source = recommended_move.from;
2226 target = recommended_move.to;
2228 recommended_move = null;
2229 let pseudogame = new Chess(display_fen);
2230 let move = pseudogame.move({
2233 promotion: 'q' // NOTE: always promote to a queen for example simplicity
2236 if (admin_password !== null) {
2237 let url = '/manual-override.pl';
2238 url += '?fen=' + encodeURIComponent(display_fen);
2239 url += '&history=' + encodeURIComponent(JSON.stringify(current_analysis_data['position']['history']));
2240 url += '&move=' + encodeURIComponent(move.san);
2241 url += '&player_w=' + encodeURIComponent(current_analysis_data['position']['player_w']);
2242 url += '&player_b=' + encodeURIComponent(current_analysis_data['position']['player_b']);
2243 url += '&password=' + encodeURIComponent(admin_password);
2248 // Move ahead on the line we're on -- this includes history if we've
2250 if (current_display_line &&
2251 current_display_move < current_display_line.pv.length - 1 &&
2252 current_display_line.pv[current_display_move + 1] === move.san) {
2257 // Walk down the displayed lines until we find one that starts with
2258 // this move, then select that. Note that this gives us a good priority
2259 // order (PV, then multi-PV lines; history was already dealt with above,
2260 // as it's the only line that originates backwards).
2261 for (let i = 1; i < display_lines.length; ++i) {
2262 if (i == 1 && current_display_line) {
2263 // Do not choose PV if not on it.
2266 let line = display_lines[i];
2267 if (line.pv[line.start_display_move_num] === move.san) {
2273 // Shouldn't really be here if we have hash probes, but there's really
2274 // nothing we can do.
2275 // FIXME: Just make a new line, probably (even if we don't have hash moves).
2276 // As it is, we can actually drag (but not click) such a move in the UI,
2277 // but it has no effect on what we're probing.
2279 // End of dragging-related code.
2281 function fmt_cp(v) {
2285 return "+" + (v / 100).toFixed(2);
2288 return "-" + (v / 100).toFixed(2);
2292 function format_short_score(score) {
2296 if (score[0] === 'T' || score[0] === 't') {
2297 let ret = "TB\u00a0";
2298 if (score[2]) { // Is a bound.
2299 ret = score[2] + "\u00a0TB\u00a0";
2301 if (score[0] === 'T') {
2302 return ret + Math.ceil(score[1] / 2);
2304 return ret + "-" + Math.ceil(score[1] / 2);
2306 } else if (score[0] === 'M' || score[0] === 'm') {
2307 let sign = (score[0] === 'm') ? '-' : '';
2308 if (score[2]) { // Is a bound.
2309 return score[2] + "\u00a0M " + sign + score[1];
2311 return "M " + sign + score[1];
2313 } else if (score[0] === 'd') {
2315 } else if (score[0] === 'cp') {
2316 if (score[2]) { // Is a bound.
2317 return score[2] + "\u00a0" + fmt_cp(score[1]);
2319 return fmt_cp(score[1]);
2325 function format_long_score(score) {
2329 if (score[0] === 'T') {
2330 if (score[1] == 0) {
2331 return "Won for white (tablebase)";
2333 return "White wins in " + Math.ceil(score[1] / 2);
2335 } else if (score[0] === 't') {
2336 if (score[1] == -1) {
2337 return "Won for black (tablebase)";
2339 return "Black wins in " + Math.ceil(score[1] / 2);
2341 } else if (score[0] === 'M') {
2342 if (score[1] == 0) {
2343 return "White wins by checkmate";
2345 return "White mates in " + score[1];
2347 } else if (score[0] === 'm') {
2348 if (score[1] == 0) {
2349 return "Black wins by checkmate";
2351 return "Black mates in " + score[1];
2353 } else if (score[0] === 'd') {
2354 return "Theoretical draw";
2355 } else if (score[0] === 'cp') {
2356 return "Score: " + format_short_score(score);
2361 function compute_plot_score(score) {
2362 if (score[0] === 'M' || score[0] === 'T') {
2364 } else if (score[0] === 'm' || score[0] === 't') {
2366 } else if (score[0] === 'd') {
2368 } else if (score[0] === 'cp') {
2369 if (score[1] > 500) {
2371 } else if (score[1] < -500) {
2381 * @param score The score digest tuple.
2382 * @param {?number} depth Depth the move has been computed to, or null.
2383 * @param {boolean} invert Whether black is to play.
2386 function compute_score_sort_key(score, depth, invert) {
2391 if (score[0] === 'T') {
2392 // White reaches TB win.
2393 s = 89999 - score[1];
2394 } else if (score[0] === 't') {
2395 // Black reaches TB win.
2396 s = -(89999 - score[1]);
2397 } else if (score[0] === 'M') {
2399 s = 99999 - score[1];
2400 } else if (score[0] === 'm') {
2402 s = -(99999 - score[1]);
2403 } else if (score[0] === 'd') {
2405 } else if (score[0] === 'cp') {
2417 * @param {Object} game
2419 function switch_backend(game) {
2420 // Stop looking at historic data.
2421 current_display_line = null;
2422 current_display_move = null;
2423 displayed_analysis_data = null;
2424 if (current_historic_xhr) {
2425 current_historic_xhr.abort();
2428 // If we already have a backend response going, abort it.
2429 if (current_analysis_xhr) {
2430 current_analysis_xhr.abort();
2432 if (current_hash_xhr) {
2433 current_hash_xhr.abort();
2436 // Otherwise, we should have a timer going to start a new one.
2438 if (current_analysis_request_timer) {
2439 clearTimeout(current_analysis_request_timer);
2440 current_analysis_request_timer = null;
2442 if (current_hash_display_timer) {
2443 clearTimeout(current_hash_display_timer);
2444 current_hash_display_timer = null;
2447 // Request an immediate fetch with the new backend.
2448 backend_url = game['url'];
2449 backend_hash_url = game['hashurl'];
2450 window.location.hash = '#' + game['id'];
2451 current_analysis_data = null;
2455 window['switch_backend'] = switch_backend;
2457 window['flip'] = function() { board.flip(); redraw_arrows(); };
2458 window['set_delay_ms'] = function(ms) { delay_ms = ms; console.log('Delay is now ' + ms + ' ms.'); };
2460 // Mostly from Wikipedia's chess set as of October 2022, but some pieces are from
2461 // the 2013 version, as I like those better (and it matches the 2014 PNGs; nobody
2462 // really likes change, do they?). That is wK, bK, bQ. wQ is also slightly different,
2463 // but not enough to notice.
2464 const svg_pieces = {
2465 'wK': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><g style=%22fill:none;fill-opacity:1;fill-rule:evenodd;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22><path d=%22M 22.5,11.63 L 22.5,6%22 style=%22fill:none;stroke:%23000000;stroke-linejoin:miter%22/><path d=%22M 20,8 L 25,8%22 style=%22fill:none;stroke:%23000000;stroke-linejoin:miter%22/><path d=%22M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25%22 style=%22fill:%23ffffff;stroke:%23000000;stroke-linecap:butt;stroke-linejoin:miter%22/><path d=%22M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z %22 style=%22fill:%23ffffff;stroke:%23000000%22/><path d=%22M 11.5,30 C 17,27 27,27 32.5,30%22 style=%22fill:none;stroke:%23000000%22/><path d=%22M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5%22 style=%22fill:none;stroke:%23000000%22/><path d=%22M 11.5,37 C 17,34 27,34 32.5,37%22 style=%22fill:none;stroke:%23000000%22/></g></svg>',
2466 'wQ': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><g style=%22fill:%23ffffff;stroke:%23000000;stroke-width:1.5;stroke-linejoin:round%22><path d=%22M 9,26 C 17.5,24.5 30,24.5 36,26 L 38.5,13.5 L 31,25 L 30.7,10.9 L 25.5,24.5 L 22.5,10 L 19.5,24.5 L 14.3,10.9 L 14,25 L 6.5,13.5 L 9,26 z%22/><path d=%22M 9,26 C 9,28 10.5,28 11.5,30 C 12.5,31.5 12.5,31 12,33.5 C 10.5,34.5 11,36 11,36 C 9.5,37.5 11,38.5 11,38.5 C 17.5,39.5 27.5,39.5 34,38.5 C 34,38.5 35.5,37.5 34,36 C 34,36 34.5,34.5 33,33.5 C 32.5,31 32.5,31.5 33.5,30 C 34.5,28 36,28 36,26 C 27.5,24.5 17.5,24.5 9,26 z%22/><path d=%22M 11.5,30 C 15,29 30,29 33.5,30%22 style=%22fill:none%22/><path d=%22M 12,33.5 C 18,32.5 27,32.5 33,33.5%22 style=%22fill:none%22/><circle cx=%226%22 cy=%2212%22 r=%222%22/><circle cx=%2214%22 cy=%229%22 r=%222%22/><circle cx=%2222.5%22 cy=%228%22 r=%222%22/><circle cx=%2231%22 cy=%229%22 r=%222%22/><circle cx=%2239%22 cy=%2212%22 r=%222%22/></g></svg>',
2467 'wR': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><g style=%22opacity:1;fill:%23ffffff;fill-opacity:1;fill-rule:evenodd;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 transform=%22translate(0,0.3)%22><path d=%22M 9,39 L 36,39 L 36,36 L 9,36 L 9,39 z %22 style=%22stroke-linecap:butt%22/><path d=%22M 12,36 L 12,32 L 33,32 L 33,36 L 12,36 z %22 style=%22stroke-linecap:butt%22/><path d=%22M 11,14 L 11,9 L 15,9 L 15,11 L 20,11 L 20,9 L 25,9 L 25,11 L 30,11 L 30,9 L 34,9 L 34,14%22 style=%22stroke-linecap:butt%22/><path d=%22M 34,14 L 31,17 L 14,17 L 11,14%22/><path d=%22M 31,17 L 31,29.5 L 14,29.5 L 14,17%22 style=%22stroke-linecap:butt;stroke-linejoin:miter%22/><path d=%22M 31,29.5 L 32.5,32 L 12.5,32 L 14,29.5%22/><path d=%22M 11,14 L 34,14%22 style=%22fill:none;stroke:%23000000;stroke-linejoin:miter%22/></g></svg>',
2468 'wB': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><g style=%22opacity:1;fill:none;fill-rule:evenodd;fill-opacity:1;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 transform=%22translate(0,0.6)%22><g style=%22fill:%23ffffff;stroke:%23000000;stroke-linecap:butt%22><path d=%22M 9,36 C 12.39,35.03 19.11,36.43 22.5,34 C 25.89,36.43 32.61,35.03 36,36 C 36,36 37.65,36.54 39,38 C 38.32,38.97 37.35,38.99 36,38.5 C 32.61,37.53 25.89,38.96 22.5,37.5 C 19.11,38.96 12.39,37.53 9,38.5 C 7.65,38.99 6.68,38.97 6,38 C 7.35,36.54 9,36 9,36 z%22/><path d=%22M 15,32 C 17.5,34.5 27.5,34.5 30,32 C 30.5,30.5 30,30 30,30 C 30,27.5 27.5,26 27.5,26 C 33,24.5 33.5,14.5 22.5,10.5 C 11.5,14.5 12,24.5 17.5,26 C 17.5,26 15,27.5 15,30 C 15,30 14.5,30.5 15,32 z%22/><path d=%22M 25 8 A 2.5 2.5 0 1 1 20,8 A 2.5 2.5 0 1 1 25 8 z%22/></g><path d=%22M 17.5,26 L 27.5,26 M 15,30 L 30,30 M 22.5,15.5 L 22.5,20.5 M 20,18 L 25,18%22 style=%22fill:none;stroke:%23000000;stroke-linejoin:miter%22/></g></svg>',
2469 'wN': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><g style=%22opacity:1;fill:none;fill-opacity:1;fill-rule:evenodd;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 transform=%22translate(0,0.3)%22><path d=%22M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18%22 style=%22fill:%23ffffff;stroke:%23000000%22/><path d=%22M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10%22 style=%22fill:%23ffffff;stroke:%23000000%22/><path d=%22M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z%22 style=%22fill:%23000000;stroke:%23000000%22/><path d=%22M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z%22 transform=%22matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)%22 style=%22fill:%23000000;stroke:%23000000%22/></g></svg>',
2470 'wP': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><path d=%22m 22.5,9 c -2.21,0 -4,1.79 -4,4 0,0.89 0.29,1.71 0.78,2.38 C 17.33,16.5 16,18.59 16,21 c 0,2.03 0.94,3.84 2.41,5.03 C 15.41,27.09 11,31.58 11,39.5 H 34 C 34,31.58 29.59,27.09 26.59,26.03 28.06,24.84 29,23.03 29,21 29,18.59 27.67,16.5 25.72,15.38 26.21,14.71 26.5,13.89 26.5,13 c 0,-2.21 -1.79,-4 -4,-4 z%22 style=%22opacity:1;fill:%23ffffff;fill-opacity:1;fill-rule:nonzero;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22/></svg>',
2471 'bK': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><g style=%22fill:none;fill-opacity:1;fill-rule:evenodd;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22><path d=%22M 22.5,11.63 L 22.5,6%22 style=%22fill:none;stroke:%23000000;stroke-linejoin:miter%22 id=%22path6570%22/><path d=%22M 22.5,25 C 22.5,25 27,17.5 25.5,14.5 C 25.5,14.5 24.5,12 22.5,12 C 20.5,12 19.5,14.5 19.5,14.5 C 18,17.5 22.5,25 22.5,25%22 style=%22fill:%23000000;fill-opacity:1;stroke-linecap:butt;stroke-linejoin:miter%22/><path d=%22M 11.5,37 C 17,40.5 27,40.5 32.5,37 L 32.5,30 C 32.5,30 41.5,25.5 38.5,19.5 C 34.5,13 25,16 22.5,23.5 L 22.5,27 L 22.5,23.5 C 19,16 9.5,13 6.5,19.5 C 3.5,25.5 11.5,29.5 11.5,29.5 L 11.5,37 z %22 style=%22fill:%23000000;stroke:%23000000%22/><path d=%22M 20,8 L 25,8%22 style=%22fill:none;stroke:%23000000;stroke-linejoin:miter%22/><path d=%22M 32,29.5 C 32,29.5 40.5,25.5 38.03,19.85 C 34.15,14 25,18 22.5,24.5 L 22.51,26.6 L 22.5,24.5 C 20,18 9.906,14 6.997,19.85 C 4.5,25.5 11.85,28.85 11.85,28.85%22 style=%22fill:none;stroke:%23ffffff%22/><path d=%22M 11.5,30 C 17,27 27,27 32.5,30 M 11.5,33.5 C 17,30.5 27,30.5 32.5,33.5 M 11.5,37 C 17,34 27,34 32.5,37%22 style=%22fill:none;stroke:%23ffffff%22/></g></svg>',
2472 'bQ': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns:svg=%22http://www.w3.org/2000/svg%22 xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22 id=%22svg3128%22><defs/><g id=%22layer1%22><path d=%22M 8 12 A 2 2 0 1 1 4,12 A 2 2 0 1 1 8 12 z%22 style=%22opacity:1;fill:%23000000;fill-opacity:1;stroke:%23000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 id=%22path5571%22/><path d=%22M 9 13 A 2 2 0 1 1 5,13 A 2 2 0 1 1 9 13 z%22 transform=%22translate(15.5,-5.5)%22 style=%22opacity:1;fill:%23000000;fill-opacity:1;stroke:%23000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 id=%22path5573%22/><path d=%22M 9 13 A 2 2 0 1 1 5,13 A 2 2 0 1 1 9 13 z%22 transform=%22translate(32,-1)%22 style=%22opacity:1;fill:%23000000;fill-opacity:1;stroke:%23000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 id=%22path5575%22/><path d=%22M 9 13 A 2 2 0 1 1 5,13 A 2 2 0 1 1 9 13 z%22 transform=%22translate(7,-4.5)%22 style=%22opacity:1;fill:%23000000;fill-opacity:1;stroke:%23000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 id=%22path5577%22/><path d=%22M 9 13 A 2 2 0 1 1 5,13 A 2 2 0 1 1 9 13 z%22 transform=%22translate(24,-4)%22 style=%22opacity:1;fill:%23000000;fill-opacity:1;stroke:%23000000;stroke-width:1;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 id=%22path5579%22/><path d=%22M 9,26 C 17.5,24.5 30,24.5 36,26 L 38,14 L 31,25 L 31,11 L 25.5,24.5 L 22.5,9.5 L 19.5,24.5 L 14,10.5 L 14,25 L 7,14 L 9,26 z %22 style=%22fill:%23000000;fill-opacity:1;fill-rule:evenodd;stroke:%23000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1%22 id=%22path5581%22/><path d=%22M 9,26 C 9,28 10.5,28 11.5,30 C 12.5,31.5 12.5,31 12,33.5 C 10.5,34.5 10.5,36 10.5,36 C 9,37.5 11,38.5 11,38.5 C 17.5,39.5 27.5,39.5 34,38.5 C 34,38.5 35.5,37.5 34,36 C 34,36 34.5,34.5 33,33.5 C 32.5,31 32.5,31.5 33.5,30 C 34.5,28 36,28 36,26 C 27.5,24.5 17.5,24.5 9,26 z %22 style=%22fill:%23000000;fill-opacity:1;fill-rule:evenodd;stroke:%23000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:round;stroke-opacity:1%22 id=%22path5583%22/><path d=%22M 11.5,30 C 15,29 30,29 33.5,30%22 style=%22fill:none;fill-opacity:0.75;fill-rule:evenodd;stroke:%23ffffff;stroke-width:1px;stroke-linecap:round;stroke-linejoin:round;stroke-opacity:1%22 id=%22path5585%22/><path d=%22M 12,33.5 C 18,32.5 27,32.5 33,33.5%22 style=%22fill:none;fill-opacity:0.75;fill-rule:evenodd;stroke:%23ffffff;stroke-width:1px;stroke-linecap:round;stroke-linejoin:round;stroke-opacity:1%22 id=%22path5587%22/><path d=%22M 10.5,36 C 15.5,35 29,35 34,36%22 style=%22fill:none;fill-opacity:0.75;fill-rule:evenodd;stroke:%23ffffff;stroke-width:1px;stroke-linecap:round;stroke-linejoin:round;stroke-opacity:1%22 id=%22path5589%22/></g></svg>',
2473 'bR': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><g style=%22opacity:1;fill:%23000000;fill-opacity:1;fill-rule:evenodd;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 transform=%22translate(0,0.3)%22><path d=%22M 9,39 L 36,39 L 36,36 L 9,36 L 9,39 z %22 style=%22stroke-linecap:butt%22/><path d=%22M 12.5,32 L 14,29.5 L 31,29.5 L 32.5,32 L 12.5,32 z %22 style=%22stroke-linecap:butt%22/><path d=%22M 12,36 L 12,32 L 33,32 L 33,36 L 12,36 z %22 style=%22stroke-linecap:butt%22/><path d=%22M 14,29.5 L 14,16.5 L 31,16.5 L 31,29.5 L 14,29.5 z %22 style=%22stroke-linecap:butt;stroke-linejoin:miter%22/><path d=%22M 14,16.5 L 11,14 L 34,14 L 31,16.5 L 14,16.5 z %22 style=%22stroke-linecap:butt%22/><path d=%22M 11,14 L 11,9 L 15,9 L 15,11 L 20,11 L 20,9 L 25,9 L 25,11 L 30,11 L 30,9 L 34,9 L 34,14 L 11,14 z %22 style=%22stroke-linecap:butt%22/><path d=%22M 12,35.5 L 33,35.5 L 33,35.5%22 style=%22fill:none;stroke:%23ffffff;stroke-width:1;stroke-linejoin:miter%22/><path d=%22M 13,31.5 L 32,31.5%22 style=%22fill:none;stroke:%23ffffff;stroke-width:1;stroke-linejoin:miter%22/><path d=%22M 14,29.5 L 31,29.5%22 style=%22fill:none;stroke:%23ffffff;stroke-width:1;stroke-linejoin:miter%22/><path d=%22M 14,16.5 L 31,16.5%22 style=%22fill:none;stroke:%23ffffff;stroke-width:1;stroke-linejoin:miter%22/><path d=%22M 11,14 L 34,14%22 style=%22fill:none;stroke:%23ffffff;stroke-width:1;stroke-linejoin:miter%22/></g></svg>',
2474 'bB': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><g style=%22opacity:1;fill:none;fill-rule:evenodd;fill-opacity:1;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 transform=%22translate(0,0.6)%22><g style=%22fill:%23000000;stroke:%23000000;stroke-linecap:butt%22><path d=%22M 9,36 C 12.39,35.03 19.11,36.43 22.5,34 C 25.89,36.43 32.61,35.03 36,36 C 36,36 37.65,36.54 39,38 C 38.32,38.97 37.35,38.99 36,38.5 C 32.61,37.53 25.89,38.96 22.5,37.5 C 19.11,38.96 12.39,37.53 9,38.5 C 7.65,38.99 6.68,38.97 6,38 C 7.35,36.54 9,36 9,36 z%22/><path d=%22M 15,32 C 17.5,34.5 27.5,34.5 30,32 C 30.5,30.5 30,30 30,30 C 30,27.5 27.5,26 27.5,26 C 33,24.5 33.5,14.5 22.5,10.5 C 11.5,14.5 12,24.5 17.5,26 C 17.5,26 15,27.5 15,30 C 15,30 14.5,30.5 15,32 z%22/><path d=%22M 25 8 A 2.5 2.5 0 1 1 20,8 A 2.5 2.5 0 1 1 25 8 z%22/></g><path d=%22M 17.5,26 L 27.5,26 M 15,30 L 30,30 M 22.5,15.5 L 22.5,20.5 M 20,18 L 25,18%22 style=%22fill:none;stroke:%23ffffff;stroke-linejoin:miter%22/></g></svg>',
2475 'bN': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><g style=%22opacity:1;fill:none;fill-opacity:1;fill-rule:evenodd;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22 transform=%22translate(0,0.3)%22><path d=%22M 22,10 C 32.5,11 38.5,18 38,39 L 15,39 C 15,30 25,32.5 23,18%22 style=%22fill:%23000000;stroke:%23000000%22/><path d=%22M 24,18 C 24.38,20.91 18.45,25.37 16,27 C 13,29 13.18,31.34 11,31 C 9.958,30.06 12.41,27.96 11,28 C 10,28 11.19,29.23 10,30 C 9,30 5.997,31 6,26 C 6,24 12,14 12,14 C 12,14 13.89,12.1 14,10.5 C 13.27,9.506 13.5,8.5 13.5,7.5 C 14.5,6.5 16.5,10 16.5,10 L 18.5,10 C 18.5,10 19.28,8.008 21,7 C 22,7 22,10 22,10%22 style=%22fill:%23000000;stroke:%23000000%22/><path d=%22M 9.5 25.5 A 0.5 0.5 0 1 1 8.5,25.5 A 0.5 0.5 0 1 1 9.5 25.5 z%22 style=%22fill:%23ffffff;stroke:%23ffffff%22/><path d=%22M 15 15.5 A 0.5 1.5 0 1 1 14,15.5 A 0.5 1.5 0 1 1 15 15.5 z%22 transform=%22matrix(0.866,0.5,-0.5,0.866,9.693,-5.173)%22 style=%22fill:%23ffffff;stroke:%23ffffff%22/><path d=%22M 24.55,10.4 L 24.1,11.85 L 24.6,12 C 27.75,13 30.25,14.49 32.5,18.75 C 34.75,23.01 35.75,29.06 35.25,39 L 35.2,39.5 L 37.45,39.5 L 37.5,39 C 38,28.94 36.62,22.15 34.25,17.66 C 31.88,13.17 28.46,11.02 25.06,10.5 L 24.55,10.4 z %22 style=%22fill:%23ffffff;stroke:none%22/></g></svg>',
2476 'bP': 'data:image/svg+xml,<?xml version=%221.0%22 encoding=%22UTF-8%22 standalone=%22no%22?>%0A<!DOCTYPE svg PUBLIC %22-//W3C//DTD SVG 1.1//EN%22 %22http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd%22>%0A<svg xmlns=%22http://www.w3.org/2000/svg%22 version=%221.1%22 width=%2245%22 height=%2245%22><path d=%22m 22.5,9 c -2.21,0 -4,1.79 -4,4 0,0.89 0.29,1.71 0.78,2.38 C 17.33,16.5 16,18.59 16,21 c 0,2.03 0.94,3.84 2.41,5.03 C 15.41,27.09 11,31.58 11,39.5 H 34 C 34,31.58 29.59,27.09 26.59,26.03 28.06,24.84 29,23.03 29,21 29,18.59 27.67,16.5 25.72,15.38 26.21,14.71 26.5,13.89 26.5,13 c 0,-2.21 -1.79,-4 -4,-4 z%22 style=%22opacity:1;fill:%23000000;fill-opacity:1;fill-rule:nonzero;stroke:%23000000;stroke-width:1.5;stroke-linecap:round;stroke-linejoin:miter;stroke-miterlimit:4;stroke-dasharray:none;stroke-opacity:1%22/></svg>',
2479 function svg_piece_theme(piece) {
2480 return svg_pieces[piece];
2484 unique = get_unique();
2486 // Load settings from HTML5 local storage if available.
2487 if (supports_html5_storage() && window['localStorage']['enable_sound']) {
2488 set_sound(parseInt(window['localStorage']['enable_sound']));
2493 let admin_match = window.location.href.match(/\?password=([a-zA-Z0-9_-]+)/);
2494 if (admin_match !== null) {
2495 admin_password = admin_match[1];
2499 board = new window.ChessBoard('board', {
2500 onMoveEnd: function() { board_is_animating = false; },
2503 pieceTheme: svg_piece_theme,
2504 onDragStart: onDragStart,
2506 onSnapEnd: onSnapEnd
2508 document.getElementById("board").addEventListener('mousedown', mousedownSquare);
2509 document.getElementById("board").addEventListener('mouseup', mouseupSquare);
2511 if (window['inline_json']) {
2512 let j = window['inline_json'];
2513 process_update_response(j['data'], { 'get': (h) => j['headers'][h] });
2514 delete window['inline_json'];
2517 window.addEventListener('resize', function() {
2519 update_sparkline(displayed_analysis_data || current_analysis_data);
2520 update_board_highlight();
2523 new ResizeObserver(() => update_sparkline(displayed_analysis_data || current_analysis_data)).observe(document.getElementById('scoresparkcontainer'));
2524 window.addEventListener('keyup', function(event) {
2525 if (event.which == 39) { // Left arrow.
2527 } else if (event.which == 37) { // Right arrow.
2529 } else if (event.which >= 49 && event.which <= 57) { // 1-9.
2530 let num = event.which - 49;
2531 if (current_games && current_games.length >= num) {
2532 switch_backend(current_games[num]);
2534 } else if (event.which == 78) { // N.
2538 window.addEventListener('hashchange', possibly_switch_game_from_hash, false);
2539 possibly_switch_game_from_hash();
2541 document.addEventListener('DOMContentLoaded', init);