]> ruin.nu Git - ndwebbie.git/blob - scripts/parsealliances.pl
Remove the old stats automatically when parsing
[ndwebbie.git] / scripts / parsealliances.pl
1 #!/usr/bin/perl
2 q{
3 /***************************************************************************
4  *   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
5  *                                                                         *
6  *   This program is free software; you can redistribute it and/or modify  *
7  *   it under the terms of the GNU General Public License as published by  *
8  *   the Free Software Foundation; either version 2 of the License, or     *
9  *   (at your option) any later version.                                   *
10  *                                                                         *
11  *   This program is distributed in the hope that it will be useful,       *
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
14  *   GNU General Public License for more details.                          *
15  *                                                                         *
16  *   You should have received a copy of the GNU General Public License     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
20  ***************************************************************************/
21 };
22
23 use strict;
24 use warnings;
25 use DBI;
26 use DBD::Pg qw(:pg_types);
27
28 use LWP::Simple;
29
30 use FindBin;
31 use lib "$FindBin::Bin/../lib";
32
33 use ND::Include;
34 use ND::DB;
35
36 our $dbh = ND::DB::DB();
37
38 my $tick = $ARGV[0];
39 my $hour;
40 $dbh->begin_work;
41 my $dumps = $dbh->prepare("SELECT dump,modified FROM dumps WHERE tick = ? and type = 'alliance' ORDER BY modified LIMIT 1");
42 $dumps->execute($tick);
43 my @alliances;
44 if (@_ = $dumps->fetchrow){
45         $_ = $_[0];     
46         $hour = (gmtime($_[1]))[2];
47         my $allianceid = $dbh->prepare(qq{SELECT find_alliance_id(?)});
48         while (m/\d+\t\"(.+)\"\t(\d+)\t(\d+)\t(\d+)/g){
49                 $allianceid->execute($1);
50                 my ($id) = $allianceid->fetchrow;
51                 push @alliances,[$tick,$id,$3,$2,$4,0,0,0,0,0,0,0,0,0,0,0,0];
52         }
53 }
54
55
56 for my $i (3,4){
57         @alliances = sort {$b->[$i] <=> $a->[$i]} @alliances;
58         my $rank = 0;
59         for my $alliance (@alliances) {
60                 $rank++;
61                 $alliance->[$i+2] = $rank;
62     }
63 }
64 my $insert = $dbh->prepare(q{INSERT INTO alliance_stats (tick,aid,members,
65         size,score,
66         sizerank,scorerank,
67         size_gain,score_gain,
68         sizerank_gain,scorerank_gain,
69         size_gain_day,score_gain_day,
70         sizerank_gain_day,scorerank_gain_day,
71         members_gain,members_gain_day
72         ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}) or die $dbh->errstr;
73
74 my $findalliance = $dbh->prepare(q{SELECT tick,aid,members,
75         size, score,
76         sizerank, scorerank,
77         size_gain, score_gain,
78         sizerank_gain, scorerank_gain,
79         size_gain_day, score_gain_day,
80         sizerank_gain_day, scorerank_gain_day,
81         members_gain,members_gain_day
82 FROM alliance_stats WHERE aid = $1 AND tick < $2 ORDER BY tick DESC LIMIT 1}) or die $dbh->errstr;
83
84 $dbh->do(q{DELETE FROM alliance_stats WHERE tick = $1},undef,$tick);
85
86 for my $alliance (@alliances) {
87
88         $findalliance->execute($alliance->[1],$tick) or die $dbh->errstr;
89         if (my @oldAlliance = $findalliance->fetchrow_array){
90                 for my $i (1,2){
91                         $alliance->[$i+6] = $alliance->[$i+2] - $oldAlliance[$i+2];
92                         $alliance->[$i+8] = $alliance->[$i+4] - $oldAlliance[$i+4];
93                         $alliance->[$i+10] = $alliance->[$i+6] + $oldAlliance[$i+10] if $hour;
94                         $alliance->[$i+12] = $alliance->[$i+8] + $oldAlliance[$i+12] if $hour;
95                 }
96                 $alliance->[15] = $alliance->[2] - $oldAlliance[+2];
97                 $alliance->[16] = $alliance->[15] + $oldAlliance[16] if $hour;
98
99         }
100         $insert->execute(@{$alliance}) or die $dbh->errstr;
101 }
102
103 $dbh->commit;