1 #**************************************************************************
2 # Copyright (C) 2006 by Michael Andreen <harvATruinDOTnu> *
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. *
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. *
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 #**************************************************************************/
27 $ND::TEMPLATE->param(TITLE => 'Create/Edit Raids');
29 die "You don't have access" unless isBC();
31 my @alliances = alliances();
32 $BODY->param(Alliances => \@alliances);
35 if (param('raid') =~ /^(\d+)$/){
36 my $query = $DBH->prepare(q{SELECT id,tick,waves,message,released_coords,open FROM raids WHERE id = ?});
37 $raid = $DBH->selectrow_hashref($query,undef,$1);
39 if (param('cmd') eq 'submit'){
40 my $query = $DBH->prepare(q{INSERT INTO raids (tick,waves,message) VALUES(?,?,'')});
41 if ($query->execute(param('tick'),param('waves'))){
42 $raid = $DBH->last_insert_id(undef,undef,undef,undef,"raids_id_seq");
43 my $query = $DBH->prepare(q{SELECT id,tick,waves,message,released_coords,open FROM raids WHERE id = ?});
44 $raid = $DBH->selectrow_hashref($query,undef,$raid);
46 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
51 if (param('cmd') eq 'remove'){
52 $DBH->do(q{UPDATE raids SET open = FALSE, removed = TRUE WHERE id = ?},undef,$raid->{id});
53 }elsif (param('cmd') eq 'Open'){
54 if($DBH->do(q{UPDATE raids SET open = TRUE, removed = FALSE WHERE id = ?},undef,$raid->{id})){
58 }elsif (param('cmd') eq 'Close'){
59 if ($DBH->do(q{UPDATE raids SET open = FALSE WHERE id = ?},undef,$raid->{id})){
62 }elsif (param('cmd') eq 'showcoords'){
63 if($DBH->do(q{UPDATE raids SET released_coords = TRUE WHERE id = ?},undef,$raid->{id})){
64 $raid->{released_coords} = 1;
66 }elsif (param('cmd') eq 'hidecoords'){
67 if($DBH->do(q{UPDATE raids SET released_coords = FALSE WHERE id = ?},undef,$raid->{id})){
68 $raid->{released_coords} = 0;
70 }elsif (param('cmd') eq 'change'){
72 my $message = escapeHTML(param('message'));
73 $raid->{message} = $message;
74 $raid->{waves} = param('waves');
75 $raid->{tick} = param('tick');
76 unless ($DBH->do(qq{UPDATE raids SET message = ?, tick = ?, waves = ? WHERE id = ?}
77 ,undef,$message,param('tick'),param('waves'),$raid->{id})){
78 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
81 if (param('sizelimit') =~ /^(\d+)$/){
82 $sizelimit = "AND p.size >= $1";
83 unless ($DBH->do(qq{DELETE FROM raid_targets WHERE id IN (SELECT t.id FROM current_planet_stats p
84 JOIN raid_targets t ON p.id = t.planet WHERE p.size < ? AND t.raid = ?)},undef,$1,$raid->{id})){
85 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
88 my $targets = param('targets');
89 my $addtarget = $DBH->prepare(qq{INSERT INTO raid_targets(raid,planet) (
90 SELECT ?, id FROM current_planet_stats p WHERE x = ? AND y = ? AND COALESCE(z = ?,TRUE) $sizelimit)});
91 while ($targets =~ m/(\d+):(\d+)(?::(\d+))?/g){
92 unless ($addtarget->execute($raid->{id},$1,$2,$3)){
93 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
96 if (param('alliance') =~ /^(\d+)$/ && $1 != 1){
97 $LOG->execute($ND::UID,"BC adding alliance $1 to raid");
98 my $addtarget = $DBH->prepare(qq{INSERT INTO raid_targets(raid,planet) (
99 SELECT ?,id FROM current_planet_stats p WHERE alliance_id = ? $sizelimit)});
100 unless ($addtarget->execute($raid->{id},$1)){
101 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
104 my $groups = $DBH->prepare('SELECT gid,groupname FROM groups WHERE attack');
105 my $delgroup = $DBH->prepare(q{DELETE FROM raid_access WHERE raid = ? AND gid = ?});
106 my $addgroup = $DBH->prepare(q{INSERT INTO raid_access (raid,gid) VALUES(?,?)});
108 while (my $group = $groups->fetchrow_hashref){
110 if (param($group->{gid}) eq 'remove'){
112 }elsif(param($group->{gid}) eq 'add'){
116 unless ($query->execute($raid->{id},$group->{gid})){
117 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
121 unless ($DBH->commit){
122 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
125 if (param('removeTarget')){
127 unless($DBH->do(q{DELETE FROM raid_targets WHERE raid = ? AND id = ?}
128 ,undef,$raid->{id},param('removeTarget'))){
129 $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
134 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});
135 $groups->execute($raid ? $raid->{id} : undef);
139 while (my $group = $groups->fetchrow_hashref){
141 push @remgroups,{Id => $group->{gid}, Name => $group->{groupname}};
143 push @addgroups,{Id => $group->{gid}, Name => $group->{groupname}};
146 $BODY->param(RemoveGroups => \@remgroups);
147 $BODY->param(AddGroups => \@addgroups);
152 $BODY->param(Raid => $raid->{id});
154 $BODY->param(Open => 'Close');
156 $BODY->param(Open => 'Open');
158 if($raid->{released_coords}){
159 $BODY->param(ShowCoords => 'hidecoords');
160 $BODY->param(ShowCoordsName => 'Hide');
162 $BODY->param(ShowCoords => 'showcoords');
163 $BODY->param(ShowCoordsName => 'Show');
165 $BODY->param(Waves => $raid->{waves});
166 $BODY->param(LandingTick => $raid->{tick});
167 $BODY->param(Message => $raid->{message});
169 my $order = "p.x,p.y,p.z";
170 if (param('order') =~ /^(score|size|value|xp|race)$/){
174 my $targetquery = $DBH->prepare(qq{SELECT r.id,coords(x,y,z),raid,comment,size,score,value,race,planet_status AS planetstatus,relationship,comment
175 FROM current_planet_stats p JOIN raid_targets r ON p.id = r.planet
178 $targetquery->execute($raid->{id}) or $error .= $DBH->errstr;
180 while (my $target = $targetquery->fetchrow_hashref){
181 push @targets,$target;
183 $BODY->param(Targets => \@targets);
185 $BODY->param(Waves => 3);
186 $BODY->param(LandingTick => $ND::TICK+12);
188 $BODY->param(Error => $error);