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 #**************************************************************************/
20 package NDWeb::Pages::LaunchCoonfirmation;
22 use warnings FATAL => 'all';
23 use CGI qw/:standard/;
27 use base qw/NDWeb::XMLPage/;
29 $NDWeb::Page::PAGES{launchConfirmation} = __PACKAGE__;
34 $self->{TITLE} = 'Launch Confirmation';
35 my $DBH = $self->{DBH};
37 return $self->noAccess unless $self->isMember;
42 if (defined param('cmd') && param('cmd') eq 'submit'){
43 my $missions = param('mission');
44 my $findplanet = $DBH->prepare("SELECT planetid(?,?,?,?)");
45 my $findattacktarget = $DBH->prepare(q{SELECT c.target,c.wave,c.launched FROM raid_claims c
46 JOIN raid_targets t ON c.target = t.id
47 JOIN raids r ON t.raid = r.id
48 WHERE c.uid = ? AND r.tick+c.wave-1 = ? AND t.planet = ?
49 AND r.open AND not r.removed});
50 my $finddefensetarget = $DBH->prepare(q{SELECT c.id FROM calls c
51 JOIN users u ON c.member = u.uid
52 WHERE u.planet = $1 AND c.landing_tick = $2
54 my $informDefChannel = $DBH->prepare(q{INSERT INTO defense_missions
55 (fleet,call) VALUES (?,?)});
56 my $addattackpoint = $DBH->prepare(q{UPDATE users SET
57 attack_points = attack_points + 1 WHERE uid = ?});
58 my $launchedtarget = $DBH->prepare(q{UPDATE raid_claims SET launched = True
59 WHERE uid = ? AND target = ? AND wave = ?});
60 my $addfleet = $DBH->prepare(q{INSERT INTO fleets
61 (uid,name,mission,sender,target,tick,eta,back,amount)
62 VALUES (?,?,?,?,?,?,?,?,?) RETURNING id
64 my $addships = $DBH->prepare(q{INSERT INTO fleet_ships (id,ship,amount)
68 while ($missions =~ m/([^\n]+)\s+(\d+):(\d+):(\d+)\s+(\d+):(\d+):(\d+)
69 \s+\((?:(\d+)\+)?(\d+)\).*?(?:\d+hrs\s+)?\d+mins?\s+
70 (Attack|Defend|Return|Fake Attack|Fake Defend)
72 (?:Launching\ in\ tick\ (\d+),\ arrival\ in\ tick\ (\d+)
73 |ETA:\ \d+,\ Return\ ETA:\ (\d+)
76 next if $10 eq 'Return';
79 my $tick = $self->{TICK}+$9;
80 $tick += $8 if defined $8;
86 my $back = $tick + $eta - 1;
92 $mission{Tick} = $tick;
93 $mission{Mission} = $mission;
94 $mission{Target} = "$x:$y:$z";
95 $mission{Back} = $back;
97 my ($planet_id) = $DBH->selectrow_array($findplanet,undef,$x,$y,$z,$self->{TICK});
99 my $findtarget = $finddefensetarget;
100 if ($mission eq 'Attack'){
101 $findtarget = $findattacktarget;
102 $findtarget->execute($ND::UID,$tick,$planet_id) or warn $DBH->errstr;;
103 }elsif ($mission eq 'Defend'){
104 $findtarget = $finddefensetarget;
105 $findtarget->execute($planet_id,$tick) or warn $DBH->errstr;;
111 while ($ships =~ m/((?:\w+ )*\w+)\s+\w+\s+(?:(?:\w+|-)\s+){3}(?:Steal|Normal|Emp|Normal\s+Cloaked|Pod|Structure Killer)\s+(\d+)/g){
113 push @ships,{Ship => $1, Amount => $2};
115 $mission{Ships} = \@ships;
118 warn "No ships in: $ships";
121 my $fleet = $DBH->selectrow_array($addfleet,undef,$ND::UID,$name,$mission
122 ,$self->{PLANET},$planet_id,$tick,$eta,$back,$amount);
123 $mission{Fleet} = $fleet;
124 for my $ship (@ships){
125 $addships->execute($fleet,$ship->{Ship},$ship->{Amount})
126 or warn $DBH->errstr;
129 if ($findtarget->rows == 0){
130 $mission{Warning} = p b 'No matching target!';
131 }elsif ($mission eq 'Attack'){
132 my $claim = $findtarget->fetchrow_hashref;
133 if ($claim->{launched}){
134 $mission{Warning} = "Already launched on this target:$claim->{target},$claim->{wave},$claim->{launched}";
136 $addattackpoint->execute($ND::UID) or warn $DBH->errstr;
137 $launchedtarget->execute($ND::UID,$claim->{target},$claim->{wave}) or warn $DBH->errstr;
138 $mission{Warning} = "OK:$claim->{target},$claim->{wave},$claim->{launched}";
139 log_message $ND::UID,"Gave attack point for confirmation on $mission mission to $x:$y:$z, landing tick $tick";
141 }elsif ($mission eq 'Defend'){
142 my $call = $findtarget->fetchrow_hashref;
143 $informDefChannel->execute($fleet,$call->{id}) or warn $DBH->errstr;
146 log_message $ND::UID,"Pasted confirmation for $mission mission to $x:$y:$z, landing tick $tick";
147 push @missions,\%mission;
149 $DBH->commit or warn $DBH->errstr;
150 $BODY->param(Missions => \@missions);
152 $BODY->param(Error => $error);