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 ***************************************************************************/
27 use DBD::Pg qw(:pg_types);
30 use lib "$FindBin::Bin/../lib";
35 our $dbh = ND::DB::DB();
40 my $dumps = $dbh->prepare("SELECT dump,modified FROM dumps WHERE tick = ? and type = 'alliance' ORDER BY modified LIMIT 1");
41 $dumps->execute($tick);
43 if (@_ = $dumps->fetchrow){
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];
56 @alliances = sort {$b->[$i] <=> $a->[$i]} @alliances;
58 for my $alliance (@alliances) {
60 $alliance->[$i+2] = $rank;
63 my $insert = $dbh->prepare(q{INSERT INTO alliance_stats (tick,aid,members,
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;
73 my $findalliance = $dbh->prepare(q{SELECT tick,aid,members,
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;
83 $dbh->do(q{DELETE FROM alliance_stats WHERE tick = $1},undef,$tick);
85 for my $alliance (@alliances) {
87 $findalliance->execute($alliance->[1],$tick) or die $dbh->errstr;
88 if (my @oldAlliance = $findalliance->fetchrow_array){
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;
95 $alliance->[15] = $alliance->[2] - $oldAlliance[+2];
96 $alliance->[16] = $alliance->[15] + $oldAlliance[16] if $hour;
99 $insert->execute(@{$alliance}) or die $dbh->errstr;