]> ruin.nu Git - ndwebbie.git/blob - scripts/parsealliances.pl
Use local::lib properly
[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 local::lib;
26 use DBI;
27 use DBD::Pg qw(:pg_types);
28
29 use LWP::Simple;
30
31 use FindBin;
32 use lib "$FindBin::Bin/../lib";
33
34 use ND::Include;
35 use ND::DB;
36
37 our $dbh = ND::DB::DB();
38
39 my $tick = $ARGV[0];
40 my $hour;
41 $dbh->begin_work;
42 my $dumps = $dbh->prepare("SELECT dump,modified FROM dumps WHERE tick = ? and type = 'alliance' ORDER BY modified LIMIT 1");
43 $dumps->execute($tick);
44 my @alliances;
45 if (@_ = $dumps->fetchrow){
46         $_ = $_[0];     
47         $hour = (gmtime($_[1]))[2];
48         my $allianceid = $dbh->prepare(qq{SELECT find_alliance_id(?)});
49         while (m/\d+\t\"(.+)\"\t(\d+)\t(\d+)\t(\d+)/g){
50                 $allianceid->execute($1);
51                 my ($id) = $allianceid->fetchrow;
52                 push @alliances,[$tick,$id,$3,$2,$4,0,0,0,0,0,0,0,0,0,0,0,0];
53         }
54 }
55
56
57 for my $i (3,4){
58         @alliances = sort {$b->[$i] <=> $a->[$i]} @alliances;
59         my $rank = 0;
60         for my $alliance (@alliances) {
61                 $rank++;
62                 $alliance->[$i+2] = $rank;
63     }
64 }
65 my $insert = $dbh->prepare(q{INSERT INTO alliance_stats (tick,aid,members,
66         size,score,
67         sizerank,scorerank,
68         size_gain,score_gain,
69         sizerank_gain,scorerank_gain,
70         size_gain_day,score_gain_day,
71         sizerank_gain_day,scorerank_gain_day,
72         members_gain,members_gain_day
73         ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)}) or die $dbh->errstr;
74
75 my $findalliance = $dbh->prepare(q{SELECT tick,aid,members,
76         size, score,
77         sizerank, scorerank,
78         size_gain, score_gain,
79         sizerank_gain, scorerank_gain,
80         size_gain_day, score_gain_day,
81         sizerank_gain_day, scorerank_gain_day,
82         members_gain,members_gain_day
83 FROM alliance_stats WHERE aid = $1 AND tick < $2 ORDER BY tick DESC LIMIT 1}) or die $dbh->errstr;
84
85 $dbh->do(q{DELETE FROM alliance_stats WHERE tick = $1},undef,$tick);
86
87 for my $alliance (@alliances) {
88
89         $findalliance->execute($alliance->[1],$tick) or die $dbh->errstr;
90         if (my @oldAlliance = $findalliance->fetchrow_array){
91                 for my $i (1,2){
92                         $alliance->[$i+6] = $alliance->[$i+2] - $oldAlliance[$i+2];
93                         $alliance->[$i+8] = $alliance->[$i+4] - $oldAlliance[$i+4];
94                         $alliance->[$i+10] = $alliance->[$i+6] + $oldAlliance[$i+10] if $hour;
95                         $alliance->[$i+12] = $alliance->[$i+8] + $oldAlliance[$i+12] if $hour;
96                 }
97                 $alliance->[15] = $alliance->[2] - $oldAlliance[+2];
98                 $alliance->[16] = $alliance->[15] + $oldAlliance[16] if $hour;
99
100         }
101         $insert->execute(@{$alliance}) or die $dbh->errstr;
102 }
103
104 $dbh->commit;