]> git.sesse.net Git - remoteglot-book/blobdiff - www/opening-stats.pl
Show a game summary instead of the entire PGN.
[remoteglot-book] / www / opening-stats.pl
index 076a6a3636ae0498b0af861c121de4ed5f3941de..adf138f0a80dbb63a2cab9f533156280e33aa019 100755 (executable)
@@ -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;