3 /***************************************************************************
4 * Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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 ***************************************************************************/
26 use DBD::Pg qw(:pg_types);
31 use lib "$FindBin::Bin/../lib";
36 our $dbh = ND::DB::DB();
41 my $dumps = $dbh->prepare("SELECT dump,modified FROM dumps WHERE tick = ? and type = 'alliance' ORDER BY modified LIMIT 1");
42 $dumps->execute($tick);
44 if (@_ = $dumps->fetchrow){
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];
57 @alliances = sort {$b->[$i] <=> $a->[$i]} @alliances;
59 for my $alliance (@alliances) {
61 $alliance->[$i+2] = $rank;
64 my $insert = $dbh->prepare(q{INSERT INTO alliance_stats (tick,aid,members,
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;
74 my $findalliance = $dbh->prepare(q{SELECT tick,aid,members,
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;
84 $dbh->do(q{DELETE FROM alliance_stats WHERE tick = $1},undef,$tick);
86 for my $alliance (@alliances) {
88 $findalliance->execute($alliance->[1],$tick) or die $dbh->errstr;
89 if (my @oldAlliance = $findalliance->fetchrow_array){
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;
96 $alliance->[15] = $alliance->[2] - $oldAlliance[+2];
97 $alliance->[16] = $alliance->[15] + $oldAlliance[16] if $hour;
100 $insert->execute(@{$alliance}) or die $dbh->errstr;