#!/usr/bin/perl # Given the list of game numbers, generate a table of win/lose # along with score # You will already have these modules if you are using # ActiveState perl on Windows. # If you dont have these perl modules you can get them from # http://www.cpan.org/modules/by-module/LWP/ # http://www.cpan.org/modules/by-module/HTTP/ use LWP; use LWP::Simple; use HTTP::Request::Common; $fn = $ARGV[0]; if ($fn eq ""){ die "You did not provide a file with the game numbers\n"; } # read in the file with the game numbers open(FH, "<$fn"); $gn = join('', ); close FH; # keep only the numbers in the gn array $gn =~ s/\D/ /gs; @gn = split(/ +/, $gn); # get the data for the games from the arimaa.com site foreach $gn (@gn){ my(%gd); $p{gid} = $gn; %gd = doPost("http://arimaa.com/arimaa/games/pgn.cgi", %p); # so that we don't hit the server with too many requests at once sleep 1; push(@g, \%gd); } ###################################### # we are now ready to build the table # generate list of players and their win loss data undef %p; foreach $g (@g){ $p{$g->{wplayer}} = 1; $p{$g->{bplayer}} = 1; if ($g->{result} eq "w"){ $r{"$g->{wplayer}-$g->{bplayer}"} .= "1"; $r{"$g->{bplayer}-$g->{wplayer}"} .= "0"; $win{$g->{wplayer}} += 1; $tie{$g->{wplayer}} -= $g->{plycount}; $tie{$g->{bplayer}} += $g->{plycount}; } if ($g->{result} eq "b"){ $r{"$g->{wplayer}-$g->{bplayer}"} .= "0"; $r{"$g->{bplayer}-$g->{wplayer}"} .= "1"; $win{$g->{bplayer}} += 1; $tie{$g->{bplayer}} -= $g->{plycount}; $tie{$g->{wplayer}} += $g->{plycount}; } if ($g->{result} eq "d"){ $r{"$g->{wplayer}-$g->{bplayer}"} .= "="; $r{"$g->{bplayer}-$g->{wplayer}"} .= "="; $win{$g->{wplayer}} += 0.5; $win{$g->{bplayer}} += 0.5; } if ($g->{result} eq "a"){ $r{"$g->{wplayer}-$g->{bplayer}"} .= "-"; $r{"$g->{bplayer}-$g->{wplayer}"} .= "-"; } $tot{$g->{wplayer}} += 1; $tot{$g->{bplayer}} += 1; } while (($n, $x) = each(%p)){ if ($n eq ""){ next; } if (length($n)>$ln){ $ln = length($n); } push(@p, $n); } ######################################## # print the table print substr(" ", 0, $ln+2); foreach $s (@p){ $p = $s; $p =~ s/^bot_/b_/i; $p = substr($p, 0, 4); $p = substr("$p ", 0, 4); $p = ucfirst($p); print "$p "; } print " Score Tiebreak\n"; foreach $f (@p){ $fx = substr("$f ", 0, $ln+2); $fx = ucfirst($fx); print "$fx"; foreach $s (@p){ $res = $r{"$f-$s"} . "xxxx"; ($res) = ($res =~ m/(....)/); print "$res "; } if ($win{$f} eq ""){ $win{$f} = 0; } if ($tot{$f} eq ""){ $tot{$f} = 0; } print substr(" $win{$f}/$tot{$f} ",0,9) . "$tie{$f}"; print "\n"; } print "\n"; exit; sub doPost{ my($u, %p) = @_; my($ua, $res, %res); my($k, $v); # Note that the request will timeout after 3 minutes # if a response is not received # http://perldoc.com/perl5.8.0/lib/LWP/UserAgent.html # See the timeout parameter in the methods section of the above URL # wait (set to 0 or 1) and maxwait (set to number of seconds) # are preset by the caller if ($p{wait} > 0){ # We set the client to wait for about 20 sec longer than what # we tell the server we are going to be wait for $ua = LWP::UserAgent->new(timeout => $p{maxwait} + 20); } else{ $ua = LWP::UserAgent->new; } $res = $ua->request(POST "$u", \%p); # print $res->as_string; # print $res->content(); %res = str2hash($res->content()); return %res; } sub str2hash{ my($s) = @_; my(%r); ($r{moves}) = ($s =~ m/(\n1w[\s\S]+)/s); ($r{gid}) = ($s =~ m/GameId: (\d+)/s); ($r{wplayer}) = ($s =~ m/White: (\w+)/s); ($r{bplayer}) = ($s =~ m/Black: (\w+)/s); ($r{result}) = ($s =~ m/ResultCode: (\w)/s); ($r{reason}) = ($s =~ m/ReasonCode: (\w)/s); ($r{plycount}) = ($s =~ m/PlyCount: (\d+)/s); return %r; }