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