]> git.sesse.net Git - remoteglot-book/blob - opening-stats.pl
581a0e26baa43f23f0a2c058e6ba88bb2561d5a4
[remoteglot-book] / opening-stats.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 use CGI;
5 use JSON::XS;
6 use lib '..';
7 use Position;
8 use IPC::Open2;
9
10 my $cgi = CGI->new;
11 my $fen = $ARGV[0];
12 my $filter_prev_pos_hash = $ARGV[1] // 0;
13 my ($chld_out, $chld_in);
14 my $pid = IPC::Open2::open2($chld_out, $chld_in, "./binlookup", "./open.mtbl", "40");
15
16 # Root position.
17 my $pos = Position->from_fen($fen);
18 my $bpfen_hex = unpack('H*', $pos->bitpacked_fen);
19 my $prev_pos_hash_hex = unpack('H*', pack('S', $filter_prev_pos_hash));
20 print $chld_in $bpfen_hex, "\n", $prev_pos_hash_hex, "\n";
21
22 chomp (my $line = <$chld_out>);  # Root position hash.
23 print $line, "\n";
24
25 chomp ($line = <$chld_out>);  # Actual stats.
26 print $line, "\n";
27 my ($white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo, $timestamp, $pgn_file_number, $pgn_start_position, @moves) = split / /, $line;
28
29 # Explore one move out.
30 for my $move (@moves) {
31         my ($np, $uci_move) = $pos->make_pretty_move($move);
32         my $hex = unpack('H*', $np->bitpacked_fen);
33         print $chld_in $hex, "\n\n";
34         my $line = <$chld_out>;  # Ignore position hash.
35         $line = <$chld_out>;
36         print "$move $line";
37 }