]> git.sesse.net Git - remoteglot-book/blobdiff - www/js/book.js
Fix base opening-stats.pl for the new computer game count.
[remoteglot-book] / www / js / book.js
index a100fc85afc1e48fd9953a16b4936bc23e23a9d7..cc948fdc3b2152c50014b7eb8dcaee63e818d1b3 100644 (file)
@@ -60,6 +60,12 @@ var update = function() {
        fetch_analysis();
 }
 
+var get_history_url = function() {
+       var history = game.history({ verbose: true }).map(function(x) { return x.san; });
+       history.length = move_override;
+       return '/?' + history.join(',');
+}
+
 var fetch_analysis = function() {
        var fen = current_display_fen();
        $.ajax({
@@ -86,6 +92,8 @@ var headings = [
        [ "Move", TYPE_MOVE ],
        [ "Games", TYPE_INTEGER ],
        [ "%", TYPE_RATIO ],
+       [ "CGames", TYPE_INTEGER ],
+       [ "Comp%", TYPE_RATIO ],
        [ "Win%", TYPE_RATIO ],
        [ "WWin", TYPE_INTEGER ],
        [ "%WW", TYPE_RATIO ],
@@ -161,12 +169,15 @@ var show_lines = function(data, game) {
                var white = parseInt(move['white']);
                var draw = parseInt(move['draw']);
                var black = parseInt(move['black']);
+               var computer = parseInt(move['computer']);
 
                line.push(move['move']);  // Move.
                transpose_only.push(move['transpose_only']);
                var num = white + draw + black;
                line.push(num);  // N.
                line.push(num / total_num);  // %.
+               line.push(computer);  // CGames.
+               line.push(computer / num);  // Comp%.
 
                // Win%.
                var white_win_ratio = (white + 0.5 * draw) / num;
@@ -236,11 +247,19 @@ var show_lines = function(data, game) {
                                td.appendChild(move_a);
                                $(move_a).text(line[j]);
                        } else if (headings[j][1] == TYPE_INTEGER) {
-                               add_td(tr, line[j]);
+                               add_td(tr, line[j] || 0);
                        } else if (headings[j][1] == TYPE_FLOAT) {
-                               add_td(tr, line[j].toFixed(1));
+                               if (isNaN(line[j]) || !isFinite(line[j])) {
+                                       add_td(tr, '');
+                               } else {
+                                       add_td(tr, line[j].toFixed(1));
+                               }
                        } else {
-                               add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
+                               if (isNaN(line[j]) || !isFinite(line[j])) {
+                                       add_td(tr, '');
+                               } else {
+                                       add_td(tr, (100.0 * line[j]).toFixed(1) + "%");
+                               }
                        }
                }
 
@@ -254,7 +273,7 @@ var set_includetransp = function(value) {
 }
 window['set_includetransp'] = set_includetransp;
 
-var make_move = function(move) {
+var make_move = function(move, do_update) {
        var history = game.history({ verbose: true });
        if (move_override < history.length && history[move_override].san == move) {
                // User effectively only moved forward in history.
@@ -273,7 +292,11 @@ var make_move = function(move) {
                fens.push(game.fen());
                ++move_override;
        }
-       update();
+
+       if (do_update !== false) {
+               update();
+               window.history.pushState(null, null, get_history_url());
+       }
 }
 window['make_move'] = make_move;
 
@@ -281,6 +304,7 @@ var prev_move = function() {
        if (move_override > 0) {
                --move_override;
                update();
+               window.history.replaceState(null, null, get_history_url());
        }
 }
 window['prev_move'] = prev_move;
@@ -289,13 +313,17 @@ var next_move = function() {
        if (move_override < game.history().length) {
                ++move_override;
                update();
+               window.history.replaceState(null, null, get_history_url());
        }
 }
 window['next_move'] = next_move;
 
-var set_move = function(n) {
+var set_move = function(n, do_update) {
        move_override = n;
-       update();
+       if (do_update !== false) {
+               update();
+               window.history.replaceState(null, null, get_history_url());
+       }
 }
 window['set_move'] = set_move;
 
@@ -333,6 +361,29 @@ var onSnapEnd = function(source, target) {
        make_move(pseudogame.history({ verbose: true }).pop().san);
 }
 
+var onpopstate = function() {
+       var old_moves = game.history({ verbose: true }).map(function(x) { return x.san; });
+       var new_moves = document.location.search.replace(/^\?/, "").split(",");
+
+       if (new_moves.length == 1 && new_moves[0] == "") {
+               new_moves = [];
+       }
+
+       var num_shared_moves;
+       for (num_shared_moves = 0; num_shared_moves < Math.min(old_moves.length, new_moves.length); ++num_shared_moves) {
+               if (old_moves[i] != new_moves[i]) {
+                       break;
+               }
+       }
+
+       set_move(num_shared_moves, false);
+       for (var i = num_shared_moves; i < new_moves.length; ++i) {
+               make_move(new_moves[i], false);
+       }
+       update();
+       window.history.replaceState(null, null, get_history_url());
+}
+
 var init = function() {
        // Create board.
        board = new window.ChessBoard('board', {
@@ -342,7 +393,9 @@ var init = function() {
                onDrop: onDrop,
                onSnapEnd: onSnapEnd
        });
-       update();
+
+       window.onpopstate = onpopstate;
+       onpopstate();
 
        $(window).keyup(function(event) {
                if (event.which == 39) {