]> git.sesse.net Git - remoteglot/blob - www/opening-stats.pl
Clean up module usage in parse-pgn.pl.
[remoteglot] / www / 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 ECO;
9
10 ECO::unpersist("../book/openings.txt");
11
12 my $cgi = CGI->new;
13 my $fen = $cgi->param('fen');
14 my $pos = Position->from_fen($fen);
15 my $hex = unpack('H*', $pos->bitpacked_fen);
16 open my $fh, "-|", "../book/binlookup", "../book/open.mtbl", $hex
17         or die "../book/binlookup: $!";
18
19 my $opening;
20
21 my @moves = ();
22 while (<$fh>) {
23         chomp;
24         my ($move, $white, $draw, $black, $opening_num, $white_avg_elo, $black_avg_elo, $num_elo) = split;
25         push @moves, {
26                 move => $move,
27                 white => $white * 1,
28                 draw => $draw * 1,
29                 black => $black * 1,
30                 white_avg_elo => $white_avg_elo * 1,
31                 black_avg_elo => $black_avg_elo * 1,
32                 num_elo => $num_elo * 1
33         };
34         $opening = $ECO::openings[$opening_num];
35 }
36 close $fh;
37
38 @moves = sort { num($b) <=> num($a) } @moves;
39
40 print $cgi->header(-type=>'application/json');
41 print JSON::XS::encode_json({ moves => \@moves, opening => $opening });
42
43 sub num {
44         my $x = shift;
45         return $x->{'white'} + $x->{'draw'} + $x->{'black'};
46 }