]> git.sesse.net Git - remoteglot-book/blobdiff - www/opening-stats.pl
Indent fix.
[remoteglot-book] / www / opening-stats.pl
index ad578a0b7db95747cfcba67b80bc81ede6eca190..58a0f9be02a9ed230808fb00ce281abeef3531e9 100755 (executable)
@@ -48,28 +48,54 @@ eval {
        $root_game->{'eco'} = $pgn->eco;
        $root_game->{'moves'} = int(((scalar @{$pgn->moves}) + 1) / 2);
 };
+if ($@) {
+       print STDERR "Error while getting root move: $@\n";
+}
 
 # 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.
-               my ($alt_json_pos, undef) = get_json_move($np, $root_aux_data->{'pos_hash'}, $chld_in, $chld_out);
-               if (!defined($alt_json_pos)) {
+               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;
+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;
@@ -105,6 +131,7 @@ sub read_root_pgn {
                or die "../pgnnames.txt: $!";
        while (<$pgnnamesfh>) {
                chomp;
+               s/^comp://;
                push @pgnnames, $_;
        }
        close $pgnnamesfh;
@@ -147,14 +174,14 @@ sub get_json_move {
 
        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 => int($white),
+               draw => int($draw),
+               black => int($black),
+               computer => int($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,
-               opening_num => $opening_num,
+               num_elo => int($num_elo),
+               opening_num => $opening_num,  # Keep as string.
        };
        my $aux_data = {  # Only relevant for the root.
                pos_hash => $pos_hash * 1,