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