diff --git a/legacyworlds-server-data/db-structure/parts/050-updates/015-empire-resources.sql b/legacyworlds-server-data/db-structure/parts/050-updates/015-empire-resources.sql index 6004945..64d1448 100644 --- a/legacyworlds-server-data/db-structure/parts/050-updates/015-empire-resources.sql +++ b/legacyworlds-server-data/db-structure/parts/050-updates/015-empire-resources.sql @@ -29,30 +29,46 @@ 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 + WHEN _emp_resources.empres_possessed + _raw.res_delta > 0 THEN + _emp_resources.empres_possessed + _raw.res_delta 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 + WHEN _emp_resources.empres_possessed + _raw.res_delta < 0 THEN + -( _emp_resources.empres_possessed + _raw.res_delta ) ELSE 0 END - FROM sys.updates _upd_sys - INNER JOIN emp.empires_updates _upd_emp - USING ( updtgt_id , updtype_id , update_id ) - INNER JOIN emp.planets _emp_planets - ON empire_id = name_id - INNER JOIN verse.planet_resources _pl_resources - USING ( planet_id ) + 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 - WHERE _upd_sys.update_last = $1 - AND _upd_sys.update_state = 'PROCESSING' - AND _emp_resources.empire_id = _upd_emp.name_id - AND _emp_resources.resource_name_id = _pl_resources.resource_name_id; + FROM sys.updates _upd_sys + INNER JOIN emp.empires_updates _upd_emp + USING ( updtgt_id , updtype_id , update_id ) + INNER JOIN emp.planets _emp_planets + ON empire_id = name_id + INNER JOIN verse.planet_resources _pl_resources + 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 + AND _upd_sys.update_state = 'PROCESSING' + ) _raw + + WHERE _emp_resources.empire_id = _raw.empire_id + AND _emp_resources.resource_name_id = _raw.resource_name_id; $process_empire_resources_updates$ LANGUAGE SQL; diff --git a/legacyworlds-server-data/db-structure/tests/admin/050-updates/015-empire-resources/010-process-empire-resources-updates.sql b/legacyworlds-server-data/db-structure/tests/admin/050-updates/015-empire-resources/010-process-empire-resources-updates.sql index 5e670c7..969aa97 100644 --- a/legacyworlds-server-data/db-structure/tests/admin/050-updates/015-empire-resources/010-process-empire-resources-updates.sql +++ b/legacyworlds-server-data/db-structure/tests/admin/050-updates/015-empire-resources/010-process-empire-resources-updates.sql @@ -10,9 +10,9 @@ BEGIN; /* We need: * - three types of resources, - * - two empire with resources records, and the corresponding update + * - three empires with resources records, and the corresponding update * record, - * - two planets, set to belong to the aforementioned empires, with + * - three planets, set to belong to the aforementioned empires, with * resources records attached. */ \i utils/strings.sql @@ -21,13 +21,14 @@ BEGIN; \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 ); + SELECT _create_raw_planets( 3 , 'testPlanet' ); + SELECT _create_emp_names( 3 , 'testEmp' ); 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; + 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: * - income > upkeep for testResource1 , @@ -39,20 +40,33 @@ BEGIN; UPDATE verse.planet_resources SET pres_upkeep = 34 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 */ UPDATE sys.updates SET update_state = 'PROCESSING' , update_last = 0; - /* Create testEmp2 empire, make sure it will not be processed */ - SELECT emp.create_empire( _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) , 1 ); + /* Create testEmp3 empire, make sure it will not be processed */ + SELECT emp.create_empire( _get_emp_name( 'testEmp3' ) , _get_map_name( 'testPlanet3' ) , 1 ); UPDATE sys.updates _su SET update_state = 'PROCESSED' , update_last = 0 FROM emp.empires_updates _eu 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 ******/ - SELECT plan( 19 ); + SELECT plan( 31 ); SELECT sys.process_empire_resources_updates( 10 ); SELECT diag_test_name( 'EMPIRE_RESOURCES update - Wrong update identifier (1/6)' ); @@ -75,6 +89,7 @@ BEGIN; 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' ) @@ -99,10 +114,36 @@ BEGIN; 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 - 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 is( SUM( empres_owed + empres_possessed ) , 0::DOUBLE PRECISION ) FROM emp.resources - WHERE empire_id = _get_emp_name( 'testEmp2' ); + WHERE empire_id = _get_emp_name( 'testEmp3' ); SELECT * FROM finish( ); ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/050-updates/015-empire-resources/010-process-empire-resources-updates.sql b/legacyworlds-server-data/db-structure/tests/user/050-updates/015-empire-resources/010-process-empire-resources-updates.sql index 80c7ff8..eba8788 100644 --- a/legacyworlds-server-data/db-structure/tests/user/050-updates/015-empire-resources/010-process-empire-resources-updates.sql +++ b/legacyworlds-server-data/db-structure/tests/user/050-updates/015-empire-resources/010-process-empire-resources-updates.sql @@ -4,7 +4,7 @@ BEGIN; 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 * FROM finish( );