]> ruin.nu Git - ndwebbie.git/blob - check.pl
perl authhandler and minor tweaks
[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 use warnings FATAL => 'all';
22 no warnings qw(uninitialized);
23
24 $ND::TEMPLATE->param(TITLE => 'Check planets and galaxies');
25
26 our $BODY;
27 our $DBH;
28 our $LOG;
29
30 $BODY->param(isBC => isMember() && (isOfficer() || isBC));
31
32
33 die "You don't have access" unless $ND::ATTACKER;
34
35 my ($x,$y,$z);
36 if (param('coords') =~ /(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?(?: |:(\d+))?/){
37         $x = $1;
38         $y = $2;
39         $z = $3;
40         $BODY->param(Coords => param('coords'));
41 }else{
42         die "Bad coords";
43 }
44
45 if (isMember() && param('cmd') eq 'arbiter'){
46         my $query = $DBH->prepare(q{SELECT count(*) AS friendlies FROM current_planet_stats WHERE x = ? AND y = ? 
47                 AND (planet_status IN ('Friendly','NAP') OR relationship IN ('Friendly','NAP'))});
48         my ($count) = $DBH->selectrow_array($query,undef,$x,$y);
49         if ($count > 0){
50                 $BODY->param(Arbiter => '<b>DO NOT ATTACK THIS GAL</b>');
51         }else{
52                 $BODY->param(Arbiter => '<b>KILL THESE BASTARDS</b>');
53         }
54         $LOG->execute($ND::UID,"Arbiter check on $x:$y");
55 }
56
57 my $where = '';
58 my $extra_columns = '';
59
60 $where = 'AND z = ?' if defined $z;
61 if (isMember() && isOfficer()){
62         $extra_columns = ",planet_status,hit_us, alliance,relationship,nick";
63 }elsif (isMember() && isBC()){
64         $extra_columns = ", planet_status,hit_us, alliance,relationship";
65 }
66
67 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});
68
69 if (defined $z){
70         $query->execute($x,$y,$z);
71 }else{
72         $query->execute($x,$y);
73         if (isMember() && (isBC() || isOfficer()) && !isHC()){
74                 $LOG->execute($ND::UID,"BC browsing $x:$y");
75         }
76 }
77 my @planets;
78 my $planet_id = undef;
79 my $i = 0;
80 while (my ($id,$coords,$planet,$race,$size,$score,$value,$xp,$sizerank,$scorerank,$valuerank,$xprank
81                 ,$fleetvalue,$resvalue,$planet_status,$hit_us,$alliance,$relationship,$nick) = $query->fetchrow){
82         $planet_id = $id;
83         my %planet = (Coords => $coords, Planet => $planet, Race => $race, Size => "$size ($sizerank)"
84                 , Score => "$score ($scorerank)", Value => "$value ($valuerank)", XP => "$xp ($xprank)"
85                 , FleetValue => "$fleetvalue ($resvalue)");
86         if (isMember() && (isOfficer() || isBC())){
87                 $planet{HitUs} = $hit_us;
88                 $planet{Alliance} = "$alliance ($relationship)";
89                 $planet{Nick} = "$nick ($planet_status)";
90                 $planet{PlanetStatus} = $planet_status;
91                 $planet{Relationship} = $relationship;
92                 $planet{isBC} = 1;
93                 if ($z && $alliance eq 'NewDawn'){
94                         $LOG->execute($ND::UID,"BC browsing ND planet $coords tick $ND::TICK");
95                 }
96         }
97         $i++;
98         $planet{ODD} = $i % 2;
99         push @planets,\%planet;
100 }
101 $BODY->param(Planets => \@planets);
102
103 if ($z && $planet_id){
104         $BODY->param(OnePlanet => 1);
105
106         my $query = $DBH->prepare(q{ 
107 SELECT i.mission, i.tick AS landingtick,MIN(eta) AS eta, i.amount, coords(p.x,p.y,p.z) AS target
108 FROM intel i
109         JOIN (planets
110                 NATURAL JOIN planet_stats) p ON i.target = p.id
111         JOIN (planets
112                 NATURAL JOIN planet_stats) p2 ON i.sender = p2.id
113 WHERE  p.tick = ( SELECT max(tick) FROM planet_stats) AND i.tick > tick() AND i.uid = -1 
114         AND p2.tick = p.tick AND p2.id = ?
115 GROUP BY p.x,p.y,p.z,p2.x,p2.y,p2.z,i.mission,i.tick,i.amount,i.ingal,i.uid
116 ORDER BY p.x,p.y,p.z});
117         $query->execute($planet_id);
118         my @missions;
119         while (my ($mission,$landingtick,$eta,$amount,$target) = $query->fetchrow){
120                 push @missions,{Target => $target, Mission => $mission, LandingTick => $landingtick
121                         , ETA => $eta, Amount => $amount};
122         }
123         $BODY->param(Missions => \@missions);
124
125         my @scans;
126         $query = $DBH->prepare(q{SELECT value,tick FROM planet_stats 
127                 WHERE id = ? AND tick > tick() - 24});
128         my $scan = q{
129 <p>Value the last 24 ticks</p>
130 <table><tr><th>Tick</th><th>Value</th><th>Difference</th></tr>};
131         my $old = 0;
132         $query->execute($planet_id);
133         while (my($value,$tick) = $query->fetchrow){
134                 my $diff = $value-$old;
135                 $old = $value;
136                 my $class = 'Defend';
137                 $class = 'Attack' if $diff < 0;
138                 $scan .= qq{<tr><td>$tick</td><td>$value</td><td class="$class">$diff</td></tr>};
139         }
140         $scan .= q{</table>};
141         push @scans, {Scan => $scan};
142
143         $query = $DBH->prepare(q{SELECT x,y,z,tick FROM planet_stats WHERE id = ?});
144         $scan = q{
145 <p>Previous Coords</p>
146 <table><tr><th>Tick</th><th>Value</th><th>Difference</th></tr>};
147         $query->execute($planet_id);
148         $x = $y = $z = 0;
149         while (my($nx,$ny,$nz,$tick) = $query->fetchrow){
150                 if ($nx != $x || $ny != $y || $nz != $z){
151                         $x = $nx;
152                         $y = $ny;
153                         $z = $nz;
154                         $scan .= qq{<tr><td>$tick</td><td>$x:$y:$z</td></tr>};
155                 }
156         }
157         $scan .= q{</table>};
158         push @scans, {Scan => $scan};
159
160         $query = $DBH->prepare(q{SELECT DISTINCT ON (type) type,scan_id, tick, scan FROM scans WHERE planet = ?
161                 GROUP BY type,scan_id, tick, scan ORDER BY type,tick DESC});
162         $query->execute($planet_id);
163         my %scans;
164         while (my($type,$scan_id,$tick,$scan) = $query->fetchrow){
165                 $scans{$type} = [$scan_id,$tick,$scan];
166         }
167         for my $type ('Planet','Jumpgate','Unit','Military','Fleet Analysis','Surface Analysis','Technology Analysis','News'){
168                 next unless exists $scans{$type};
169                 my $scan_id = $scans{$type}->[0];
170                 my $tick = $scans{$type}->[1];
171                 my $scan = $scans{$type}->[2];
172                 if ($ND::TICK - $tick > 10){
173                         $scan =~ s{<table( cellpadding="\d+")?>}{<table$1 class="old">};
174                 }
175                 push @scans,{Scan => qq{
176 <p><b><a href="http://game.planetarion.com/showscan.pl?scan_id=$scan_id">$type</a> Scan from tick $tick</b></p>
177 $scan}};
178         }
179
180         $BODY->param(Scans => \@scans);
181 }
182
183 1;