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);
30 use lib qw{/var/www/ndawn/};
35 our $dbh = ND::DB::DB();
42 my $dumps = $dbh->prepare("SELECT dump,modified FROM dumps WHERE tick = ? and type = ? ORDER BY modified LIMIT 1");
43 $dumps->execute($tick,"alliance");
45 if (@_ = $dumps->fetchrow){
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];
58 @alliances = sort {$b->[$i] <=> $a->[$i]} @alliances;
60 for my $alliance (@alliances) {
62 $alliance->[$i+2] = $rank;
65 my $insert = $dbh->prepare(q{INSERT INTO alliance_stats (tick,id,members,
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;
75 my $findalliance = $dbh->prepare(q{SELECT tick,id,members,
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 id = $1 AND tick < $2 ORDER BY tick DESC LIMIT 1}) or die $dbh->errstr;
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;
103 $dumps->execute($tick,"planet");
104 if (@_ = $dumps->fetchrow){
106 $hour = (gmtime($_[1]))[2];
107 my $planetid = $dbh->prepare(qq{SELECT findplanetid(?,?,?)});
108 while (m/(\d+)\t(\d+)\t(\d+)\t\"(.*)\"\t\"(.*)\"\t(Ter|Cat|Zik|Xan|Etd)\t(\d+)\t(\d+)\t(\d+)\t(\d+)/g){
109 $planetid->execute($5,$4,$6);
110 my @id = $planetid->fetchrow;
111 push @planets,[$tick,$id[0],$1,$2,$3,$7,$8,$9,$10,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
116 @planets = sort {$b->[$i] <=> $a->[$i]} @planets;
118 for my $planet (@planets) {
120 $planet->[$i+4] = $rank;
124 my $findplanets = $dbh->prepare(q{SELECT tick,id, x, y, z,
125 size, score, value, xp,
126 sizerank, scorerank, valuerank, xprank,
127 size_gain, score_gain, value_gain, xp_gain,
128 sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain,
129 size_gain_day, score_gain_day, value_gain_day, xp_gain_day,
130 sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day
131 FROM planet_stats WHERE tick = (SELECT MAX(tick) FROM planet_stats WHERE tick < $1)});
132 $insert = $dbh->prepare(q{INSERT INTO planet_stats(tick,id, x, y, z,
133 size, score, value,xp,
134 sizerank,scorerank,valuerank,xprank,
135 size_gain, score_gain, value_gain, xp_gain,
136 sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain,
137 size_gain_day, score_gain_day, value_gain_day, xp_gain_day,
138 sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day)
139 VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)});
140 $findplanets->execute($tick) or die $dbh->errstr;
142 while (my @planet = $findplanets->fetchrow){
143 $oldStats{$planet[1]} = \@planet;
145 for my $planet (@planets) {
146 #print "$planet->[1]\n";
147 my $oldPlanet = $oldStats{$planet->[1]};
151 $planet->[$i+12] = $planet->[$i+4] - $oldPlanet->[$i+4];
152 $planet->[$i+16] = $planet->[$i+8] - $oldPlanet->[$i+8];
153 $planet->[$i+20] = $planet->[$i+12] + $oldPlanet->[$i+20] if $hour;
154 $planet->[$i+24] = $planet->[$i+16] + $oldPlanet->[$i+24] if $hour;
156 if (($planet->[2] != $oldPlanet->[2]) or
157 ($planet->[3] != $oldPlanet->[3]) or
158 ($planet->[4] != $oldPlanet->[4])){
159 #print "Planet has moved from $oldPlanet[2]:$oldPlanet[3]:$oldPlanet[4] to $planet->[2]:$planet->[3]:$planet->[4]\n";
160 intel_log -3, $planet->[1],"Planet has moved from $oldPlanet->[2]:$oldPlanet->[3]:$oldPlanet->[4] to $planet->[2]:$planet->[3]:$planet->[4] tick $tick";
163 #print "@{$oldPlanet}\n";
164 #print "@{$planet}\n";
165 $insert->execute(@{$planet}) or die $dbh->errstr;
169 $dumps->execute($tick,"galaxy");
171 if (@_ = $dumps->fetchrow){
173 $hour = (gmtime($_[1]))[2];
174 while (m/(\d+)\t(\d+)\t\"(?:.+)\"\t(\d+)\t(\d+)\t(\d+)\t(\d+)/g){
175 push @galaxies,[$tick,$1,$2,$3,$4,$5,$6,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
180 @galaxies = sort {$b->[$i] <=> $a->[$i]} @galaxies;
182 for my $galaxy (@galaxies) {
184 $galaxy->[$i+4] = $rank;
188 my $findgalaxy = $dbh->prepare(q{SELECT tick,x, y,
189 size, score, value, xp,
190 sizerank, scorerank, valuerank, xprank,
191 size_gain, score_gain, value_gain, xp_gain,
192 sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain,
193 size_gain_day, score_gain_day, value_gain_day, xp_gain_day,
194 sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day,
195 planets,planets_gain,planets_gain_day
196 FROM galaxies WHERE x = $1 AND y = $2 AND tick < $3 ORDER BY tick DESC LIMIT 1});
197 $insert = $dbh->prepare(q{INSERT INTO galaxies(tick, x, y,
198 size, score, value,xp,
199 sizerank,scorerank,valuerank,xprank,
200 size_gain, score_gain, value_gain, xp_gain,
201 sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain,
202 size_gain_day, score_gain_day, value_gain_day, xp_gain_day,
203 sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day,
204 planets,planets_gain,planets_gain_day
205 ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)});
206 my $countplanets = $dbh->prepare(q{SELECT count(*) from planet_stats where x = $1 and y = $2 and tick = $3});
207 for my $galaxy (@galaxies) {
209 my ($planets) = $dbh->selectrow_array($countplanets,undef,$galaxy->[1],$galaxy->[2],$tick) or die $dbh->errstr;
210 $galaxy->[27] = $planets;
211 $findgalaxy->execute($galaxy->[1],$galaxy->[2],$tick) or die $dbh->errstr;
212 if (my @oldGalaxy = $findgalaxy->fetchrow_array){
214 $galaxy->[$i+10] = $galaxy->[$i+2] - $oldGalaxy[$i+2];
215 $galaxy->[$i+14] = $galaxy->[$i+6] - $oldGalaxy[$i+6];
216 $galaxy->[$i+18] = $galaxy->[$i+10] + $oldGalaxy[$i+18] if $hour;
217 $galaxy->[$i+22] = $galaxy->[$i+14] + $oldGalaxy[$i+22] if $hour;
219 $galaxy->[28] = $galaxy->[27] - $oldGalaxy[27];
220 $galaxy->[29] = $galaxy->[28] + $oldGalaxy[29] if $hour;
223 $insert->execute(@{$galaxy}) or die $dbh->errstr;
224 #print "@{$galaxy}\n";
231 $countplanets->finish;
233 $findalliance->finish;