]> git.sesse.net Git - remoteglot/blobdiff - www/js/remoteglot.js
Make the print_pv() logic hopefully slightly easier to understand.
[remoteglot] / www / js / remoteglot.js
index 594759157e5d1fa664fe146c824a56f6c5222c37..2e66c82fb03fb406a0ec8180762593501134e836 100644 (file)
@@ -75,34 +75,29 @@ let occupied_by_arrows = [];
 
 /** Currently displayed refutation lines (on-screen).
  * Can either come from the current_analysis_data, displayed_analysis_data,
- * or hash_refutation_lines.
+ * or hash_refutation_lines (choose_displayed_refutation_lines() chooses which one).
  *
- * TODO: This is a mess, and we should probably just let update_refutation_lines()
- * decide anew every time instead of storing the state here in multiple global
- * variables.
- *
- * @type {Array.<{
+ * @typedef {{
  *     score: Array,
  *     depth: string,
  *     pv: Array.<string>,
  *     move: string
- * }>}
+ * }}
  * @private
  */
-let refutation_lines = [];
+var RefutationLine;
 
 /** Refutation lines from current hash probe.
- *
  * If non-null, will override refutation lines from the base position.
- * Note that these are relative to display_fen, not base_fen.
+ *
+ * @type {Array.<RefutationLine>}
  */
 let hash_refutation_lines = null;
 
 /**
- * What FEN refutation_lines is relative to. Will usually be base_fen,
- * but if another move gets loaded while we are looking, they can diverge.
+ * What FEN hash_refutation_lines is relative to.
  */
-let refutation_lines_base_fen = null;
+let hash_refutation_lines_base_fen = null;
 
 /** @type {number} @private */
 let ims = 0;
@@ -175,8 +170,6 @@ let display_fen = null;
 /** @typedef {{
  *    start_fen: string,
  *    pv: Array.<string>,
- *    move_num: number,
- *    toplay: string,
  *    scores: Array<{first_move: number, score: Object}>,
  *    start_display_move_num: number
  * }}
@@ -715,8 +708,6 @@ function add_pv(start_fen, pv, scores, start_display_move_num, opt_limit, opt_sh
        display_lines.push({
                start_fen: start_fen,
                pv: pv,
-               move_num: find_move_num(start_fen),
-               toplay: find_toplay(start_fen),
                scores: scores,
                start_display_move_num: start_display_move_num
        });
@@ -740,37 +731,16 @@ function add_pv(start_fen, pv, scores, start_display_move_num, opt_limit, opt_sh
 function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
        let display_line = display_lines[line_num];
        let pv = display_line.pv;
-       let move_num = display_line.move_num;
-       let toplay = display_line.toplay;
-
-       // Truncate PV at the start if needed.
-       let start_display_move_num = display_line.start_display_move_num;
-       if (start_display_move_num > 0) {
-               pv = pv.slice(start_display_move_num);
-               let to_add = start_display_move_num;
-               if (toplay === 'b') {
-                       ++move_num;
-                       toplay = 'w';
-                       --to_add;
-               }
-               if (to_add % 2 == 1) {
-                       toplay = 'b';
-                       --to_add;
-               }
-               move_num += to_add / 2;
-               if (splicepos !== null && splicepos > 0) {
-                       --splicepos;
-               }
-       }
+       let move_num = find_move_num(display_line.start_fen);
+       let toplay = find_toplay(display_line.start_fen);
+       let halfmove_num = move_num * 2 + (toplay === 'w' ? 0 : 1);  // From two, for simplicity.
 
        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
-               // opt_showlast is set, then it is the history, and thus,
-               // the UI should be to expand the history.
+
+       // Truncate PV at the start if needed.
+       let to_skip = display_line.start_display_move_num;
+       if (opt_limit && opt_showlast && pv.length - to_skip > opt_limit) {
+               // Explicit (UI-visible) truncation from the start, for the history.
                ret.appendChild(document.createTextNode('('));
                let link = document.createElement('a');
                link.className = 'move';
@@ -778,25 +748,37 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
                link.textContent = '…';
                ret.appendChild(link);
                ret.appendChild(document.createTextNode(') '));
-               i = pv.length - opt_limit;
-               if (i % 2 == 1) {
-                       ++i;
+               to_skip = pv.length - opt_limit;
+               to_skip += to_skip % 2;  // Make sure it starts on a white move.
+       }
+       if (to_skip > 0) {
+               pv = pv.slice(to_skip);
+               halfmove_num += to_skip;
+               if (splicepos !== null) {
+                       splicepos -= to_skip;
+                       if (splicepos < 0) {
+                               splicepos = 0;
+                       }
                }
-               move_num += i / 2;
-       } else if (toplay == 'b' && pv.length > 0) {
-               ret.appendChild(document.createTextNode(move_num + '. … '));
        }
-       for (; i < pv.length; ++i) {
+
+       // The initial move number needs to go before any (TB: …) marker.
+       if (halfmove_num % 2 == 1) {
+               // Black move.
+               ret.appendChild(document.createTextNode((halfmove_num - 1) / 2 + '. … '));
+       } else {
+               // White move.
+               ret.appendChild(document.createTextNode(halfmove_num / 2 + '. '));
+       }
+       let in_tb = false;
+       for (let i = 0; i < pv.length; ++i, ++halfmove_num) {
                let prefix = '';
                if (splicepos === i) {
-                       prefix = '(TB: ';
+                       prefix = '(TB:';
                        in_tb = true;
                }
 
-               if (toplay == 'b' && i == 0) {
-                       ++move_num;
-                       toplay = 'w';
-               } else if (toplay == 'w') {
+               if (halfmove_num % 2 == 0 && i != 0) {
                        if (i > opt_limit && !opt_showlast) {
                                if (in_tb) {
                                        prefix += ')';
@@ -804,12 +786,9 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
                                ret.appendChild(document.createTextNode(prefix + ' (…)'));
                                return ret;
                        }
-                       prefix += ' ' + move_num + '. ';
-                       ++move_num;
-                       toplay = 'b';
+                       prefix += ' ' + (halfmove_num / 2) + '. ';
                } else {
                        prefix += ' ';
-                       toplay = 'w';
                }
                ret.appendChild(document.createTextNode(prefix));
 
@@ -865,12 +844,23 @@ function collapse_history(truncate_history) {
 }
 window['collapse_history'] = collapse_history;
 
-/** Update the HTML display of multi-PV from the global "refutation_lines".
+function choose_displayed_refutation_lines() {
+       if (hash_refutation_lines) {
+               // If we're in hash exploration, that takes precedence.
+               return [hash_refutation_lines, hash_refutation_lines_base_fen];
+       } else {
+               let data = displayed_analysis_data || current_analysis_data;
+               return [data['refutation_lines'], data['position']['fen']];
+       }
+}
+
+/** Update the HTML display of multi-PV.
  *
  * Also recreates the global "display_lines".
  */
 function update_refutation_lines() {
-       if (refutation_lines_base_fen === null) {
+       const [refutation_lines, refutation_lines_base_fen] = choose_displayed_refutation_lines();
+       if (!refutation_lines) {
                return;
        }
        if (display_lines.length > 2) {
@@ -1257,8 +1247,6 @@ function update_board() {
                document.getElementById("refutationlines").replaceChildren();
                document.getElementById("whiteclock").replaceChildren();
                document.getElementById("blackclock").replaceChildren();
-               refutation_lines = [];
-               refutation_lines_base_fen = null;
                update_refutation_lines();
                clear_arrows();
                update_displayed_line();
@@ -1395,12 +1383,7 @@ function update_board() {
                }
        }
 
-       // Update the refutation lines.
        base_fen = data['position']['fen'];
-       if (hash_refutation_lines === null) {
-               refutation_lines = data['refutation_lines'];
-               refutation_lines_base_fen = base_fen;
-       }
        update_refutation_lines();
 
        // Update the sparkline last, since its size depends on how everything else reflowed.
@@ -1745,15 +1728,6 @@ function format_tooltip(data, halfmove_num) {
        }
 }
 
-/**
- * @param {boolean} truncate_history
- */
-function set_truncate_history(truncate_history) {
-       truncate_display_history = truncate_history;
-       update_refutation_lines();
-}
-window['set_truncate_history'] = set_truncate_history;
-
 /**
  * @param {number} line_num
  * @param {number} move_num
@@ -1763,7 +1737,6 @@ function show_line(line_num, move_num) {
                current_display_line = null;
                current_display_move = null;
                hash_refutation_lines = null;
-               refutation_lines_base_fen = base_fen;
                if (displayed_analysis_data) {
                        // TODO: Support exiting to history position if we are in an
                        // analysis line of a history position.
@@ -2089,7 +2062,7 @@ function show_explore_hash_results(data, fen) {
        }
        current_hash_display_timer = null;
        hash_refutation_lines = data['lines'];
-       refutation_lines_base_fen = fen;
+       hash_refutation_lines_base_fen = fen;
        update_board();
 }
 
@@ -2201,6 +2174,7 @@ function get_best_move(game, source, target, invert) {
        let best_move = null;
        let best_move_score = null;
 
+       let refutation_lines = choose_displayed_refutation_lines()[0];
        for (let move in refutation_lines) {
                let line = refutation_lines[move];
                if (!line['score']) {