]> ruin.nu Git - ndwebbie.git/blobdiff - lib/NDWeb/Controller/Calls.pm
Change database structure for planets and alliances + PG 8.4 changes
[ndwebbie.git] / lib / NDWeb / Controller / Calls.pm
index 7edc10b48bfad06506db7cc8a94cfa3d8913a07c..5c3298a675dddc5581c770c3751c0b11b4dee33d 100644 (file)
@@ -34,7 +34,7 @@ sub list : Local {
        my ( $self, $c, $type ) = @_;
        my $dbh = $c->model;
 
-       my $where = q{open AND c.landing_tick-6 > tick()};
+       my $where = q{open AND landing_tick-6 > tick()};
        my $order = q{landing_tick DESC, defprio DESC};
        if (defined $type){
                if ($type eq 'covered'){
@@ -44,7 +44,7 @@ sub list : Local {
                }elsif ($type eq 'uncovered'){
                        $where = 'not covered';
                }elsif ($type eq 'recent'){
-                       $where = q{c.landing_tick > tick()};
+                       $where = q{landing_tick > tick()};
                        $order = q{x,y,z};
                }
        }
@@ -53,35 +53,10 @@ sub list : Local {
        $c->stash(maxprio => $dbh->selectrow_array($pointlimits,undef,'DEFMAXPRIO'));
 
        my $query = $dbh->prepare(qq{
-               SELECT id,coords(x,y,z),planet,landing_tick,dc,curreta,fleets
-                       ,defprio
-                       ,array_accum(COALESCE(race::text,'')) AS race
-                       ,array_accum(COALESCE(amount,0)) AS amount
-                       ,array_accum(COALESCE(eta,0)) AS eta
-                       ,array_accum(COALESCE(shiptype,'')) AS shiptype
-                       ,array_accum(COALESCE(alliance,'?')) AS alliance
-                       ,array_accum(coords) AS attackers
-               FROM (SELECT c.id, p.x,p.y,p.z,p.id AS planet, defprio
-                       ,u.defense_points, u.attack_points, c.landing_tick
-                       ,dc.username AS dc, (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.fid) AS fleets
-                       FROM calls c
-                               JOIN users_defprio u ON c.member = u.uid
-                               JOIN current_planet_stats p ON u.planet = p.id
-                               LEFT OUTER JOIN incomings i ON i.call = c.id
-                               LEFT OUTER JOIN current_planet_stats p2 ON i.sender = p2.id
-                               LEFT OUTER JOIN users dc ON c.dc = dc.uid
-                               LEFT OUTER JOIN launch_confirmations f ON f.target = u.planet
-                                       AND f.landing_tick = c.landing_tick AND f.back = f.landing_tick + f.eta - 1
-                       WHERE $where
-                       GROUP BY c.id, p.x,p.y,p.z,p.id, defprio, c.landing_tick
-                               ,u.defense_points,u.attack_points,dc.username
-                               ,p2.race,i.amount,i.eta,i.shiptype,p2.alliance,p2.x,p2.y,p2.z
-                       ) c
-               GROUP BY id, x,y,z,planet,landing_tick, defense_points, attack_points
-                       ,dc,curreta,fleets, defprio
-               ORDER BY $order
+SELECT *, pid AS planet, coords(x,y,z)
+FROM full_defcalls
+WHERE $where
+ORDER BY $order
                });
        $query->execute;
        $c->stash(calls => $query->fetchall_arrayref({}));
@@ -105,34 +80,36 @@ sub edit : Local {
 
        my $outgoings = $dbh->prepare(q{ 
 (
-       SELECT DISTINCT ON (mission,name) fid,mission,name,tick, NULL AS eta
-               ,amount, NULL AS coords, planet, NULL AS back, NULL AS recalled
+       SELECT DISTINCT ON (mission,name) 1 AS type, fid,mission,name,tick, NULL AS eta
+               ,amount, NULL AS coords, pid AS planet, NULL AS back, NULL AS recalled
        FROM fleets f
-       WHERE planet = $1 AND tick <= $2 AND (
+       WHERE pid = $1 AND tick <= $2 AND (
                        mission = 'Full fleet'
                        OR fid IN (SELECT fid FROM fleet_scans)
                ) AND (
                        mission = 'Full fleet'
                        OR tick >= $2 - 12
                )
-       ORDER BY mission,name,tick DESC
 ) UNION (
-       SELECT fid,mission,name,landing_tick AS tick, eta, amount
-               , coords(x,y,z), t.id AS planet, back
+       SELECT 2 AS type, MAX(fid) AS fid,mission,name,landing_tick AS tick, eta, amount
+               , coords(x,y,z), pid AS planet, back
                , (back <> landing_tick + eta - 1) AS recalled
-       FROM fleets f
-               JOIN launch_confirmations USING (fid)
-       LEFT OUTER JOIN current_planet_stats t ON target = t.id
-       WHERE f.planet = $1 AND back >= $2 AND landing_tick - eta - 12 < $2
+       FROM launch_confirmations
+               JOIN (
+                       SELECT fid,amount,name,mission FROM fleets WHERE pid = $1
+               ) f  USING (fid)
+               LEFT OUTER JOIN current_planet_stats t USING (pid)
+       WHERE back >= $2 AND landing_tick - eta - 12 < $2
+       GROUP BY mission,name,landing_tick,eta,amount,back,x,y,z,pid
 ) UNION (
        SELECT DISTINCT ON (tick,x,y,z,mission,name,amount)
-               NULL as fid, i.mission, i.name, i.tick,eta
-               , i.amount, coords(x,y,z), t.id AS planet, back, NULL AS recalled
+               3 AS type, NULL as fid, i.mission, i.name, i.tick,eta
+               , i.amount, coords(x,y,z), t.pid AS planet, back, NULL AS recalled
        FROM intel i
-       LEFT OUTER JOIN current_planet_stats t ON i.target = t.id
+       LEFT OUTER JOIN current_planet_stats t ON i.target = t.pid
        WHERE uid = -1 AND i.sender = $1 AND i.tick > $2 - 14 AND i.tick < $2 + 14
        ORDER BY i.tick,x,y,z,mission,name,amount,back
-)
+) ORDER BY type, mission,name,tick DESC
        });
        my $ships = $dbh->prepare(q{SELECT ship,amount FROM fleet_ships
                WHERE fid = ? ORDER BY num
@@ -167,14 +144,18 @@ SELECT ship,amount from ships_home WHERE planet = $1 AND tick = $2
 
        $c->stash(fleets => \@fleets);
 
-       my $defenders = $dbh->prepare(q{ 
-SELECT DISTINCT ON (x,y,z,s.id,name,amount,back) fid,mission, name, eta
-       , amount, coords(x,y,z) AS coords, landing_tick AS tick, f.planet
+       my $defenders = $dbh->prepare(q{
+SELECT DISTINCT ON (x,y,z,pid,name,amount,back) fid,mission, name, eta
+       , amount, coords(x,y,z) AS coords, landing_tick AS tick, pid AS planet
        ,back, (back <> landing_tick + eta - 1) AS recalled
-FROM launch_confirmations lc
-       JOIN fleets f USING (fid)
-       LEFT OUTER JOIN current_planet_stats s ON f.planet = s.id
-WHERE target = ? AND landing_tick = ? AND mission = 'Defend'
+FROM fleets f
+       LEFT OUTER JOIN current_planet_stats s USING (pid)
+       JOIN (
+               SELECT fid,back,eta,landing_tick
+               FROM launch_confirmations
+               WHERE pid = $1 AND landing_tick = $2
+       ) lc USING (fid)
+WHERE mission = 'Defend'
 ORDER BY x,y,z
        });
 
@@ -195,9 +176,9 @@ ORDER BY x,y,z
 
        my $attackers = $dbh->prepare(q{
                SELECT coords(p.x,p.y,p.z), p.planet_status, p.race,i.eta,i.amount
-                       ,i.fleet,i.shiptype,p.relationship,p.alliance,i.id,p.id AS planet
+                       ,i.fleet,i.shiptype,p.relationship,p.alliance,i.id,pid AS planet
                FROM incomings i
-                       JOIN current_planet_stats p ON i.sender = p.id
+                       JOIN current_planet_stats p USING (pid)
                WHERE i.call = ?
                ORDER BY p.x,p.y,p.z
        });
@@ -233,13 +214,14 @@ sub defleeches : Local {
        my $query = $dbh->prepare(q{SELECT username,defense_points,count(id) AS calls
                , SUM(fleets) AS fleets, SUM(recalled) AS recalled
                ,count(NULLIF(fleets,0)) AS defended_calls
-               FROM (SELECT username,defense_points,c.id,count(f.target) AS fleets
+               FROM (SELECT username,defense_points,c.id,count(f.back) AS fleets
                        , count(NULLIF(f.landing_tick + f.eta -1 = f.back,TRUE)) AS recalled
                        FROM users u JOIN calls c ON c.member = u.uid
                                LEFT OUTER JOIN (
-                                       SELECT * FROM launch_confirmations JOIN fleets USING (fid)
+                                       SELECT lc.pid,landing_tick,eta,back
+                                       FROM launch_confirmations lc JOIN fleets f USING (fid)
                                        WHERE mission = 'Defend'
-                               ) f ON u.planet = f.target AND c.landing_tick = f.landing_tick
+                               ) f USING (pid,landing_tick)
                        GROUP BY username,defense_points,c.id
                ) d
                GROUP BY username,defense_points ORDER BY fleets DESC, defense_points
@@ -359,11 +341,11 @@ sub findCall : Private {
        my $query = $dbh->prepare(q{
                SELECT c.id, coords(p.x,p.y,p.z), c.landing_tick, c.info, covered
                        ,open, dc.username AS dc, u.defense_points,c.member AS uid
-                       ,u.planet, u.username AS member, u.sms,c.ftid,calc
+                       ,p.pid AS planet, u.username AS member, u.sms,c.ftid,calc
                FROM calls c 
                JOIN users u ON c.member = u.uid
+               JOIN current_planet_stats p USING (pid)
                LEFT OUTER JOIN users dc ON c.dc = dc.uid
-               JOIN current_planet_stats p ON u.planet = p.id
                WHERE c.id = ?
                });
        $call = $dbh->selectrow_hashref($query,undef,$call);