]> git.sesse.net Git - remoteglot-book/commitdiff
Fix some transposition handling, and add a checkbox to not include them anymore.
authorSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 14 Dec 2014 22:32:07 +0000 (23:32 +0100)
committerSteinar H. Gunderson <sgunderson@bigfoot.com>
Sun, 14 Dec 2014 22:32:07 +0000 (23:32 +0100)
binlookup.cpp
opening-stats.pl
www/book.html
www/js/book.js
www/opening-stats.pl

index 19d03938a39bc2e1f952fbed437ccc6ae0a65dde..d442e4295dbd824f34c0c35c278f4becc02cb960 100644 (file)
@@ -54,8 +54,10 @@ int main(int argc, char **argv)
                if (bpfen.empty()) {
                        break;
                }
+               string prev_pos_hash = read_hex_line(stdin);
 
-               int bucket = hash_key_to_bucket(bpfen.data(), bpfen.size(), num_buckets);
+               string searchkey = bpfen + prev_pos_hash;
+               int bucket = hash_key_to_bucket(searchkey.data(), searchkey.size(), num_buckets);
                if (mtbls[bucket] == NULL) {
                        char filename[256];
                        snprintf(filename, sizeof(filename), "%s.part%04d", argv[1], bucket);
@@ -68,7 +70,7 @@ int main(int argc, char **argv)
                uint16_t board_hash = util::Hash32(bpfen.data(), bpfen.size());
                printf("%d\n", board_hash);
 
-               mtbl_iter *it = mtbl_source_get_prefix(srcs[bucket], (const uint8_t *)bpfen.data(), bpfen.size());
+               mtbl_iter *it = mtbl_source_get_prefix(srcs[bucket], (const uint8_t *)searchkey.data(), searchkey.size());
 
                const uint8_t *key, *val;
                size_t len_key, len_val;
index 24fdc99c1e31459d46c468df1f4f9469ba96af56..581a0e26baa43f23f0a2c058e6ba88bb2561d5a4 100755 (executable)
@@ -9,18 +9,20 @@ use IPC::Open2;
 
 my $cgi = CGI->new;
 my $fen = $ARGV[0];
+my $filter_prev_pos_hash = $ARGV[1] // 0;
 my ($chld_out, $chld_in);
 my $pid = IPC::Open2::open2($chld_out, $chld_in, "./binlookup", "./open.mtbl", "40");
 
 # Root position.
 my $pos = Position->from_fen($fen);
-my $hex = unpack('H*', $pos->bitpacked_fen);
-print $chld_in $hex, "\n";
+my $bpfen_hex = unpack('H*', $pos->bitpacked_fen);
+my $prev_pos_hash_hex = unpack('H*', pack('S', $filter_prev_pos_hash));
+print $chld_in $bpfen_hex, "\n", $prev_pos_hash_hex, "\n";
 
 chomp (my $line = <$chld_out>);  # Root position hash.
 print $line, "\n";
 
-chomp (my $line = <$chld_out>);  # Actual stats.
+chomp ($line = <$chld_out>);  # Actual stats.
 print $line, "\n";
 my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo, $timestamp, $pgn_file_number, $pgn_start_position, @moves) = split / /, $line;
 
@@ -28,7 +30,7 @@ my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_el
 for my $move (@moves) {
        my ($np, $uci_move) = $pos->make_pretty_move($move);
        my $hex = unpack('H*', $np->bitpacked_fen);
-       print $chld_in $hex, "\n";
+       print $chld_in $hex, "\n\n";
        my $line = <$chld_out>;  # Ignore position hash.
        $line = <$chld_out>;
        print "$move $line";
index cb5a07fe62444ccbcb980a68304641903eafe95e..50fd70ef6a85e104bd4d8105204a2941007d4995 100644 (file)
   </div>
 </div>
 <div id="analysis">
+  <p>
+    <label>
+      <input type="checkbox" name="includetransp" checked="checked" onclick="javascript:set_includetransp(this.checked)" />Include transpositions
+    </label>
+  </p>
   <table>
     <thead>
       <tr id="headings">
index 78dfed815cf8c3c782266ab5e47cf377383a58a0..6017fd9d0d5eb6a91a1238089c080a7af460c819 100644 (file)
@@ -3,6 +3,7 @@
 var board = null;
 var history = [];
 var move_override = 0;
+var includetransp = true;
 
 var entity_map = {
        "&": "&amp;",
@@ -18,9 +19,14 @@ function escape_html(string) {
        });
 }
 
-var get_game = function() {
+var get_game = function(skip_last_move) {
+       var moves_to_drop = 0;
+       if (skip_last_move === true) {
+               moves_to_drop = 1;
+       }
+
        var game = new Chess();
-       for (var i = 0; i < move_override; ++i) {
+       for (var i = 0; i < move_override - moves_to_drop; ++i) {
                game.move(history[i]);
        }
        return game;
@@ -59,8 +65,15 @@ var update = function() {
 
 var fetch_analysis = function() {
        var game = get_game();
+       var fen = game.fen();
+       var prevfen = "";
+       if (move_override > 0) {
+               prevfen = get_game(true).fen();
+       }
        $.ajax({
-               url: "/opening-stats.pl?fen=" + encodeURIComponent(game.fen())
+               url: "/opening-stats.pl?fen=" + encodeURIComponent(fen) +
+                       ";prevfen=" + encodeURIComponent(prevfen) +
+                       ";includetransp=" + (includetransp ? 1 : 0)
        }).done(function(data, textstatus, xhr) {
                show_lines(data, game);
        });
@@ -240,6 +253,12 @@ var show_lines = function(data, game) {
        }
 }
 
+var set_includetransp = function(value) {
+       includetransp = value;
+       update();
+}
+window['set_includetransp'] = set_includetransp;
+
 var make_move = function(move) {
        if (move_override < history.length && history[move_override] == move) {
                // User effectively only moved forward in history.
@@ -303,7 +322,6 @@ var onDrop = function(source, target) {
                history.push(new_history[i].san);
        }
        move_override = history.length;
-       update();
 };
 
 // update the board position after the piece snap 
@@ -311,7 +329,7 @@ var onDrop = function(source, target) {
 var onSnapEnd = function() {
        var game = get_game();
        board.position(game.fen());
-       fetch_analysis();
+       update();
 };
 
 var init = function() {
index 28dd4436f69219e09d63d02451cc5f17073cde51..8e490c17d87f9441c9939a950a7f20186aad0de4 100755 (executable)
@@ -17,12 +17,24 @@ my $pid = IPC::Open2::open2($chld_out, $chld_in, "../binlookup", "../open.mtbl",
 
 # Root position. Basically ignore everything except the opening (and later some root game stuff).
 my $fen = $cgi->param('fen') // 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
+my $prevfen = $cgi->param('prevfen') // '';
 my $includetransp = $cgi->param('includetransp') // 1;
 
 my $pos = Position->from_fen($fen);
-my ($json_root_pos, $root_aux_data) = get_json_move($pos, undef, $chld_in, $chld_out);  # TODO: include previous hash if $includetransp == 0
-my $opening = $openings{$json_root_pos->{'opening_num'}} // 'A00: Start position';
+my ($json_root_pos, $root_aux_data);
+if ($includetransp) {
+       ($json_root_pos, $root_aux_data) = get_json_move($pos, undef, $chld_in, $chld_out);
+} else {
+       my $prev_pos_hash = 0;
+       if ($prevfen ne '') {
+               my $prevpos = Position->from_fen($prevfen);
+               my (undef, $prev_aux_data) = get_json_move($prevpos, undef, $chld_in, $chld_out);
+               $prev_pos_hash = $prev_aux_data->{'pos_hash'};
+       }
+       ($json_root_pos, $root_aux_data) = get_json_move($pos, $prev_pos_hash, $chld_in, $chld_out);
+}
 
+my $opening = $openings{$json_root_pos->{'opening_num'}} // 'A00: Start position';
 my @json_moves = ($json_root_pos);
 
 my $root_game;
@@ -111,17 +123,19 @@ sub read_root_pgn {
 
 sub get_json_move {
        my ($pos, $filter_prev_pos_hash, $chld_in, $chld_out) = @_;
-       my $hex = unpack('H*', $pos->bitpacked_fen);
+       my $bpfen_hex = unpack('H*', $pos->bitpacked_fen);
+       my $prev_pos_hash_hex = '';
        if (defined($filter_prev_pos_hash)) {
-               $hex .= unpack('H*', pack('S', $filter_prev_pos_hash));
+               $prev_pos_hash_hex .= unpack('H*', pack('S', $filter_prev_pos_hash));
        }
-       print $chld_in $hex, "\n";
+       print $chld_in $bpfen_hex, "\n", $prev_pos_hash_hex, "\n";
 
        # Read the hash of this position.
        chomp (my $pos_hash = <$chld_out>);
 
        chomp (my $line = <$chld_out>);
        if ($line eq '-') {
+               warn "Missing pos '" . $pos->fen . "' " . $filter_prev_pos_hash;
                return ({}, {});
        }