1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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 #**************************************************************************/
20 package ND::Web::Pages::Check;
22 use warnings FATAL => 'all';
23 no warnings qw(uninitialized);
25 use CGI qw/:standard/;
28 $ND::PAGES{check} = {parse => \&parse, process => \&process, render=> \&render};
32 #TODO: Improved apache conf needed
33 #if ($uri =~ m{^/.*/((\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?(?: |:(\d+))?)$}){
44 $ND::TEMPLATE->param(TITLE => 'Check planets and galaxies');
46 $BODY->param(isBC => isMember() && (isOfficer() || isBC));
48 return $ND::NOACCESS unless $ND::ATTACKER;
51 if (param('coords') =~ /(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?(?: |:(\d+))?/){
55 $BODY->param(Coords => param('coords'));
57 $ND::ERROR .= p b q{Couldn't parse coords};
61 if (isMember() && param('cmd') eq 'arbiter'){
62 my $query = $DBH->prepare(q{SELECT count(*) AS friendlies FROM current_planet_stats WHERE x = ? AND y = ?
63 AND (planet_status IN ('Friendly','NAP') OR relationship IN ('Friendly','NAP'))});
64 my ($count) = $DBH->selectrow_array($query,undef,$x,$y);
66 $BODY->param(Arbiter => '<b>DO NOT ATTACK THIS GAL</b>');
68 $BODY->param(Arbiter => '<b>KILL THESE BASTARDS</b>');
70 log_message $ND::UID,"Arbiter check on $x:$y";
74 my $extra_columns = '';
76 $where = 'AND z = ?' if defined $z;
77 if (isMember() && isOfficer()){
78 $extra_columns = ",planet_status,hit_us, alliance,relationship,nick";
79 }elsif (isMember() && isBC()){
80 $extra_columns = ", planet_status,hit_us, alliance,relationship";
83 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});
86 $query->execute($x,$y,$z);
88 $query->execute($x,$y);
89 if (isMember() && (isBC() || isOfficer()) && !isHC()){
90 log_message $ND::UID,"BC browsing $x:$y";
94 my $planet_id = undef;
96 while (my ($id,$coords,$planet,$race,$size,$score,$value,$xp,$sizerank,$scorerank,$valuerank,$xprank
97 ,$fleetvalue,$resvalue,$planet_status,$hit_us,$alliance,$relationship,$nick) = $query->fetchrow){
99 my %planet = (Coords => $coords, Planet => $planet, Race => $race, Size => "$size ($sizerank)"
100 , Score => "$score ($scorerank)", Value => "$value ($valuerank)", XP => "$xp ($xprank)"
101 , FleetValue => "$fleetvalue ($resvalue)");
102 if (isMember() && (isOfficer() || isBC())){
103 $planet{HitUs} = $hit_us;
104 $planet{Alliance} = "$alliance ($relationship)";
105 $planet{Nick} = "$nick ($planet_status)";
106 $planet{PlanetStatus} = $planet_status;
107 $planet{Relationship} = $relationship;
109 if ($z && $alliance eq 'NewDawn' && not (isHC || isOfficer)){
110 log_message $ND::UID,"BC browsing ND planet $coords tick $ND::TICK";
114 $planet{ODD} = $i % 2;
115 push @planets,\%planet;
117 $BODY->param(Planets => \@planets);
119 if ($z && $planet_id){
120 $BODY->param(OnePlanet => 1);
122 my $query = $DBH->prepare(q{
123 SELECT i.mission, i.tick AS landingtick,MIN(eta) AS eta, i.amount, coords(p.x,p.y,p.z) AS target
126 NATURAL JOIN planet_stats) p ON i.target = p.id
128 NATURAL JOIN planet_stats) p2 ON i.sender = p2.id
129 WHERE p.tick = ( SELECT max(tick) FROM planet_stats) AND i.tick > tick() AND i.uid = -1
130 AND p2.tick = p.tick AND p2.id = ?
131 GROUP BY p.x,p.y,p.z,p2.x,p2.y,p2.z,i.mission,i.tick,i.amount,i.ingal,i.uid
132 ORDER BY p.x,p.y,p.z});
133 $query->execute($planet_id);
135 while (my ($mission,$landingtick,$eta,$amount,$target) = $query->fetchrow){
136 push @missions,{Target => $target, Mission => $mission, LandingTick => $landingtick
137 , ETA => $eta, Amount => $amount};
139 $BODY->param(Missions => \@missions);
142 $query = $DBH->prepare(q{SELECT value,tick FROM planet_stats
143 WHERE id = ? AND tick > tick() - 24});
145 <p>Value the last 24 ticks</p>
146 <table><tr><th>Tick</th><th>Value</th><th>Difference</th></tr>};
148 $query->execute($planet_id);
149 while (my($value,$tick) = $query->fetchrow){
150 my $diff = $value-$old;
152 my $class = 'Defend';
153 $class = 'Attack' if $diff < 0;
154 $scan .= qq{<tr><td>$tick</td><td>$value</td><td class="$class">$diff</td></tr>};
156 $scan .= q{</table>};
157 push @scans, {Scan => $scan};
159 $query = $DBH->prepare(q{SELECT x,y,z,tick FROM planet_stats WHERE id = ?});
161 <p>Previous Coords</p>
162 <table><tr><th>Tick</th><th>Value</th><th>Difference</th></tr>};
163 $query->execute($planet_id);
165 while (my($nx,$ny,$nz,$tick) = $query->fetchrow){
166 if ($nx != $x || $ny != $y || $nz != $z){
170 $scan .= qq{<tr><td>$tick</td><td>$x:$y:$z</td></tr>};
173 $scan .= q{</table>};
174 push @scans, {Scan => $scan};
176 $query = $DBH->prepare(q{SELECT DISTINCT ON (type) type,scan_id, tick, scan FROM scans WHERE planet = ?
177 GROUP BY type,scan_id, tick, scan ORDER BY type,tick DESC});
178 $query->execute($planet_id);
180 while (my($type,$scan_id,$tick,$scan) = $query->fetchrow){
181 $scans{$type} = [$scan_id,$tick,$scan];
183 for my $type ('Planet','Jumpgate','Unit','Military','Fleet Analysis','Surface Analysis','Technology Analysis','News'){
184 next unless exists $scans{$type};
185 my $scan_id = $scans{$type}->[0];
186 my $tick = $scans{$type}->[1];
187 my $scan = $scans{$type}->[2];
188 if ($ND::TICK - $tick > 10){
189 $scan =~ s{<table( cellpadding="\d+")?>}{<table$1 class="old">};
191 push @scans,{Scan => qq{
192 <p><b><a href="http://game.planetarion.com/showscan.pl?scan_id=$scan_id">$type</a> Scan from tick $tick</b></p>
196 $BODY->param(Scans => \@scans);