]> icculus.org git repositories - divverent/nexuiz.git/blob - misc/tools/WeaponEncounterProfile.pm
weapon profiler
[divverent/nexuiz.git] / misc / tools / WeaponEncounterProfile.pm
1 #!/usr/bin/perl
2
3 package WeaponEncounterProfile;
4 use strict;
5 use warnings;
6
7 sub new
8 {
9         my ($cls, $filename) = @_;
10         my $self = bless { fn => $filename }, 'WeaponEncounterProfile';
11         $self->load();
12         return $self;
13 }
14
15 sub load($)
16 {
17         my ($self) = @_;
18         $self->{stats} = {};
19         $self->{mapstats} = {};
20         $self->{addrstats} = {};
21         $self->{allstats} = {};
22         open my $fh, "<", $self->{fn}
23                 or return;
24         while(<$fh>)
25         {
26                 chomp;
27                 my ($addr, $map, $attackerweapon, $targweapon, $value) = split /\t/, $_;
28                 $self->{stats}->{$addr}{$map}{$attackerweapon}{$targweapon} += $value;
29                 $self->{mapstats}->{$map}{$attackerweapon}{$targweapon} += $value;
30                 $self->{addrstats}->{$addr}{$attackerweapon}{$targweapon} += $value;
31                 $self->{allstats}->{$attackerweapon}{$targweapon} += $value;
32         }
33 }
34
35 sub save($)
36 {
37         my ($self) = @_;
38         open my $fh, ">", $self->{fn}
39                 or die "save: $!";
40         while(my ($addr, $addrhash) = each %{$self->{stats}})
41         {
42                 while(my ($map, $maphash) = each %$addrhash)
43                 {
44                         while(my ($attackerweapon, $attackerweaponhash) = each %$maphash)
45                         {
46                                 while(my ($targweapon, $value) = each %$attackerweaponhash)
47                                 {
48                                         print $fh "$addr\t$map\t$attackerweapon\t$targweapon\t$value\n";
49                                 }
50                         }
51                 }
52         }
53 }
54
55 sub event($$$$$$)
56 {
57         my ($self, $addr, $map, $attackerweapon, $targweapon, $type) = @_;
58         return if $map eq '';
59         if($type > 0)
60         {
61                 $self->{stats}->{$addr}{$map}{$attackerweapon}{$targweapon} += $type;
62                 $self->{mapstats}->{$map}{$attackerweapon}{$targweapon} += $type;
63                 $self->{addrstats}->{$addr}{$attackerweapon}{$targweapon} += $type;
64                 $self->{allstats}->{$attackerweapon}{$targweapon} += $type;
65         }
66 }
67
68 sub allstats($$)
69 {
70         my ($self, $callback) = @_;
71         # send global stats
72         $callback->(undef, undef, $self->{allstats});
73         # send per-host stats
74         while(my ($k, $v) = each %{$self->{addrstats}})
75         {
76                 $callback->($k, undef, $v);
77         }
78         # send per-map stats
79         while(my ($k, $v) = each %{$self->{mapstats}})
80         {
81                 $callback->(undef, $k, $v);
82         }
83         # send single stats
84         while(my ($k1, $v1) = each %{$self->{stats}})
85         {
86                 while(my ($k2, $v2) = each %$v1)
87                 {
88                         $callback->($k1, $k2, $v2);
89                 }
90         }
91 }
92
93 1;