]> ruin.nu Git - ndwebbie.git/blob - scripts/parseplanets.pl
Remove the old stats automatically when parsing
[ndwebbie.git] / scripts / parseplanets.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 DBI;
26 use DBD::Pg qw(:pg_types);
27
28 use LWP::Simple;
29
30 use FindBin;
31 use lib "$FindBin::Bin/../lib";
32
33 use ND::Include;
34 use ND::DB;
35
36 our $dbh = ND::DB::DB();
37
38 my $tick = $ARGV[0];
39 my $hour;
40 $dbh->begin_work;
41 my $dumps = $dbh->prepare("SELECT dump,modified FROM dumps WHERE tick = ? and type = 'planet' ORDER BY modified LIMIT 1");
42
43 my @planets;
44 $dumps->execute($tick);
45 if (@_ = $dumps->fetchrow){
46         $_ = $_[0];
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];
53         }
54 }
55
56 for my $i (8,7,5,6){
57         @planets = sort {$b->[$i] <=> $a->[$i]} @planets;
58         my $rank = 0;
59         for my $planet (@planets) {
60                 $rank++;
61                 $planet->[$i+4] = $rank;
62     }
63 }
64
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;
82 my %oldStats;
83 while (my @planet = $findplanets->fetchrow){
84         $oldStats{$planet[1]} = \@planet;
85 }
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]};
92
93         if ($oldPlanet){
94                 for my $i (1,2,3,4){
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;
99                 }
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");
105                 }
106         }
107         #print "@{$oldPlanet}\n";
108         #print "@{$planet}\n";
109         $insert->execute(@{$planet}) or die $dbh->errstr;
110 }
111 $dbh->commit;