]> ruin.nu Git - ndwebbie.git/blobdiff - lib/NDWeb/Controller/Calls.pm
Show value percentage for defenders on call page
[ndwebbie.git] / lib / NDWeb / Controller / Calls.pm
index 76d7f63a04a7bd768b2c01dc778935eb15ac696b..0ba04a513c40f2ceabe72a14b0bab6b504afb216 100644 (file)
@@ -146,17 +146,32 @@ SELECT ship,amount from ships_home WHERE pid = $1 AND tick = $2
        $c->stash(fleets => \@fleets);
 
        my $defenders = $dbh->prepare(q{
+WITH lc AS (
+       SELECT fid,uid,back,eta,landing_tick
+       FROM launch_confirmations
+       WHERE pid = $1 AND landing_tick = $2
+), lcp AS (
+       SELECT DISTINCT ON (fid) fid,tick,value
+       FROM lc
+               JOIN users u USING (uid)
+               JOIN planet_stats USING (pid)
+       WHERE tick <= landing_tick - eta
+       ORDER BY fid,tick DESC
+), lcv AS (
+       SELECT fid,(sum(amount*(metal+crystal+eonium)) / value)::int  AS value_ratio
+       FROM lcp
+               JOIN fleet_ships fs USING (fid)
+               JOIN ship_stats s ON s.name = fs.ship
+       GROUP BY fid, value
+)
 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
+       ,back, (back <> landing_tick + eta - 1) AS recalled, value_ratio
 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'
+       JOIN lc USING (fid)
+       JOIN lcv USING (fid)
+WHERE mission IN ('Defend', 'Fake Defend')
 ORDER BY x,y,z
        });
 
@@ -218,20 +233,10 @@ sub defleeches : Local {
        my ( $self, $c, $type ) = @_;
        my $dbh = $c->model;
 
-       my $query = $dbh->prepare(q{SELECT username,defense_points,count(call) AS calls
-               , SUM(fleets) AS fleets, SUM(recalled) AS recalled
-               ,count(NULLIF(fleets,0)) AS defended_calls
-               FROM (SELECT username,defense_points,call,count(f.back) AS fleets
-                       , count(NULLIF(f.landing_tick + f.eta -1 = f.back,TRUE)) AS recalled
-                       FROM users u JOIN calls c USING (uid)
-                               LEFT OUTER JOIN (
-                                       SELECT lc.pid,landing_tick,eta,back
-                                       FROM launch_confirmations lc JOIN fleets f USING (fid)
-                                       WHERE mission = 'Defend'
-                               ) f USING (pid,landing_tick)
-                       GROUP BY username,defense_points,call
-               ) d
-               GROUP BY username,defense_points ORDER BY fleets DESC, defense_points
+       my $query = $dbh->prepare(q{
+SELECT username, defense_points, calls, fleets, recalled, defended_calls, value, sent_value
+FROM def_leeches
+ORDER BY value DESC NULLS LAST, defense_points
                });
        $query->execute;