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 = 'planet' ORDER BY modified LIMIT 1");
44 $dumps->execute($tick);
45 if (@_ = $dumps->fetchrow){
47 $hour = (gmtime($_[1]))[2];
48 my $planetid = $dbh->prepare(q{SELECT find_planet_id($1,$2,$3)});
49 while (m/(\d+)\t(\d+)\t(\d+)\t\"(.*)\"\t\"(.*)\"\t(Ter|Cat|Zik|Xan|Etd)\t(\d+)\t(\d+)\t(\d+)\t(\d+)/g){
50 $planetid->execute($5,$4,$6);
51 my @id = $planetid->fetchrow;
52 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];
57 @planets = sort {$b->[$i] <=> $a->[$i]} @planets;
59 for my $planet (@planets) {
61 $planet->[$i+4] = $rank;
65 my $findplanets = $dbh->prepare(q{SELECT tick,pid, x, y, z,
66 size, score, value, xp,
67 sizerank, scorerank, valuerank, xprank,
68 size_gain, score_gain, value_gain, xp_gain,
69 sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain,
70 size_gain_day, score_gain_day, value_gain_day, xp_gain_day,
71 sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day
72 FROM planet_stats WHERE tick = (SELECT MAX(tick) FROM planet_stats WHERE tick < $1)});
73 my $insert = $dbh->prepare(q{INSERT INTO planet_stats(tick,pid, x, y, z,
74 size, score, value,xp,
75 sizerank,scorerank,valuerank,xprank,
76 size_gain, score_gain, value_gain, xp_gain,
77 sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain,
78 size_gain_day, score_gain_day, value_gain_day, xp_gain_day,
79 sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day)
80 VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)});
81 $findplanets->execute($tick) or die $dbh->errstr;
83 while (my @planet = $findplanets->fetchrow){
84 $oldStats{$planet[1]} = \@planet;
86 my $intel = $dbh->prepare(q{INSERT INTO forum_posts (ftid,uid,message) VALUES(
87 (SELECT ftid FROM planets WHERE pid = $2),$1,$3)});
88 $dbh->do(q{DELETE FROM planet_stats WHERE tick = $1},undef,$tick);
89 for my $planet (@planets) {
90 #print "$planet->[1]\n";
91 my $oldPlanet = $oldStats{$planet->[1]};
95 $planet->[$i+12] = $planet->[$i+4] - $oldPlanet->[$i+4];
96 $planet->[$i+16] = $planet->[$i+8] - $oldPlanet->[$i+8];
97 $planet->[$i+20] = $planet->[$i+12] + $oldPlanet->[$i+20] if $hour;
98 $planet->[$i+24] = $planet->[$i+16] + $oldPlanet->[$i+24] if $hour;
100 if (($planet->[2] != $oldPlanet->[2]) or
101 ($planet->[3] != $oldPlanet->[3]) or
102 ($planet->[4] != $oldPlanet->[4])){
103 #print "Planet has moved from $oldPlanet[2]:$oldPlanet[3]:$oldPlanet[4] to $planet->[2]:$planet->[3]:$planet->[4]\n";
104 $intel->execute(-3, $planet->[1],"Planet has moved from $oldPlanet->[2]:$oldPlanet->[3]:$oldPlanet->[4] to $planet->[2]:$planet->[3]:$planet->[4] tick $tick");
107 #print "@{$oldPlanet}\n";
108 #print "@{$planet}\n";
109 $insert->execute(@{$planet}) or die $dbh->errstr;