X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=www%2Fopening-stats.pl;h=0da6e0546f54e1b20382c0d39787cd9292c35df0;hb=74f1261e4067cbc1b743b329ed2d419aee735cbf;hp=8e490c17d87f9441c9939a950a7f20186aad0de4;hpb=1067590eb03c8571f6e4aafc215b71bcb7a71001;p=remoteglot-book diff --git a/www/opening-stats.pl b/www/opening-stats.pl index 8e490c1..0da6e05 100755 --- a/www/opening-stats.pl +++ b/www/opening-stats.pl @@ -17,22 +17,10 @@ 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); -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 ($json_root_pos, $root_aux_data) = get_json_move($pos, undef, $chld_in, $chld_out); my $opening = $openings{$json_root_pos->{'opening_num'}} // 'A00: Start position'; my @json_moves = ($json_root_pos); @@ -62,20 +50,61 @@ eval { }; # Explore one move out. +my $white_left = $json_root_pos->{'white'}; +my $draw_left = $json_root_pos->{'draw'}; +my $black_left = $json_root_pos->{'black'}; for my $move (@{$root_aux_data->{'moves'}}) { my ($np, $uci_move) = $pos->make_pretty_move($move); my $json_pos; + + my ($json_pos_only_this_root, undef) = get_json_move($np, $root_aux_data->{'pos_hash'}, $chld_in, $chld_out); + $white_left -= $json_pos_only_this_root->{'white'}; + $draw_left -= $json_pos_only_this_root->{'draw'}; + $black_left -= $json_pos_only_this_root->{'black'}; + if ($includetransp) { ($json_pos, undef) = get_json_move($np, undef, $chld_in, $chld_out); + + # See if this move exists only due to transpositions. + if (!defined($json_pos_only_this_root)) { + $json_pos->{'transpose_only'} = 1; + } } else { - ($json_pos, undef) = get_json_move($np, $root_aux_data->{'pos_hash'}, $chld_in, $chld_out); + $json_pos = $json_pos_only_this_root; } $json_pos->{'move'} = $move; push @json_moves, $json_pos; } +# If there are any positions that are not accounted for by any moves, +# these have to be games that end here. Add them as pseudo-moves so as +# not to confuse the user. +for my $result (['1-0', 'white', $white_left], ['1/2-1/2', 'draw', $draw_left], ['0-1', 'black', $black_left]) { + next if ($result->[2] == 0); + my $move = { + move => $result->[0], + white => 0, + draw => 0, + black => 0 + }; + $move->{$result->[1]} = $result->[2]; + push @json_moves, $move; +} + +# Get stats for the root position, for the human index. +my $start_pos = Position->start_pos("white", "black"); +my ($json_start_pos, undef) = get_json_move($start_pos, 0, $chld_in, $chld_out); +my $total_games = $json_start_pos->{'white'} + $json_start_pos->{'draw'} + $json_start_pos->{'black'}; +my $computer_games = $json_start_pos->{'computer'} * 1; + print $cgi->header(-type=>'application/json'); -print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening, root_game => $root_game }); +print JSON::XS::encode_json({ + moves => \@json_moves, + opening => $opening, + root_game => $root_game, + total_games => $total_games, + computer_games => $computer_games +}); sub read_openings { open my $fh, "../openings.txt" @@ -136,14 +165,15 @@ sub get_json_move { chomp (my $line = <$chld_out>); if ($line eq '-') { warn "Missing pos '" . $pos->fen . "' " . $filter_prev_pos_hash; - return ({}, {}); + 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 ($white, $draw, $black, $computer, $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, + computer => $computer, 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,