use lib '..';
use Position;
use IPC::Open2;
+use Chess::PGN::Parse;
our %openings = ();
read_openings();
# 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); # TODO: include previous hash if $includetransp == 0
+my $opening = $openings{$json_root_pos->{'opening_num'}} // 'A00: Start position';
-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 @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);
+ } 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"
}
sub read_root_pgn {
+ my ($pgn_file_number, $pgn_start_position) = @_;
my @pgnnames;
open my $pgnnamesfh, "<", "../pgnnames.txt"
or die "../pgnnames.txt: $!";
while (<$pgnnamesfh>) {
chomp;
push @pgnnames, $_;
- warn "PGN name: $_";
}
close $pgnnamesfh;
return $root_pgn;
}
+
+sub get_json_move {
+ my ($pos, $filter_prev_pos_hash, $chld_in, $chld_out) = @_;
+ my $hex = unpack('H*', $pos->bitpacked_fen);
+ if (defined($filter_prev_pos_hash)) {
+ $hex .= unpack('H*', pack('S', $filter_prev_pos_hash));
+ }
+ print $chld_in $hex, "\n";
+
+ # Read the hash of this position.
+ chomp (my $pos_hash = <$chld_out>);
+
+ chomp (my $line = <$chld_out>);
+ if ($line eq '-') {
+ return ({}, {});
+ }
+
+ 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);
+}