]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Check.pm
bc24cf16e6c6ae0ba2ac111a71564e0773d44a0b
[ndwebbie.git] / ND / Web / Pages / Check.pm
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 package ND::Web::Pages::Check;
21 use strict;
22 use warnings FATAL => 'all';
23 no warnings qw(uninitialized);
24 use ND::Include;
25 use CGI qw/:standard/;
26 use ND::Web::Include;
27
28 $ND::PAGES{check} = {parse => \&parse, process => \&process, render=> \&render};
29
30 sub parse {
31         my ($uri) = @_;
32         #TODO: Improved apache conf needed
33         #if ($uri =~ m{^/.*/((\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?(?: |:(\d+))?)$}){
34         #       param('coords',$1);
35         #}
36 }
37
38 sub process {
39
40 }
41
42 sub render {
43         my ($DBH,$BODY) = @_;
44         $ND::TEMPLATE->param(TITLE => 'Check planets and galaxies');
45
46         $BODY->param(isBC => isMember() && (isOfficer() || isBC));
47
48         return $ND::NOACCESS unless $ND::ATTACKER;
49
50         my ($x,$y,$z);
51         if (param('coords') =~ /(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?(?: |:(\d+))?/){
52                 $x = $1;
53                 $y = $2;
54                 $z = $3;
55                 $BODY->param(Coords => param('coords'));
56         }else{
57                 $ND::ERROR .= p b q{Couldn't parse coords};
58                 return $BODY;
59         }
60
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);
65                 if ($count > 0){
66                         $BODY->param(Arbiter => '<b>DO NOT ATTACK THIS GAL</b>');
67                 }else{
68                         $BODY->param(Arbiter => '<b>KILL THESE BASTARDS</b>');
69                 }
70                 log_message $ND::UID,"Arbiter check on $x:$y";
71         }
72
73         my $where = '';
74         my $extra_columns = '';
75
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";
81         }
82
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});
84
85         if (defined $z){
86                 $query->execute($x,$y,$z);
87         }else{
88                 $query->execute($x,$y);
89                 if (isMember() && (isBC() || isOfficer()) && !isHC()){
90                         log_message $ND::UID,"BC browsing $x:$y";
91                 }
92         }
93         my @planets;
94         my $planet_id = undef;
95         my $i = 0;
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){
98                 $planet_id = $id;
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;
108                         $planet{isBC} = 1;
109                         if ($z && $alliance eq 'NewDawn' && not (isHC || isOfficer)){
110                                 log_message $ND::UID,"BC browsing ND planet $coords tick $ND::TICK";
111                         }
112                 }
113                 $i++;
114                 $planet{ODD} = $i % 2;
115                 push @planets,\%planet;
116         }
117         $BODY->param(Planets => \@planets);
118
119         if ($z && $planet_id){
120                 $BODY->param(OnePlanet => 1);
121
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
124                         FROM intel i
125                         JOIN (planets
126                         NATURAL JOIN planet_stats) p ON i.target = p.id
127                         JOIN (planets
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);
134                 my @missions;
135                 while (my ($mission,$landingtick,$eta,$amount,$target) = $query->fetchrow){
136                         push @missions,{Target => $target, Mission => $mission, LandingTick => $landingtick
137                                 , ETA => $eta, Amount => $amount};
138                 }
139                 $BODY->param(Missions => \@missions);
140
141                 my @scans;
142                 $query = $DBH->prepare(q{SELECT value,tick FROM planet_stats 
143                         WHERE id = ? AND tick > tick() - 24});
144                 my $scan = q{
145                 <p>Value the last 24 ticks</p>
146                 <table><tr><th>Tick</th><th>Value</th><th>Difference</th></tr>};
147                 my $old = 0;
148                 $query->execute($planet_id);
149                 while (my($value,$tick) = $query->fetchrow){
150                         my $diff = $value-$old;
151                         $old = $value;
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>};
155                 }
156                 $scan .= q{</table>};
157                 push @scans, {Scan => $scan};
158
159                 $query = $DBH->prepare(q{SELECT x,y,z,tick FROM planet_stats WHERE id = ?});
160                 $scan = q{
161                 <p>Previous Coords</p>
162                 <table><tr><th>Tick</th><th>Value</th><th>Difference</th></tr>};
163                 $query->execute($planet_id);
164                 $x = $y = $z = 0;
165                 while (my($nx,$ny,$nz,$tick) = $query->fetchrow){
166                         if ($nx != $x || $ny != $y || $nz != $z){
167                                 $x = $nx;
168                                 $y = $ny;
169                                 $z = $nz;
170                                 $scan .= qq{<tr><td>$tick</td><td>$x:$y:$z</td></tr>};
171                         }
172                 }
173                 $scan .= q{</table>};
174                 push @scans, {Scan => $scan};
175
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);
179                 my %scans;
180                 while (my($type,$scan_id,$tick,$scan) = $query->fetchrow){
181                         $scans{$type} = [$scan_id,$tick,$scan];
182                 }
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">};
190                         }
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>
193                                 $scan}};
194                 }
195
196                 $BODY->param(Scans => \@scans);
197         }
198         return $BODY;
199 }
200
201 1;