]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/LaunchConfirmation.pm
afb4b55d2ba92d94533e6a04f655aac4ac760fd2
[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 ND::Web::Pages::LaunchCoonfirmation;
21 use strict;
22 use warnings FATAL => 'all';
23 use CGI qw/:standard/;
24 use ND::Web::Include;
25 use ND::Include;
26
27 use base qw/ND::Web::XMLPage/;
28
29 $ND::Web::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 JOIN users u ON c.member = u.uid WHERE u.planet = $1 AND c.landing_tick = $2});
51                 my $informDefChannel = $DBH->prepare(q{INSERT INTO defense_missions (fleet,call) VALUES (?,?)});
52                 my $addattackpoint = $DBH->prepare('UPDATE users SET attack_points = attack_points + 1 WHERE uid = ?');
53                 my $launchedtarget = $DBH->prepare('UPDATE raid_claims SET launched = True WHERE uid = ? AND target = ? AND wave = ?');
54                 my $addfleet = $DBH->prepare(qq{INSERT INTO fleets (uid,target,mission,landing_tick,fleet,eta,back) VALUES (?,?,?,?,(SELECT max(fleet)+1 from fleets WHERE uid = ?),?,?)});
55                 my $addships = $DBH->prepare('INSERT INTO fleet_ships (fleet,ship,amount) VALUES (?,?,?)');
56
57                 my $fleet = $DBH->prepare("SELECT id FROM fleets WHERE uid = ? AND fleet = 0");
58                 my ($basefleet) = $DBH->selectrow_array($fleet,undef,$ND::UID) or $ND::ERROR .= p $DBH->errstr;;
59                 unless ($basefleet){
60                         my $insert = $DBH->prepare(q{INSERT INTO fleets (uid,target,mission,landing_tick,fleet,eta,back) VALUES (?,?,'Full fleet',0,0,0,0)});
61                         $insert->execute($ND::UID,$self->{PLANET}) or $ND::ERROR .= p $DBH->errstr;;
62                 }
63                 my @missions;
64                 $DBH->begin_work;
65                 while ($missions =~ m/\S+\s+(\d+):(\d+):(\d+)\s+(\d+):(\d+):(\d+)\s+\((?:(\d+)\+)?(\d+)\).*?(?:\d+hrs\s+)?\d+mins?\s+(Attack|Defend|Return|Fake Attack|Fake Defend)(.*?)(?:Launching in tick (\d+), arrival in tick (\d+)|ETA: \d+, Return ETA: (\d+)|Return ETA: (\d+))/sg){
66                         my %mission;
67                         next if $9 eq 'Return';
68
69                         my $tick = $self->{TICK}+$8;
70                         $tick += $7 if defined $7;
71                         my $eta = $8;
72                         my $mission = $9;
73                         my $x = $4;
74                         my $y = $5;
75                         my $z = $6;
76                         if ($12){
77                                 $tick = $12;
78                         }elsif ($13){
79                                 $eta += $13;
80                         }
81                         $mission{Tick} = $tick;
82                         $mission{Mission} = $mission;
83                         $mission{Target} = "$x:$y:$z";
84
85                         my ($planet_id) = $DBH->selectrow_array($findplanet,undef,$x,$y,$z,$self->{TICK});
86
87                         my $findtarget = $finddefensetarget;
88                         if ($mission eq 'Attack'){
89                                 $findtarget = $findattacktarget;
90                                 $findtarget->execute($ND::UID,$tick,$planet_id) or $ND::ERROR .= p $DBH->errstr;;
91                         }elsif ($mission eq 'Defend'){
92                                 $findtarget = $finddefensetarget;
93                                 $findtarget->execute($planet_id,$tick) or $ND::ERROR .= p $DBH->errstr;;
94                         }
95
96                         $addfleet->execute($ND::UID,$planet_id,$mission,$tick,$ND::UID,$eta,$tick+$eta-1) or $error .= '<p>'.$DBH->errstr.'</p>';
97                         my $fleet = $DBH->last_insert_id(undef,undef,undef,undef,"fleets_id_seq");
98                         $mission{Fleet} = $fleet;
99                         $mission{Back} = $tick+$eta-1;
100                         my $ships = $10;
101                         my @ships;
102                         while ($ships =~ m/((?:\w+ )*\w+)\s+\w+\s+\w+\s+(?:Steal|Normal|Emp|Normal\s+Cloaked|Pod|Struc)\s+(\d+)/g){
103                                 $addships->execute($fleet,$1,$2) or $ND::ERROR .= p $DBH->errstr;
104                                 push @ships,{Ship => $1, Amount => $2};
105                         }
106                         $mission{Ships} = \@ships;
107
108                         if ($findtarget->rows == 0){
109                                 $mission{Warning} = p b 'No matching target!';
110                         }elsif ($mission eq 'Attack'){
111                                 my $claim = $findtarget->fetchrow_hashref;
112                                 if ($claim->{launched}){
113                                         $mission{Warning} = "Already launched on this target:$claim->{target},$claim->{wave},$claim->{launched}";
114                                 }else{
115                                         $addattackpoint->execute($ND::UID) or $ND::ERROR .= p $DBH->errstr;
116                                         $launchedtarget->execute($ND::UID,$claim->{target},$claim->{wave}) or $ND::ERROR .= p $DBH->errstr;
117                                         $mission{Warning} = "OK:$claim->{target},$claim->{wave},$claim->{launched}";
118                                         log_message $ND::UID,"Gave attack point for confirmation on $mission mission to $x:$y:$z, landing tick $tick";
119                                 }
120                         }elsif ($mission eq 'Defend'){
121                                 my $call = $findtarget->fetchrow_hashref;
122                                 $informDefChannel->execute($fleet,$call->{id}) or $ND::ERROR .= p $DBH->errstr;
123                         }
124
125                         log_message $ND::UID,"Pasted confirmation for $mission mission to $x:$y:$z, landing tick $tick";
126                         push @missions,\%mission;
127                 }
128                 $DBH->commit or $error .= '<p>'.$DBH->errstr.'</p>';
129                 $BODY->param(Missions => \@missions);
130         }
131         $BODY->param(Error => $error);
132         return $BODY;
133 }
134
135
136 1;