X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=www%2Fopening-stats.pl;h=adf138f0a80dbb63a2cab9f533156280e33aa019;hb=7948ff31ce0c53b85cccb9c851dbbf36d46f9c0c;hp=076a6a3636ae0498b0af861c121de4ed5f3941de;hpb=95c49ee7d384bef444ee30aad95c7f637bed0e10;p=remoteglot-book diff --git a/www/opening-stats.pl b/www/opening-stats.pl index 076a6a3..adf138f 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(); @@ -24,14 +25,26 @@ chomp (my $line = <$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 $root_pgn; +my $root_game; eval { die "Missing PGN position data." if (!defined($pgn_file_number) || !defined($pgn_start_position)); - $root_pgn = read_root_pgn($pgn_file_number, $pgn_start_position); + my $pgntext = read_root_pgn($pgn_file_number, $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'} = scalar @{$pgn->moves}; }; -if ($@) { - $root_pgn = "Could not find root PGN. ($@)"; -} # Explore one move out. my @json_moves = (); @@ -53,7 +66,7 @@ for my $move (@moves) { } print $cgi->header(-type=>'application/json'); -print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening, root_pgn => $root_pgn }); +print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening, root_game => $root_game }); sub num { my $x = shift;