X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=www%2Fopening-stats.pl;h=3fb8946f3122fcf10fab9f73519bd801ee8ca10f;hb=f3225963af3ef8bf33b49d5b51a37d36f47e1482;hp=aea6b2d08aa984f2d4782e1817279e04acb5ec5a;hpb=bb5c5ff3563fa593b4c014f5e619c6cef855f371;p=remoteglot-book diff --git a/www/opening-stats.pl b/www/opening-stats.pl index aea6b2d..3fb8946 100755 --- a/www/opening-stats.pl +++ b/www/opening-stats.pl @@ -6,6 +6,7 @@ use JSON::XS; use lib '..'; use Position; use IPC::Open2; +use Chess::PGN::Parse; our %openings = (); read_openings(); @@ -16,48 +17,59 @@ 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 $includetransp = $cgi->param('includetransp') // 1; + my $pos = Position->from_fen($fen); -my $hex = unpack('H*', $pos->bitpacked_fen); -print $chld_in $hex, "\n"; -chomp (my $line = <$chld_out>); +my ($json_root_pos, $root_aux_data) = get_json_move($pos, undef, $chld_in, $chld_out); -my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo, $timestamp, $pgn_file_number, $pgn_start_position, @moves) = split / /, $line; -my $opening = $openings{$opening_num} // 'A00: Start position'; +my $opening = $openings{$json_root_pos->{'opening_num'}} // 'A00: Start position'; +my @json_moves = ($json_root_pos); -my $root_pgn; +my $root_game; eval { - $root_pgn = read_root_pgn($pgn_file_number, $pgn_start_position); + if (!exists($root_aux_data->{'pgn_file_number'}) || + !exists($root_aux_data->{'pgn_start_position'})) { + die "Missing PGN position data." + } + my $pgntext = read_root_pgn($root_aux_data->{'pgn_file_number'}, $root_aux_data->{'pgn_start_position'}); + my $pgn = Chess::PGN::Parse->new(undef, $pgntext); + $pgn->read_game() or die; + $pgn->parse_game() or die; + + my $tags = $pgn->tags; + $root_game = {}; + $root_game->{'white'} = $pgn->white; + $root_game->{'white_elo'} = $tags->{'WhiteElo'}; + $root_game->{'black'} = $pgn->black; + $root_game->{'black_elo'} = $tags->{'BlackElo'}; + $root_game->{'event'} = $pgn->event; + $root_game->{'date'} = $pgn->date; + $root_game->{'result'} = $pgn->result; + $root_game->{'eco'} = $pgn->eco; + $root_game->{'moves'} = int(((scalar @{$pgn->moves}) + 1) / 2); }; -if ($@) { - $root_pgn = "Could not find root PGN. ($@)"; -} # Explore one move out. -my @json_moves = (); -for my $move (@moves) { +for my $move (@{$root_aux_data->{'moves'}}) { my ($np, $uci_move) = $pos->make_pretty_move($move); - my $hex = unpack('H*', $np->bitpacked_fen); - print $chld_in $hex, "\n"; - my $line = <$chld_out>; - my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo) = split / /, $line; - push @json_moves, { - move => $move, - white => $white * 1, - draw => $draw * 1, - black => $black * 1, - white_avg_elo => $white_avg_elo * 1, - black_avg_elo => $black_avg_elo * 1, - num_elo => $num_elo * 1 - }; + my $json_pos; + if ($includetransp) { + ($json_pos, undef) = get_json_move($np, undef, $chld_in, $chld_out); + + # See if this move exists only due to transpositions. + my ($alt_json_pos, undef) = get_json_move($np, $root_aux_data->{'pos_hash'}, $chld_in, $chld_out); + if (!defined($alt_json_pos)) { + $json_pos->{'transpose_only'} = 1; + } + } else { + ($json_pos, undef) = get_json_move($np, $root_aux_data->{'pos_hash'}, $chld_in, $chld_out); + } + $json_pos->{'move'} = $move; + push @json_moves, $json_pos; } print $cgi->header(-type=>'application/json'); -print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening, root_pgn => $root_pgn }); - -sub num { - my $x = shift; - return $x->{'white'} + $x->{'draw'} + $x->{'black'}; -} +print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening, root_game => $root_game }); sub read_openings { open my $fh, "../openings.txt" @@ -75,6 +87,7 @@ sub read_openings { } sub read_root_pgn { + my ($pgn_file_number, $pgn_start_position) = @_; my @pgnnames; open my $pgnnamesfh, "<", "../pgnnames.txt" or die "../pgnnames.txt: $!"; @@ -101,3 +114,40 @@ sub read_root_pgn { return $root_pgn; } + +sub get_json_move { + my ($pos, $filter_prev_pos_hash, $chld_in, $chld_out) = @_; + my $bpfen_hex = unpack('H*', $pos->bitpacked_fen); + my $prev_pos_hash_hex = ''; + if (defined($filter_prev_pos_hash)) { + $prev_pos_hash_hex .= unpack('H*', pack('S', $filter_prev_pos_hash)); + } + 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 (undef, undef); + } + + my ($white, $draw, $black, $opening_num, $white_sum_elo, $black_sum_elo, $num_elo, $timestamp, $pgn_file_number, $pgn_start_position, @moves) = split / /, $line; + my $json_pos = { + white => $white, + draw => $draw, + black => $black, + white_avg_elo => $num_elo == 0 ? undef : $white_sum_elo / $num_elo, + black_avg_elo => $num_elo == 0 ? undef : $black_sum_elo / $num_elo, + num_elo => $num_elo, + opening_num => $opening_num, + }; + my $aux_data = { # Only relevant for the root. + pos_hash => $pos_hash * 1, + moves => \@moves, + pgn_file_number => $pgn_file_number, + pgn_start_position => $pgn_start_position, + }; + return ($json_pos, $aux_data); +}