-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Game updates - planet income and upkeep
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------
CREATE OR REPLACE FUNCTION sys.process_planet_money_updates( c_tick BIGINT )
RETURNS VOID
STRICT VOLATILE
SECURITY INVOKER
AS $$
DECLARE
rec RECORD;
incme DOUBLE PRECISION;
BEGIN
FOR rec IN SELECT p.name_id AS id , p.population AS pop ,
ph.current / p.population AS happiness ,
( ea.planet_id IS NULL ) AS produces_income
FROM sys.updates su
INNER JOIN verse.updates vu ON vu.update_id = su.id
INNER JOIN verse.planets p ON vu.planet_id = p.name_id
INNER JOIN verse.planet_happiness ph ON ph.planet_id = p.name_id
INNER JOIN verse.planet_money pm ON pm.planet_id = p.name_id
LEFT OUTER JOIN emp.abandon ea ON ea.planet_id = p.name_id
WHERE su.last_tick = c_tick AND su.status = 'PROCESSING'
AND su.gu_type = 'PLANET_MONEY'
FOR UPDATE OF p, pm
LOOP
IF rec.produces_income THEN
incme := verse.compute_income( rec.pop , rec.happiness ,
verse.get_raw_production( rec.id , 'CASH' )
);
ELSE
incme := 0;
END IF;
UPDATE verse.planet_money
SET income = incme ,
upkeep = verse.get_planet_upkeep( rec.id )
WHERE planet_id = rec.id;
END LOOP;
END;
$$ LANGUAGE plpgsql;