]> ruin.nu Git - ndwebbie.git/blob - check.pl
da2444af513fd6e0c12868f784cf87814c547bd0
[ndwebbie.git] / check.pl
1 #**************************************************************************
2 #   Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu>               *
3 #                                                                         *
4 #   This program is free software; you can redistribute it and/or modify  *
5 #   it under the terms of the GNU General Public License as published by  *
6 #   the Free Software Foundation; either version 2 of the License, or     *
7 #   (at your option) any later version.                                   *
8 #                                                                         *
9 #   This program is distributed in the hope that it will be useful,       *
10 #   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
11 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
12 #   GNU General Public License for more details.                          *
13 #                                                                         *
14 #   You should have received a copy of the GNU General Public License     *
15 #   along with this program; if not, write to the                         *
16 #   Free Software Foundation, Inc.,                                       *
17 #   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
18 #**************************************************************************/
19
20 use strict;
21
22 $ND::TEMPLATE->param(TITLE => 'Check planets and galaxies');
23
24 our $BODY;
25 our $DBH;
26
27 $BODY->param(isBC => isMember() && (isOfficer() || isBC));
28
29
30 die "You don't have access" unless $ND::ATTACKER;
31
32 my ($x,$y,$z);
33 if (param('coords') =~ /(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?(?: |:(\d+))?/){
34         $x = $1;
35         $y = $2;
36         $z = $3;
37 }else{
38         die "Bad coords";
39 }
40
41 if (param('cmd') eq 'arbiter'){
42 }
43
44 my $where = '';
45 my $extra_columns = '';
46
47 $where = 'AND z = ?' if defined $z;
48 if (isMember() && isOfficer()){
49         $extra_columns = ",planet_status,hit_us, alliance,relationship,nick";
50 }elsif (isMember() && isBC()){
51         $extra_columns = ", planet_status,hit_us, alliance,relationship";
52 }
53
54 my $query = $DBH->prepare(qq{Select id,coords(x,y,z), ((ruler || ' OF ') || p.planet) as planet,race, size, score, value, xp, sizerank, scorerank, valuerank, xprank, p.value - p.size*200 - coalesce(c.metal+c.crystal+c.eonium,0)/150 - coalesce(c.structures,(SELECT avg(structures) FROM covop_targets)::int)*1500 AS fleetvalue,(c.metal+c.crystal+c.eonium)/100 AS resvalue  $extra_columns from current_planet_stats p LEFT OUTER JOIN covop_targets c ON p.id = c.planet where x = ? AND y = ? $where order by x,y,z asc});
55
56 if (defined $z){
57         $query->execute($x,$y,$z);
58 }else{
59         $query->execute($x,$y);
60 }
61 my @planets;
62 my $planet_id = undef;
63 while (my ($id,$coords,$planet,$race,$size,$score,$value,$xp,$sizerank,$scorerank,$valuerank,$xprank
64                 ,$fleetvalue,$resvalue,$planet_status,$hit_us,$alliance,$relationship,$nick) = $query->fetchrow){
65         $planet_id = $id;
66         my %planet = (Coords => $coords, Planet => $planet, Race => $race, Size => "$size ($sizerank)"
67                 , Score => "$score ($scorerank)", Value => "$value ($valuerank)", XP => "$xp ($xprank)"
68                 , FleetValue => "$fleetvalue ($resvalue)");
69         if (isMember() && (isOfficer() || isBC())){
70                 $planet{HitUs} = $hit_us;
71                 $planet{Alliance} = "$alliance ($relationship)";
72                 $planet{Nick} = "$nick ($planet_status)";
73                 $planet{PlanetStatus} = $planet_status;
74                 $planet{Relationship} = $relationship;
75                 $planet{isBC} = 1;
76         }
77         push @planets,\%planet;
78 }
79 $BODY->param(Planets => \@planets);
80
81 if ($planet_id){
82         $BODY->param(OnePlanet => 1);
83
84         my $query = $DBH->prepare(q{ 
85 SELECT i.mission, i.tick AS landingtick,MIN(eta) AS eta, i.amount, coords(p.x,p.y,p.z) AS target
86 FROM intel i
87         JOIN (planets
88                 NATURAL JOIN planet_stats) p ON i.target = p.id
89         JOIN (planets
90                 NATURAL JOIN planet_stats) p2 ON i.sender = p2.id
91 WHERE  p.tick = ( SELECT max(tick) FROM planet_stats) AND i.tick > $TICK AND i.uid = -1 
92         AND p2.tick = p.tick AND p2.id = ?
93 GROUP BY p.x,p.y,p.z,p2.x,p2.y,p2.z,i.mission,i.tick,i.amount,i.ingal,i.uid
94 ORDER BY p.x,p.y,p.z});
95         $query->execute($planet_id);
96         my @missions;
97         while (my ($mission,$landingtick,$eta,$amount,$target) = $query->fetchrow){
98                 push @missions,{Target => $target, Mission => $mission, LandingTick => $landingtick
99                         , ETA => $eta, Amount => $amount};
100         }
101         $BODY->param(Missions => \@missions);
102
103         my @scans;
104         my $query = $DBH->prepare(q{SELECT value,tick FROM planet_stats 
105                 WHERE id = ? AND tick > tick() - 24});
106         my $scan = q{
107 <p>Value the last 24 ticks</p>
108 <table><tr><th>Tick</th><th>Value</th><th>Difference</th></tr>};
109         my $old = 0;
110         $query->execute($planet_id);
111         while (my($value,$tick) = $query->fetchrow){
112                 my $diff = $value-$old;
113                 $old = $value;
114                 my $class = 'Defend';
115                 $class = 'Attack' if $diff < 0;
116                 $scan .= qq{<tr><td>$tick</td><td>$value</td><td class="$class">$diff</td></tr>};
117         }
118         $scan .= q{</table>};
119         push @scans, {Scan => $scan};
120
121         my $query = $DBH->prepare(q{SELECT x,y,z,tick FROM planet_stats WHERE id = ?});
122         my $scan = q{
123 <p>Previous Coords</p>
124 <table><tr><th>Tick</th><th>Value</th><th>Difference</th></tr>};
125         $query->execute($planet_id);
126         $x = $y = $z = 0;
127         while (my($nx,$ny,$nz,$tick) = $query->fetchrow){
128                 if ($nx != $x || $ny != $y || $nz != $z){
129                         $x = $nx;
130                         $y = $ny;
131                         $z = $nz;
132                         $scan .= qq{<tr><td>$tick</td><td>$x:$y:$z</td></tr>};
133                 }
134         }
135         $scan .= q{</table>};
136         push @scans, {Scan => $scan};
137
138         my $query = $DBH->prepare(q{SELECT DISTINCT ON (type) type,scan_id, tick, scan FROM scans WHERE planet = ?
139                 GROUP BY type,scan_id, tick, scan ORDER BY type,tick DESC});
140         $query->execute($planet_id);
141         my %scans;
142         while (my($type,$scan_id,$tick,$scan) = $query->fetchrow){
143                 $scans{$type} = [$scan_id,$tick,$scan];
144         }
145         for my $type ('Planet','Jumpgate','Unit','Military','Fleet Analysis','Surface Analysis','Technology Analysis','News'){
146                 next unless exists $scans{$type};
147                 my $scan_id = $scans{$type}->[0];
148                 my $tick = $scans{$type}->[1];
149                 my $scan = $scans{$type}->[2];
150                 if ($ND::TICK - $tick > 10){
151                         $scan =~ s{<table( cellpadding="\d+")?>}{<table$1 class="old">};
152                 }
153                 push @scans,{Scan => qq{
154 <p><b><a href="http://game.planetarion.com/showscan.pl?scan_id=$scan_id">$type</a> Scan from tick $tick</b></p>
155 $scan}};
156         }
157
158         $BODY->param(Scans => \@scans);
159 }
160
161 1;