]> ruin.nu Git - ndwebbie.git/blob - scripts/parseplanets.pl
Use local::lib properly
[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 local::lib;
26 use DBI;
27 use DBD::Pg qw(:pg_types);
28
29 use LWP::Simple;
30
31 use FindBin;
32 use lib "$FindBin::Bin/../lib";
33
34 use ND::Include;
35 use ND::DB;
36
37 our $dbh = ND::DB::DB();
38
39 my $tick = $ARGV[0];
40 my $hour;
41 $dbh->begin_work;
42 my $dumps = $dbh->prepare("SELECT dump,modified FROM dumps WHERE tick = ? and type = 'planet' ORDER BY modified LIMIT 1");
43
44 my @planets;
45 $dumps->execute($tick);
46 if (@_ = $dumps->fetchrow){
47         $_ = $_[0];
48         $hour = (gmtime($_[1]))[2];
49         my $planetid = $dbh->prepare(q{SELECT find_planet_id($1,$2,$3)});
50         while (m/(\d+)\t(\d+)\t(\d+)\t\"(.*)\"\t\"(.*)\"\t(Ter|Cat|Zik|Xan|Etd)\t(\d+)\t(\d+)\t(\d+)\t(\d+)/g){
51                 $planetid->execute($5,$4,$6);
52                 my @id = $planetid->fetchrow;
53                 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];
54         }
55 }
56
57 for my $i (8,7,5,6){
58         @planets = sort {$b->[$i] <=> $a->[$i]} @planets;
59         my $rank = 0;
60         for my $planet (@planets) {
61                 $rank++;
62                 $planet->[$i+4] = $rank;
63     }
64 }
65
66 my $findplanets = $dbh->prepare(q{SELECT tick,pid, x, y, z,
67         size, score, value, xp,
68         sizerank, scorerank, valuerank, xprank,
69         size_gain, score_gain, value_gain, xp_gain,
70         sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain,
71         size_gain_day, score_gain_day, value_gain_day, xp_gain_day,
72         sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day
73 FROM planet_stats WHERE tick = (SELECT MAX(tick) FROM planet_stats WHERE tick < $1)});
74 my $insert = $dbh->prepare(q{INSERT INTO planet_stats(tick,pid, x, y, z,
75         size, score, value,xp,
76         sizerank,scorerank,valuerank,xprank,
77         size_gain, score_gain, value_gain, xp_gain,
78         sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain,
79         size_gain_day, score_gain_day, value_gain_day, xp_gain_day,
80         sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day)
81         VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)});
82 $findplanets->execute($tick) or die $dbh->errstr;
83 my %oldStats;
84 while (my @planet = $findplanets->fetchrow){
85         $oldStats{$planet[1]} = \@planet;
86 }
87 my $intel = $dbh->prepare(q{INSERT INTO forum_posts (ftid,uid,message) VALUES(
88                 (SELECT ftid FROM planets WHERE pid = $2),$1,$3)});
89 $dbh->do(q{DELETE FROM planet_stats WHERE tick = $1},undef,$tick);
90 for my $planet (@planets) {
91         #print "$planet->[1]\n";
92         my $oldPlanet = $oldStats{$planet->[1]};
93
94         if ($oldPlanet){
95                 for my $i (1,2,3,4){
96                         $planet->[$i+12] = $planet->[$i+4] - $oldPlanet->[$i+4];
97                         $planet->[$i+16] = $planet->[$i+8] - $oldPlanet->[$i+8];
98                         $planet->[$i+20] = $planet->[$i+12] + $oldPlanet->[$i+20] if $hour;
99                         $planet->[$i+24] = $planet->[$i+16] + $oldPlanet->[$i+24] if $hour;
100                 }
101                 if (($planet->[2] != $oldPlanet->[2]) or 
102                         ($planet->[3] != $oldPlanet->[3]) or 
103                         ($planet->[4] != $oldPlanet->[4])){
104                         #print "Planet has moved from $oldPlanet[2]:$oldPlanet[3]:$oldPlanet[4] to $planet->[2]:$planet->[3]:$planet->[4]\n";
105                         $intel->execute(-3, $planet->[1],"Planet has moved from $oldPlanet->[2]:$oldPlanet->[3]:$oldPlanet->[4] to $planet->[2]:$planet->[3]:$planet->[4] tick $tick");
106                 }
107         }
108         #print "@{$oldPlanet}\n";
109         #print "@{$planet}\n";
110         $insert->execute(@{$planet}) or die $dbh->errstr;
111 }
112 $dbh->commit;