This repository has been archived on 2025-01-04. You can view files and clone it, but cannot push or open issues or pull requests.
lwb6/legacyworlds-server-data/db-structure/parts/050-updates/110-planet-money.sql
Emmanuel BENOîT e50775ec76 Database definition & tests organisation
* The main loader script has been updated to generate the list of files
it needs to load automatically. As a consequence, files that contained
manually-maintained lists of scripts have been removed, and definition
directories have been renamed accordingly.

* PostgreSQL extension loading and configuration has been moved to a
separate script to be loaded automatically in the main transaction.

* Data and function definition scripts that had the -data or -functions
suffix have been renamed (the suffix is unnecessary).

* Unit tests have been reorganised to follow the definition's structure.

* Documentation has been improved
2012-01-06 11:19:19 +01:00

45 lines
No EOL
1.3 KiB
PL/PgSQL

-- 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 REAL;
BEGIN
FOR rec IN SELECT p.name_id AS id , p.population AS pop ,
( ph.current / p.population )::REAL 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;