]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/LaunchConfirmation.pm
Converted addintel
[ndwebbie.git] / NDWeb / Pages / LaunchConfirmation.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::LaunchCoonfirmation;
21 use strict;
22 use warnings FATAL => 'all';
23 use CGI qw/:standard/;
24 use NDWeb::Include;
25 use ND::Include;
26
27 use base qw/NDWeb::XMLPage/;
28
29 $NDWeb::Page::PAGES{launchConfirmation} = __PACKAGE__;
30
31 sub render_body {
32         my $self = shift;
33         my ($BODY) = @_;
34         $self->{TITLE} = 'Launch Confirmation';
35         my $DBH = $self->{DBH};
36
37         return $self->noAccess unless $self->isMember;
38
39         my $error;
40
41
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
53                 });
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
63                 });
64                 my $addships = $DBH->prepare(q{INSERT INTO fleet_ships (id,ship,amount)
65                         VALUES (?,?,?)});
66                 my @missions;
67                 $DBH->begin_work;
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)
71                         (.*?)
72                         (?:Launching\ in\ tick\ (\d+),\ arrival\ in\ tick\ (\d+)
73                                 |ETA:\ \d+,\ Return\ ETA:\ (\d+)
74                                 |Return\ ETA:\ (\d+)
75                                 )/sgx){
76                         next if $10 eq 'Return';
77                         my %mission;
78                         my $name = $1;
79                         my $tick = $self->{TICK}+$9;
80                         $tick += $8 if defined $8;
81                         my $eta = $9;
82                         my $mission = $10;
83                         my $x = $5;
84                         my $y = $6;
85                         my $z = $7;
86                         my $back = $tick + $eta - 1;
87                         if ($13){
88                                 $tick = $13;
89                         }elsif ($14){
90                                 $back += $14;
91                         }
92                         $mission{Tick} = $tick;
93                         $mission{Mission} = $mission;
94                         $mission{Target} = "$x:$y:$z";
95                         $mission{Back} = $back;
96
97                         my ($planet_id) = $DBH->selectrow_array($findplanet,undef,$x,$y,$z,$self->{TICK});
98
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;;
106                         }
107
108                         my $ships = $11;
109                         my @ships;
110                         my $amount = 0;
111                         while ($ships =~ m/((?:\w+ )*\w+)\s+\w+\s+(?:(?:\w+|-)\s+){3}(?:Steal|Normal|Emp|Normal\s+Cloaked|Pod|Structure Killer)\s+(\d+)/g){
112                                 $amount += $2;
113                                 push @ships,{Ship => $1, Amount => $2};
114                         }
115                         $mission{Ships} = \@ships;
116
117                         if ($amount == 0){
118                                 warn "No ships in: $ships";
119                                 next;
120                         }
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;
127                         }
128
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}";
135                                 }else{
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";
140                                 }
141                         }elsif ($mission eq 'Defend'){
142                                 my $call = $findtarget->fetchrow_hashref;
143                                 $informDefChannel->execute($fleet,$call->{id}) or warn $DBH->errstr;
144                         }
145
146                         log_message $ND::UID,"Pasted confirmation for $mission mission to $x:$y:$z, landing tick $tick";
147                         push @missions,\%mission;
148                 }
149                 $DBH->commit or warn $DBH->errstr;
150                 $BODY->param(Missions => \@missions);
151         }
152         $BODY->param(Error => $error);
153         return $BODY;
154 }
155
156
157 1;