]> git.sesse.net Git - remoteglot-book/blobdiff - www/opening-stats.pl
Fix some transposition handling, and add a checkbox to not include them anymore.
[remoteglot-book] / www / opening-stats.pl
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 ({}, {});
        }