]> ruin.nu Git - ndwebbie.git/blobdiff - database/available_ships.sql
intel role
[ndwebbie.git] / database / available_ships.sql
index e2905836739c24eae77675fa7d3f8d10d85d5ad5..e7d70b7d95315cf8c3c798792103891287473606 100644 (file)
@@ -1,28 +1,39 @@
-DROP VIEW available_ships;
-CREATE VIEW available_ships AS
-SELECT uid,username, planet, ship
+/*CREATE TABLE ticks (t INTEGER PRIMARY KEY);
+
+INSERT INTO ticks (SELECT * FROM generate_series(0,10000));
+*/
+
+CREATE OR REPLACE VIEW ships_home AS
+SELECT tick, uid,username, pid, ship
        , COALESCE(f.amount - o.amount,f.amount) AS amount
        , COALESCE(fleets,3) AS fleets
 FROM users u JOIN (
-       SELECT planet,ship,amount
+       SELECT t AS tick, pid,ship,amount
        FROM (
-               SELECT DISTINCT ON (planet,mission) planet,mission, fid
-               FROM fleets f
-               WHERE tick <= tick()
+               SELECT DISTINCT ON (t,pid,mission) t,pid,mission, fid
+               FROM ticks
+                       CROSS JOIN fleets f
+               WHERE tick <= t
                        AND name IN ('Main','Advanced Unit')
                        AND mission = 'Full fleet'
-               ORDER BY planet,mission,tick DESC, fid DESC
+               ORDER BY t,pid,mission,tick DESC, fid DESC
        ) f
                JOIN fleet_ships fs USING (fid)
        
-) f USING (planet) LEFT OUTER JOIN (
-       SELECT planet, ship, SUM(fs.amount) AS amount, 3 - COUNT(DISTINCT fid) AS fleets
-       FROM fleets f
-               JOIN launch_confirmations USING (fid)
+) f USING (pid) LEFT OUTER JOIN (
+       SELECT t AS tick, pid, ship, SUM(fs.amount) AS amount
+               , 3 - COUNT(DISTINCT fid) AS fleets
+       FROM ticks
+               CROSS JOIN fleets f
+               JOIN (SELECT landing_tick, fid, back, eta FROM launch_confirmations) lc USING (fid)
                JOIN fleet_ships fs USING (fid)
-       WHERE back > tick()
-               AND landing_tick - eta - 12 < tick()
-       GROUP BY planet,ship
-) o USING (planet,ship)
-WHERE COALESCE(f.amount - o.amount,f.amount) > 0
+       WHERE back > t
+               AND landing_tick - eta - 12 < t
+       GROUP BY t,pid,ship
+) o USING (tick,pid,ship)
+WHERE COALESCE(f.amount - o.amount,f.amount) > 0;
 
+CREATE OR REPLACE VIEW available_ships AS
+SELECT uid,username, pid, ship, amount, fleets
+FROM ships_home
+WHERE tick = tick();