#!/usr/bin/perl -w
use strict;
use Getopt::Std;
my $logfile = '/usr/games/lib/nethackdir/logfile';
my %opts; getopts('prgau', \%opts);
%opts = qw(p 1 r 1 g 1 a 1 u 1) if !%opts;
my(%roles, %races, %genders, %aligns, %names);
# Each hash value is an array consisting of the number of games, the total
# score, the total maximum level, & the total score per level.
sub statsLine {
my($label, $qty, $scores, $lvls, $per) = @_;
printf "$label: $qty games, %.6g pts, lvl %.6g, %.6g pts/lvl\n", $scores/$qty,
$lvls/$qty, $per/$qty;
}
open my $log, '<', $logfile or die "Error: couldn't read logfile: $!\n";
while (<$log>) {
my @fields = split / /, $_, 16; # Assume that the logfile is valid.
@fields[15,16] = split /,/, $fields[15], 2;
foreach ($roles{$fields[11]}, $races{$fields[12]}, $genders{$fields[13]}, $aligns{$fields[14]}, $names{$fields[15]}) {
$_->[0]++;
$_->[1] += $fields[1];
$_->[2] += $fields[4];
$_->[3] += $fields[1] / $fields[4];
}
}
if ($opts{p}) {
print "Role statistics:\n";
statsLine $_, @{$roles{$_}} foreach sort keys %roles;
print "\n";
}
if ($opts{r}) {
print "Race statistics:\n";
statsLine $_, @{$races{$_}} foreach sort keys %races;
print "\n";
}
if ($opts{g}) {
print "Gender statistics:\n";
statsLine $_, @{$genders{$_}} foreach sort keys %genders;
print "\n";
}
if ($opts{a}) {
print "Alignment statistics:\n";
statsLine $_, @{$aligns{$_}} foreach sort keys %aligns;
print "\n";
}
if ($opts{u}) {
print "Name statistics:\n";
statsLine $_, @{$names{$_}} foreach sort keys %names;
print "\n";
}