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/025-empire-debt.sql
Emmanuel BENOîT 56eddcc4f0 Game updates improvements
* Added a set of tables which define game updates and their targets.
These definitions replace the old enumerate type. Added a set of
triggers which automatically create specific update tables, insert
missing entries, etc... when game update types are being manipulated.

* Removed manual insertion of game updates from empire creation
function and universe generator.

* Added registration of core update targets (i.e. planets and empires),
updated all existing game update processing functions and added type
registrations

* Created Maven project for game updates control components, moved
existing components from the -simple project, rewritten most of what
they contained, added new components for server-side update batch
processing
2012-02-03 16:25:03 +01:00

69 lines
No EOL
2.1 KiB
PL/PgSQL

-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Game updates - damage fleets and buildings when an
-- empire is out of cash and has too much upkeep
--
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------
CREATE OR REPLACE FUNCTION sys.process_empire_debt_updates( c_tick BIGINT )
RETURNS VOID
STRICT VOLATILE
SECURITY INVOKER
AS $$
DECLARE
fleet_dr REAL;
bld_dr REAL;
empire INT;
debt REAL;
upkeep REAL;
BEGIN
fleet_dr := sys.get_constant( 'game.debt.fleet');
bld_dr := sys.get_constant( 'game.debt.buildings');
FOR empire, debt IN SELECT e.name_id AS id , e.debt
FROM sys.updates _upd_sys
INNER JOIN emp.empires_updates eu
USING ( updtgt_id , updtype_id , update_id )
INNER JOIN emp.empires e USING ( name_id )
WHERE _upd_sys.update_last = c_tick
AND _upd_sys.update_state = 'PROCESSING'
AND e.debt > 0
FOR UPDATE
LOOP
PERFORM sys.write_log( 'EmpireDebt' , 'DEBUG'::log_level , 'Handling debt for empire #'
|| empire || ' (at tick: ' || debt || '; daily: ' || ( debt * 1440 ) || ')' );
debt := debt * 1440.0;
-- Does the empire own fleets?
SELECT INTO upkeep sum( d.upkeep * s.amount )
FROM fleets.fleets f
INNER JOIN fleets.ships s ON s.fleet_id = f.id
INNER JOIN tech.buildables d ON d.name_id = s.ship_id
WHERE f.owner_id = empire;
IF upkeep IS NOT NULL
THEN
PERFORM fleets.handle_debt( empire , upkeep , ( CASE WHEN debt > upkeep THEN upkeep ELSE debt END ) , fleet_dr );
debt := debt - upkeep;
CONTINUE WHEN debt <= 0;
END IF;
-- Does the empire have buildings?
SELECT INTO upkeep sum( d.upkeep * b.amount )
FROM emp.planets ep
INNER JOIN verse.planet_buildings b ON b.planet_id = ep.planet_id
INNER JOIN tech.buildables d ON d.name_id = b.building_id
WHERE ep.empire_id = empire;
CONTINUE WHEN NOT FOUND OR upkeep = 0;
PERFORM verse.handle_debt( empire , upkeep , debt , bld_dr );
END LOOP;
END;
$$ LANGUAGE plpgsql;
SELECT sys.register_update_type( 'Empires' , 'EmpireDebt' ,
'The effects of empires'' debts are being computed.' ,
'process_empire_debt_updates'
);