]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/LaunchConfirmation.pm
79ce12fbb2cb6e50d02932abe069eacde3dbbae7
[ndwebbie.git] / ND / Web / 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 NULL});
51                 my $addattackpoint = $DBH->prepare('UPDATE users SET attack_points = attack_points + 1 WHERE uid = ?');
52                 my $launchedtarget = $DBH->prepare('UPDATE raid_claims SET launched = True WHERE uid = ? AND target = ? AND wave = ?');
53                 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 = ?),?,?)});
54                 my $addships = $DBH->prepare('INSERT INTO fleet_ships (fleet,ship,amount) VALUES (?,?,?)');
55
56                 my $fleet = $DBH->prepare("SELECT id FROM fleets WHERE uid = ? AND fleet = 0");
57                 my ($basefleet) = $DBH->selectrow_array($fleet,undef,$ND::UID) or $ND::ERROR .= p $DBH->errstr;;
58                 unless ($basefleet){
59                         my $insert = $DBH->prepare(q{INSERT INTO fleets (uid,target,mission,landing_tick,fleet,eta,back) VALUES (?,?,'Full fleet',0,0,0,0)});
60                         $insert->execute($ND::UID,$self->{PLANET}) or $ND::ERROR .= p $DBH->errstr;;
61                 }
62                 my @missions;
63                 $DBH->begin_work;
64                 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+))/sg){
65                         my %mission;
66
67                         my $tick = $self->{TICK}+$8;
68                         $tick += $7 if defined $7;
69                         my $eta = $8;
70                         my $mission = $9;
71                         my $x = $4;
72                         my $y = $5;
73                         my $z = $6;
74                         if ($12){
75                                 $tick = $12;
76                         }elsif ($13){
77                                 $eta += $13;
78                         }
79                         $mission{Tick} = $tick;
80                         $mission{Mission} = $mission;
81                         $mission{Target} = "$x:$y:$z";
82
83                         my ($planet_id) = $DBH->selectrow_array($findplanet,undef,$x,$y,$z,$self->{TICK});
84
85                         my $findtarget = $finddefensetarget;
86                         if ($mission eq 'Attack'){
87                                 $findtarget = $findattacktarget;
88                         }elsif ($mission eq 'Defend'){
89                                 $findtarget = $finddefensetarget;
90                         }
91
92                         $findtarget->execute($ND::UID,$tick,$planet_id) or $ND::ERROR .= p $DBH->errstr;;
93
94                         if ($findtarget->rows == 0){
95                                 $mission{Warning} = "YOU DON'T HAVE A TARGET WITH THAT LANDING TICK";
96                         }elsif ($mission eq 'Attack'){
97                                 my $claim = $findtarget->fetchrow_hashref;
98                                 if ($claim->{launched}){
99                                         $mission{Warning} = "Already launched on this target:$claim->{target},$claim->{wave},$claim->{launched}";
100                                 }else{
101                                         $addattackpoint->execute($ND::UID) or $ND::ERROR .= p $DBH->errstr;
102                                         $launchedtarget->execute($ND::UID,$claim->{target},$claim->{wave}) or $ND::ERROR .= p $DBH->errstr;
103                                         $mission{Warning} = "OK:$claim->{target},$claim->{wave},$claim->{launched}";
104                                         log_message $ND::UID,"Gave attack point for confirmation on $mission mission to $x:$y:$z, landing tick $tick";
105                                 }
106                         }
107
108                         $addfleet->execute($ND::UID,$planet_id,$mission,$tick,$ND::UID,$eta,$tick+$eta-1) or $error .= '<p>'.$DBH->errstr.'</p>';
109                         my $fleet = $DBH->last_insert_id(undef,undef,undef,undef,"fleets_id_seq");
110                         $mission{Fleet} = $fleet;
111                         $mission{Back} = $tick+$eta-1;
112                         my $ships = $10;
113                         my @ships;
114                         while ($ships =~ m/((?:\w+ )*\w+)\s+\w+\s+\w+\s+(?:Steal|Normal|Emp|Normal\s+Cloaked|Pod|Struc)\s+(\d+)/g){
115                                 $addships->execute($fleet,$1,$2) or $ND::ERROR .= p $DBH->errstr;
116                                 push @ships,{Ship => $1, Amount => $2};
117                         }
118                         $mission{Ships} = \@ships;
119                         log_message $ND::UID,"Pasted confirmation for $mission mission to $x:$y:$z, landing tick $tick";
120                         push @missions,\%mission;
121                 }
122                 $DBH->commit or $error .= '<p>'.$DBH->errstr.'</p>';
123                 $BODY->param(Missions => \@missions);
124         }
125         $BODY->param(Error => $error);
126         return $BODY;
127 }
128
129
130 1;