]> git.sesse.net Git - remoteglot/blobdiff - www/js/remoteglot.js
Lowercase toplay internally, for consistency with FEN.
[remoteglot] / www / js / remoteglot.js
index c0cba6a6120514406e02311e5a7f4307ec8761af..870703c4edc7be33337cbc4212f63082ab5d8711 100644 (file)
@@ -397,12 +397,10 @@ function redraw_arrows() {
 /** @param {!string} fen
  * @return {!string}
  *
- * Return whose side it is to play (W or B), given a FEN.
- * This is in uppercase due to historical reasons, which we
- * should get rid of.
+ * Return whose side it is to play (w or b), given a FEN.
  */
 function find_toplay(fen) {
-       return fen.split(' ')[1].toUpperCase();
+       return fen.split(' ')[1];
 }
 
 /** @param {!string} fen
@@ -689,19 +687,17 @@ function thousands(x) {
 /**
  * @param {!string} start_fen
  * @param {Array.<string>} pv
- * @param {number} move_num
- * @param {!string} toplay
  * @param {Array<{ first_move: integer, score: Object }>} scores
  * @param {number} start_display_move_num
  * @param {number=} opt_limit
  * @param {boolean=} opt_showlast
  */
-function add_pv(start_fen, pv, move_num, toplay, scores, start_display_move_num, opt_limit, opt_showlast) {
+function add_pv(start_fen, pv, scores, start_display_move_num, opt_limit, opt_showlast) {
        display_lines.push({
                start_fen: start_fen,
                pv: pv,
-               move_num: parseInt(move_num),
-               toplay: toplay,
+               move_num: find_move_num(start_fen),
+               toplay: find_toplay(start_fen),
                scores: scores,
                start_display_move_num: start_display_move_num
        });
@@ -733,13 +729,13 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
        if (start_display_move_num > 0) {
                pv = pv.slice(start_display_move_num);
                let to_add = start_display_move_num;
-               if (toplay === 'B') {
+               if (toplay === 'b') {
                        ++move_num;
-                       toplay = 'W';
+                       toplay = 'w';
                        --to_add;
                }
                if (to_add % 2 == 1) {
-                       toplay = 'B';
+                       toplay = 'b';
                        --to_add;
                }
                move_num += to_add / 2;
@@ -753,7 +749,7 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
        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
+               // We assume here that toplay is 'w'. We also assume that if
                // opt_showlast is set, then it is the history, and thus,
                // the UI should be to expand the history.
                ret.appendChild(document.createTextNode('('));
@@ -768,7 +764,7 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
                        ++i;
                }
                move_num += i / 2;
-       } else if (toplay == 'B' && pv.length > 0) {
+       } else if (toplay == 'b' && pv.length > 0) {
                ret.appendChild(document.createTextNode(move_num + '. … '));
        }
        for (; i < pv.length; ++i) {
@@ -778,10 +774,10 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
                        in_tb = true;
                }
 
-               if (toplay == 'B' && i == 0) {
+               if (toplay == 'b' && i == 0) {
                        ++move_num;
-                       toplay = 'W';
-               } else if (toplay == 'W') {
+                       toplay = 'w';
+               } else if (toplay == 'w') {
                        if (i > opt_limit && !opt_showlast) {
                                if (in_tb) {
                                        prefix += ')';
@@ -791,10 +787,10 @@ function print_pv(line_num, splicepos, opt_limit, opt_showlast) {
                        }
                        prefix += ' ' + move_num + '. ';
                        ++move_num;
-                       toplay = 'B';
+                       toplay = 'b';
                } else {
                        prefix += ' ';
-                       toplay = 'W';
+                       toplay = 'w';
                }
                ret.appendChild(document.createTextNode(prefix));
 
@@ -886,9 +882,7 @@ function update_refutation_lines() {
                moves.push(move);
        }
 
-       let move_num = find_move_num(base_fen);
-       let toplay = find_toplay(base_fen);
-       let invert = (toplay === 'B');
+       let invert = (find_toplay(base_fen) === 'b');
        if (current_display_line && current_display_move % 2 == 0 && !current_display_line_is_history) {
                invert = !invert;
        }
@@ -925,7 +919,7 @@ function update_refutation_lines() {
                        let pv_td = document.createElement("td");
                        tr.appendChild(pv_td);
                        pv_td.classList.add("pv");
-                       pv_td.append(add_pv(base_fen, base_line.concat([ line['move'] ]), move_num, toplay, scores, start_display_move_num));
+                       pv_td.append(add_pv(base_fen, base_line.concat([ line['move'] ]), scores, start_display_move_num));
 
                        tbl.append(tr);
                        continue;
@@ -954,7 +948,7 @@ function update_refutation_lines() {
                let pv_td = document.createElement("td");
                tr.appendChild(pv_td);
                pv_td.classList.add("pv");
-               pv_td.append(add_pv(base_fen, base_line.concat(line['pv']), move_num, toplay, scores, start_display_move_num, 10));
+               pv_td.append(add_pv(base_fen, base_line.concat(line['pv']), scores, start_display_move_num, 10));
 
                tbl.append(tr);
        }
@@ -1106,8 +1100,8 @@ function update_board() {
        // unconditionally taken from current_data (we're not interested in
        // historic history).
        if (current_data['position']['history']) {
-               let start = (current_data['position'] && current_data['position']['start_fen']) ? current_data['position']['start_fen'] : 'start';
-               add_pv(start, current_data['position']['history'], 1, 'W', null, 0, 8, true);
+               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';
+               add_pv(start, current_data['position']['history'], null, 0, 8, true);
        } else {
                display_lines.push(null);
        }
@@ -1182,18 +1176,18 @@ function update_board() {
                // Find the previous move.
                let previous_move_num, previous_toplay;
                let fen = data['position']['fen'];
-               if (find_toplay(fen) === 'B') {
+               if (find_toplay(fen) === 'b') {
                        previous_move_num = find_move_num(fen);
-                       previous_toplay = 'W';
+                       previous_toplay = 'w';
                } else {
                        previous_move_num = find_move_num(fen) - 1;
-                       previous_toplay = 'B';
+                       previous_toplay = 'b';
                }
 
                last_move = format_move_with_number(
                        data['position']['last_move'],
                        previous_move_num,
-                       previous_toplay == 'W');
+                       previous_toplay == 'w');
                headline += ' after ' + last_move;
        } else {
                last_move = null;
@@ -1331,7 +1325,7 @@ function update_board() {
                        hiddenboard.move(data['pv'][i + 1]);  // To keep continuity.
                }
 
-               let alt_moves = find_nonstupid_moves(data, 30, toplay === 'B');
+               let alt_moves = find_nonstupid_moves(data, 30, toplay === 'b');
                for (let i = 1; i < alt_moves.length && i < 3; ++i) {
                        hiddenboard = new Chess(base_fen);
                        let move = patch_move(hiddenboard.move(alt_moves[i]));
@@ -1343,7 +1337,7 @@ function update_board() {
 
        // See if all semi-reasonable moves have only one possible response.
        if (data['pv'].length >= 2) {
-               let nonstupid_moves = find_nonstupid_moves(data, 300, toplay === 'B');
+               let nonstupid_moves = find_nonstupid_moves(data, 300, toplay === 'b');
                let hiddenboard = new Chess(base_fen);
                hiddenboard.move(data['pv'][0]);
                let response = hiddenboard.move(data['pv'][1]);
@@ -1404,7 +1398,7 @@ function update_sparkline(data) {
                if (first_move_num !== undefined) {
                        let fen = data['position']['fen'];
                        let last_move_num = find_move_num(fen) * 2 - 3;
-                       if (find_toplay(fen) === 'B') {
+                       if (find_toplay(fen) === 'b') {
                                ++last_move_num;
                        }