]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/Calls.pm
calls class
[ndwebbie.git] / ND / Web / Pages / Calls.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 package ND::Web::Pages::Calls;
20 use strict;
21 use warnings FATAL => 'all';
22 use ND::Include;
23 use CGI qw/:standard/;
24 use ND::Web::Include;
25
26 our @ISA = qw/ND::Web::XMLPage/;
27
28 $ND::Web::Page::PAGES{calls} = __PACKAGE__;
29
30 sub render_body {
31         my $self = shift;
32         my ($BODY) = @_;
33         $self->{TITLE} = 'Defense Calls';
34         my $DBH = $self->{DBH};
35
36         return $self->noAccess unless $self->isDC;
37
38         my $error;
39
40         my $call;
41         if (defined param('call') && param('call') =~ /^(\d+)$/){
42                 my $query = $DBH->prepare(q{
43                         SELECT c.id, coords(p.x,p.y,p.z), c.landing_tick, c.info, covered, open, dc.username AS dc, u.defense_points,c.member
44                         FROM calls c 
45                         JOIN users u ON c.member = u.uid
46                         LEFT OUTER JOIN users dc ON c.dc = dc.uid
47                         JOIN current_planet_stats p ON u.planet = p.id
48                         WHERE c.id = ?});
49                 $call = $DBH->selectrow_hashref($query,undef,$1);
50         }
51         if ($call && defined param('cmd')){
52                 if (param('cmd') eq 'Submit'){
53                         $DBH->begin_work;
54                         if (param('ctick')){
55                                 if ($DBH->do(q{UPDATE calls SET landing_tick = ? WHERE id = ?}
56                                                 ,undef,param('tick'),$call->{id})){
57                                         $call->{landing_tick} = param('tick');
58                                         log_message $ND::UID,"DC updated landing tick for call $call->{id}";
59                                 }else{
60                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
61                                 }
62                         }
63                         if (param('cinfo')){
64                                 if ($DBH->do(q{UPDATE calls SET info = ? WHERE id = ?}
65                                                 ,undef,param('info'),$call->{id})){
66                                         $call->{info} = param('info');
67                                         log_message $ND::UID,"DC updated info for call $call->{id}";
68                                 }else{
69                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
70                                 }
71                         }
72                         $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
73                 }elsif(param('cmd') =~ /^(Cover|Uncover|Ignore|Open|Take) call$/){
74                         $error .= "test";
75                         my $extra_vars = '';
76                         if (param('cmd') eq 'Cover call'){
77                                 $extra_vars = ", covered = TRUE, open = FALSE";
78                         }elsif (param('cmd') eq 'Uncover call'){
79                                 $extra_vars = ", covered = FALSE, open = TRUE";
80                         }elsif (param('cmd') eq 'Ignore call'){
81                                 $extra_vars = ", covered = FALSE, open = FALSE";
82                         }elsif (param('cmd') eq 'Open call'){
83                                 $extra_vars = ", covered = FALSE, open = TRUE";
84                         }
85                         if ($DBH->do(qq{UPDATE calls SET dc = ? $extra_vars WHERE id = ?},
86                                         ,undef,$ND::UID,$call->{id})){
87                                 $call->{covered} = (param('cmd') eq 'Cover call');
88                                 $call->{open} = (param('cmd') =~ /^(Uncover|Open) call$/);
89                                 $call->{DC} = $self->{USER};
90                         }else{
91                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
92                         }
93                 }elsif(param('cmd') eq 'Remove'){
94                         $DBH->begin_work;
95                         my $query = $DBH->prepare(q{DELETE FROM incomings WHERE id = ? AND call = ?});
96                         for my $param (param()){
97                                 if ($param =~ /^change:(\d+)$/){
98                                         if($query->execute($1,$call->{id})){
99                                                 log_message $ND::UID,"DC deleted fleet: $1, call $call->{id}";
100                                         }else{
101                                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
102                                         }
103                                 }
104                         }
105                         $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
106                 }elsif(param('cmd') eq 'Change'){
107                         $DBH->begin_work;
108                         my $query = $DBH->prepare(q{UPDATE incomings SET shiptype = ? WHERE id = ? AND call = ?});
109                         for my $param (param()){
110                                 if ($param =~ /^change:(\d+)$/){
111                                         my $shiptype = escapeHTML(param("shiptype:$1"));
112                                         if($query->execute($shiptype,$1,$call->{id})){
113                                                 log_message $ND::UID,"DC set fleet: $1, call $call->{id} to: $shiptype";
114                                         }else{
115                                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
116                                         }
117                                 }
118                         }
119                         $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
120                 }
121         }
122
123         if ($call){
124                 $BODY->param(Call => $call->{id});
125                 $BODY->param(Coords => $call->{coords});
126                 $BODY->param(DefensePoints => $call->{defense_points});
127                 $BODY->param(LandingTick => $call->{landing_tick});
128                 $BODY->param(ETA => $call->{landing_tick}-$self->{TICK});
129                 $BODY->param(Info => $call->{info});
130                 $BODY->param(DC => $call->{dc});
131                 if ($call->{covered}){
132                         $BODY->param(Cover => 'Uncover');
133                 }else{
134                         $BODY->param(Cover => 'Cover');
135                 }
136                 if ($call->{open} && !$call->{covered}){
137                         $BODY->param(Ignore => 'Ignore');
138                 }else{
139                         $BODY->param(Ignore => 'Open');
140                 }
141                 my $fleets = $DBH->prepare(q{
142                         SELECT id,mission,landing_tick,eta, back FROM fleets WHERE uid = ? AND (fleet = 0 OR (back >= ? AND landing_tick - eta - 11 < ? ))
143                         ORDER BY fleet ASC});
144                 my $ships = $DBH->prepare('SELECT ship,amount FROM fleet_ships WHERE fleet = ?');
145                 $fleets->execute($call->{member},$call->{landing_tick},$call->{landing_tick});
146                 my @fleets;
147                 my $i = 0;
148                 while (my $fleet = $fleets->fetchrow_hashref){
149                         if ($fleet->{back} == $call->{landing_tick}){
150                                 $fleet->{Fleetcatch} = 1;
151                         }
152                         $ships->execute($fleet->{id});
153                         my @ships;
154                         while (my $ship = $ships->fetchrow_hashref){
155                                 $i++;
156                                 $ship->{ODD} = $i % 2;
157                                 push @ships,$ship;
158                         }
159                         $fleet->{Ships} = \@ships;
160                         push @fleets, $fleet;
161                 }
162                 $BODY->param(Fleets => \@fleets);
163
164                 my $attackers = $DBH->prepare(q{
165                         SELECT coords(p.x,p.y,p.z), p.planet_status, p.race,i.eta,i.amount,i.fleet,i.shiptype,p.relationship,p.alliance,i.id
166                         FROM incomings i
167                         JOIN current_planet_stats p ON i.sender = p.id
168                         WHERE i.call = ?
169                         ORDER BY p.x,p.y,p.z});
170                 $attackers->execute($call->{id});
171                 my @attackers;
172                 $i = 0;
173                 while(my $attacker = $attackers->fetchrow_hashref){
174                         $i++;
175                         $attacker->{ODD} = $i % 2;
176                         push @attackers,$attacker;
177                 }
178                 $BODY->param(Attackers => \@attackers);
179         }else{
180                 my $where = 'open AND c.landing_tick-6 > tick()';
181                 if (defined param('show')){
182                         if (param('show') eq 'covered'){
183                                 $where = 'covered';
184                         }elsif (param('show') eq 'all'){
185                                 $where = 'true';
186                         }elsif (param('show') eq 'uncovered'){
187                                 $where = 'not covered';
188                         }
189                 }
190                 my $pointlimits = $DBH->prepare(q{SELECT value :: int FROM misc WHERE id = ?});
191                 my ($minpoints) = $DBH->selectrow_array($pointlimits,undef,'DEFMIN');
192                 my ($maxpoints) = $DBH->selectrow_array($pointlimits,undef,'DEFMAX');
193
194                 my $query = $DBH->prepare(qq{
195                         SELECT c.id, coords(p.x,p.y,p.z), u.defense_points, c.landing_tick, c.dc,
196                         TRIM('/' FROM concat(p2.race||' /')) AS race, TRIM('/' FROM concat(i.amount||' /')) AS amount,
197                         TRIM('/' FROM concat(i.eta||' /')) AS eta, TRIM('/' FROM concat(i.shiptype||' /')) AS shiptype,
198                         TRIM('/' FROM concat(c.landing_tick - tick() ||' /')) AS curreta,
199                         TRIM('/' FROM concat(p2.alliance ||' /')) AS alliance,
200                         TRIM('/' FROM concat(coords(p2.x,p2.y,p2.z) ||' /')) AS attackers
201                         FROM calls c 
202                         JOIN incomings i ON i.call = c.id
203                         JOIN users u ON c.member = u.uid
204                         JOIN current_planet_stats p ON u.planet = p.id
205                         JOIN current_planet_stats p2 ON i.sender = p2.id
206                         WHERE $where
207                         GROUP BY c.id, p.x,p.y,p.z, u.username, c.landing_tick, c.info,u.defense_points,c.dc
208                         ORDER BY c.landing_tick DESC
209                         })or $error .= $DBH->errstr;
210                 $query->execute or $error .= $DBH->errstr;
211                 my @calls;
212                 my $i = 0;
213                 my $tick = $self->{TICK};
214                 while (my $call = $query->fetchrow_hashref){
215                         if ($call->{defense_points} < $minpoints){
216                                 $call->{DefPrio} = 'LowestPrio';
217                         }elsif ($call->{defense_points} < $maxpoints){
218                                 $call->{DefPrio} = 'MediumPrio';
219                         }else{
220                                 $call->{DefPrio} = 'HighestPrio';
221                         }
222                         while ($tick - 24 > $call->{landing_tick}){
223                                 $tick -= 24;
224                                 push @calls,{};
225                                 $i = 0;
226                         }
227                         $call->{dc} = 'Hostile' unless defined $call->{dc};
228                         $i++;
229                         $call->{ODD} = $i % 2;
230                         $call->{shiptype} = escapeHTML($call->{shiptype});
231                         push @calls, $call;
232                 }
233                 $BODY->param(Calls => \@calls);
234         }
235         $BODY->param(Error => $error);
236         return $BODY;
237 }
238 1;