]> ruin.nu Git - ndwebbie.git/blob - scripts/parseplanets.pl
Use FindBin and send USR1 to irssi
[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 $ND::DBH = $dbh;
39
40 my $tick = $ARGV[0];
41 my $hour;
42 $dbh->begin_work;
43 my $dumps = $dbh->prepare("SELECT dump,modified FROM dumps WHERE tick = ? and type = 'planet' ORDER BY modified LIMIT 1");
44
45 my @planets;
46 $dumps->execute($tick);
47 if (@_ = $dumps->fetchrow){
48         $_ = $_[0];
49         $hour = (gmtime($_[1]))[2];
50         my $planetid = $dbh->prepare(qq{SELECT findplanetid(?,?,?)});
51         while (m/(\d+)\t(\d+)\t(\d+)\t\"(.*)\"\t\"(.*)\"\t(Ter|Cat|Zik|Xan|Etd)\t(\d+)\t(\d+)\t(\d+)\t(\d+)/g){
52                 $planetid->execute($5,$4,$6);
53                 my @id = $planetid->fetchrow;
54                 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];
55         }
56 }
57
58 for my $i (8,7,5,6){
59         @planets = sort {$b->[$i] <=> $a->[$i]} @planets;
60         my $rank = 0;
61         for my $planet (@planets) {
62                 $rank++;
63                 $planet->[$i+4] = $rank;
64     }
65 }
66
67 my $findplanets = $dbh->prepare(q{SELECT tick,id, x, y, z, 
68         size, score, value, xp, 
69         sizerank, scorerank, valuerank, xprank, 
70         size_gain, score_gain, value_gain, xp_gain, 
71         sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain, 
72         size_gain_day, score_gain_day, value_gain_day, xp_gain_day, 
73         sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day
74 FROM planet_stats WHERE tick = (SELECT MAX(tick) FROM planet_stats WHERE tick < $1)});
75 my $insert = $dbh->prepare(q{INSERT INTO planet_stats(tick,id, x, y, z, 
76         size, score, value,xp,
77         sizerank,scorerank,valuerank,xprank,
78         size_gain, score_gain, value_gain, xp_gain,
79         sizerank_gain, scorerank_gain, valuerank_gain, xprank_gain,
80         size_gain_day, score_gain_day, value_gain_day, xp_gain_day,
81         sizerank_gain_day, scorerank_gain_day, valuerank_gain_day, xprank_gain_day)
82         VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)});
83 $findplanets->execute($tick) or die $dbh->errstr;
84 my %oldStats;
85 while (my @planet = $findplanets->fetchrow){
86         $oldStats{$planet[1]} = \@planet;
87 }
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_log -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;