]> ruin.nu Git - ndwebbie.git/blob - NDWeb/Pages/EditRaid.pm
e701f1d2d418492ee9912fd9e05dc4d88c2e1968
[ndwebbie.git] / NDWeb / 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                         my $remove = $DBH->prepare(q{DELETE FROM raid_targets WHERE raid = ? AND id = ?});
144                         for $_ (param()){
145                                 if (/^comment:(\d+)$/){
146                                         $comment->execute(escapeHTML(param($_)),$1) or $error .= p($DBH->errstr);
147                                 }elsif(/^unclaim:(\d+):(\d+)$/){
148                                         $unclaim->execute($1,$2) or $error .= p($DBH->errstr);
149                                         log_message $ND::UID,"BC unclaimed target $1 wave $2.";
150                                 }elsif(/^block:(\d+):(\d+)$/){
151                                         $block->execute($1,$2) or $error .= p($DBH->errstr);
152                                 }elsif(/^remove:(\d+)$/){
153                                         $remove->execute($raid->{id},$1) or $error .= p($DBH->errstr);
154                                 }
155                         }
156                         $DBH->commit or $error .= p($DBH->errstr);
157                 }
158         }
159
160         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});
161         $groups->execute($raid ? $raid->{id} : undef);
162
163         my @addgroups;
164         my @remgroups;
165         while (my $group = $groups->fetchrow_hashref){
166                 if ($group->{raid}){
167                         push @remgroups,{Id => $group->{gid}, Name => $group->{groupname}};
168                 }else{
169                         push @addgroups,{Id => $group->{gid}, Name => $group->{groupname}};
170                 }
171         }
172         $BODY->param(RemoveGroups => \@remgroups);
173         $BODY->param(AddGroups => \@addgroups);
174
175
176         if ($raid){
177
178                 $BODY->param(Raid => $raid->{id});
179                 if($raid->{open}){
180                         $BODY->param(Open => 'Close');
181                 }else{
182                         $BODY->param(Open => 'Open');
183                 }
184                 if($raid->{released_coords}){
185                         $BODY->param(ShowCoords => 'hidecoords');
186                         $BODY->param(ShowCoordsName => 'Hide');
187                 }else{
188                         $BODY->param(ShowCoords => 'showcoords');
189                         $BODY->param(ShowCoordsName => 'Show');
190                 }
191                 $BODY->param(Waves => $raid->{waves});
192                 $BODY->param(LandingTick => $raid->{tick});
193                 $BODY->param(Message => $raid->{message});
194
195                 my $order = "p.x,p.y,p.z";
196                 if (param('order') && param('order') =~ /^(score|size|value|xp|race)$/){
197                         $order = "$1 DESC";
198                 }
199
200                 my $targetquery = $DBH->prepare(qq{SELECT r.id,coords(x,y,z),comment,size,score,value,race,planet_status AS planetstatus,relationship,comment,r.planet
201                         FROM current_planet_stats p JOIN raid_targets r ON p.id = r.planet 
202                         WHERE r.raid = ?
203                         ORDER BY $order});
204                 my $claims =  $DBH->prepare(qq{ SELECT username,launched FROM raid_claims
205                         NATURAL JOIN users WHERE target = ? AND wave = ?});
206                 $targetquery->execute($raid->{id}) or $error .= $DBH->errstr;
207                 my @targets;
208                 while (my $target = $targetquery->fetchrow_hashref){
209                         my @waves;
210                         for my $i (1 .. $raid->{waves}){
211                                 $claims->execute($target->{id},$i);
212                                 my $claimers;
213                                 if ($claims->rows != 0){
214                                         my $owner = 0;
215                                         my @claimers;
216                                         while (my $claim = $claims->fetchrow_hashref){
217                                                 $claim->{username} .= '*' if ($claim->{launched});
218                                                 push @claimers,$claim->{username};
219                                         }
220                                         $claimers = join '/', @claimers;
221                                 }
222                                 push @waves,{Wave => $i, Claimers => $claimers};
223                         }
224                         $target->{waves} = \@waves;
225
226                         my $scans = $DBH->prepare(q{SELECT DISTINCT ON (type) type, tick, scan FROM scans 
227                                 WHERE planet = ? AND type ~ 'Unit|Planet|Advanced Unit|.* Analysis' AND tick + 24 > tick() AND scan is not null
228                                 GROUP BY type, tick, scan ORDER BY type ,tick DESC});
229                         $scans->execute($target->{planet});
230                         delete $target->{planet};
231                         my %scans;
232                         while (my $scan = $scans->fetchrow_hashref){
233                                 $scans{$scan->{type}} = $scan;
234                         }
235
236                         my @scans;
237                         for my $type ('Planet','Unit','Advanced Unit','Surface Analysis','Technology Analysis'){
238                                 next unless exists $scans{$type};
239                                 my $scan = $scans{$type};
240                                 if ($self->{TICK} - $scan->{tick} > 5){
241                                         $scan->{scan} =~ s{<table( cellpadding="\d+")?>}{<table class="old">};
242                                 }
243                                 if ($type eq 'Planet'){
244                                         $target->{PlanetScan} = $scan->{scan};
245                                         next;
246                                 }
247                                 push @scans,{Scan => $scan->{scan}};
248                         }
249                         $target->{Scans} = \@scans;
250                         push @targets,$target;
251                 }
252                 $BODY->param(Targets => \@targets);
253         }else{
254                 $BODY->param(Waves => 3);
255                 my @time = gmtime;
256                 $BODY->param(LandingTick => $self->{TICK} + 24 - $time[2] + 12);
257         }
258         $BODY->param(Error => $error);
259         return $BODY;
260 }
261
262 1;