]> git.sesse.net Git - remoteglot/commitdiff
Clean up the refutation line choice logic a bit.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 27 Dec 2023 21:56:12 +0000 (22:56 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Wed, 27 Dec 2023 21:56:12 +0000 (22:56 +0100)
www/js/remoteglot.js

index 34b413f2927f9c5bdc5137a4cf2e1e7acc188419..cf5fba704f23f826dc4601a27ad68483295d36bc 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;
@@ -861,12 +856,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) {
@@ -1253,8 +1259,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();
@@ -1391,12 +1395,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.
@@ -1750,7 +1749,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.
@@ -2076,7 +2074,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();
 }
 
@@ -2188,6 +2186,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']) {