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 #**************************************************************************/
19 package NDWeb::Pages::Calls;
23 use CGI qw/:standard/;
26 use base qw/NDWeb::XMLPage/;
28 $NDWeb::Page::PAGES{calls} = __PACKAGE__;
33 $self->{TITLE} = 'Defense Calls';
34 my $DBH = $self->{DBH};
36 return $self->noAccess unless $self->isDC;
39 if (defined param('call') && param('call') =~ /^(\d+)$/){
40 my $query = $DBH->prepare(q{
41 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,u.planet, u.username AS member, u.sms
43 JOIN users u ON c.member = u.uid
44 LEFT OUTER JOIN users dc ON c.dc = dc.uid
45 JOIN current_planet_stats p ON u.planet = p.id
47 $call = $DBH->selectrow_hashref($query,undef,$1);
49 if ($call && defined param('cmd')){
50 if (param('cmd') eq 'Submit'){
53 if ($DBH->do(q{UPDATE calls SET landing_tick = ? WHERE id = ?}
54 ,undef,param('tick'),$call->{id})){
55 $call->{landing_tick} = param('tick');
56 log_message $ND::UID,"DC updated landing tick for call $call->{id}";
62 if ($DBH->do(q{UPDATE calls SET info = ? WHERE id = ?}
63 ,undef,param('info'),$call->{id})){
64 $call->{info} = param('info');
65 log_message $ND::UID,"DC updated info for call $call->{id}";
70 $DBH->commit or warn $DBH->errstr;
71 }elsif(param('cmd') =~ /^(Cover|Uncover|Ignore|Open|Take) call$/){
73 if (param('cmd') eq 'Cover call'){
74 $extra_vars = ", covered = TRUE, open = FALSE";
75 }elsif (param('cmd') eq 'Uncover call'){
76 $extra_vars = ", covered = FALSE, open = TRUE";
77 }elsif (param('cmd') eq 'Ignore call'){
78 $extra_vars = ", covered = FALSE, open = FALSE";
79 }elsif (param('cmd') eq 'Open call'){
80 $extra_vars = ", covered = FALSE, open = TRUE";
82 if ($DBH->do(qq{UPDATE calls SET dc = ? $extra_vars WHERE id = ?},
83 ,undef,$ND::UID,$call->{id})){
84 $call->{covered} = (param('cmd') eq 'Cover call');
85 $call->{open} = (param('cmd') =~ /^(Uncover|Open) call$/);
86 $call->{DC} = $self->{USER};
90 }elsif(param('cmd') eq 'Remove'){
92 my $query = $DBH->prepare(q{DELETE FROM incomings WHERE id = ? AND call = ?});
93 for my $param (param()){
94 if ($param =~ /^change:(\d+)$/){
95 if($query->execute($1,$call->{id})){
96 log_message $ND::UID,"DC deleted fleet: $1, call $call->{id}";
102 $DBH->commit or warn $DBH->errstr;
103 }elsif(param('cmd') eq 'Change'){
105 my $query = $DBH->prepare(q{UPDATE incomings SET shiptype = ? WHERE id = ? AND call = ?});
106 for my $param (param()){
107 if ($param =~ /^change:(\d+)$/){
108 my $shiptype = escapeHTML(param("shiptype:$1"));
109 if($query->execute($shiptype,$1,$call->{id})){
110 log_message $ND::UID,"DC set fleet: $1, call $call->{id} to: $shiptype";
116 $DBH->commit or warn $DBH->errstr;
121 $BODY->param(Call => $call->{id});
122 $BODY->param(Coords => $call->{coords});
123 $BODY->param(DefensePoints => $call->{defense_points});
124 $BODY->param(LandingTick => $call->{landing_tick});
125 $BODY->param(ETA => $call->{landing_tick}-$self->{TICK});
126 $BODY->param(Info => $call->{info});
127 $BODY->param(DC => $call->{dc});
128 $BODY->param(Member => $call->{member});
129 $BODY->param(SMS => $call->{sms});
130 if ($call->{covered}){
131 $BODY->param(Cover => 'Uncover');
133 $BODY->param(Cover => 'Cover');
135 if ($call->{open} && !$call->{covered}){
136 $BODY->param(Ignore => 'Ignore');
138 $BODY->param(Ignore => 'Open');
141 my $outgoings = $DBH->prepare(q{
142 SELECT i.id,i.mission, i.name, i.tick,eta
143 , i.amount, coords(x,y,z) AS target
145 LEFT OUTER JOIN (planets
146 NATURAL JOIN planet_stats) t ON i.target = t.id
147 AND t.tick = ( SELECT MAX(tick) FROM planet_stats)
149 AND (i.tick > $2 - 14 OR i.mission = 'Full Fleet')
150 ORDER BY i.tick,x,y,z
152 my $ships = $DBH->prepare('SELECT ship,amount FROM fleet_ships WHERE id = ?');
153 $outgoings->execute($call->{planet},$call->{landing_tick});
155 while (my $fleet = $outgoings->fetchrow_hashref){
156 if (defined $fleet->{back} &&
157 $fleet->{back} == $call->{landing_tick}){
158 $fleet->{Fleetcatch} = 1;
160 $ships->execute($fleet->{id});
162 while (my $ship = $ships->fetchrow_hashref){
165 $fleet->{Ships} = \@ships;
166 push @fleets, $fleet;
168 $BODY->param(Fleets => \@fleets);
170 my $defenders = $DBH->prepare(q{
171 SELECT i.id,i.mission, i.name, i.tick,eta
172 , i.amount, coords(x,y,z) AS sender
174 LEFT OUTER JOIN (planets
175 NATURAL JOIN planet_stats) s ON i.sender = s.id
176 AND s.tick = ( SELECT MAX(tick) FROM planet_stats)
178 AND i.tick = ? AND i.mission = 'Defend'
179 ORDER BY i.tick,x,y,z
182 $defenders->execute($call->{planet},$call->{landing_tick}) or warn $DBH->errstr;
184 while (my $fleet = $defenders->fetchrow_hashref){
185 $fleet->{CLASS} = $fleet->{mission};
186 $ships->execute($fleet->{id});
188 while (my $ship = $ships->fetchrow_hashref){
191 $fleet->{Ships} = \@ships;
193 push @defenders, $fleet;
195 $BODY->param(Defenders => \@defenders);
197 my $attackers = $DBH->prepare(q{
198 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,p.id AS planet
200 JOIN current_planet_stats p ON i.sender = p.id
204 $attackers->execute($call->{id});
206 while(my $attacker = $attackers->fetchrow_hashref){
207 $outgoings->execute($attacker->{planet},$call->{landing_tick});
209 while (my $mission = $outgoings->fetchrow_hashref){
210 $mission->{eta} = '?' if not defined $mission->{eta};
211 $mission->{amount} = '?' if not defined $mission->{amount};
212 $mission->{CLASS} = $mission->{mission};
214 $ships->execute($mission->{id});
215 while (my $ship = $ships->fetchrow_hashref){
218 push @ships, {ship => 'No', amount => 'ships'} if @ships == 0;
219 $mission->{ships} = \@ships;
220 push @missions,$mission;
222 delete $attacker->{planet};
223 $attacker->{missions} = \@missions;
224 push @attackers,$attacker;
226 $BODY->param(Attackers => \@attackers);
228 my $where = 'open AND c.landing_tick-6 > tick()';
229 if (defined param('show')){
230 if (param('show') eq 'covered'){
232 }elsif (param('show') eq 'all'){
234 }elsif (param('show') eq 'uncovered'){
235 $where = 'not covered';
238 my $pointlimits = $DBH->prepare(q{SELECT value :: int FROM misc WHERE id = ?});
239 my ($minpoints) = $DBH->selectrow_array($pointlimits,undef,'DEFMIN');
240 my ($maxpoints) = $DBH->selectrow_array($pointlimits,undef,'DEFMAX');
242 my $query = $DBH->prepare(qq{
243 SELECT id,coords(x,y,z),defense_points,landing_tick,dc,curreta,fleets,
244 TRIM('/' FROM concat(DISTINCT race||' /')) AS race, TRIM('/' FROM concat(amount||' /')) AS amount,
245 TRIM('/' FROM concat(DISTINCT eta||' /')) AS eta, TRIM('/' FROM concat(DISTINCT shiptype||' /')) AS shiptype,
246 TRIM('/' FROM concat(DISTINCT alliance ||' /')) AS alliance,
247 TRIM('/' FROM concat(coords||' /')) AS attackers
248 FROM (SELECT c.id, p.x,p.y,p.z, u.defense_points, c.landing_tick, dc.username AS dc,
249 (c.landing_tick - tick()) AS curreta,p2.race, i.amount, i.eta, i.shiptype, p2.alliance,
250 coords(p2.x,p2.y,p2.z), COUNT(DISTINCT f.id) AS fleets
252 JOIN incomings i ON i.call = c.id
253 JOIN users u ON c.member = u.uid
254 JOIN current_planet_stats p ON u.planet = p.id
255 JOIN current_planet_stats p2 ON i.sender = p2.id
256 LEFT OUTER JOIN users dc ON c.dc = dc.uid
257 LEFT OUTER JOIN fleets f ON f.target = u.planet AND f.tick = c.landing_tick AND f.back = f.tick + f.eta - 1
259 GROUP BY c.id, p.x,p.y,p.z, c.landing_tick, u.defense_points,dc.username,p2.race,i.amount,i.eta,i.shiptype,p2.alliance,p2.x,p2.y,p2.z) a
260 GROUP BY id, x,y,z,landing_tick, defense_points,dc,curreta,fleets
261 ORDER BY landing_tick DESC
262 })or warn $DBH->errstr;
263 $query->execute or warn $DBH->errstr;
265 my $tick = $self->{TICK};
266 while (my $call = $query->fetchrow_hashref){
267 if ($call->{defense_points} < $minpoints){
268 $call->{DefPrio} = 'LowestPrio';
269 }elsif ($call->{defense_points} < $maxpoints){
270 $call->{DefPrio} = 'MediumPrio';
272 $call->{DefPrio} = 'HighestPrio';
274 while ($tick - 24 > $call->{landing_tick}){
278 $call->{attackers} =~ s{(\d+:\d+:\d+)}{<a href="/check?coords=$1">$1</a>}g;
279 $call->{dcstyle} = 'Hostile' unless defined $call->{dc};
280 $call->{shiptype} = $call->{shiptype};
283 $BODY->param(Calls => \@calls);