]> ruin.nu Git - ndwebbie.git/blob - lib/NDWeb/Controller/Raids.pm
Add member access by default
[ndwebbie.git] / lib / NDWeb / Controller / Raids.pm
1 package NDWeb::Controller::Raids;
2
3 use strict;
4 use warnings;
5 use parent 'Catalyst::Controller';
6
7 use POSIX;
8 use NDWeb::Include;
9 use ND::Include;
10
11 =head1 NAME
12
13 NDWeb::Controller::Raids - Catalyst Controller
14
15 =head1 DESCRIPTION
16
17 Catalyst Controller.
18
19 =head1 METHODS
20
21 =cut
22
23
24 =head2 index 
25
26 =cut
27
28 sub index :Path :Args(0) {
29         my ( $self, $c ) = @_;
30         my $dbh = $c->model;
31
32         my $launched = 0;
33         my $query = $dbh->prepare(q{SELECT r.id,released_coords AS releasedcoords,tick,waves*COUNT(DISTINCT rt.id) AS waves,
34                         COUNT(rc.uid) AS claims, COUNT(nullif(rc.launched,false)) AS launched,COUNT(NULLIF(rc.uid > 0,true)) AS blocked
35                 FROM raids r JOIN raid_targets rt ON r.id = rt.raid
36                         LEFT OUTER JOIN raid_claims rc ON rt.id = rc.target
37                 WHERE open AND not removed AND r.id 
38                         IN (SELECT raid FROM raid_access NATURAL JOIN groupmembers WHERE uid = ?)
39                 GROUP BY r.id,released_coords,tick,waves});
40         $query->execute($c->user->id);
41         my @raids;
42         while (my $raid = $query->fetchrow_hashref){
43                 $raid->{waves} -= $raid->{blocked};
44                 $raid->{claims} -= $raid->{blocked};
45                 delete $raid->{blocked};
46                 $launched += $raid->{launched};
47                 push @raids,$raid;
48         }
49         $c->stash(raids => \@raids);
50
51         if ($c->check_user_roles(qw/raids_info/)){
52                 my $query = $dbh->prepare(q{SELECT r.id,open ,tick,waves*COUNT(DISTINCT rt.id) AS waves,
53                         COUNT(rc.uid) AS claims, COUNT(nullif(rc.launched,false)) AS launched ,COUNT(NULLIF(uid > 0,true)) AS blocked
54                 FROM raids r JOIN raid_targets rt ON r.id = rt.raid
55                         LEFT OUTER JOIN raid_claims rc ON rt.id = rc.target
56                 WHERE not removed AND (not open 
57                         OR r.id NOT IN (SELECT raid FROM raid_access NATURAL JOIN groupmembers WHERE uid = ?))
58                 GROUP BY r.id,open,tick,waves});
59                 $query->execute($c->user->id);
60                 my @raids;
61                 while (my $raid = $query->fetchrow_hashref){
62                         $raid->{waves} -= $raid->{blocked};
63                         $raid->{claims} -= $raid->{blocked};
64                         delete $raid->{blocked};
65                         $launched += $raid->{launched};
66                         push @raids,$raid;
67                 }
68                 $c->stash(closedraids => \@raids);
69
70
71                 $query = $dbh->prepare(q{SELECT r.id,tick,waves*COUNT(DISTINCT rt.id) AS waves,
72                         COUNT(rc.uid) AS claims, COUNT(nullif(rc.launched,false)) AS launched ,COUNT(NULLIF(uid > 0,true)) AS blocked
73                 FROM raids r JOIN raid_targets rt ON r.id = rt.raid
74                         LEFT OUTER JOIN raid_claims rc ON rt.id = rc.target
75                 WHERE removed
76                 GROUP BY r.id,tick,waves});
77                 $query->execute;
78                 my @oldraids;
79                 while (my $raid = $query->fetchrow_hashref){
80                         $raid->{waves} -= $raid->{blocked};
81                         $raid->{claims} -= $raid->{blocked};
82                         delete $raid->{blocked};
83                         $launched += $raid->{launched};
84                         push @oldraids,$raid;
85                 }
86                 $c->stash(removedraids => \@oldraids);
87                 $c->stash(launched => $launched);
88         }
89
90 }
91
92 sub view : Local {
93         my ( $self, $c, $raid ) = @_;
94         my $dbh = $c->model;
95
96         $c->forward('findRaid');
97         $raid = $c->stash->{raid};
98
99         $c->stash(comma => \&comma_value);
100
101         $c->stash(raid => $raid->{id});
102         my $planet;
103         if ($c->user->planet){
104                 my $query = $dbh->prepare("SELECT value, score,x,y FROM current_planet_stats WHERE id = ?");
105                 $planet = $dbh->selectrow_hashref($query,undef,$c->user->planet);
106         }
107         $c->stash(message => parseMarkup($raid->{message}));
108         $c->stash(landingtick => $raid->{tick});
109         my $targetquery = $dbh->prepare(q{SELECT r.id, r.planet, size, score, value
110                 , p.x,p.y,p.z, race
111                 , p.value - p.size*200 -
112                         COALESCE(ps.metal+ps.crystal+ps.eonium,0)/150 -
113                         COALESCE(ss.total ,(SELECT
114                                 COALESCE(avg(total),0) FROM
115                                 structure_scans)::int)*1500 AS fleetvalue
116                 ,(metal+crystal+eonium)/100 AS resvalue, comment
117                 , hidden, light, medium, heavy, metal, crystal, eonium
118                 ,metal_roids, crystal_roids, eonium_roids
119                 ,amps, distorters, light_fac, medium_fac, heavy_fac
120                 ,hulls, waves
121                 FROM current_planet_stats p
122                         JOIN raid_targets r ON p.id = r.planet
123                         LEFT OUTER JOIN current_planet_scans ps ON p.id = ps.planet
124                         LEFT OUTER JOIN current_structure_scans ss ON p.id = ss.planet
125                         LEFT OUTER JOIN current_tech_scans ts ON p.id = ts.planet
126                 WHERE r.raid = $1
127                         AND NOT COALESCE(p.x = $2 AND p.y = $3,False)
128                 ORDER BY size});
129         $targetquery->execute($raid->{id},$planet->{x},$planet->{y});
130         my @targets;
131         while (my $target = $targetquery->fetchrow_hashref){
132                 if ($planet){
133                         if ($planet->{x} == $target->{x}){
134                                 $target->{style} = 'incluster';
135                         }
136                         $target->{scorebash} = 'bash' if ($target->{score}/$planet->{score} < 0.4);
137                         $target->{valuebash} = 'bash' if ($target->{value}/$planet->{value} < 0.4);
138                         #next if ($target->{score}/$planet->{score} < 0.4) && ($target->{value}/$planet->{value} < 0.4);
139                 }
140
141                 my $unitscans = $dbh->prepare(q{ 
142                         SELECT DISTINCT ON (name) i.id,i.name, i.tick, i.amount 
143                         FROM fleets i
144                         WHERE  i.uid = -1
145                                 AND i.sender = ?
146                                 AND i.mission = 'Full fleet'
147                         GROUP BY i.id,i.tick,i.name,i.amount
148                         ORDER BY name,i.tick DESC
149                 });
150                 $unitscans->execute($target->{planet});
151                 my $ships = $dbh->prepare(q{SELECT ship,amount FROM fleet_ships
152                         WHERE id = ? ORDER BY num
153                 });
154                 my @missions;
155                 while (my $mission = $unitscans->fetchrow_hashref){
156                         my @ships;
157                         $ships->execute($mission->{id});
158                         while (my $ship = $ships->fetchrow_hashref){
159                                 push @ships,$ship;
160                         }
161                         push @ships, {ship => 'No', amount => 'ships'} if @ships == 0;
162                         $mission->{ships} = \@ships;
163                         push @missions,$mission;
164                 }
165                 $target->{missions} = \@missions;
166
167                 my @roids;
168                 my @claims;
169                 my $size = $target->{size};
170                 for (my $i = 1; $i <= $raid->{waves}; $i++){
171                         my $roids = floor(0.25*$size);
172                         $size -= $roids;
173                         my $xp = 0;
174                         if ($planet){
175                                 $xp = pa_xp($roids,$planet->{score},$planet->{value},$target->{score},$target->{value});
176                         }
177                         push @roids,{wave => $i, roids => $roids, xp => $xp};
178                         push @claims,{wave => $i}
179                 }
180                 $target->{roids} = \@roids;
181                 $target->{claims} = \@claims;
182
183                 my $num = pow(10,length($target->{score})-2);
184                 $target->{score} = "Hidden"; #ceil($target->{score}/$num)*$num;
185                 $num = pow(10,length($target->{value})-2);
186                 $target->{value} = "Hidden"; #ceil($target->{value}/$num)*$num;
187                 $num = pow(10,length($target->{size})-2);
188                 $target->{size} = floor($target->{size}/$num)*$num;
189                 $num = pow(10,length($target->{fleetvalue})-2);
190                 $target->{fleetvalue} = floor($target->{fleetvalue}/$num)*$num;
191                 if (defined $target->{resvalue}){
192                         $num = pow(10,length($target->{resvalue})-2);
193                         $target->{resvalue} = floor($target->{resvalue}/$num)*$num;
194                 }
195                 $target->{comment} = parseMarkup($target->{comment}) if ($target->{comment});
196                 $target->{hidden} = int($target->{hidden} / 100);
197
198                 push @targets,$target;
199         }
200         @targets = sort {$b->{roids}[0]{xp} <=> $a->{roids}[0]{xp} or $b->{size} <=> $a->{size}} @targets;
201
202         $c->stash(targets => \@targets);
203 }
204
205 sub edit : Local {
206         my ($self, $c, $raid, $order) = @_;
207         my $dbh = $c->model;
208
209         my $query = $dbh->prepare(q{SELECT id,tick,waves,message,released_coords,open,ftid
210                 FROM raids WHERE id = ?
211         });
212         $raid = $dbh->selectrow_hashref($query,undef,$raid);
213
214         $c->stash(raid => $raid);
215
216         $c->stash(errors => $c->flash->{errors});
217
218         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});
219         $groups->execute($raid ? $raid->{id} : undef);
220
221         my @addgroups;
222         my @remgroups;
223         while (my $group = $groups->fetchrow_hashref){
224                 if ($group->{raid}){
225                         push @remgroups,$group;
226                 }else{
227                         push @addgroups,$group;
228                 }
229         }
230         $c->stash(removegroups => \@remgroups);
231         $c->stash(addgroups => \@addgroups);
232
233         if ($order && $order =~ /^(score|size|value|xp)rank$/){
234                 $order .= " ASC";
235         }elsif ($order && $order eq 'race'){
236                 $order .= ' ASC';
237         }else {
238                 $order .= 'p.x,p.y,p.z';
239         }
240
241         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, s.scans
242                 FROM raid_targets r
243                         JOIN current_planet_stats p ON p.id = r.planet
244                         LEFT OUTER JOIN ( SELECT planet, array_accum(s::text) AS scans
245                                 FROM ( SELECT DISTINCT ON (planet,type) planet,scan_id,type, tick
246                                         FROM scans
247                                         WHERE tick > tick() - 24
248                                         ORDER BY planet,type ,tick DESC
249                                         ) s
250                                 GROUP BY planet
251                         ) s ON s.planet = r.planet
252                 WHERE r.raid = ?
253                 ORDER BY $order
254                 });
255         my $claims =  $dbh->prepare(q{ SELECT username,launched FROM raid_claims
256                 NATURAL JOIN users WHERE target = ? AND wave = ?});
257
258         $targetquery->execute($raid->{id});
259         my @targets;
260         while (my $target = $targetquery->fetchrow_hashref){
261                 my @waves;
262                 for my $i (1 .. $raid->{waves}){
263                         $claims->execute($target->{id},$i);
264                         my $claimers;
265                         if ($claims->rows != 0){
266                                 my $owner = 0;
267                                 my @claimers;
268                                 while (my $claim = $claims->fetchrow_hashref){
269                                         $claim->{username} .= '*' if ($claim->{launched});
270                                         push @claimers,$claim->{username};
271                                 }
272                                 $claimers = join '/', @claimers;
273                         }
274                         push @waves,{wave => $i, claimers => $claimers};
275                 }
276                 $target->{waves} = \@waves;
277
278                 $target->{scans} = array_expand $target->{scans};
279                 push @targets,$target;
280         }
281         $c->stash(targets => \@targets);
282
283         $c->forward('listAlliances');
284 }
285
286 sub postraidupdate : Local {
287         my ($self, $c, $raid) = @_;
288         my $dbh = $c->model;
289
290         $dbh->begin_work;
291         $dbh->do(q{UPDATE raids SET message = ?, tick = ?, waves = ? WHERE id = ?}
292                 ,undef,html_escape $c->req->param('message')
293                 ,$c->req->param('tick'),$c->req->param('waves')
294                 ,$raid);
295
296         $c->forward('log',[$raid, 'BC updated raid']);
297
298         my $groups = $dbh->prepare(q{SELECT gid,groupname FROM groups WHERE attack});
299         my $delgroup = $dbh->prepare(q{DELETE FROM raid_access WHERE raid = ? AND gid = ?});
300         my $addgroup = $dbh->prepare(q{INSERT INTO raid_access (raid,gid) VALUES(?,?)});
301
302         $groups->execute();
303         while (my $group = $groups->fetchrow_hashref){
304                 my $query;
305                 next unless defined $c->req->param($group->{gid});
306                 my $command = $c->req->param($group->{gid});
307                 if ( $command eq 'remove'){
308                         $query = $delgroup;
309                 }elsif($command eq 'add'){
310                         $query = $addgroup;
311                 }
312                 if ($query){
313                         $query->execute($raid,$group->{gid});
314                         $c->forward('log',[$raid, "BC '$command' access for $group->{gid} ($group->{groupname})"]);
315                 }
316         }
317         $dbh->commit;
318
319         $c->res->redirect($c->uri_for('edit',$raid));
320 }
321
322 sub postaddtargets : Local {
323         my ($self, $c, $raid) = @_;
324         my $dbh = $c->model;
325
326         my $sizelimit = $c->req->param('sizelimit');
327         $sizelimit = -1 unless $sizelimit;
328
329         my $targets = $c->req->param('targets');
330         my $addtarget = $dbh->prepare(qq{INSERT INTO raid_targets(raid,planet) (
331                 SELECT ?, id FROM current_planet_stats p
332                 WHERE x = ? AND y = ? AND COALESCE(z = ?,TRUE)
333                 AND p.size > ?
334                 )});
335         my @errors;
336         while ($targets =~ m/(\d+):(\d+)(?::(\d+))?/g){
337                 my ($x,$y,$z) = ($1, $2, $3);
338                 eval {
339                         $addtarget->execute($raid,$1,$2,$3,$sizelimit);
340                 };
341
342                 if ($@ =~ /duplicate key value violates unique constraint "raid_targets_raid_key"/){
343                         if ($z){
344                                 push @errors, "Planet already exists: $x:$y:$z";
345                         }else{
346                                 push @errors, "A planet from $x:$y already exists in the raid,"
347                                         ." either remove it or add the planets separately.";
348                         }
349                 }else {
350                         push @errors, $@;
351                 }
352         }
353         if ($c->req->param('alliance') =~ /^(\d+)$/ && $1 != 1){
354                 my $addtarget = $dbh->prepare(qq{INSERT INTO raid_targets(raid,planet) (
355                         SELECT ?,id FROM current_planet_stats p WHERE alliance_id = ? AND p.size > ?)
356                         });
357                 eval {
358                         $addtarget->execute($raid,$1,$sizelimit);
359                         $c->forward('log',[$raid,"BC adding alliance $1 to raid"]);
360                 };
361                 if ($@ =~ /duplicate key value violates unique constraint "raid_targets_raid_key"/){
362                         push @errors, "A planet from this alliance has already been added to the raid,"
363                                 ." either remove it or add the planets separately.";
364                 }else {
365                         push @errors, $@;
366                 }
367         }
368
369         $c->flash(errors => \@errors) if @errors;
370         $c->res->redirect($c->uri_for('edit',$raid));
371 }
372
373 sub posttargetupdates : Local {
374         my ($self, $c, $raid) = @_;
375         my $dbh = $c->model;
376
377         my @errors;
378         my $comment = $dbh->prepare(q{UPDATE raid_targets SET comment = ? WHERE id = ?});
379         my $unclaim =  $dbh->prepare(q{DELETE FROM raid_claims WHERE target = ? AND wave = ?});
380         my $block = $dbh->prepare(q{INSERT INTO raid_claims (target,uid,wave) VALUES(?,-2,?)});
381         my $claim = $dbh->prepare(q{INSERT INTO raid_claims (target,uid,wave)
382                 VALUES($1,(SELECT uid FROM users WHERE username ILIKE $3),$2)
383                 });
384         my $unblock =  $dbh->prepare(q{DELETE FROM raid_claims
385                 WHERE target = ? AND wave = ? AND uid = -2
386                 });
387         my $remove = $dbh->prepare(q{DELETE FROM raid_targets WHERE raid = ? AND id = ?});
388
389         for $_ ($c->req->param()){
390                 if (/^comment:(\d+)$/){
391                         $comment->execute(html_escape $c->req->param($_),$1);
392                 }elsif(/^unclaim:(\d+):(\d+)$/){
393                         $unclaim->execute($1,$2);
394                         $c->forward('log',[$raid,"BC unclaimed target $1 wave $2."]);
395                 }elsif(/^block:(\d+):(\d+)$/){
396                         $block->execute($1,$2);
397                         $c->forward('log',[$raid,"BC blocked target $1 wave $2."]);
398                 }elsif(/^claim:(\d+):(\d+)$/){
399                         my $target = $1;
400                         my $wave = $2;
401                         my @claims = split /[, ]+/, $c->req->param($_);
402                         for (@claims){
403                                 eval {
404                                         $claim->execute($target,$wave,$_);
405                                 };
406                                 if ($@ =~ /null value in column "uid"/){
407                                         push @errors, "Could not find user: " . html_escape $_;
408                                 }elsif ($@ =~ /more than one row returned by a subquery/){
409                                         push @errors, "This matched several users, please refine: " . html_escape $_;
410                                 }else {
411                                         push @errors, $@;
412                                 }
413                         }
414                         if(@claims){
415                                 $unblock->execute($target,$wave);
416                                 $c->forward('log',[$raid,"BC claimed target $1 wave $2 for @claims."]);
417                         }
418                 }elsif(/^remove:(\d+)$/){
419                         $remove->execute($raid,$1);
420                         $c->forward('log',[$raid,"BC removed target $1"]);
421                 }
422         }
423
424         $c->flash(errors => \@errors) if @errors;
425         $c->res->redirect($c->uri_for('edit',$raid));
426 }
427
428 sub open : Local {
429         my ($self, $c, $raid) = @_;
430
431         $c->model->begin_work;
432         $c->model->do(q{UPDATE raids SET open = TRUE, removed = FALSE WHERE id = ?}
433                 ,undef,$raid);
434         $c->forward('log',[$raid, "BC opened raid"]);
435         $c->model->commit;
436
437         $c->res->redirect($c->req->referer);
438 }
439
440 sub close : Local {
441         my ($self, $c, $raid) = @_;
442
443         $c->model->begin_work;
444         $c->model->do(q{UPDATE raids SET open = FALSE WHERE id = ?}
445                 ,undef,$raid);
446         $c->forward('log',[$raid, "BC closed raid"]);
447         $c->model->commit;
448
449         $c->res->redirect($c->req->referer);
450 }
451
452 sub remove : Local {
453         my ($self, $c, $raid) = @_;
454
455         $c->model->begin_work;
456         $c->model->do(q{UPDATE raids SET open = FALSE, removed = TRUE WHERE id = ?}
457                 ,undef,$raid);
458         $c->forward('log',[$raid, "BC removed raid"]);
459         $c->model->commit;
460
461         $c->res->redirect($c->req->referer);
462 }
463
464 sub showcoords : Local {
465         my ($self, $c, $raid) = @_;
466
467         $c->model->begin_work;
468         $c->model->do(q{UPDATE raids SET released_coords = TRUE WHERE id = ?}
469                 ,undef,$raid);
470         $c->forward('log',[$raid, "BC released coords"]);
471         $c->model->commit;
472
473         $c->res->redirect($c->req->referer);
474 }
475
476 sub hidecoords : Local {
477         my ($self, $c, $raid) = @_;
478
479         $c->model->begin_work;
480         $c->model->do(q{UPDATE raids SET released_coords = FALSE WHERE id = ?}
481                 ,undef,$raid);
482         $c->forward('log',[$raid, "BC hid coords"]);
483         $c->model->commit;
484
485         $c->res->redirect($c->req->referer);
486 }
487
488 sub create : Local {
489         my ($self, $c) = @_;
490         $c->stash(waves => 3);
491         my @time = gmtime;
492         $c->stash(landingtick => $c->stash->{TICK} + 24 - $time[2] + 12);
493 }
494
495 sub postcreate : Local {
496         my ($self, $c) = @_;
497         my $dbh = $c->model;
498
499         $dbh->begin_work;
500         my $query = $dbh->prepare(q{INSERT INTO raids (tick,waves,message) VALUES(?,?,?) RETURNING (id)});
501         $query->execute($c->req->param('tick'),$c->req->param('waves')
502                 ,html_escape $c->req->param('message'));
503         my $raid = $query->fetchrow_array;
504         $c->forward('log',[$raid,"Created raid landing at tick: ".$c->req->param('tick')]);
505
506         if ($c->req->param('gal') || $c->req->param('target')) {
507                 my @gals = $c->req->param('gal');
508                 my @targets = $c->req->param('target');
509
510                 my $addtarget = $dbh->prepare(q{INSERT INTO raid_targets(raid,planet) (
511                         SELECT $1,id FROM current_planet_stats p WHERE (planet_status IN ('','Hostile')
512                                 AND (relationship IS NULL OR relationship IN ('','Hostile')))
513                                 AND (id = ANY ($2) OR ( size > $4 AND (x,y) IN (
514                                         SELECT x,y FROM current_planet_stats WHERE id = ANY ($3)))
515                                 )
516                         )
517                 });
518                 $addtarget->execute($raid,\@targets,\@gals,$c->req->param('sizelimit'));
519                 $c->forward('log',[$raid,"BC added planets (@targets) and the gals for (@gals)"]);
520         }
521         $dbh->do(q{INSERT INTO raid_access (raid,gid) VALUES(?,2)}
522                 ,undef,$raid);
523         $dbh->commit;
524
525         $c->res->redirect($c->uri_for('edit',$raid));
526 }
527
528 sub targetlist : Local {
529         my ($self, $c, $alliances, $order) = @_;
530         my $dbh = $c->model;
531
532         $c->stash(comma => \&comma_value);
533         $c->stash(allies => $alliances);
534         $alliances ||= '';
535         my @alliances = split /,/, $alliances;
536
537         $c->forward('listAlliances');
538
539         if ($order && $order =~ /^(sizerank|valuerank|scorerank|xprank|nfvalue|nfvalue2)$/){
540                 $order = "$1";
541         }else{
542                 $order = "nfvalue";
543         }
544         $order = "p.$order" if $order =~ /rank$/;
545
546         my $query = $dbh->prepare(q{
547 SELECT p.id, coords(p.x,p.y,p.z),p.x,p.y,p.alliance, p.score, p.value, p.size, p.xp,nfvalue, nfvalue - sum(pa.value) AS nfvalue2, p.race
548 FROM current_planet_stats p
549         JOIN (SELECT g.x,g.y, sum(p.value) AS nfvalue
550                 FROM galaxies g join current_planet_stats p on g.x = p.x AND g.y = p.y
551                 WHERE g.tick = (SELECT max(tick) from galaxies)
552                         AND (planet_status IN ('','Hostile')
553                                 AND (relationship IS NULL OR relationship IN ('','Hostile')))
554                 GROUP BY g.x,g.y
555         ) g ON p.x = g.x AND p.y = g.y
556         JOIN current_planet_stats pa ON pa.x = g.x AND pa.y = g.y
557 WHERE p.x <> 200
558         AND p.alliance_id = ANY ($1)
559         AND pa.alliance_id = ANY ($1)
560         AND p.relationship IN ('','Hostile')
561 GROUP BY p.id, p.x,p.y,p.z,p.alliance, p.score, p.value, p.size, p.xp, nfvalue,p.race
562         ,p.scorerank,p.valuerank,p.sizerank,p.xprank
563 ORDER BY
564                 } . $order);
565         $query->execute(\@alliances);
566         $c->stash(planets => $query->fetchall_arrayref({}) );
567         $c->forward('create');
568 }
569
570 sub posttargetalliances : Local {
571         my ($self, $c) = @_;
572
573         $c->res->redirect($c->uri_for('targetlist',join ',',$c->req->param('alliances')));
574 }
575
576 sub listAlliances : Private {
577         my ($self, $c) = @_;
578         my @alliances;
579         my $query = $c->model->prepare(q{SELECT id,name FROM alliances
580                 WHERE relationship IN ('','Hostile')
581                         AND id IN (SELECT alliance_id FROM planets)
582                  ORDER BY LOWER(name)
583                 });
584         $query->execute;
585         push @alliances,{id => -1, name => ''};
586         while (my $ally = $query->fetchrow_hashref){
587                 push @alliances,$ally;
588         }
589         $c->stash(alliances => \@alliances);
590 }
591
592 sub log : Private {
593         my ($self, $c, $raid, $message) = @_;
594         my $dbh = $c->model;
595
596         my $log = $dbh->prepare(q{INSERT INTO forum_posts (uid,ftid,message)
597                 VALUES($1,(SELECT ftid FROM raids WHERE id = $2),$3)
598                 });
599         $log->execute($c->user->id,$raid,$message);
600 }
601
602 sub findRaid : Private {
603         my ( $self, $c, $raid ) = @_;
604         my $dbh = $c->model;
605
606         my $query = $dbh->prepare(q{SELECT id,tick,waves,message,released_coords
607                 FROM raids WHERE id = ? AND open AND not removed AND id IN
608                         (SELECT raid FROM raid_access NATURAL JOIN groupmembers WHERE uid = ?)
609                 });
610         $raid = $dbh->selectrow_hashref($query,undef,$raid,$c->user->id);
611         $c->stash(raid => $raid);
612 }
613
614 =head1 AUTHOR
615
616 Michael Andreen (harv@ruin.nu)
617
618 =head1 LICENSE
619
620 GPL 2.0, or later.
621
622 =cut
623
624 1;