screp: parse player data
authorShiar <shiar@shiar.org>
Mon, 12 Nov 2007 20:03:52 +0000 (21:03 +0100)
committerShiar <shiar@shiar.org>
Mon, 12 Nov 2007 20:03:52 +0000 (21:03 +0100)
Shows players by name and race.

screp

diff --git a/screp b/screp
index 7bf178fa6e8d43708fb63cd696387aaf24e6cf4e..910f3d2f5ad4bc465805883e2b9589d1f641aa65 100755 (executable)
--- a/screp
+++ b/screp
@@ -414,6 +414,8 @@ sub open {
 
 }
 
+my @race = (qw(Z T P), (undef) x 3, '-');
+
 sub showtime {
        my $time = shift() * .042;
        my $minutes = int($time / 60);
@@ -445,14 +447,32 @@ $_ eq "\10"x8 . "\0"x4 or warn sprintf(
 ) for $head->{mag2};
 delete $head->{$_} for qw(mag1 mag2 unknown1 unknown2);
 
-my @playdata = unpack "a36"x12 . "V8C8", $headdata[0]
+my @playdata = unpack "Va32"x12 . "V8C8", $headdata[0]
        or die "Couldn't parse player data in replay header\n";
 
-my @player;
-push @player, unpackhash("x11Z25", shift @playdata, qw/name/) for 0 .. 11;
+my (@player, @slot);
+for (0 .. 11) {
+       my $number = shift @playdata;
+       defined $player[$number] and warn "Player #$number redefined";
+       my ($data) = unpackhash("VcccZ25", shift @playdata, qw(
+               slot type race unknown name
+       ));
+       defined $race[$_] ? ($data->{race} = $race[$_]) :
+               warn "Unknown race #$_ for player $number"
+               for $data->{race};
+       $slot[$data->{slot}] = $number if $data->{slot} < 16;
+       $player[$number] = $data;
+}
 $player[$_]->{color} = shift @playdata for 0 .. 7;
 $player[$_]->{index} = shift @playdata for 0 .. 7;
 
+sub showplayer {
+       my $id = shift;
+       my $playdata = $player[$slot[$id]];
+       return defined $playdata ?
+               sprintf '%s (%s)', $playdata->{name}, $playdata->{race} : "#$id";
+}
+
 printf "%s: %s\n", $_, $head->{$_} for qw(name creator);
 use Date::Format;
 printf "created: %s\n", time2str('%Y-%m-%d %X', $_) for $head->{time};
@@ -498,9 +518,10 @@ for (@$map) {
 for my $player (sort keys %stats) {
        my $row = $stats{$player};
        $row->{last} ||= $map->[-1][0];
-#      printf("%d:%6d actions (%3d micro,%4d macro);%4d APM\n",
-       printf("%d:%6d actions in%7d frames (%s) = %d APM\n",
-               $player, $row->{actions}, $row->{last},
+#      printf("%-16s%6d actions (%3d micro,%4d macro);%4d APM\n",
+       my $name = showplayer($player);
+       printf("%-16s%6d actions in%7d frames (%s) = %d APM\n",
+               $name, $row->{actions}, $row->{last},
                showtime($row->{last}),
 #              $row->{micro} / $row->{last} * 60 / .042 * 1.05,
 #              $row->{macro} / $row->{last} * 60 / .042 * 1.05,