diff --git a/legacyworlds-server-data/db-structure/parts/030-data/110-empires.sql b/legacyworlds-server-data/db-structure/parts/030-data/110-empires.sql index a92e197..f6015dc 100644 --- a/legacyworlds-server-data/db-structure/parts/030-data/110-empires.sql +++ b/legacyworlds-server-data/db-structure/parts/030-data/110-empires.sql @@ -135,11 +135,12 @@ CREATE TABLE emp.mining_settings( resource_name_id INT NOT NULL , /* Weight to give to this type of resource when there are no planet- - * specific settings. + * specific settings. The weight is a value between 0 (lowest priority) + * and 4 (highest priority) */ empmset_weight INT NOT NULL - DEFAULT 1 - CHECK( empmset_weight >= 0 ) , + DEFAULT 2 + CHECK( empmset_weight BETWEEN 0 AND 4 ) , /* Primary key on (empire,resource type) pairs */ PRIMARY KEY( empire_id , resource_name_id ) @@ -176,10 +177,12 @@ CREATE TABLE emp.planet_mining_settings( /* Identifier of the type of resources */ resource_name_id INT NOT NULL , - /* Weight to give to this type of resource */ + /* Weight to give to this type of resource. Works in a manner similar to + * the empmset_weight column of emp.mining_settings. + */ emppmset_weight INT NOT NULL - DEFAULT 1 - CHECK( emppmset_weight >= 0 ) , + DEFAULT 2 + CHECK( emppmset_weight BETWEEN 0 AND 4 ) , /* Primary key on (empire,resource type) pairs */ PRIMARY KEY( empire_id , planet_id , resource_name_id ) diff --git a/legacyworlds-server-data/db-structure/parts/040-functions/040-empire.sql b/legacyworlds-server-data/db-structure/parts/040-functions/040-empire.sql index 8c18e73..67204c1 100644 --- a/legacyworlds-server-data/db-structure/parts/040-functions/040-empire.sql +++ b/legacyworlds-server-data/db-structure/parts/040-functions/040-empire.sql @@ -7,43 +7,60 @@ -- -------------------------------------------------------- --- --- Empire creation --- --- Parameters: --- nid Empire name identifier --- pid Planet identifier --- icash Initial cash --- -CREATE OR REPLACE FUNCTION emp.create_empire( nid INT , pid INT , icash REAL ) +/* + * Empire creation + * + * This function inserts the rows that represent an empire and its settings. + * It also initialises the empire's updates. + * + * Parameters: + * _name_id Empire name identifier + * _planet_id Planet identifier + * _initial_cash Initial cash + */ +DROP FUNCTION IF EXISTS emp.create_empire( INT , INT , REAL ); +CREATE FUNCTION emp.create_empire( + _name_id INT , + _planet_id INT , + _initial_cash REAL ) RETURNS VOID - STRICT - VOLATILE + STRICT VOLATILE SECURITY INVOKER AS $$ DECLARE - uid BIGINT; - utp update_type; + _update BIGINT; + _update_type update_type; BEGIN -- Add empire and give initial planet INSERT INTO emp.empires ( name_id , cash ) - VALUES ( nid , icash ); + VALUES ( _name_id , _initial_cash ); INSERT INTO emp.planets ( planet_id , empire_id ) - VALUES ( pid , nid ); + VALUES ( _planet_id , _name_id ); + + -- Add mining settings + INSERT INTO emp.mining_settings ( empire_id , resource_name_id ) + SELECT _name_id , _resource.resource_name_id + FROM defs.natural_resources _resource; -- Add empire update records - FOR utp IN SELECT x FROM unnest( enum_range( NULL::update_type ) ) AS x - WHERE x::text LIKE 'EMPIRE_%' + FOR _update_type IN SELECT _type + FROM unnest( enum_range( NULL::update_type ) ) AS _type + WHERE _type::text LIKE 'EMPIRE_%' LOOP INSERT INTO sys.updates( gu_type ) - VALUES ( utp ) - RETURNING id INTO uid; + VALUES ( _update_type ) + RETURNING id INTO _update; INSERT INTO emp.updates ( update_id , empire_id ) - VALUES ( uid , nid ); + VALUES ( _update , _name_id ); END LOOP; END; $$ LANGUAGE plpgsql; +REVOKE EXECUTE + ON FUNCTION emp.create_empire( INT , INT , REAL ) + FROM PUBLIC; + + -- -- Returns a planet owner's empire size diff --git a/legacyworlds-server-data/db-structure/parts/040-functions/045-empire-mining.sql b/legacyworlds-server-data/db-structure/parts/040-functions/045-empire-mining.sql new file mode 100644 index 0000000..9c4545f --- /dev/null +++ b/legacyworlds-server-data/db-structure/parts/040-functions/045-empire-mining.sql @@ -0,0 +1,234 @@ +-- LegacyWorlds Beta 6 +-- PostgreSQL database scripts +-- +-- Functions that control and compute empire mining +-- +-- Copyright(C) 2004-2010, DeepClone Development +-- -------------------------------------------------------- + + +/* + * Prepare for an update on empire mining settings + * + * This function creates a temporary table mimicking the structure of + * emp.mining_settings, and stores default settings into it. + * + * Parameters: + * _empire_id The empire's identifier + * + * Returns: + * ? True if the empire exists, false otherwise + */ +DROP FUNCTION IF EXISTS emp.mset_update_start( INT ); +CREATE FUNCTION emp.mset_update_start( _empire_id INT ) + RETURNS BOOLEAN + STRICT VOLATILE + SECURITY DEFINER + AS $mset_update_start$ +BEGIN + + CREATE TEMPORARY TABLE mset_update( + empire_id INT , + resource_name_id INT , + empmset_weight INT + ) ON COMMIT DROP; + + INSERT INTO mset_update + SELECT _mset.empire_id , _mset.resource_name_id , 2 + FROM emp.empires _empire + INNER JOIN emp.mining_settings _mset + ON _empire.name_id = _mset.empire_id + WHERE _empire.name_id = _empire_id + FOR SHARE OF _empire + FOR UPDATE OF _mset; + + RETURN FOUND; + +END; +$mset_update_start$ LANGUAGE PLPGSQL; + +REVOKE EXECUTE + ON FUNCTION emp.mset_update_start( INT ) + FROM PUBLIC; +GRANT EXECUTE + ON FUNCTION emp.mset_update_start( INT ) + TO :dbuser; + + +/* + * Prepare for an update on planet-specific mining settings + * + * This function creates a temporary table mimicking the structure of + * emp.planet_mining_settings, and stores default settings into it, using the + * planet's list of resource provider as the source. + * + * Parameters: + * _empire_id The empire's identifier + * _planet_id The planet's identifier + * + * Returns: + * ? True if the empire exists and owns the planet, false + * otherwise + */ +DROP FUNCTION IF EXISTS emp.mset_update_start( INT , INT ); +CREATE FUNCTION emp.mset_update_start( _empire_id INT , _planet_id INT ) + RETURNS BOOLEAN + STRICT VOLATILE + SECURITY DEFINER + AS $mset_update_start$ +BEGIN + + CREATE TEMPORARY TABLE mset_update( + empire_id INT , + planet_id INT , + resource_name_id INT , + empmset_weight INT + ) ON COMMIT DROP; + + PERFORM 1 + FROM emp.empires _empire + INNER JOIN emp.planets _emp_planet + ON _empire.name_id = _emp_planet.empire_id + INNER JOIN verse.planets _planet + ON _planet.name_id = _emp_planet.planet_id + INNER JOIN verse.resource_providers _resprov + ON _resprov.planet_id = _planet.name_id + WHERE _empire.name_id = _empire_id + AND _planet.name_id = _planet_id + FOR SHARE OF _empire, _emp_planet , _planet , _resprov; + IF NOT FOUND THEN + RETURN FALSE; + END IF; + + PERFORM 1 + FROM emp.planet_mining_settings + WHERE empire_id = _empire_id AND planet_id = _planet_id + FOR UPDATE; + + INSERT INTO mset_update + SELECT _empire_id , _planet_id , resource_name_id , 2 + FROM verse.resource_providers + WHERE planet_id = _planet_id; + + RETURN TRUE; + +END; +$mset_update_start$ LANGUAGE PLPGSQL; + +REVOKE EXECUTE + ON FUNCTION emp.mset_update_start( INT , INT ) + FROM PUBLIC; +GRANT EXECUTE + ON FUNCTION emp.mset_update_start( INT , INT ) + TO :dbuser; + + + +/* + * Update the weight of some resource + * + * This function updates the weight of a resource in the temporary table. It + * must be called after emp.mset_update_start() has initialised the table. + * + * Parameters: + * _resource_id The resource's identifier + * _weight The setting's new value + * + * Returns: + * ? True if the resource exists, false otherwise. + */ +DROP FUNCTION IF EXISTS emp.mset_update_set( INT , INT ); +CREATE FUNCTION emp.mset_update_set( _resource_id INT , _weight INT ) + RETURNS BOOLEAN + STRICT VOLATILE + SECURITY DEFINER + AS $mset_update_set$ +BEGIN + + UPDATE mset_update + SET empmset_weight = _weight + WHERE resource_name_id = _resource_id; + RETURN FOUND; + +END; +$mset_update_set$ LANGUAGE PLPGSQL; + + +REVOKE EXECUTE + ON FUNCTION emp.mset_update_set( INT , INT ) + FROM PUBLIC; +GRANT EXECUTE + ON FUNCTION emp.mset_update_set( INT , INT ) + TO :dbuser; + + + +/* + * Apply a pending update to empire or planet mining settings + * + * This function is called once mining settings (whether empire-wise or + * planet-specific) have been uploaded. It will apply all changes to the + * actual mining settings table. + * + * Returns: + * ? True if the update was applied, false if one of the + * weights was invalid. + */ +DROP FUNCTION IF EXISTS emp.mset_update_apply( ); +CREATE FUNCTION emp.mset_update_apply( ) + RETURNS BOOLEAN + STRICT VOLATILE + SECURITY DEFINER + AS $mset_update_apply$ + +DECLARE + _empire INT; + _planet INT; + +BEGIN + BEGIN + + -- Get empire and planet identifier (will cause exception if this is + -- not a planet settings update). + SELECT INTO _empire , _planet + empire_id , planet_id + FROM mset_update + LIMIT 1; + + DELETE FROM emp.planet_mining_settings + WHERE empire_id = _empire AND planet_id = _planet; + INSERT INTO emp.planet_mining_settings ( + empire_id , planet_id , resource_name_id , + emppmset_weight + ) SELECT empire_id , planet_id , + resource_name_id , empmset_weight + FROM mset_update; + + EXCEPTION + + -- These are empire-wide settings + WHEN undefined_column THEN + UPDATE emp.mining_settings _settings + SET empmset_weight = _update.empmset_weight + FROM mset_update _update + WHERE _update.empire_id = _settings.empire_id + AND _update.resource_name_id = _settings.resource_name_id; + END; + RETURN TRUE; + +EXCEPTION + + -- One of the weights was invalid + WHEN check_violation THEN + RETURN FALSE; + +END; +$mset_update_apply$ LANGUAGE PLPGSQL; + + +REVOKE EXECUTE + ON FUNCTION emp.mset_update_apply( ) + FROM PUBLIC; +GRANT EXECUTE + ON FUNCTION emp.mset_update_apply( ) + TO :dbuser; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/030-data/110-empires/020-empire-mining-settings.sql b/legacyworlds-server-data/db-structure/tests/admin/030-data/110-empires/020-empire-mining-settings.sql index 8fb1158..5ddf3c4 100644 --- a/legacyworlds-server-data/db-structure/tests/admin/030-data/110-empires/020-empire-mining-settings.sql +++ b/legacyworlds-server-data/db-structure/tests/admin/030-data/110-empires/020-empire-mining-settings.sql @@ -14,10 +14,10 @@ BEGIN; SELECT id , 0 FROM naming.empire_names; /****** TESTS BEGIN HERE ******/ - SELECT plan( 10 ); + SELECT plan( 13 ); - SELECT diag_test_name( 'Valid empire mining settings record' ); + SELECT diag_test_name( 'emp.mining_settings - Valid record' ); PREPARE _test_this AS INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight @@ -25,12 +25,12 @@ BEGIN; _get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) , 0 ); SELECT lives_ok( '_test_this' ); - - SELECT diag_test_name( 'Duplicate empire mining settings record' ); + + SELECT diag_test_name( 'emp.mining_settings - Duplicate record' ); SELECT throws_ok( '_test_this' , 23505 ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire mining settings records with same empire but different types' ); + SELECT diag_test_name( 'emp.mining_settings - Same empire, different types' ); PREPARE _test_this AS INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight @@ -40,7 +40,7 @@ BEGIN; SELECT lives_ok( '_test_this' ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire mining settings records with same type but different empires' ); + SELECT diag_test_name( 'emp.mining_settings - Same type, different empires' ); PREPARE _test_this AS INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight @@ -51,8 +51,21 @@ BEGIN; DEALLOCATE ALL; DELETE FROM emp.mining_settings; + SELECT diag_test_name( 'emp.mining_settings - Valid record with default weight' ); + PREPARE _test_this AS + INSERT INTO emp.mining_settings( + empire_id , resource_name_id + ) VALUES ( + _get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) + ); + SELECT lives_ok( '_test_this' ); + DEALLOCATE ALL; - SELECT diag_test_name( 'Empire mining settings record with NULL empire identifier' ); + SELECT diag_test_name( 'emp.mining_settings - Default weight = 2' ); + SELECT is( empmset_weight , 2 ) FROM emp.mining_settings; + DELETE FROM emp.mining_settings; + + SELECT diag_test_name( 'emp.mining_settings - NULL empire identifier' ); PREPARE _test_this AS INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight @@ -62,7 +75,7 @@ BEGIN; SELECT throws_ok( '_test_this' , 23502 ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire mining settings record with invalid empire identifier' ); + SELECT diag_test_name( 'emp.mining_settings - Invalid empire identifier' ); PREPARE _test_this AS INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight @@ -73,7 +86,7 @@ BEGIN; DEALLOCATE ALL; - SELECT diag_test_name( 'Empire mining settings record with NULL resource identifier' ); + SELECT diag_test_name( 'emp.mining_settings - NULL resource identifier' ); PREPARE _test_this AS INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight @@ -83,7 +96,7 @@ BEGIN; SELECT throws_ok( '_test_this' , 23502 ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire mining settings record with invalid resource identifier' ); + SELECT diag_test_name( 'emp.mining_settings - Invalid resource identifier' ); PREPARE _test_this AS INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight @@ -94,7 +107,7 @@ BEGIN; DEALLOCATE ALL; - SELECT diag_test_name( 'Empire mining settings record with NULL weight' ); + SELECT diag_test_name( 'emp.mining_settings - NULL weight' ); PREPARE _test_this AS INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight @@ -104,7 +117,7 @@ BEGIN; SELECT throws_ok( '_test_this' , 23502 ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire mining settings record with weight < 0' ); + SELECT diag_test_name( 'emp.mining_settings - Weight < 0' ); PREPARE _test_this AS INSERT INTO emp.mining_settings( empire_id , resource_name_id , empmset_weight @@ -114,6 +127,16 @@ BEGIN; SELECT throws_ok( '_test_this' , 23514 ); DEALLOCATE ALL; + SELECT diag_test_name( 'emp.mining_settings - Weight > 4' ); + PREPARE _test_this AS + INSERT INTO emp.mining_settings( + empire_id , resource_name_id , empmset_weight + ) VALUES ( + _get_emp_name( 'testUser1' ) , _get_string( 'testResource1' ) , 5 + ); + SELECT throws_ok( '_test_this' , 23514 ); + DEALLOCATE ALL; + SELECT * FROM finish( ); ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/030-data/110-empires/030-empire-planet-mining-settings.sql b/legacyworlds-server-data/db-structure/tests/admin/030-data/110-empires/030-empire-planet-mining-settings.sql index ea1e154..15137d8 100644 --- a/legacyworlds-server-data/db-structure/tests/admin/030-data/110-empires/030-empire-planet-mining-settings.sql +++ b/legacyworlds-server-data/db-structure/tests/admin/030-data/110-empires/030-empire-planet-mining-settings.sql @@ -1,5 +1,5 @@ /* - * Test constraints and foreign keys on emp.mining_settings + * Test constraints and foreign keys on emp.planet_mining_settings */ BEGIN; @@ -20,10 +20,10 @@ BEGIN; -- No provider for testResource2 on testPlanet2 /****** TESTS BEGIN HERE ******/ - SELECT plan( 14 ); + SELECT plan( 17 ); - SELECT diag_test_name( 'Valid empire planet mining settings record' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Valid record' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -34,11 +34,11 @@ BEGIN; ); SELECT lives_ok( '_test_this' ); - SELECT diag_test_name( 'Duplicate empire planet mining settings record' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Duplicate record' ); SELECT throws_ok( '_test_this' , 23505 ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining settings records with different types' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Different types' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -50,7 +50,7 @@ BEGIN; SELECT lives_ok( '_test_this' ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining settings records with different empires' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Different empires' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -62,7 +62,7 @@ BEGIN; SELECT lives_ok( '_test_this' ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining settings records with different planets' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Different planets' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -73,10 +73,25 @@ BEGIN; ); SELECT lives_ok( '_test_this' ); DEALLOCATE ALL; - DELETE FROM emp.mining_settings; + DELETE FROM emp.planet_mining_settings; + + SELECT diag_test_name( 'emp.planet_mining_settings - Valid record with default weight' ); + PREPARE _test_this AS + INSERT INTO emp.planet_mining_settings( + empire_id , planet_id , resource_name_id + ) VALUES ( + _get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet1' ) , + _get_string( 'testResource1' ) + ); + SELECT lives_ok( '_test_this' ); + DEALLOCATE ALL; + + SELECT diag_test_name( 'emp.planet_mining_settings - Default weight = 2' ); + SELECT is( emppmset_weight , 2 ) FROM emp.planet_mining_settings; + DELETE FROM emp.planet_mining_settings; - SELECT diag_test_name( 'Empire planet mining setting record with NULL empire identifier' ); + SELECT diag_test_name( 'emp.planet_mining_settings - NULL empire identifier' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -88,7 +103,7 @@ BEGIN; SELECT throws_ok( '_test_this' , 23502 ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining setting record with invalid empire identifier' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Invalid empire identifier' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -101,7 +116,7 @@ BEGIN; DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining setting record with NULL planet identifier' ); + SELECT diag_test_name( 'emp.planet_mining_settings - NULL planet identifier' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -113,7 +128,7 @@ BEGIN; SELECT throws_ok( '_test_this' , 23502 ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining setting record with invalid planet identifier' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Invalid planet identifier' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -126,7 +141,7 @@ BEGIN; DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining setting record with NULL resource identifier' ); + SELECT diag_test_name( 'emp.planet_mining_settings - NULL resource identifier' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -138,7 +153,7 @@ BEGIN; SELECT throws_ok( '_test_this' , 23502 ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining setting record with invalid resource identifier' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Invalid resource identifier' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -151,7 +166,7 @@ BEGIN; DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining setting record with invalid resource provider identifier' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Invalid resource provider identifier' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -164,7 +179,7 @@ BEGIN; DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining setting record with NULL weight' ); + SELECT diag_test_name( 'emp.planet_mining_settings - NULL weight' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -176,7 +191,7 @@ BEGIN; SELECT throws_ok( '_test_this' , 23502 ); DEALLOCATE ALL; - SELECT diag_test_name( 'Empire planet mining setting record with weight < 0' ); + SELECT diag_test_name( 'emp.planet_mining_settings - Weight < 0' ); PREPARE _test_this AS INSERT INTO emp.planet_mining_settings( empire_id , planet_id , @@ -188,6 +203,18 @@ BEGIN; SELECT throws_ok( '_test_this' , 23514 ); DEALLOCATE ALL; + SELECT diag_test_name( 'emp.planet_mining_settings - Weight > 4' ); + PREPARE _test_this AS + INSERT INTO emp.planet_mining_settings( + empire_id , planet_id , + resource_name_id , emppmset_weight + ) VALUES ( + _get_emp_name( 'testUser1' ) , _get_map_name( 'testPlanet1' ) , + _get_string( 'testResource1' ) , 5 + ); + SELECT throws_ok( '_test_this' , 23514 ); + DEALLOCATE ALL; + SELECT * FROM finish( ); ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/040-empire/010-create-empire.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/040-empire/010-create-empire.sql new file mode 100644 index 0000000..55f1b48 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/040-empire/010-create-empire.sql @@ -0,0 +1,68 @@ +/* + * Test the emp.create_empire() function + */ +BEGIN; + /* We need a pair of natural resources, a basic resource, a planet + * and an empire name. + */ + \i utils/strings.sql + \i utils/resources.sql + \i utils/accounts.sql + \i utils/naming.sql + \i utils/universe.sql + SELECT _create_natural_resources( 2 , 'natRes' ); + SELECT _create_resources( 1 , 'basicRes' ); + SELECT _create_raw_planets( 1 , 'testPlanet' ); + SELECT _create_emp_names( 1 , 'testEmp' ); + + /***** TESTS BEGIN HERE *****/ + SELECT plan( 7 ); + + SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , + _get_map_name( 'testPlanet1' ) , + 200.0 ); + + SELECT diag_test_name( 'emp.create_empire() - Empire exists' ); + SELECT is( COUNT(*)::INT , 1 ) FROM emp.empires + WHERE name_id = _get_emp_name( 'testEmp1' ); + SELECT diag_test_name( 'emp.create_empire() - Empire cash' ); + SELECT is( cash , 200.0::REAL ) FROM emp.empires + WHERE name_id = _get_emp_name( 'testEmp1' ); + SELECT diag_test_name( 'emp.create_empire() - Empire debt' ); + SELECT is( debt , 0.0::REAL ) FROM emp.empires + WHERE name_id = _get_emp_name( 'testEmp1' ); + + SELECT diag_test_name( 'emp.create_empire() - Empire mining settings include all natural resources' ); + SELECT is( COUNT(*)::INT , 2 ) + FROM defs.natural_resources + INNER JOIN emp.mining_settings + USING ( resource_name_id ) + WHERE empire_id = _get_emp_name( 'testEmp1' ); + + SELECT diag_test_name( 'emp.create_empire() - Empire mining settings do not include basic resources' ); + SELECT is( COUNT(*)::INT , 2 ) + FROM defs.resources + INNER JOIN emp.mining_settings + USING ( resource_name_id ) + WHERE empire_id = _get_emp_name( 'testEmp1' ); + + SELECT diag_test_name( 'emp.create_empire() - Empire mining settings are all set to the same value' ); + SELECT is( COUNT( _temp.* )::INT , 1 ) + FROM ( SELECT DISTINCT empmset_weight + FROM emp.mining_settings + WHERE empire_id = _get_emp_name( 'testEmp1' ) + ) AS _temp; + + SELECT diag_test_name( 'emp.create_empire() - Empire update records' ); + SELECT is( _eur.quantity , _utv.quantity) + FROM ( + SELECT COUNT(*) AS quantity FROM emp.updates + WHERE empire_id = _get_emp_name( 'testEmp1' ) + ) AS _eur , ( + SELECT COUNT(*) AS quantity + FROM unnest( enum_range( NULL::update_type ) ) AS _row + WHERE _row::TEXT LIKE 'EMPIRE_%' + ) AS _utv; + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/010-mset-update-start.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/010-mset-update-start.sql new file mode 100644 index 0000000..b73c7bc --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/010-mset-update-start.sql @@ -0,0 +1,43 @@ +/* + * Test the emp.mset_update_start( INT ) function + */ +BEGIN; + /* We need a pair of natural resources and an empire with mining settings. */ + \i utils/strings.sql + \i utils/resources.sql + \i utils/accounts.sql + \i utils/naming.sql + \i utils/universe.sql + SELECT _create_natural_resources( 2 , 'natRes' ); + SELECT _create_resources( 1 , 'basicRes' ); + SELECT _create_raw_planets( 1 , 'testPlanet' ); + SELECT _create_emp_names( 1 , 'testEmp' ); + SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , + _get_map_name( 'testPlanet1' ) , + 200.0 ); + + /***** TESTS BEGIN HERE *****/ + SELECT plan( 6 ); + + SELECT diag_test_name( 'emp.mset_update_start( INT ) - Return value on bad empire identifier' ); + SELECT ok( NOT emp.mset_update_start( _get_bad_emp_name( ) ) ); + SELECT diag_test_name( 'emp.mset_update_start( INT ) - Temporary table exists despite bad empire identifier' ); + SELECT has_table( 'mset_update' ); + DROP TABLE mset_update; + + + SELECT diag_test_name( 'emp.mset_update_start( INT ) - Return value on valid empire identifier' ); + SELECT ok( emp.mset_update_start( _get_emp_name( 'testEmp1' ) ) ); + SELECT diag_test_name( 'emp.mset_update_start( INT ) - Temporary table exists' ); + SELECT has_table( 'mset_update' ); + SELECT diag_test_name( 'emp.mset_update_start( INT ) - Temporary table contains all required entries' ); + SELECT is( COUNT(*)::INT , 2 ) + FROM mset_update + WHERE empire_id = _get_emp_name( 'testEmp1' ); + SELECT diag_test_name( 'emp.mset_update_start( INT ) - Temporary table does not contain extra entries' ); + SELECT is( COUNT(*)::INT , 0 ) + FROM mset_update + WHERE empire_id <> _get_emp_name( 'testEmp1' ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/015-mset-update-start-planet.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/015-mset-update-start-planet.sql new file mode 100644 index 0000000..816508a --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/015-mset-update-start-planet.sql @@ -0,0 +1,74 @@ +/* + * Test the emp.mset_update_start( INT , INT ) function + */ +BEGIN; + /* We need a pair of natural resources, an empire, a pair of planets with + * resource providers. The empire must own one of the planets. + */ + \i utils/strings.sql + \i utils/resources.sql + \i utils/accounts.sql + \i utils/naming.sql + \i utils/universe.sql + SELECT _create_natural_resources( 2 , 'testResource' ); + SELECT _create_raw_planets( 3 , 'testPlanet' ); + SELECT _create_emp_names( 1 , 'testEmp' ); + SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , + _get_map_name( 'testPlanet1' ) , + 200.0 ); + SELECT _create_resource_provider( 'testPlanet1' , 'testResource1' ); + SELECT _create_resource_provider( 'testPlanet2' , 'testResource1' ); + INSERT INTO emp.planets ( empire_id , planet_id ) + VALUES ( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet3' ) ); + + /***** TESTS BEGIN HERE *****/ + SELECT plan( 14 ); + + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Return value on bad empire identifier' ); + SELECT ok( NOT emp.mset_update_start( _get_bad_emp_name( ) , _get_map_name( 'testPlanet1' ) ) ); + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Temporary table exists despite bad empire identifier' ); + SELECT has_table( 'mset_update' ); + DROP TABLE mset_update; + + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Return value on bad planet identifier' ); + SELECT ok( NOT emp.mset_update_start( _get_emp_name( 'testEmp1' ) , _get_bad_map_name( ) ) ); + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Temporary table exists despite bad planet identifier' ); + SELECT has_table( 'mset_update' ); + DROP TABLE mset_update; + + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Return value on unowned planet identifier' ); + SELECT ok( NOT emp.mset_update_start( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet2' ) ) ); + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Temporary table exists despite unowned planet identifier' ); + SELECT has_table( 'mset_update' ); + DROP TABLE mset_update; + + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Return value on unowned planet identifier' ); + SELECT ok( NOT emp.mset_update_start( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet2' ) ) ); + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Temporary table exists despite unowned planet identifier' ); + SELECT has_table( 'mset_update' ); + DROP TABLE mset_update; + + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Return value on planet with no resource providers' ); + SELECT ok( NOT emp.mset_update_start( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet3' ) ) ); + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Temporary table exists despite planet with no resource providers' ); + SELECT has_table( 'mset_update' ); + DROP TABLE mset_update; + + + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Return value on valid identifiers' ); + SELECT ok( emp.mset_update_start( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet1' ) ) ); + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Temporary table exists' ); + SELECT has_table( 'mset_update' ); + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Temporary table contains all required entries' ); + SELECT is( COUNT(*)::INT , 1 ) + FROM mset_update + WHERE empire_id = _get_emp_name( 'testEmp1' ) + AND planet_id = _get_map_name( 'testPlanet1' ); + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Temporary table does not contain extra entries' ); + SELECT is( COUNT(*)::INT , 0 ) + FROM mset_update + WHERE empire_id <> _get_emp_name( 'testEmp1' ) + OR planet_id <> _get_map_name( 'testPlanet1' ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/020-mset-update-set.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/020-mset-update-set.sql new file mode 100644 index 0000000..7515bfb --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/020-mset-update-set.sql @@ -0,0 +1,37 @@ +/* + * Test the emp.mset_update_set() function + */ +BEGIN; + CREATE TEMPORARY TABLE mset_update( + empire_id INT , + resource_name_id INT , + empmset_weight INT + ) ON COMMIT DROP; + INSERT INTO mset_update VALUES ( 1 , 1 , 0 ) , ( 1 , 2 , 0 ); + + /***** TESTS BEGIN HERE *****/ + SELECT plan( 7 ); + + SELECT diag_test_name( 'emp.mset_update_set( ) - Valid update' ); + SELECT ok( emp.mset_update_set( 1 , 1 ) ); + SELECT diag_test_name( 'emp.mset_update_set( ) - Valid update results (1/2)' ); + SELECT is( empmset_weight , 1 ) FROM mset_update WHERE resource_name_id = 1; + SELECT diag_test_name( 'emp.mset_update_set( ) - Valid update results (2/2)' ); + SELECT is( empmset_weight , 0 ) FROM mset_update WHERE resource_name_id = 2; + DELETE FROM mset_update; + + INSERT INTO mset_update VALUES ( 1 , 1 , 0 ) , ( 1 , 2 , 0 ); + SELECT diag_test_name( 'emp.mset_update_set( ) - Update on unknown resource' ); + SELECT ok( NOT emp.mset_update_set( 12 , 1 ) ); + SELECT diag_test_name( 'emp.mset_update_set( ) - Unknown resource update results (1/2)' ); + SELECT is( empmset_weight , 0 ) FROM mset_update WHERE resource_name_id = 1; + SELECT diag_test_name( 'emp.mset_update_set( ) - Unknown resource update results (2/2)' ); + SELECT is( empmset_weight , 0 ) FROM mset_update WHERE resource_name_id = 2; + DELETE FROM mset_update; + + INSERT INTO mset_update VALUES ( 1 , 1 , 0 ) , ( 1 , 2 , 0 ); + SELECT diag_test_name( 'emp.mset_update_set( ) - Update with invalid weight' ); + SELECT ok( emp.mset_update_set( 1 , -1 ) ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/030-mset-update-apply.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/030-mset-update-apply.sql new file mode 100644 index 0000000..74d49b1 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-mining/030-mset-update-apply.sql @@ -0,0 +1,93 @@ +/* + * Test the emp.mset_update_apply() function + */ +BEGIN; + /* + * Remove foreign keys from the empire mining settings table, insert some + * data into it. + */ + ALTER TABLE emp.mining_settings + DROP CONSTRAINT fk_empmset_empire , + DROP CONSTRAINT fk_empmset_resource; + INSERT INTO emp.mining_settings ( empire_id , resource_name_id ) + VALUES ( 1 , 1 ) , ( 1 , 2 ); + + /* Create the temporary table */ + CREATE TEMPORARY TABLE mset_update( + empire_id INT , + resource_name_id INT , + empmset_weight INT + ) ON COMMIT DROP; + INSERT INTO mset_update VALUES ( 1 , 1 , 0 ) , ( 1 , 2 , 4 ); + + /***** TESTS BEGIN HERE *****/ + SELECT no_plan( ); + + SELECT diag_test_name( 'emp.mset_update_apply() - Applying valid empire update' ); + SELECT ok( emp.mset_update_apply( ) ); + SELECT diag_test_name( 'emp.mset_update_apply() - Empire update results' ); + SELECT set_eq( + $$ SELECT resource_name_id , empmset_weight FROM emp.mining_settings $$ , + $$ VALUES ( 1 , 0 ) , ( 2 , 4 ) $$ + ); + + /* Reset temporary table and settings */ + DELETE FROM mset_update; + DELETE FROM emp.mining_settings; + INSERT INTO emp.mining_settings ( empire_id , resource_name_id ) + VALUES ( 1 , 1 ) , ( 1 , 2 ); + + INSERT INTO mset_update VALUES ( 1 , 1 , -1 ) , ( 1 , 2 , 4 ); + SELECT diag_test_name( 'emp.mset_update_apply() - Applying invalid empire update' ); + SELECT ok( NOT emp.mset_update_apply( ) ); + SELECT diag_test_name( 'emp.mset_update_apply() - Invalid empire update results' ); + SELECT set_eq( + $$ SELECT resource_name_id , empmset_weight FROM emp.mining_settings $$ , + $$ VALUES ( 1 , 2 ) , ( 2 , 2 ) $$ + ); + + + /* Re-create the temporary table to store planet mining settings, remove + * constraints from the planet mining settings table, insert data into + * both. + */ + ALTER TABLE emp.planet_mining_settings + DROP CONSTRAINT fk_emppmset_empire , + DROP CONSTRAINT fk_emppmset_resource; + INSERT INTO emp.planet_mining_settings ( empire_id , planet_id , resource_name_id ) + VALUES ( 1 , 1 , 1 ) , ( 1 , 1 , 2 ); + + DROP TABLE mset_update; + CREATE TEMPORARY TABLE mset_update( + empire_id INT , + planet_id INT , + resource_name_id INT , + empmset_weight INT + ) ON COMMIT DROP; + INSERT INTO mset_update VALUES ( 1 , 1 , 1 , 0 ) , ( 1 , 1 , 2 , 4 ); + + SELECT diag_test_name( 'emp.mset_update_apply() - Applying valid planet update' ); + SELECT ok( emp.mset_update_apply( ) ); + SELECT diag_test_name( 'emp.mset_update_apply() - Planet update results' ); + SELECT set_eq( + $$ SELECT resource_name_id , emppmset_weight FROM emp.planet_mining_settings $$ , + $$ VALUES ( 1 , 0 ) , ( 2 , 4 ) $$ + ); + + /* Reset temporary table and settings */ + DELETE FROM mset_update; + DELETE FROM emp.planet_mining_settings; + INSERT INTO emp.planet_mining_settings ( empire_id , planet_id , resource_name_id ) + VALUES ( 1 , 1 , 1 ) , ( 1 , 1 , 2 ); + + INSERT INTO mset_update VALUES ( 1 , 1 , 1 , -1 ) , ( 1 , 1 , 2 , 4 ); + SELECT diag_test_name( 'emp.mset_update_apply() - Applying invalid planet update' ); + SELECT ok( NOT emp.mset_update_apply( ) ); + SELECT diag_test_name( 'emp.mset_update_apply() - Invalid planet update results' ); + SELECT set_eq( + $$ SELECT resource_name_id , emppmset_weight FROM emp.planet_mining_settings $$ , + $$ VALUES ( 1 , 2 ) , ( 2 , 2 ) $$ + ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/040-empire/010-create-empire.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/040-empire/010-create-empire.sql new file mode 100644 index 0000000..11e01b6 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/user/040-functions/040-empire/010-create-empire.sql @@ -0,0 +1,14 @@ +/* + * Test privileges on emp.create_empire() + */ +BEGIN; + + SELECT plan( 1 ); + + SELECT diag_test_name( 'emp.create_empire() - Privileges' ); + SELECT throws_ok( $$ + SELECT emp.create_empire( 1 , 1 , 200.0 ) + $$ , 42501 ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/010-mset-update-start.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/010-mset-update-start.sql new file mode 100644 index 0000000..598c26b --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/010-mset-update-start.sql @@ -0,0 +1,14 @@ +/* + * Test privileges on emp.mset_update_start( INT ) + */ +BEGIN; + + SELECT plan( 1 ); + + SELECT diag_test_name( 'emp.mset_update_start( INT ) - Privileges' ); + SELECT lives_ok( $$ + SELECT emp.mset_update_start( 1 ) + $$ ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/015-mset-update-start-planet.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/015-mset-update-start-planet.sql new file mode 100644 index 0000000..cf0e1ec --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/015-mset-update-start-planet.sql @@ -0,0 +1,14 @@ +/* + * Test privileges on emp.mset_update_start( INT , INT ) + */ +BEGIN; + + SELECT plan( 1 ); + + SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Privileges' ); + SELECT lives_ok( $$ + SELECT emp.mset_update_start( 1 , 1 ) + $$ ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/020-mset-update-set.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/020-mset-update-set.sql new file mode 100644 index 0000000..cc37514 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/020-mset-update-set.sql @@ -0,0 +1,14 @@ +/* + * Test privileges on emp.mset_update_set() + */ +BEGIN; + SELECT emp.mset_update_start( 1 ); + SELECT plan( 1 ); + + SELECT diag_test_name( 'emp.mset_update_set() - Privileges' ); + SELECT lives_ok( $$ + SELECT emp.mset_update_set( 1 , -1 ) + $$ ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/030-mset-update-apply.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/030-mset-update-apply.sql new file mode 100644 index 0000000..6f349fb --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-mining/030-mset-update-apply.sql @@ -0,0 +1,15 @@ +/* + * Test privileges on emp.mset_update_set() + */ +BEGIN; + SELECT emp.mset_update_start( 1 ); + + SELECT plan( 1 ); + + SELECT diag_test_name( 'emp.mset_update_apply() - Privileges' ); + SELECT lives_ok( $$ + SELECT emp.mset_update_apply( ) + $$ ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file