14 my ($chld_out, $chld_in);
15 my $pid = IPC::Open2::open2($chld_out, $chld_in, "../binlookup", "../open.mtbl", "40");
17 # Root position. Basically ignore everything except the opening (and later some root game stuff).
18 my $fen = $cgi->param('fen') // 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1';
19 my $pos = Position->from_fen($fen);
20 my $hex = unpack('H*', $pos->bitpacked_fen);
21 print $chld_in $hex, "\n";
22 chomp (my $line = <$chld_out>);
24 my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo, $timestamp, $pgn_file_number, $pgn_start_position, @moves) = split / /, $line;
25 my $opening = $openings{$opening_num} // 'A00: Start position';
27 # Explore one move out.
29 for my $move (@moves) {
30 my ($np, $uci_move) = $pos->make_pretty_move($move);
31 my $hex = unpack('H*', $np->bitpacked_fen);
32 print $chld_in $hex, "\n";
33 my $line = <$chld_out>;
34 my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo) = split / /, $line;
40 white_avg_elo => $white_avg_elo * 1,
41 black_avg_elo => $black_avg_elo * 1,
42 num_elo => $num_elo * 1
46 print $cgi->header(-type=>'application/json');
47 print JSON::XS::encode_json({ moves => \@json_moves, opening => $opening });
51 return $x->{'white'} + $x->{'draw'} + $x->{'black'};
55 open my $fh, "../openings.txt"
56 or die "../openings.txt: $!";
57 for my $line (<$fh>) {
59 my ($hash, $eco, $opening, $variation, $subvariation) = split /\t/, $line;
60 if ($variation eq '') {
61 $openings{$hash} = $eco . ": " . $opening;
63 $openings{$hash} = $eco . ": " . $opening . ": " . $variation;