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/010-empire-money.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

92 lines
No EOL
2.5 KiB
PL/PgSQL

-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Game updates - empire money
--
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------
CREATE OR REPLACE FUNCTION sys.process_empire_money_updates( c_tick BIGINT )
RETURNS VOID
STRICT VOLATILE
SECURITY INVOKER
AS $$
DECLARE
rec RECORD;
c_cash REAL;
c_debt REAL;
BEGIN
-- Lock empires for update
PERFORM e.name_id
FROM sys.updates su
INNER JOIN emp.empires_updates eu
USING ( updtgt_id , updtype_id , update_id )
INNER JOIN emp.empires e
USING ( name_id )
WHERE su.update_last = c_tick
AND su.update_state = 'PROCESSING'
FOR UPDATE OF e;
-- Select all money-related data from empires being updated
FOR rec IN SELECT e.name_id AS id , e.cash AS cash , e.debt AS debt ,
( pov.planet_income - pov.planet_upkeep ) AS p_money ,
fov.fleet_upkeep AS f_money , ( v.status = 'PROCESSED' ) AS on_vacation
FROM sys.updates su
INNER JOIN emp.empires_updates eu
USING ( updtgt_id , updtype_id , update_id )
INNER JOIN emp.empires e
USING ( name_id )
INNER JOIN emp.fleets_overview fov ON fov.empire = e.name_id
INNER JOIN emp.planets_overview pov ON pov.empire = e.name_id
INNER JOIN naming.empire_names en ON en.id = e.name_id
LEFT OUTER JOIN users.vacations v ON v.account_id = en.owner_id
WHERE su.update_last = c_tick
AND su.update_state = 'PROCESSING'
LOOP
-- Compute new cash reserve
c_cash := 0;
IF rec.p_money IS NOT NULL THEN
c_cash := c_cash + rec.p_money;
END IF;
IF rec.f_money IS NOT NULL THEN
c_cash := c_cash - rec.f_money;
END IF;
-- Effects of vacation mode
IF rec.on_vacation
THEN
c_cash := c_cash / sys.get_constant( 'vacation.cashDivider' );
END IF;
-- Handle debt
c_cash := rec.cash + c_cash / 1440.0;
IF c_cash < 0 THEN
c_debt := -c_cash;
c_cash := 0;
ELSE
c_debt := 0;
END IF;
IF rec.debt > 0 AND c_debt = 0
THEN
PERFORM events.debt_event( rec.id , FALSE );
ELSEIF rec.debt = 0 AND c_debt > 0
THEN
PERFORM events.debt_event( rec.id , TRUE );
END IF;
-- Update empire
UPDATE emp.empires SET cash = c_cash , debt = c_debt
WHERE name_id = rec.id;
END LOOP;
END;
$$ LANGUAGE plpgsql;
SELECT sys.register_update_type( 'Empires' , 'EmpireMoney' ,
'Empires'' money is being updated using the previous update''s results. '
|| 'This update type should disappear, as everything it does will '
|| 'be replaced by the resources update.' ,
'process_empire_money_updates'
);