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