Empire resources update
* Added a new update that computes the amount of resources possessed and owed by empires. At the moment this computation ignores fleets, as their upkeep does not use the resource system.
This commit is contained in:
parent
b49bc1a44f
commit
04e550709a
4 changed files with 186 additions and 2 deletions
|
@ -29,9 +29,18 @@ CREATE TYPE log_type
|
|||
-- Update types
|
||||
CREATE TYPE update_type AS ENUM (
|
||||
|
||||
/* Empires' money is being updated using the previous update's results */
|
||||
/* Empires' money is being updated using the previous update's results
|
||||
*
|
||||
* FIXME: this update type should disappear, as everything it does will
|
||||
* be replaced by the resources update.
|
||||
*/
|
||||
'EMPIRE_MONEY' ,
|
||||
|
||||
/* Empires' resources are being updated using the previous update's
|
||||
* results.
|
||||
*/
|
||||
'EMPIRE_RESOURCES' ,
|
||||
|
||||
/* Empire research points are being attributed to technologies */
|
||||
'EMPIRE_RESEARCH' ,
|
||||
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
-- LegacyWorlds Beta 6
|
||||
-- PostgreSQL database scripts
|
||||
--
|
||||
-- Game updates - empire resources
|
||||
--
|
||||
-- Copyright(C) 2004-2012, DeepClone Development
|
||||
-- --------------------------------------------------------
|
||||
|
||||
|
||||
/*
|
||||
* Empire resources update
|
||||
*
|
||||
* For each empire in the update batch, compute the new amount of resources
|
||||
* based on the upkeep and income from planets.
|
||||
*
|
||||
* FIXME: this should also include fleet upkeeps. This situation will be
|
||||
* resolved once the system is modified to rely solely on resources.
|
||||
*
|
||||
* Parameters:
|
||||
* _tick The identifier of the game update
|
||||
*/
|
||||
DROP FUNCTION IF EXISTS sys.process_empire_resources_updates( BIGINT );
|
||||
CREATE FUNCTION sys.process_empire_resources_updates( _tick BIGINT )
|
||||
RETURNS VOID
|
||||
STRICT VOLATILE
|
||||
SECURITY INVOKER
|
||||
AS $process_empire_resources_updates$
|
||||
|
||||
UPDATE emp.resources _emp_resources
|
||||
|
||||
SET empres_possessed = CASE
|
||||
WHEN _emp_resources.empres_possessed + _pl_resources.pres_income - _pl_resources.pres_upkeep > 0 THEN
|
||||
_emp_resources.empres_possessed + _pl_resources.pres_income - _pl_resources.pres_upkeep
|
||||
ELSE
|
||||
0
|
||||
END ,
|
||||
empres_owed = CASE
|
||||
WHEN _emp_resources.empres_possessed + _pl_resources.pres_income - _pl_resources.pres_upkeep < 0 THEN
|
||||
_pl_resources.pres_upkeep - _emp_resources.empres_possessed - _pl_resources.pres_income
|
||||
ELSE
|
||||
0
|
||||
END
|
||||
|
||||
FROM sys.updates _upd_sys
|
||||
INNER JOIN emp.updates _upd_emp
|
||||
ON _upd_emp.update_id = _upd_sys.id
|
||||
INNER JOIN emp.planets _emp_planets
|
||||
USING ( empire_id )
|
||||
INNER JOIN verse.planet_resources _pl_resources
|
||||
USING ( planet_id )
|
||||
|
||||
WHERE _upd_sys.last_tick = $1
|
||||
AND _upd_sys.status = 'PROCESSING'
|
||||
AND _upd_sys.gu_type = 'EMPIRE_RESOURCES'
|
||||
AND _emp_resources.empire_id = _upd_emp.empire_id
|
||||
AND _emp_resources.resource_name_id = _pl_resources.resource_name_id;
|
||||
|
||||
$process_empire_resources_updates$ LANGUAGE SQL;
|
||||
|
||||
|
||||
REVOKE EXECUTE
|
||||
ON FUNCTION sys.process_empire_resources_updates( BIGINT )
|
||||
FROM PUBLIC;
|
|
@ -0,0 +1,101 @@
|
|||
/*
|
||||
* Test the sys.process_empire_resources_updates() function
|
||||
*/
|
||||
BEGIN;
|
||||
/* We need:
|
||||
* - three types of resources,
|
||||
* - two empire with resources records, and the corresponding update
|
||||
* record,
|
||||
* - two planets, set to belong to the aforementioned empires, with
|
||||
* resources records attached.
|
||||
*/
|
||||
\i utils/strings.sql
|
||||
\i utils/resources.sql
|
||||
\i utils/accounts.sql
|
||||
\i utils/naming.sql
|
||||
\i utils/universe.sql
|
||||
SELECT _create_resources( 3 , 'testResource' );
|
||||
SELECT _create_raw_planets( 2 , 'testPlanet' );
|
||||
SELECT _create_emp_names( 2 , 'testEmp' );
|
||||
SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet1' ) , 1 );
|
||||
INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
|
||||
SELECT _get_map_name( 'testPlanet1' ) , resource_name_id FROM defs.resources;
|
||||
INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
|
||||
SELECT _get_map_name( 'testPlanet2' ) , resource_name_id FROM defs.resources;
|
||||
|
||||
/* Set up the planets' resource records so that:
|
||||
* - income > upkeep for testResource1 ,
|
||||
* - income < upkeep for testResource2 ,
|
||||
* - income = upkeep = 0 for testResource3 ,
|
||||
*/
|
||||
UPDATE verse.planet_resources SET pres_income = 12
|
||||
WHERE resource_name_id = _get_string( 'testResource1' );
|
||||
UPDATE verse.planet_resources SET pres_upkeep = 34
|
||||
WHERE resource_name_id = _get_string( 'testResource2' );
|
||||
|
||||
/* Make sure update 0 is set to be processed for testEmp1 */
|
||||
UPDATE sys.updates
|
||||
SET status = 'PROCESSING' , last_tick = 0;
|
||||
|
||||
/* Create testEmp2 empire, make sure it will not be processed */
|
||||
SELECT emp.create_empire( _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) , 1 );
|
||||
UPDATE sys.updates _su
|
||||
SET status = 'PROCESSED' , last_tick = 0
|
||||
FROM emp.updates _eu
|
||||
WHERE _eu.update_id = _su.id AND _eu.empire_id = _get_emp_name( 'testEmp2' );
|
||||
|
||||
/****** TESTS BEGIN HERE ******/
|
||||
SELECT plan( 19 );
|
||||
|
||||
SELECT sys.process_empire_resources_updates( 10 );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (1/6)' );
|
||||
SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource1' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (2/6)' );
|
||||
SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource1' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (3/6)' );
|
||||
SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource2' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (4/6)' );
|
||||
SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource2' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (5/6)' );
|
||||
SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource3' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (6/6)' );
|
||||
SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource3' );
|
||||
|
||||
SELECT sys.process_empire_resources_updates( 0 );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 1 - Possessed quantity increased' );
|
||||
SELECT is( empres_possessed , 12::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource1' )
|
||||
AND empire_id = _get_emp_name( 'testEmp1' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 1 - Owed quantity unchanged' );
|
||||
SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource1' )
|
||||
AND empire_id = _get_emp_name( 'testEmp1' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 2 - Possessed quantity unchanged' );
|
||||
SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource2' )
|
||||
AND empire_id = _get_emp_name( 'testEmp1' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 2 - Owed quantity increased' );
|
||||
SELECT is( empres_owed , 34::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource2' )
|
||||
AND empire_id = _get_emp_name( 'testEmp1' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 3 - Possessed quantity unchanged' );
|
||||
SELECT is( empres_possessed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource3' )
|
||||
AND empire_id = _get_emp_name( 'testEmp1' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 3 - Owed quantity unchanged' );
|
||||
SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
|
||||
WHERE resource_name_id = _get_string( 'testResource3' )
|
||||
AND empire_id = _get_emp_name( 'testEmp1' );
|
||||
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resources unchanged for already processed empire' );
|
||||
SELECT is( SUM( empres_owed + empres_possessed ) , 0::DOUBLE PRECISION )
|
||||
FROM emp.resources
|
||||
WHERE empire_id = _get_emp_name( 'testEmp2' );
|
||||
|
||||
SELECT * FROM finish( );
|
||||
ROLLBACK;
|
|
@ -0,0 +1,11 @@
|
|||
/*
|
||||
* Test privileges on sys.process_empire_resources_updates()
|
||||
*/
|
||||
BEGIN;
|
||||
SELECT plan( 1 );
|
||||
|
||||
SELECT diag_test_name( 'sys.process_empire_resources_updates() - Privileges' );
|
||||
SELECT throws_ok( 'SELECT sys.process_empire_resources_updates( 1 )' , 42501 );
|
||||
|
||||
SELECT * FROM finish( );
|
||||
ROLLBACK;
|
Reference in a new issue