X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=www%2Fopening-stats.pl;h=d3034359040ddd40296746a0e2a34e061948cd16;hb=e9b5b1e5b285542f44f0c52ee0b470b22ac7381b;hp=9b68e918540cb047110ae14bd7a12d3e54fbe4e6;hpb=f7c995499d630b90cf8ebaeae6ace9d8eccf676c;p=remoteglot-book diff --git a/www/opening-stats.pl b/www/opening-stats.pl index 9b68e91..d303435 100755 --- a/www/opening-stats.pl +++ b/www/opening-stats.pl @@ -5,24 +5,34 @@ use CGI; use JSON::XS; use lib '..'; use Position; +use IPC::Open2; our %openings = (); read_openings(); my $cgi = CGI->new; -my $fen = $cgi->param('fen'); +my ($chld_out, $chld_in); +my $pid = IPC::Open2::open2($chld_out, $chld_in, "../binlookup", "../open.mtbl", "40"); + +# 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 $pos = Position->from_fen($fen); my $hex = unpack('H*', $pos->bitpacked_fen); -open my $fh, "-|", "../binlookup", "../open.mtbl", $hex - or die "../binlookup: $!"; +print $chld_in $hex, "\n"; +chomp (my $line = <$chld_out>); -my $opening; +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 @moves = (); -while (<$fh>) { - chomp; - my ($move, $white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo) = split; - push @moves, { +# Explore one move out. +my @json_moves = (); +for my $move (@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, @@ -31,14 +41,10 @@ while (<$fh>) { black_avg_elo => $black_avg_elo * 1, num_elo => $num_elo * 1 }; - $opening = $openings{$opening_num} // 'A00: Start position'; } -close $fh; - -@moves = sort { num($b) <=> num($a) } @moves; print $cgi->header(-type=>'application/json'); -print JSON::XS::encode_json({ moves => \@moves, opening => $opening }); +print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening }); sub num { my $x = shift;