]> ruin.nu Git - ndwebbie.git/blobdiff - NDWeb/Pages/Main.pm
Better session support and auth logging
[ndwebbie.git] / NDWeb / Pages / Main.pm
index d35a5b174fa13174b824627d5c12622d429c46e4..083b96da133a0fb365905090a9661e197b19ccfa 100644 (file)
@@ -34,47 +34,57 @@ sub render_body {
        $self->{TITLE} = 'Main Page';
        my $DBH = $self->{DBH};
 
-       my $error;
-
        if (defined param('cmd')){
                if (param('cmd') eq 'fleet'){
-                       $DBH->begin_work;
-                       my $fleet = $DBH->prepare("SELECT id FROM fleets WHERE uid = ? AND fleet = 0");
-                       my ($id) = $DBH->selectrow_array($fleet,undef,$ND::UID);
-                       unless ($id){
-                               my $insert = $DBH->prepare(q{INSERT INTO fleets (uid,target,mission,landing_tick,fleet,eta,back) VALUES (?,?,'Full fleet',0,0,0,0)});
-                               $insert->execute($ND::UID,$self->{PLANET});
-                               ($id) = $DBH->selectrow_array($fleet,undef,$ND::UID);
-                       }
-                       my $delete = $DBH->prepare("DELETE FROM fleet_ships WHERE fleet = ?");
-                       my $insert = $DBH->prepare('INSERT INTO fleet_ships (fleet,ship,amount) VALUES (?,?,?)');
-                       $fleet = param('fleet');
+                       my $fleet = param('fleet');
                        $fleet =~ s/,//g;
-                       my $match = 0;
+                       my $amount = 0;
+                       my @ships;
                        while ($fleet =~ m/((?:[A-Z][a-z]+ )*[A-Z][a-z]+)\s+(\d+)/g){
-                               unless($match){
-                                       $match = 1;
-                                       $delete->execute($id);
+                               $amount += $2;
+                               push @ships, [$1,$2];
+                       }
+                       if ($amount){
+                               $DBH->begin_work;
+                               eval{
+                                       my $insert = $DBH->prepare(q{INSERT INTO fleets 
+                                               (uid,sender,name,mission,tick,amount)
+                                               VALUES (?,?,'Main','Full fleet',tick(),?) RETURNING id});
+                                       my ($id) = $DBH->selectrow_array($insert,undef,$self->{UID}
+                                               ,$self->{PLANET},$amount) or die $DBH->errstr;
+                                       $insert = $DBH->prepare('INSERT INTO fleet_ships 
+                                               (id,ship,amount) VALUES (?,?,?)');
+                                       for my $s (@ships){
+                                               unshift @{$s},$id;
+                                               $insert->execute(@{$s}) or die $DBH->errstr;
+                                       }
+                               };
+                               if ($@){
+                                       warn $@;
+                                       $DBH->rollback;
+                               }else{
+                                       $DBH->commit;
+                                       $self->{RETURN} = 'REDIRECT';
+                                       $self->{REDIR_LOCATION} = "/main";
+                                       return;
                                }
-                               $insert->execute($id,$1,$2) or $error .= '<p>'.$DBH->errstr.'</p>';
+                       }else{
+                               warn 'Fleet does not contain any ships';
                        }
-                       $fleet = $DBH->prepare('UPDATE fleets SET landing_tick = tick() WHERE id = ?');
-                       $fleet->execute($id) if $match;
-                       $DBH->commit;
                }elsif (param('cmd') eq 'Recall Fleets'){
                        $DBH->begin_work;
-                       my $updatefleets = $DBH->prepare('UPDATE fleets SET back = tick() + (tick() - (landing_tick - eta))  WHERE uid = ? AND id = ?');
+                       my $updatefleets = $DBH->prepare('UPDATE fleets SET back = tick() + (tick() - (tick - eta))  WHERE uid = ? AND id = ?');
 
                        for my $param (param()){
                                if ($param =~ /^change:(\d+)$/){
                                        if($updatefleets->execute($ND::UID,$1)){
                                                log_message $ND::UID,"Member recalled fleet $1";
                                        }else{
-                                               $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
+                                               warn $DBH->errstr;
                                        }
                                }
                        }
-                       $DBH->commit or $error .= '<p>'.$DBH->errstr.'</p>';
+                       $DBH->commit or warn $DBH->errstr;
                }elsif (param('cmd') eq 'Change Fleets'){
                        $DBH->begin_work;
                        my $updatefleets = $DBH->prepare('UPDATE fleets SET back = ? WHERE uid = ? AND id = ?');
@@ -83,11 +93,11 @@ sub render_body {
                                        if($updatefleets->execute(param("back:$1"),$ND::UID,$1)){
                                                log_message $ND::UID,"Member set fleet $1 to be back tick: ".param("back:$1");
                                        }else{
-                                               $error .= "<p> Something went wrong: ".$DBH->errstr."</p>";
+                                               warn $DBH->errstr;
                                        }
                                }
                        }
-                       $DBH->commit or $error .= '<p>'.$DBH->errstr.'</p>';
+                       $DBH->commit or warn $DBH->errstr;
                }
        }
        if (param('sms')){
@@ -106,12 +116,7 @@ sub render_body {
                $query->execute($1,$2,$3,$ND::UID);
        }
 
-       my ($motd) = $DBH->selectrow_array("SELECT value FROM misc WHERE id='MOTD'");
-
-       $BODY->param(MOTD => parseMarkup($motd));
-       $BODY->param(Username => $self->{USER});
        $BODY->param(isMember => $self->isMember());
-       $BODY->param(isHC => $self->isHC());
        my @groups = map {name => $_}, sort keys %{$self->{GROUPS}};
        $BODY->param(Groups => \@groups);
 
@@ -131,38 +136,41 @@ sub render_body {
 
        $BODY->param(Planet => $planet);
 
-       $query = $DBH->prepare(qq{
-               SELECT c.id, c.landing_tick, dc.username,c.covered,
-               TRIM('/' FROM concat(p2.race||' /')) AS race, TRIM('/' FROM concat(i.amount||' /')) AS amount,
-               TRIM('/' FROM concat(i.eta||' /')) AS eta, TRIM('/' FROM concat(i.shiptype||' /')) AS shiptype,
-               (c.landing_tick - tick()) AS curreta,
-               TRIM('/' FROM concat(coords(p2.x,p2.y,p2.z) ||' /')) AS attackers
-               FROM calls c 
-               JOIN incomings i ON i.call = c.id
-               JOIN current_planet_stats p2 ON i.sender = p2.id
-               LEFT OUTER JOIN users dc ON c.dc = dc.uid
-               WHERE c.member = ? AND (c.landing_tick - tick())  > 0
-               GROUP BY c.id, c.landing_tick,dc.username,c.covered
-               ORDER BY c.landing_tick DESC
-               })or $error .= $DBH->errstr;
-       $query->execute($ND::UIN) or $error .= $DBH->errstr;
-
-       my $i = 0;
+       my $calls = $DBH->prepare(qq{
+               SELECT id,landing_tick,dc,curreta,covered,
+                               TRIM('/' FROM concat(DISTINCT race||' /')) AS race, TRIM('/' FROM concat(amount||' /')) AS amount,
+                               TRIM('/' FROM concat(DISTINCT eta||' /')) AS eta, TRIM('/' FROM concat(DISTINCT shiptype||' /')) AS shiptype,
+                               TRIM('/' FROM concat(coords||' /')) AS attackers 
+                       FROM (SELECT c.id,p.x,p.y,p.z, u.defense_points, c.landing_tick, dc.username AS dc,covered,
+                               (c.landing_tick - tick()) AS curreta,p2.race, i.amount, i.eta, i.shiptype, p2.alliance,
+                               coords(p2.x,p2.y,p2.z), COUNT(DISTINCT f.id) AS fleets
+                       FROM calls c 
+                       JOIN incomings i ON i.call = c.id
+                       JOIN users u ON c.member = u.uid
+                       JOIN current_planet_stats p ON u.planet = p.id
+                       JOIN current_planet_stats p2 ON i.sender = p2.id
+                       LEFT OUTER JOIN users dc ON c.dc = dc.uid
+                       LEFT OUTER JOIN fleets f ON f.target = u.planet AND f.tick = c.landing_tick AND f.back = f.tick + f.eta - 1
+                       WHERE u.uid = ? AND c.landing_tick >= tick()
+                       GROUP BY c.id, p.x,p.y,p.z, c.landing_tick, u.defense_points,dc.username,covered,p2.race,i.amount,i.eta,i.shiptype,p2.alliance,p2.x,p2.y,p2.z) a
+                       GROUP BY id, x,y,z,landing_tick, defense_points,dc,covered,curreta,fleets
+                       ORDER BY landing_tick DESC
+               })or warn  $DBH->errstr;
+       $calls->execute($ND::UID) or warn $DBH->errstr;
+
        my @calls;
-       while (my $call = $query->fetchrow_hashref){
+       while (my $call = $calls->fetchrow_hashref){
                $call->{attackers} =~ s{(\d+:\d+:\d+)}{<a href="/check?coords=$1">$1</a>}g;
-               unless(defined $call->{username}){
-                       $call->{dc} = 'Hostile';
-                       $call->{username} = 'none';
+               unless(defined $call->{dc}){
+                       $call->{activedc} = 'Hostile';
+                       $call->{dc} = 'none';
                }
                if($call->{covered}){
                        $call->{covered} = 'Friendly';
                }else{
                        $call->{covered} = 'Hostile';
                }
-               $i++;
-               $call->{ODD} = $i % 2;
-               $call->{shiptype} = escapeHTML($call->{shiptype});
+               $call->{shiptype} = $call->{shiptype};
                push @calls, $call;
        }
        $BODY->param(Calls => \@calls);
@@ -197,28 +205,58 @@ sub render_body {
        }
 
 
-       $query = $DBH->prepare(q{SELECT f.fleet,f.id, coords(x,y,z) AS target, mission, sum(fs.amount) AS amount, landing_tick, back
+       $query = $DBH->prepare(q{SELECT f.id, coords(x,y,z) AS target, mission
+               , f.amount, tick, back
 FROM fleets f 
-JOIN fleet_ships fs ON f.id = fs.fleet 
-JOIN current_planet_stats p ON f.target = p.id
-WHERE f.uid = ? AND (f.fleet = 0 OR back >= ?)
-GROUP BY f.fleet,f.id, x,y,z, mission, landing_tick,back
-ORDER BY f.fleet
+LEFT OUTER JOIN current_planet_stats p ON f.target = p.id
+WHERE NOT ingal AND f.uid = ? AND f.sender = ? AND 
+       (back >= ? OR (tick >= tick() -  24 AND name = 'Main'))
+GROUP BY f.id, x,y,z, mission, tick,back,f.amount
+ORDER BY x,y,z,mission,tick
                });
 
-       $query->execute($ND::UID,$self->{TICK}) or $error .= '<p>'.$DBH->errstr.'</p>';
+       my $ships = $DBH->prepare(q{SELECT ship,amount FROM fleet_ships
+               WHERe id = ? ORDER BY num
+       });
+
+       $query->execute($self->{UID},$self->{PLANET},$self->{TICK}) or warn $DBH->errstr;
        my @fleets;
-       $i = 0;
        while (my $fleet = $query->fetchrow_hashref){
-               $i++;
-               $fleet->{ODD} = $i % 2;
+               my @ships;
+               $ships->execute($fleet->{id});
+               while (my $ship = $ships->fetchrow_hashref){
+                       push @ships,$ship;
+               }
+               $fleet->{ships} = \@ships;
                push @fleets,$fleet;
        }
        $BODY->param(Fleets => \@fleets);
 
        $BODY->param(SMS => $sms);
        $BODY->param(Hostname => $hostname);
-       $BODY->param(Error => $error);
+
+       if ($self->isMember()){
+               my $announcements = $DBH->prepare(q{SELECT ft.ftid AS id,u.username,ft.subject,
+                       count(NULLIF(COALESCE(fp.time > ftv.time,TRUE),FALSE)) AS unread,count(fp.fpid) AS posts,
+                       date_trunc('seconds',max(fp.time)::timestamp) as last_post,
+                       min(fp.time)::date as posting_date, ft.sticky
+                       FROM forum_threads ft JOIN forum_posts fp USING (ftid) 
+                               JOIN users u ON u.uid = ft.uid
+                               LEFT OUTER JOIN (SELECT * FROM forum_thread_visits WHERE uid = $1) ftv ON ftv.ftid = ft.ftid
+                       WHERE ft.fbid = 1
+                       GROUP BY ft.ftid, ft.subject,ft.sticky,u.username
+                       HAVING count(NULLIF(COALESCE(ft.sticky OR fp.time > ftv.time,TRUE),FALSE)) >= $2
+                       ORDER BY sticky DESC,last_post DESC
+               });
+               $announcements->execute($ND::UID,1) or warn $DBH->errstr;
+               my @threads;
+               while (my $thread = $announcements->fetchrow_hashref){
+                       push @threads,$thread;
+               }
+               $BODY->param(Announcements => \@threads);
+       }
+
+
        return $BODY;
 }