Empire resources and vacation mode

* Modified the empire resources update to check for vacation mode and
apply the modifier from vacation.cashDivider if necessary.

* Some parts of the database need to be reloaded:
  -> 050-updates/015-empire-resources.sql
This commit is contained in:
Emmanuel BENOîT 2012-02-16 10:22:30 +01:00
parent 96670d45be
commit cb65a6e643
3 changed files with 83 additions and 26 deletions

View file

@ -29,18 +29,27 @@ AS $process_empire_resources_updates$
UPDATE emp.resources _emp_resources UPDATE emp.resources _emp_resources
SET empres_possessed = CASE SET empres_possessed = CASE
WHEN _emp_resources.empres_possessed + _pl_resources.pres_income - _pl_resources.pres_upkeep > 0 THEN WHEN _emp_resources.empres_possessed + _raw.res_delta > 0 THEN
_emp_resources.empres_possessed + _pl_resources.pres_income - _pl_resources.pres_upkeep _emp_resources.empres_possessed + _raw.res_delta
ELSE ELSE
0 0
END , END ,
empres_owed = CASE empres_owed = CASE
WHEN _emp_resources.empres_possessed + _pl_resources.pres_income - _pl_resources.pres_upkeep < 0 THEN WHEN _emp_resources.empres_possessed + _raw.res_delta < 0 THEN
_pl_resources.pres_upkeep - _emp_resources.empres_possessed - _pl_resources.pres_income -( _emp_resources.empres_possessed + _raw.res_delta )
ELSE ELSE
0 0
END END
FROM (
SELECT _upd_emp.name_id AS empire_id , _pl_resources.resource_name_id ,
( _pl_resources.pres_income - _pl_resources.pres_upkeep ) / ( CASE
WHEN _vacation.account_id IS NULL THEN
1
ELSE
sys.get_constant( 'vacation.cashDivider' )
END ) AS res_delta
FROM sys.updates _upd_sys FROM sys.updates _upd_sys
INNER JOIN emp.empires_updates _upd_emp INNER JOIN emp.empires_updates _upd_emp
USING ( updtgt_id , updtype_id , update_id ) USING ( updtgt_id , updtype_id , update_id )
@ -48,11 +57,18 @@ AS $process_empire_resources_updates$
ON empire_id = name_id ON empire_id = name_id
INNER JOIN verse.planet_resources _pl_resources INNER JOIN verse.planet_resources _pl_resources
USING ( planet_id ) USING ( planet_id )
INNER JOIN naming.empire_names _emp_name
ON _emp_name.id = _upd_emp.name_id
LEFT OUTER JOIN users.vacations _vacation
ON _vacation.account_id = _emp_name.owner_id
AND _vacation.status = 'PROCESSED'
WHERE _upd_sys.update_last = $1 WHERE _upd_sys.update_last = $1
AND _upd_sys.update_state = 'PROCESSING' AND _upd_sys.update_state = 'PROCESSING'
AND _emp_resources.empire_id = _upd_emp.name_id ) _raw
AND _emp_resources.resource_name_id = _pl_resources.resource_name_id;
WHERE _emp_resources.empire_id = _raw.empire_id
AND _emp_resources.resource_name_id = _raw.resource_name_id;
$process_empire_resources_updates$ LANGUAGE SQL; $process_empire_resources_updates$ LANGUAGE SQL;

View file

@ -10,9 +10,9 @@ BEGIN;
/* We need: /* We need:
* - three types of resources, * - three types of resources,
* - two empire with resources records, and the corresponding update * - three empires with resources records, and the corresponding update
* record, * record,
* - two planets, set to belong to the aforementioned empires, with * - three planets, set to belong to the aforementioned empires, with
* resources records attached. * resources records attached.
*/ */
\i utils/strings.sql \i utils/strings.sql
@ -21,13 +21,14 @@ BEGIN;
\i utils/naming.sql \i utils/naming.sql
\i utils/universe.sql \i utils/universe.sql
SELECT _create_resources( 3 , 'testResource' ); SELECT _create_resources( 3 , 'testResource' );
SELECT _create_raw_planets( 2 , 'testPlanet' ); SELECT _create_raw_planets( 3 , 'testPlanet' );
SELECT _create_emp_names( 2 , 'testEmp' ); SELECT _create_emp_names( 3 , 'testEmp' );
SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet1' ) , 1 );
INSERT INTO verse.planet_resources ( planet_id , resource_name_id ) INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
SELECT _get_map_name( 'testPlanet1' ) , resource_name_id FROM defs.resources; SELECT _get_map_name( 'testPlanet1' ) , resource_name_id FROM defs.resources;
INSERT INTO verse.planet_resources ( planet_id , resource_name_id ) INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
SELECT _get_map_name( 'testPlanet2' ) , resource_name_id FROM defs.resources; SELECT _get_map_name( 'testPlanet2' ) , resource_name_id FROM defs.resources;
INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
SELECT _get_map_name( 'testPlanet3' ) , resource_name_id FROM defs.resources;
/* Set up the planets' resource records so that: /* Set up the planets' resource records so that:
* - income > upkeep for testResource1 , * - income > upkeep for testResource1 ,
@ -39,20 +40,33 @@ BEGIN;
UPDATE verse.planet_resources SET pres_upkeep = 34 UPDATE verse.planet_resources SET pres_upkeep = 34
WHERE resource_name_id = _get_string( 'testResource2' ); WHERE resource_name_id = _get_string( 'testResource2' );
/* Create testEmp1 empire */
SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet1' ) , 1 );
/* Create testEmp2 empire and add a vacation mode entry; also set the vacation.cashDivider constant */
SELECT emp.create_empire( _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) , 1 );
ALTER TABLE users.vacations
DROP CONSTRAINT fk_vacations_accounts;
INSERT INTO users.vacations ( account_id , since , status )
SELECT owner_id , now( ) , 'PROCESSED'
FROM naming.empire_names
WHERE id = _get_emp_name( 'testEmp2' );
SELECT sys.uoc_constant( 'vacation.cashDivider' , '(test)' , 'Resources' , 2 );
/* Make sure update 0 is set to be processed for testEmp1 */ /* Make sure update 0 is set to be processed for testEmp1 */
UPDATE sys.updates UPDATE sys.updates
SET update_state = 'PROCESSING' , update_last = 0; SET update_state = 'PROCESSING' , update_last = 0;
/* Create testEmp2 empire, make sure it will not be processed */ /* Create testEmp3 empire, make sure it will not be processed */
SELECT emp.create_empire( _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) , 1 ); SELECT emp.create_empire( _get_emp_name( 'testEmp3' ) , _get_map_name( 'testPlanet3' ) , 1 );
UPDATE sys.updates _su UPDATE sys.updates _su
SET update_state = 'PROCESSED' , update_last = 0 SET update_state = 'PROCESSED' , update_last = 0
FROM emp.empires_updates _eu FROM emp.empires_updates _eu
WHERE _eu.update_id = _su.update_id WHERE _eu.update_id = _su.update_id
AND _eu.name_id = _get_emp_name( 'testEmp2' ); AND _eu.name_id = _get_emp_name( 'testEmp3' );
/****** TESTS BEGIN HERE ******/ /****** TESTS BEGIN HERE ******/
SELECT plan( 19 ); SELECT plan( 31 );
SELECT sys.process_empire_resources_updates( 10 ); SELECT sys.process_empire_resources_updates( 10 );
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (1/6)' ); SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (1/6)' );
@ -75,6 +89,7 @@ BEGIN;
WHERE resource_name_id = _get_string( 'testResource3' ); WHERE resource_name_id = _get_string( 'testResource3' );
SELECT sys.process_empire_resources_updates( 0 ); SELECT sys.process_empire_resources_updates( 0 );
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 1 - Possessed quantity increased' ); SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 1 - Possessed quantity increased' );
SELECT is( empres_possessed , 12::DOUBLE PRECISION ) FROM emp.resources SELECT is( empres_possessed , 12::DOUBLE PRECISION ) FROM emp.resources
WHERE resource_name_id = _get_string( 'testResource1' ) WHERE resource_name_id = _get_string( 'testResource1' )
@ -99,10 +114,36 @@ BEGIN;
SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources SELECT is( empres_owed , 0::DOUBLE PRECISION ) FROM emp.resources
WHERE resource_name_id = _get_string( 'testResource3' ) WHERE resource_name_id = _get_string( 'testResource3' )
AND empire_id = _get_emp_name( 'testEmp1' ); AND empire_id = _get_emp_name( 'testEmp1' );
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 1 / Vacation - Possessed quantity increased' );
SELECT is( empres_possessed , 6::DOUBLE PRECISION ) FROM emp.resources
WHERE resource_name_id = _get_string( 'testResource1' )
AND empire_id = _get_emp_name( 'testEmp2' );
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 1 / Vacation - 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( 'testEmp2' );
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 2 / Vacation - 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( 'testEmp2' );
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 2 / Vacation - Owed quantity increased' );
SELECT is( empres_owed , 17::DOUBLE PRECISION ) FROM emp.resources
WHERE resource_name_id = _get_string( 'testResource2' )
AND empire_id = _get_emp_name( 'testEmp2' );
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 3 / Vacation - 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( 'testEmp2' );
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resource 3 / Vacation - 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( 'testEmp2' );
SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resources unchanged for already processed empire' ); SELECT diag_test_name( 'EMPIRE_RESOURCES update - Resources unchanged for already processed empire' );
SELECT is( SUM( empres_owed + empres_possessed ) , 0::DOUBLE PRECISION ) SELECT is( SUM( empres_owed + empres_possessed ) , 0::DOUBLE PRECISION )
FROM emp.resources FROM emp.resources
WHERE empire_id = _get_emp_name( 'testEmp2' ); WHERE empire_id = _get_emp_name( 'testEmp3' );
SELECT * FROM finish( ); SELECT * FROM finish( );
ROLLBACK; ROLLBACK;

View file

@ -4,7 +4,7 @@
BEGIN; BEGIN;
SELECT plan( 1 ); SELECT plan( 1 );
SELECT diag_test_name( 'sys.process_empire_resources_updates() - Privileges' ); SELECT diag_test_name( 'sys.process_empire_resources_updates() - No EXECUTE privilege' );
SELECT throws_ok( 'SELECT sys.process_empire_resources_updates( 1 )' , 42501 ); SELECT throws_ok( 'SELECT sys.process_empire_resources_updates( 1 )' , 42501 );
SELECT * FROM finish( ); SELECT * FROM finish( );