]> ruin.nu Git - ndwebbie.git/blobdiff - alliances.pl
don't show posts for autogenerated boards, and ND::Include needed for calls
[ndwebbie.git] / alliances.pl
index 101f2db12a9caf69664041cc55b5d3a81ef4afa5..4f89a48f264131c3e28794f7f11d0d67a400cb27 100644 (file)
 #**************************************************************************/
 
 use strict;
-use POSIX;
+use warnings FATAL => 'all';
+use ND::Include;
 our $BODY;
 our $DBH;
-our $LOG;
+our $ERROR;
 my $error;
 
 $ND::TEMPLATE->param(TITLE => 'Alliances');
@@ -29,16 +30,39 @@ $ND::TEMPLATE->param(TITLE => 'Alliances');
 die "You don't have access" unless isHC();
 
 my $alliance;
-if (param('alliance') =~ /^(\d+)$/){
+if (defined param('alliance') && param('alliance') =~ /^(\d+)$/){
        my $query = $DBH->prepare(q{SELECT id,name, relationship FROM alliances WHERE id = ?});
        $alliance = $DBH->selectrow_hashref($query,undef,$1);
 }
-if ($alliance && param ('cmd') eq 'coords'){
-       $DBH->begin_work;
-       $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
-}
-if ($alliance && param ('cmd') eq 'change'){
+if ($alliance && defined param('cmd') && param ('cmd') eq 'change'){
        $DBH->begin_work;
+       if (param('crelationship')){
+               my $value = escapeHTML(param('relationship'));
+               if ($DBH->do(q{UPDATE alliances SET relationship = ? WHERE id =?}
+                               ,undef,$value,$alliance->{id})){
+                       $alliance->{relationship} = $value;
+                       log_message $ND::UID,"HC set alliance: $alliance->{id} relationship: $value";
+               }else{
+                       $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
+               }
+       }
+       my $coords = param('coords');
+       my $findplanet = $DBH->prepare(q{SELECT id FROM current_planet_stats WHERE x = ? AND y = ? AND z = ?});
+       my $addplanet = $DBH->prepare(q{
+UPDATE planets SET alliance_id = $2, nick = coalesce($3,nick)
+WHERE id = $1;
+               });
+       while ($coords =~ m/(\d+):(\d+):(\d+)(?:\s+nick=\s*(\S+))?/g){
+               my ($id) = $DBH->selectrow_array($findplanet,undef,$1,$2,$3) or $ERROR .= p $DBH->errstr;
+               if ($addplanet->execute($id,$alliance->{id},$4)){
+                       my $nick = '';
+                       $nick = '(nick $4)' if defined $4;
+                       $error .= "<p> Added planet $1:$2:$3 $nick to this alliance</p>";
+                       intel_log $ND::UID,$id,"HC Added planet $1:$2:$3 $nick to alliance: $alliance->{id} ($alliance->{name})";
+               }else{
+                       $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
+               }
+       }
        $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
 }
 
@@ -47,12 +71,12 @@ if ($alliance){
        $BODY->param(Id => $alliance->{id});
        my @relationships;
        for my $relationship ("&nbsp;","Friendly", "NAP", "Hostile"){
-               push @relationships,{Rel => $relationship, Selected => $relationship eq $alliance->{relationship}}
+               push @relationships,{Rel => $relationship, Selected => defined $alliance->{relationship} && $relationship eq $alliance->{relationship}}
        }
        $BODY->param(Relationships => \@relationships);
 
        my $order = "p.x,p.y,p.z";
-       if (param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
+       if (defined param('order') && param('order') =~ /^(score|size|value|xp|hit_us|race)$/){
                $order = "$1 DESC";
        }
        my $members = $DBH->prepare(qq{
@@ -65,9 +89,9 @@ ORDER BY $order});
        $members->execute($alliance->{id});
        my $i = 0;
        while (my $member = $members->fetchrow_hashref){
+               $i++;
                $member->{ODD} = $i % 2;
                push @members,$member;
-               $i++;
        }
        $BODY->param(Members => \@members);
 
@@ -79,22 +103,22 @@ ORDER BY $order});
        $query->execute($alliance->{id},$alliance->{id},$alliance->{id},$alliance->{id}) or $error .= $DBH->errstr;
 
        my @intel;
-       my $i = 0;
+       $i = 0;
        while (my $intel = $query->fetchrow_hashref){
                if ($intel->{ingal}){
                        $intel->{missionclass} = 'ingal';
                }else{
                        $intel->{missionclass} = $intel->{mission};
                }
+               $i++;
                $intel->{ODD} = $i % 2;
                push @intel,$intel;
-               $i++;
        }
        $BODY->param(Intel => \@intel);
 }else{
 
        my $order = "score DESC";
-       if (param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
+       if (defined param('order') && param('order') =~ /^(score|kscore|size|ksize|members|kmem|kxp|kxp|scavg|kscavg|siavg|ksiavg|kxpavg|kvalue|kvalavg)$/){
                $order = "$1 DESC";
        }
        my $query = $DBH->prepare(qq{
@@ -112,10 +136,10 @@ ORDER BY $order
        my @alliances;
        my $i = 0;
        while (my $alliance = $query->fetchrow_hashref){
-               next if ($alliance->{score} < 1 && $alliance->{kscore} < 1);
+               next unless (defined $alliance->{score} || $alliance->{kscore} > 0);
+               $i++;
                $alliance->{ODD} = $i % 2;
                push @alliances, $alliance;
-               $i++;
        }
        $BODY->param(Alliances => \@alliances);
 }