]> ruin.nu Git - ndwebbie.git/blob - launchConfirmation.pl
nicer output
[ndwebbie.git] / launchConfirmation.pl
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 use strict;
21 use warnings FATAL => 'all';
22
23 $ND::TEMPLATE->param(TITLE => 'Launch Confirmation');
24
25 our $BODY;
26 our $DBH;
27 our $LOG;
28 my $error;
29
30
31
32 die "You don't have access" unless isMember();
33
34 if (defined param('cmd') && param('cmd') eq 'submit'){
35         my $missions = param('mission');
36         my $findplanet = $DBH->prepare("SELECT planetid(?,?,?,?)");
37         my $findattacktarget = $DBH->prepare(q{SELECT c.target,c.wave,c.launched FROM  raid_claims c
38                 JOIN raid_targets t ON c.target = t.id
39                 JOIN raids r ON t.raid = r.id
40                 WHERE c.uid = ? AND r.tick+c.wave-1 = ? AND t.planet = ?
41                 AND r.open AND not r.removed});
42         my $finddefensetarget = $DBH->prepare(q{SELECT NULL});
43         my $addattackpoint = $DBH->prepare('UPDATE users SET attack_points = attack_points + 1 WHERE uid = ?');
44         my $launchedtarget = $DBH->prepare('UPDATE raid_claims SET launched = True WHERE uid = ? AND target = ? AND wave = ?');
45         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 = ?),?,?)});
46         my $addships = $DBH->prepare('INSERT INTO fleet_ships (fleet,ship,amount) VALUES (?,?,?)');
47
48         my $fleet = $DBH->prepare("SELECT id FROM fleets WHERE uid = ? AND fleet = 0");
49         my ($basefleet) = $DBH->selectrow_array($fleet,undef,$ND::UID);
50         unless ($basefleet){
51                 my $insert = $DBH->prepare(q{INSERT INTO fleets (uid,target,mission,landing_tick,fleet,eta,back) VALUES (?,?,'Full fleet',0,0,0,0)});
52                 $insert->execute($ND::UID,$ND::PLANET);
53         }
54         my @missions;
55         $DBH->begin_work;
56         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){
57                 my %mission;
58
59                 my $tick = $ND::TICK+$7+$8;
60                 my $eta = $8;
61                 my $mission = $9;
62                 my $x = $4;
63                 my $y = $5;
64                 my $z = $6;
65                 $mission{Tick} = $tick;
66                 $mission{Mission} = $mission;
67                 $mission{Target} = "$x:$y:$z";
68                 if ($12){
69                         $tick = $12;
70                 }elsif ($13){
71                         $eta += $13;
72                 }
73
74                 my ($planet_id) = $DBH->selectrow_array($findplanet,undef,$x,$y,$z,$ND::TICK);
75
76                 my $findtarget = $finddefensetarget;
77                 if ($mission eq 'Attack'){
78                         $findtarget = $findattacktarget;
79                 }elsif ($mission eq 'Defend'){
80                         $findtarget = $finddefensetarget;
81                 }
82
83                 $findtarget->execute($ND::UID,$tick,$planet_id);
84
85                 if ($findtarget->rows == 0){
86                         $mission{Warning} = "YOU DON'T HAVE A TARGET WITH THAT LANDING TICK";
87                 }elsif ($mission eq 'Attack'){
88                         my $claim = $findtarget->fetchrow_hashref;
89                         if ($claim->{launched}){
90                                 $mission{Warning} = "Already launched on this target:$claim->{target},$claim->{wave},$claim->{launched}";
91                         }else{
92                                 $addattackpoint->execute($ND::UID);
93                                 $launchedtarget->execute($ND::UID,$claim->{target},$claim->{wave});
94                                 $mission{Warning} = "OK:$claim->{target},$claim->{wave},$claim->{launched}";
95                                 $LOG->execute($ND::UID,"Gave attack point for confirmation on $mission mission to $x:$y:$z, landing tick $tick");
96                         }
97                 }
98
99                 $addfleet->execute($ND::UID,$planet_id,$mission,$tick,$ND::UID,$eta,$tick+$eta-1) or $error .= '<p>'.$DBH->errstr.'</p>';
100                 my $fleet = $DBH->last_insert_id(undef,undef,undef,undef,"fleets_id_seq");
101                 $mission{Fleet} = $fleet;
102                 $mission{Back} = $tick+$eta-1;
103                 my $ships = $10;
104                 my @ships;
105                 while ($ships =~ m/((?:\w+ )*\w+)\s+\w+\s+\w+\s+(?:Steal|Normal|Emp|Normal\s+Cloaked|Pod|Struc)\s+(\d+)/g){
106                         $addships->execute($fleet,$1,$2);
107                         push @ships,{Ship => $1, Amount => $2};
108                 }
109                 $mission{Ships} = \@ships;
110                 $LOG->execute($ND::UID,"Pasted confirmation for $mission mission to $x:$y:$z, landing tick $tick");
111                 push @missions,\%mission;
112         }
113         $DBH->commit or $error .= '<p>'.$DBH->errstr.'</p>';
114         $BODY->param(Missions => \@missions);
115 }
116 $BODY->param(Error => $error);
117
118
119 1;