]> ruin.nu Git - ndwebbie.git/blobdiff - intel.pl
restructure
[ndwebbie.git] / intel.pl
index 2b33c5e4849d19afff8f79f7781aa83ea798f6b4..7f1ec4a71ded074fc31410ff4e686cbb44b43da6 100644 (file)
--- a/intel.pl
+++ b/intel.pl
 #**************************************************************************/
 
 use strict;
-use POSIX;
+use warnings FATAL => 'all';
+use ND::Web::Forum;
+
 our $BODY;
 our $DBH;
 our $LOG;
+our $ERROR;
+my $error;
 
 $ND::TEMPLATE->param(TITLE => 'Intel');
 
 die "You don't have access" unless isIntel() || isHC();
 
 my $planet;
-if (param('coords') =~ /(\d+)(?: |:)(\d+)(?: |:)(\d+)/){
+if (defined param('coords') && param('coords') =~ /^(\d+)(?: |:)(\d+)(?: |:)(\d+)$/){
        my $query = $DBH->prepare(q{SELECT x,y,z,coords(x,y,z),id, nick, alliance,alliance_id, planet_status,channel FROM current_planet_stats
 WHERE  x = ? AND y = ? AND z = ?});
        $planet = $DBH->selectrow_hashref($query,undef,$1,$2,$3);
 }
 
 my $showticks = 'AND (i.tick - i.eta) > (tick() - 48)';
-if (param('show') eq 'all'){
-       $showticks = '';
-}elsif (param('show') =~ /^(\d+)$/){
-       $showticks = "AND (i.tick - i.eta) > tick() - $1";
+if (defined param('show')){
+       if (param('show') eq 'all'){
+               $showticks = '';
+       }elsif (param('show') =~ /^(\d+)$/){
+               $showticks = "AND (i.tick - i.eta) > (tick() - $1)";
+       }
 }
 
-if ($planet){
+my $thread;
+if (defined $planet){
+       $thread = $DBH->selectrow_hashref(q{SELECT ftid AS id, subject FROM forum_threads
+               where planet = $1},undef,$planet->{id}) or $ERROR .= p($DBH->errstr);
+}
+
+if (defined param('cmd') && param('cmd') eq 'coords'){
+       my $coords = param('coords');
+       $DBH->do(q{CREATE TEMPORARY TABLE coordlist (
+       x integer NOT NULL,
+       y integer NOT NULL,
+       z integer NOT NULL,
+       PRIMARY KEY (x,y,z)
+               )});
+       my $insert = $DBH->prepare(q{INSERT INTO coordlist (x,y,z) VALUES(?,?,?)});
+       while ($coords =~ m/(\d+):(\d+):(\d+)/g){
+               $insert->execute($1,$2,$3);
+       }
+       my $planets = $DBH->prepare(q{SELECT (((p.x || ':') || p.y) || ':') || p.z AS coords, alliance FROM current_planet_stats p
+       JOIN coordlist c ON p.x = c.x AND p.y = c.y AND p.z = c.z
+ORDER BY alliance, p.x, p.y, p.z});
+       $planets->execute;
+       my @planets;
+       while (my $planet = $planets->fetchrow_hashref){
+               push @planets,$planet;
+       }
+       $BODY->param(CoordList => \@planets);
+}
+if (defined $thread and defined param('cmd') and param('cmd') eq 'forumpost'){
+       addForumPost($DBH,$thread,$ND::UID,param('message'));
+}
+
+if ($planet && defined param('cmd')){
        if (param('cmd') eq 'change'){
                $DBH->begin_work;
                if (param('cnick')){
@@ -50,7 +88,7 @@ if ($planet){
                                        ,undef,$value,$planet->{id})){
                                $planet->{nick} = $value;
                        }else{
-                               print "<p> Something went wrong: ".$DBH->errstr."</p>";
+                               $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
                        }
                }
                if (param('cchannel')){
@@ -59,7 +97,7 @@ if ($planet){
                                        ,undef,$value,$planet->{id})){
                                $planet->{channel} = $value;
                        }else{
-                               print "<p> Something went wrong: ".$DBH->errstr."</p>";
+                               $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
                        }
                }
                if (param('cstatus')){
@@ -68,7 +106,7 @@ if ($planet){
                                        ,undef,$value,$planet->{id})){
                                $planet->{planet_status} = $value;
                        }else{
-                               print "<p> Something went wrong: ".$DBH->errstr."</p>";
+                               $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
                        }
                }
                if (param('calliance')){
@@ -76,10 +114,10 @@ if ($planet){
                                        ,undef,param('alliance'),$planet->{id})){
                                $planet->{alliance_id} = param('alliance');
                        }else{
-                               print "<p> Something went wrong: ".$DBH->errstr."</p>";
+                               $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
                        }
                }
-               $DBH->commit or print "<p> Something went wrong: ".$DBH->errstr."</p>";
+               $DBH->commit or $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
        }
 }
 
@@ -102,38 +140,46 @@ if ($planet){
        $BODY->param(Channel => $planet->{channel});
        my @status;
        for my $status ("&nbsp;","Friendly", "NAP", "Hostile"){
-               push @status,{Status => $status, Selected => $status eq $planet->{planet_status}}
+               push @status,{Status => $status, Selected => defined $planet->{planet_status} && $status eq $planet->{planet_status}}
        }
        $BODY->param(PlanetStatus => \@status);
        my @alliances = alliances($planet->{alliance_id});
        $BODY->param(Alliances => \@alliances);
 
+       $BODY->param(Thread => viewForumThread $thread);
+
        my $query = $DBH->prepare(intelquery('o.alliance AS oalliance,coords(o.x,o.y,o.z) AS origin',"t.id = ? $showticks"));
-       $query->execute($planet->{id}) or print $DBH->errstr;
+       $query->execute($planet->{id}) or $error .= $DBH->errstr;
        my @intellists;
-       my @intel;
+       my @incomings;
+       my $i = 0;
        while (my $intel = $query->fetchrow_hashref){
                if ($intel->{ingal}){
                        $intel->{missionclass} = 'ingal';
                }else{
                        $intel->{missionclass} = $intel->{mission};
                }
-               push @intel,$intel;
+               $i++;
+               $intel->{ODD} = $i % 2;
+               push @incomings,$intel;
        }
-       push @intellists,{Message => 'Incoming fleets', Intel => \@intel, Origin => 1};
+       push @intellists,{Message => 'Incoming fleets', Intel => \@incomings, Origin => 1};
 
-       my $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
-       $query->execute($planet->{id}) or print $DBH->errstr;
-       my @intel;
+       $query = $DBH->prepare(intelquery('t.alliance AS talliance,coords(t.x,t.y,t.z) AS target',"o.id = ? $showticks"));
+       $query->execute($planet->{id}) or $error .= $DBH->errstr;
+       my @outgoings;
+       $i = 0;
        while (my $intel = $query->fetchrow_hashref){
                if ($intel->{ingal}){
                        $intel->{missionclass} = 'ingal';
                }else{
                        $intel->{missionclass} = $intel->{mission};
                }
-               push @intel,$intel;
+               $i++;
+               $intel->{ODD} = $i % 2;
+               push @outgoings,$intel;
        }
-       push @intellists,{Message => 'Outgoing Fleets', Intel => \@intel, Target => 1};
+       push @intellists,{Message => 'Outgoing Fleets', Intel => \@outgoings, Target => 1};
 
        $BODY->param(IntelLIsts => \@intellists);
 
@@ -143,18 +189,22 @@ if ($planet){
                                OR ( t.alliance_id = o.alliance_id AND i.mission = 'Attack'))
                        AND i.sender NOT IN (SELECT planet FROM users u NATURAL JOIN groupmembers gm WHERE gid = 8 AND planet IS NOT NULL)
                        $showticks}));
+       $query->execute() or $error .= $DBH->errstr;
 
        my @intellists;
        my @intel;
+       my $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;
        }
-       push @intellists,{Message => q{Missions where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
+       push @intellists,{Message => q{Intel where alliances doesn't match}, Intel => \@intel, Origin => 1, Target => 1};
        $BODY->param(IntelLIsts => \@intellists);
 }
 my $query = $DBH->prepare(q{SELECT i.id, u.username, i.message, report_date FROM intel_messages i
@@ -166,5 +216,6 @@ while (my $message = $query->fetchrow_hashref){
        $message->{message} = parseMarkup($message->{message});
        push @messages,$message;
 }
-$BODY->param(IntelMessages => \@messages);
+#$BODY->param(IntelMessages => \@messages);
+$BODY->param(Error => $error);
 1;