]> ruin.nu Git - ndwebbie.git/blob - ND/Web/Pages/EditRaid.pm
editraid class
[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 our @ISA = 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'){
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: ".$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                 }
139         }
140         if ($raid && param('removeTarget')){
141                 $error .= "test";
142                 unless($DBH->do(q{DELETE FROM raid_targets WHERE raid = ? AND id = ?}
143                                 ,undef,$raid->{id},param('removeTarget'))){
144                         $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
145                 }
146         }
147
148         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});
149         $groups->execute($raid ? $raid->{id} : undef);
150
151         my @addgroups;
152         my @remgroups;
153         while (my $group = $groups->fetchrow_hashref){
154                 if ($group->{raid}){
155                         push @remgroups,{Id => $group->{gid}, Name => $group->{groupname}};
156                 }else{
157                         push @addgroups,{Id => $group->{gid}, Name => $group->{groupname}};
158                 }
159         }
160         $BODY->param(RemoveGroups => \@remgroups);
161         $BODY->param(AddGroups => \@addgroups);
162
163
164         if ($raid){
165
166                 $BODY->param(Raid => $raid->{id});
167                 if($raid->{open}){
168                         $BODY->param(Open => 'Close');
169                 }else{
170                         $BODY->param(Open => 'Open');
171                 }
172                 if($raid->{released_coords}){
173                         $BODY->param(ShowCoords => 'hidecoords');
174                         $BODY->param(ShowCoordsName => 'Hide');
175                 }else{
176                         $BODY->param(ShowCoords => 'showcoords');
177                         $BODY->param(ShowCoordsName => 'Show');
178                 }
179                 $BODY->param(Waves => $raid->{waves});
180                 $BODY->param(LandingTick => $raid->{tick});
181                 $BODY->param(Message => $raid->{message});
182
183                 my $order = "p.x,p.y,p.z";
184                 if (param('order') && param('order') =~ /^(score|size|value|xp|race)$/){
185                         $order = "$1 DESC";
186                 }
187
188                 my $targetquery = $DBH->prepare(qq{SELECT r.id,coords(x,y,z),raid,comment,size,score,value,race,planet_status AS planetstatus,relationship,comment
189                         FROM current_planet_stats p JOIN raid_targets r ON p.id = r.planet 
190                         WHERE r.raid = ?
191                         ORDER BY $order});
192                 $targetquery->execute($raid->{id}) or $error .= $DBH->errstr;
193                 my @targets;
194                 while (my $target = $targetquery->fetchrow_hashref){
195                         push @targets,$target;
196                 }
197                 $BODY->param(Targets => \@targets);
198         }else{
199                 $BODY->param(Waves => 3);
200                 $BODY->param(LandingTick => $self->{TICK}+12);
201         }
202         $BODY->param(Error => $error);
203         return $BODY;
204 }
205
206 1;