]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/Check.pm
38553fe3fa5800d9c921bc989c1647c64f9faf48
[ndwebbie.git] / NDWeb / 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 NDWeb::Pages::Check;
21 use strict;
22 use warnings;
23 use ND::Include;
24 use CGI qw/:standard/;
25 use NDWeb::Include;
26
27 use base qw/NDWeb::XMLPage/;
28
29 $NDWeb::Page::PAGES{check} = __PACKAGE__;
30
31 sub parse {
32         my $self = shift;
33         if ($self->{URI} =~ m{^/.*/((\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?(?: |:(\d+))?)$}){
34                 param('coords',$1);
35         }
36 }
37
38 sub render_body {
39         my $self = shift;
40         my ($BODY) = @_;
41         $self->{TITLE} = 'Check planets and galaxies';
42         my $DBH = $self->{DBH};
43
44         return $self->noAccess unless $self->{ATTACKER};
45
46         $BODY->param(isBC => $self->isMember && ($self->isOfficer || $self->isBC));
47
48         my ($x,$y,$z);
49         if (param('coords') =~ /(\d+)(?: |:)(\d+)(?:(?: |:)(\d+))?(?: |:(\d+))?/){
50                 $x = $1;
51                 $y = $2;
52                 $z = $3;
53                 $BODY->param(Coords => "$x:$y".(defined $z ? ":$z" : ''));
54         }else{
55                 $ND::ERROR .= p b q{Couldn't parse coords};
56                 return $BODY;
57         }
58
59         if ($self->isMember && defined param('cmd') && param('cmd') eq 'arbiter'){
60                 my $query = $DBH->prepare(q{SELECT count(*) AS friendlies FROM current_planet_stats WHERE x = ? AND y = ? 
61                         AND (planet_status IN ('Friendly','NAP') OR relationship IN ('Friendly','NAP'))});
62                 my ($count) = $DBH->selectrow_array($query,undef,$x,$y);
63                 if ($count > 0){
64                         $BODY->param(Arbiter => '<b>DO NOT ATTACK THIS GAL</b>');
65                 }else{
66                         $BODY->param(Arbiter => '<b>KILL THESE BASTARDS</b>');
67                 }
68                 log_message $ND::UID,"Arbiter check on $x:$y";
69         }
70
71         my $where = '';
72         my $extra_columns = '';
73
74         $where = 'AND z = ?' if defined $z;
75         if ($self->isMember && $self->isOfficer){
76                 $extra_columns = ",planet_status,hit_us, alliance,relationship,nick";
77         }elsif ($self->isMember && $self->isBC){
78                 $extra_columns = ", planet_status,hit_us, alliance,relationship";
79         }
80
81         my $query = $DBH->prepare(qq{Select id,coords(x,y,z), ((ruler || ' OF ') || p.planet) as planet,race,
82                 size, size_gain, size_gain_day,
83                 score,score_gain,score_gain_day,
84                 value,value_gain,value_gain_day,
85                 xp,xp_gain,xp_gain_day,
86                 sizerank,sizerank_gain,sizerank_gain_day,
87                 scorerank,scorerank_gain,scorerank_gain_day,
88                 valuerank,valuerank_gain,valuerank_gain_day,
89                 xprank,xprank_gain,xprank_gain_day,
90                 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_full p LEFT OUTER JOIN covop_targets c ON p.id = c.planet where x = ? AND y = ? $where order by x,y,z asc});
91
92         if (defined $z){
93                 $query->execute($x,$y,$z);
94         }else{
95                 $query->execute($x,$y);
96                 if ($self->isMember && ($self->isBC || $self->isOfficer) && !$self->isHC){
97                         log_message $ND::UID,"BC browsing $x:$y";
98                 }
99         }
100         my @planets;
101         my $planet_id = undef;
102         my $i = 0;
103         while (my $planet = $query->fetchrow_hashref){
104                 $planet_id = $planet->{id};
105                 for my $type (qw/size score value xp/){
106                         $planet->{"${type}img"} = 'stay';
107                         $planet->{"${type}img"} = 'up' if $planet->{"${type}_gain_day"} > 0;
108                         $planet->{"${type}img"} = 'down' if $planet->{"${type}_gain_day"} < 0;
109                         $planet->{"${type}rankimg"} = 'stay';
110                         $planet->{"${type}rankimg"} = 'up' if $planet->{"${type}rank_gain_day"} < 0;
111                         $planet->{"${type}rankimg"} = 'down' if $planet->{"${type}rank_gain_day"} > 0;
112                         for my $type ($type,"${type}_gain","${type}_gain_day"){
113                                 $planet->{$type} =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g; #Add comma for ever 3 digits, i.e. 1000 => 1,000
114                         }
115                 }
116                 if ($self->isMember && ($self->isOfficer || $self->isBC)){
117                         if ($z && $planet->{alliance} eq 'NewDawn' && not ($self->isHC || $self->isOfficer)){
118                                 log_message $ND::UID,"BC browsing ND planet $planet->{coords} tick $self->{TICK}";
119                         }
120                 }
121                 $i++;
122                 $planet->{ODD} = $i % 2;
123                 delete $planet->{id};
124                 push @planets,$planet;
125         }
126         $BODY->param(GPlanets => \@planets);
127
128         if ($z && $planet_id){
129                 $BODY->param(OnePlanet => 1);
130
131                 my $query = $DBH->prepare(q{ 
132                         SELECT i.id,i.mission, i.name, i.tick AS landingtick,MIN(eta) AS eta
133                                 , i.amount, coords(t.x,t.y,t.z) AS target
134                         FROM fleets i
135                         LEFT OUTER JOIN (planets
136                                 NATURAL JOIN planet_stats) t ON i.target = t.id
137                                         AND t.tick = ( SELECT max(tick) FROM planet_stats)
138                         WHERE  i.uid = -1
139                                 AND i.sender = ?
140                                 AND (i.tick > tick() - 14 OR i.mission = 'Full Fleet')
141                         GROUP BY i.id,t.x,t.y,t.z,i.mission,i.tick,i.name,i.amount,i.ingal,i.uid
142                         ORDER BY i.tick,t.x,t.y,t.z
143                 });
144                 $query->execute($planet_id);
145                 my $ships = $DBH->prepare(q{SELECT ship,amount FROM fleet_ships where id = ?});
146                 my @missions;
147                 $i = 0;
148                 while (my $mission = $query->fetchrow_hashref){
149                         $mission->{ODD} = $i++ % 2;
150                         $mission->{CLASS} = $mission->{mission};
151                         my @ships;
152                         $ships->execute($mission->{id});
153                         my $j = 0;
154                         while (my $ship = $ships->fetchrow_hashref){
155                                 $ship->{ODD} = $j++ % 2;
156                                 push @ships,$ship;
157                         }
158                         push @ships, {ship => 'No', amount => 'ships'} if @ships == 0;
159                         $mission->{ships} = \@ships;
160                         push @missions,$mission;
161                 }
162                 $BODY->param(Missions => \@missions);
163
164                 $query = $DBH->prepare(q{SELECT value,value_gain AS gain,tick FROM planet_stats 
165                         WHERE id = ? AND tick > tick() - 24});
166                 $query->execute($planet_id);
167                 my @values;
168                 while (my $value = $query->fetchrow_hashref){
169                         $value->{class} = 'Defend';
170                         $value->{class} = 'Attack' if $value->{gain} < 0;
171                         push @values, $value;
172                 }
173                 $BODY->param(Values => \@values);
174
175                 $query = $DBH->prepare(q{SELECT type,scan_id, tick FROM scans
176                         WHERE planet = ? AND tick > tick() - 168
177                         ORDER BY tick,type DESC
178                 });
179                 $query->execute($planet_id);
180                 my @scans;
181                 $i = 0;
182                 while (my $scan = $query->fetchrow_hashref){
183                         $scan->{ODD} = $i++ % 2;
184                         push @scans,$scan;
185                 }
186                 $BODY->param(Scans => \@scans);
187
188                 $query = $DBH->prepare(q{SELECT x,y,z,tick FROM planet_stats
189                         WHERE id = ? ORDER BY tick ASC});
190                 $query->execute($planet_id);
191                 my @coords;
192                 my $c = {x => 0, y => 0, z => 0};
193                 while (my $c2 = $query->fetchrow_hashref){
194                         if ($c->{x} != $c2->{x} || $c->{y} != $c2->{y} || $c->{z} != $c2->{z}){
195                                 $c = $c2;
196                                 push @coords,$c;
197                         }
198                 }
199                 $BODY->param(OldCoords => \@coords);
200
201         }
202         $query = $DBH->prepare(q{SELECT x,y,
203                 size, size_gain, size_gain_day,
204                 score,score_gain,score_gain_day,
205                 value,value_gain,value_gain_day,
206                 xp,xp_gain,xp_gain_day,
207                 sizerank,sizerank_gain,sizerank_gain_day,
208                 scorerank,scorerank_gain,scorerank_gain_day,
209                 valuerank,valuerank_gain,valuerank_gain_day,
210                 xprank,xprank_gain,xprank_gain_day,
211                 planets,planets_gain,planets_gain_day
212         FROM galaxies g 
213         WHERE tick = ( SELECT max(tick) AS max FROM galaxies)
214                 AND x = $1 AND y = $2
215                 });
216         $query->execute($x,$y) or $ND::ERROR .= p($DBH->errstr);
217
218         my @galaxies;
219         $i = 0;
220         while (my $galaxy = $query->fetchrow_hashref){
221                 for my $type (qw/planets size score xp value/){
222                         #$galaxy->{$type} = prettyValue($galaxy->{$type});
223                         next unless defined $galaxy->{"${type}_gain_day"};
224                         $galaxy->{"${type}img"} = 'stay';
225                         $galaxy->{"${type}img"} = 'up' if $galaxy->{"${type}_gain_day"} > 0;
226                         $galaxy->{"${type}img"} = 'down' if $galaxy->{"${type}_gain_day"} < 0;
227                         unless( $type eq 'planets'){
228                                 $galaxy->{"${type}rankimg"} = 'stay';
229                                 $galaxy->{"${type}rankimg"} = 'up' if $galaxy->{"${type}rank_gain_day"} < 0;
230                                 $galaxy->{"${type}rankimg"} = 'down' if $galaxy->{"${type}rank_gain_day"} > 0;
231                         }
232                         for my $type ($type,"${type}_gain","${type}_gain_day"){
233                                 $galaxy->{$type} =~ s/(^[-+]?\d+?(?=(?>(?:\d{3})+)(?!\d))|\G\d{3}(?=\d))/$1,/g; #Add comma for ever 3 digits, i.e. 1000 => 1,000
234                         }
235                 }
236                 $i++;
237                 $galaxy->{ODD} = $i % 2;
238                 push @galaxies,$galaxy;
239         }
240         $BODY->param(Galaxies => \@galaxies);
241
242         return $BODY;
243 }
244
245 1;