]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/EditRaid.pm
only update fleet when it was possible to parse something
[ndwebbie.git] / ND / Web / Pages / EditRaid.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 package ND::Web::Pages::EditRaid;
20 use strict;
21 use warnings FATAL => 'all';
22 use ND::Include;
23 use CGI qw/:standard/;
24 use ND::Web::Include;
25
26 use base qw/ND::Web::XMLPage/;
27
28 $ND::Web::Page::PAGES{editRaid} = __PACKAGE__;
29
30 sub render_body {
31         my $self = shift;
32         my ($BODY) = @_;
33         $self->{TITLE} = 'Create/Edit Raids';
34         my $DBH = $self->{DBH};
35
36         return $self->noAccess unless $self->isBC;
37         my $error;
38
39         my @alliances = alliances();
40         $BODY->param(Alliances => \@alliances);
41
42         my $raid;
43         if (defined param 'raid' and param('raid') =~ /^(\d+)$/){
44                 my $query = $DBH->prepare(q{SELECT id,tick,waves,message,released_coords,open FROM raids WHERE id = ?});
45                 $raid = $DBH->selectrow_hashref($query,undef,$1);
46         }
47         if (defined param('cmd') && param('cmd') eq 'submit'){
48                 my $query = $DBH->prepare(q{INSERT INTO raids (tick,waves,message) VALUES(?,?,'')});
49                 if ($query->execute(param('tick'),param('waves'))){
50                         $raid = $DBH->last_insert_id(undef,undef,undef,undef,"raids_id_seq");
51                         my $query = $DBH->prepare(q{SELECT id,tick,waves,message,released_coords,open FROM raids WHERE id = ?});
52                         $raid = $DBH->selectrow_hashref($query,undef,$raid);
53                 }else{
54                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
55                 }
56         }
57
58         if ($raid && defined param('cmd')){
59                 if (param('cmd') eq 'remove'){
60                         $DBH->do(q{UPDATE raids SET open = FALSE, removed = TRUE WHERE id = ?},undef,$raid->{id});
61                 }elsif (param('cmd') eq 'Open'){
62                         if($DBH->do(q{UPDATE raids SET open = TRUE, removed = FALSE WHERE id = ?},undef,$raid->{id})){
63                                 $raid->{open} = 1;
64                                 $raid->{removed} = 0;
65                         }
66                 }elsif (param('cmd') eq 'Close'){
67                         if ($DBH->do(q{UPDATE raids SET open = FALSE WHERE id = ?},undef,$raid->{id})){
68                                 $raid->{open} = 0;
69                         }
70                 }elsif (param('cmd') eq 'showcoords'){
71                         if($DBH->do(q{UPDATE raids SET released_coords = TRUE WHERE id = ?},undef,$raid->{id})){
72                                 $raid->{released_coords} = 1;
73                         }
74                 }elsif (param('cmd') eq 'hidecoords'){
75                         if($DBH->do(q{UPDATE raids SET released_coords = FALSE WHERE id = ?},undef,$raid->{id})){
76                                 $raid->{released_coords} = 0;
77                         }
78                 }elsif (param('cmd') eq 'comment'){
79                         $DBH->do(q{UPDATE raid_targets SET comment = ? WHERE id = ?}
80                                 ,undef,escapeHTML(param('comment')),param('target'))
81                                 or $error .= p($DBH->errstr);
82
83                 }elsif (param('cmd') eq 'change' || param('cmd') eq 'submit'){
84                         $DBH->begin_work;
85                         my $message = escapeHTML(param('message'));
86                         $raid->{message} = $message;
87                         $raid->{waves} = param('waves');
88                         $raid->{tick} = param('tick');
89                         unless ($DBH->do(qq{UPDATE raids SET message = ?, tick = ?, waves = ? WHERE id = ?}
90                                         ,undef,$message,param('tick'),param('waves'),$raid->{id})){
91                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
92                         }
93                         my $sizelimit = '';
94                         if (param('sizelimit') =~ /^(\d+)$/){
95                                 $sizelimit = "AND p.size >= $1";
96                                 unless ($DBH->do(qq{DELETE FROM raid_targets WHERE id IN (SELECT t.id FROM current_planet_stats p 
97                                                 JOIN raid_targets t ON p.id = t.planet WHERE p.size < ? AND t.raid = ?)},undef,$1,$raid->{id})){
98                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
99                                 }
100                         }
101                         my $targets = param('targets');
102                         my $addtarget = $DBH->prepare(qq{INSERT INTO raid_targets(raid,planet) (
103                                 SELECT ?, id FROM current_planet_stats p WHERE x = ? AND y = ? AND COALESCE(z = ?,TRUE) $sizelimit)});
104                         while ($targets =~ m/(\d+):(\d+)(?::(\d+))?/g){
105                                 unless ($addtarget->execute($raid->{id},$1,$2,$3)){
106                                         $error .= "<p> Something went wrong when adding $1:$2".($3 ? ":$3" : '').": ".$DBH->errstr."</p>";
107                                 }
108                         }
109                         if (param('alliance') =~ /^(\d+)$/ && $1 != 1){
110                                 log_message $ND::UID,"BC adding alliance $1 to raid";
111                                 my $addtarget = $DBH->prepare(qq{INSERT INTO raid_targets(raid,planet) (
112                                         SELECT ?,id FROM current_planet_stats p WHERE alliance_id = ? $sizelimit)});
113                                 unless ($addtarget->execute($raid->{id},$1)){
114                                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
115                                 }
116                         }
117                         my $groups = $DBH->prepare('SELECT gid,groupname FROM groups WHERE attack');
118                         my $delgroup = $DBH->prepare(q{DELETE FROM raid_access WHERE raid = ? AND gid = ?});
119                         my $addgroup = $DBH->prepare(q{INSERT INTO raid_access (raid,gid) VALUES(?,?)});
120                         $groups->execute();
121                         while (my $group = $groups->fetchrow_hashref){
122                                 my $query;
123                                 next unless defined param $group->{gid};
124                                 if (param($group->{gid}) eq 'remove'){
125                                         $query = $delgroup;
126                                 }elsif(param($group->{gid}) eq 'add'){
127                                         $query = $addgroup;
128                                 }
129                                 if ($query){
130                                         unless ($query->execute($raid->{id},$group->{gid})){
131                                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
132                                         }
133                                 }
134                         }
135                         unless ($DBH->commit){
136                                 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
137                         }
138                 }elsif (param('cmd') eq 'targets'){
139                         $DBH->begin_work;
140                         my $comment = $DBH->prepare(q{UPDATE raid_targets SET comment = ? WHERE id = ?});
141                         my $unclaim =  $DBH->prepare(q{DELETE FROM raid_claims WHERE target = ? AND wave = ?});
142                         my $block = $DBH->prepare(q{INSERT INTO raid_claims (target,uid,wave) VALUES(?,-2,?)});
143                         for $_ (param()){
144                                 if (/^comment:(\d+)$/){
145                                         $comment->execute(escapeHTML(param($_)),$1) or $error .= p($DBH->errstr);
146                                 }elsif(/^unclaim:(\d+):(\d+)$/){
147                                         $unclaim->execute($1,$2) or $error .= p($DBH->errstr);
148                                         log_message $ND::UID,"BC unclaimed target $1 wave $2.";
149                                 }elsif(/^block:(\d+):(\d+)$/){
150                                         $block->execute($1,$2) or $error .= p($DBH->errstr);
151                                 }
152                         }
153                         $DBH->commit or $error .= p($DBH->errstr);
154                 }
155
156         }
157         if ($raid && param('removeTarget')){
158                 $error .= "test";
159                 unless($DBH->do(q{DELETE FROM raid_targets WHERE raid = ? AND id = ?}
160                                 ,undef,$raid->{id},param('removeTarget'))){
161                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
162                 }
163         }
164
165         my $groups = $DBH->prepare(q{SELECT g.gid,g.groupname,raid FROM groups g LEFT OUTER JOIN (SELECT gid,raid FROM raid_access WHERE raid = ?) AS ra ON g.gid = ra.gid WHERE g.attack});
166         $groups->execute($raid ? $raid->{id} : undef);
167
168         my @addgroups;
169         my @remgroups;
170         while (my $group = $groups->fetchrow_hashref){
171                 if ($group->{raid}){
172                         push @remgroups,{Id => $group->{gid}, Name => $group->{groupname}};
173                 }else{
174                         push @addgroups,{Id => $group->{gid}, Name => $group->{groupname}};
175                 }
176         }
177         $BODY->param(RemoveGroups => \@remgroups);
178         $BODY->param(AddGroups => \@addgroups);
179
180
181         if ($raid){
182
183                 $BODY->param(Raid => $raid->{id});
184                 if($raid->{open}){
185                         $BODY->param(Open => 'Close');
186                 }else{
187                         $BODY->param(Open => 'Open');
188                 }
189                 if($raid->{released_coords}){
190                         $BODY->param(ShowCoords => 'hidecoords');
191                         $BODY->param(ShowCoordsName => 'Hide');
192                 }else{
193                         $BODY->param(ShowCoords => 'showcoords');
194                         $BODY->param(ShowCoordsName => 'Show');
195                 }
196                 $BODY->param(Waves => $raid->{waves});
197                 $BODY->param(LandingTick => $raid->{tick});
198                 $BODY->param(Message => $raid->{message});
199
200                 my $order = "p.x,p.y,p.z";
201                 if (param('order') && param('order') =~ /^(score|size|value|xp|race)$/){
202                         $order = "$1 DESC";
203                 }
204
205                 my $targetquery = $DBH->prepare(qq{SELECT r.id,coords(x,y,z),raid,comment,size,score,value,race,planet_status AS planetstatus,relationship,comment,r.planet
206                         FROM current_planet_stats p JOIN raid_targets r ON p.id = r.planet 
207                         WHERE r.raid = ?
208                         ORDER BY $order});
209                 my $claims =  $DBH->prepare(qq{ SELECT username,launched FROM raid_claims
210                         NATURAL JOIN users WHERE target = ? AND wave = ?});
211                 $targetquery->execute($raid->{id}) or $error .= $DBH->errstr;
212                 my @targets;
213                 while (my $target = $targetquery->fetchrow_hashref){
214                         my @waves;
215                         for my $i (1 .. $raid->{waves}){
216                                 $claims->execute($target->{id},$i);
217                                 my $claimers;
218                                 if ($claims->rows != 0){
219                                         my $owner = 0;
220                                         my @claimers;
221                                         while (my $claim = $claims->fetchrow_hashref){
222                                                 $claim->{username} .= '*' if ($claim->{launched});
223                                                 push @claimers,$claim->{username};
224                                         }
225                                         $claimers = join '/', @claimers;
226                                 }
227                                 push @waves,{Wave => $i, Claimers => $claimers};
228                         }
229                         $target->{waves} = \@waves;
230
231                         my $scans = $DBH->prepare(q{SELECT DISTINCT ON (type) type, tick, scan FROM scans 
232                                 WHERE planet = ? AND type ~ 'Unit|Planet|Advanced Unit|.* Analysis' AND tick + 24 > tick() AND scan is not null
233                                 GROUP BY type, tick, scan ORDER BY type ,tick DESC});
234                         $scans->execute($target->{planet});
235                         delete $target->{planet};
236                         my %scans;
237                         while (my $scan = $scans->fetchrow_hashref){
238                                 $scans{$scan->{type}} = $scan;
239                         }
240
241                         my @scans;
242                         for my $type ('Planet','Unit','Advanced Unit','Surface Analysis','Technology Analysis'){
243                                 next unless exists $scans{$type};
244                                 my $scan = $scans{$type};
245                                 if ($self->{TICK} - $scan->{tick} > 5){
246                                         $scan->{scan} =~ s{<table( cellpadding="\d+")?>}{<table class="old">};
247                                 }
248                                 if ($type eq 'Planet'){
249                                         $target->{PlanetScan} = $scan->{scan};
250                                         next;
251                                 }
252                                 push @scans,{Scan => $scan->{scan}};
253                         }
254                         $target->{Scans} = \@scans;
255                         push @targets,$target;
256                 }
257                 $BODY->param(Targets => \@targets);
258         }else{
259                 $BODY->param(Waves => 3);
260                 $BODY->param(LandingTick => $self->{TICK}+12);
261         }
262         $BODY->param(Error => $error);
263         return $BODY;
264 }
265
266 1;