]> ruin.nu Git - ndwebbie.git/blobdiff - lib/NDWeb/Controller/Members.pm
Better error messages when inputing coords
[ndwebbie.git] / lib / NDWeb / Controller / Members.pm
index 89997ba64b41202ce2f043cc798aa0b118222ce8..a77ec1eaddf1e734323b9baf07b7134f28fa4725 100644 (file)
@@ -82,6 +82,12 @@ ORDER BY landing_tick DESC
                });
        $announcements->execute($c->user->id);
        $c->stash(announcements => $announcements->fetchall_arrayref({}) );
+
+       my ($attackgroups) = $dbh->selectrow_array(q{
+SELECT array_agg(gid) FROM groupmembers WHERE gid IN ('x','y','z') AND uid = $1
+               }, undef, $c->user->id);
+       $c->stash(attackgroups => $attackgroups);
+
 }
 
 sub posthostupdate : Local {
@@ -94,6 +100,23 @@ sub posthostupdate : Local {
        $c->res->redirect($c->uri_for(''));
 }
 
+sub postattackgroups : Local {
+       my ( $self, $c ) = @_;
+       my $dbh = $c->model;
+
+       my @groups = $c->req->param('class');
+       $dbh->do(q{DELETE FROM groupmembers WHERE gid IN ('x','y','z') AND gid <> ALL($1) AND uid = $2
+               },undef, \@groups, $c->user->id);
+
+       $dbh->do(q{INSERT INTO groupmembers (uid,gid) (
+               SELECT $2, gid FROM unnest($1::text[]) AS gid WHERE gid IN ('x','y','z')
+       EXCEPT
+               SELECT uid,gid FROM groupmembers WHERE uid = $2
+               )},undef, \@groups, $c->user->id);
+
+       $c->res->redirect($c->uri_for(''));
+}
+
 sub postsmsupdate : Local {
        my ( $self, $c ) = @_;
        my $dbh = $c->model;
@@ -114,15 +137,26 @@ sub postowncoords : Local {
        if ($c->user->planet){
                $c->flash(error => 'You already have a planet set.'
                        .' Contact a HC if they need to be changed');
-       }elsif ($c->req->param('planet') =~ m/(\d+)\D+(\d+)\D+(\d+)/){
+       }elsif (my ($x,$y,$z) = $c->req->param('planet') =~ m/(\d+)\D+(\d+)\D+(\d+)/){
                my $planet = $dbh->selectrow_array(q{SELECT planetid($1,$2,$3,TICK())
-                       },undef,$1,$2,$3);
+                       },undef,$x,$y,$z);
 
                if ($planet){
-                       $dbh->do(q{UPDATE users SET planet = ? WHERE uid = ?
-                               },undef, $planet , $c->user->id);
+                       eval {
+                               $dbh->do(q{UPDATE users SET pid = ? WHERE uid = ?
+                                       },undef, $planet , $c->user->id);
+                       };
+                       given ($@){
+                               when (''){}
+                               when (/duplicate key value violates/){
+                                       $c->flash(error => "The coords $x:$y:$z are already in use. Talk to hc if these are really your coords.")
+                               }
+                               default {
+                                       $c->flash(error => $@)
+                               }
+                       }
                }else{
-                       $c->flash(error => "No planet at coords: $1:$2:$3");
+                       $c->flash(error => "No planet at coords: $x:$y:$z");
                }
        }else{
                $c->flash(error => $c->req->param('planet') . " are not valid coords.");