diff --git a/legacyworlds-server-data/db-structure/parts/040-functions/145-resource-providers.sql b/legacyworlds-server-data/db-structure/parts/040-functions/145-resource-providers.sql index b79d4fd..aec2a56 100644 --- a/legacyworlds-server-data/db-structure/parts/040-functions/145-resource-providers.sql +++ b/legacyworlds-server-data/db-structure/parts/040-functions/145-resource-providers.sql @@ -59,3 +59,43 @@ REVOKE EXECUTE DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION ) FROM PUBLIC; + + + +/* + * Mining settings view + * --------------------- + * + * This view lists mining settings being used on planets owned by empires + * for each resource providers. The settings are taken from planet-specific + * settings if they are available, or from empire-wide settings. + * + * Columns: + * + * planet_id The planet's identifier + * resource_name_id The type of resources + * mset_weight The setting to use for mining priorities + * mset_specific True if the settings are specific for this planet, + * false if empire-wise settings are in use. + */ +DROP VIEW IF EXISTS emp.mining_settings_view CASCADE; +CREATE VIEW emp.mining_settings_view + AS SELECT planet_id , resource_name_id , + ( CASE + WHEN _pl_settings.planet_id IS NULL THEN + _emp_settings.empmset_weight + ELSE + _pl_settings.emppmset_weight + END ) AS mset_weight , + ( _pl_settings.planet_id IS NOT NULL ) AS mset_specific + FROM verse.resource_providers + INNER JOIN emp.planets + USING ( planet_id ) + INNER JOIN emp.mining_settings _emp_settings + USING ( empire_id , resource_name_id ) + LEFT OUTER JOIN emp.planet_mining_settings _pl_settings + USING ( planet_id , empire_id , resource_name_id ); + +GRANT SELECT + ON emp.mining_settings_view + TO :dbuser; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/145-resource-providers/020-mining-settings-view.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/145-resource-providers/020-mining-settings-view.sql new file mode 100644 index 0000000..0c60575 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/145-resource-providers/020-mining-settings-view.sql @@ -0,0 +1,107 @@ +/* + * Test the emp.mining_settings_view view + */ +BEGIN; + /* Create 1 natural resource, 3 planets and 2 empire names */ + \i utils/strings.sql + \i utils/resources.sql + \i utils/accounts.sql + \i utils/naming.sql + \i utils/universe.sql + SELECT _create_natural_resources( 1 , 'testResource' ); + SELECT _create_raw_planets( 3 , 'testPlanet' ); + SELECT _create_emp_names( 2 , 'testEmp' ); + + /* + * Create an empire which possesses a planet, but has no planet-specific + * mining settings. Modify the default empire-wide settings so we can + * check the results. + * + * The planet only has a resource provider for one type of resource. + */ + SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet1' ) , 1 ); + UPDATE emp.mining_settings + SET empmset_weight = 3 + WHERE empire_id = _get_emp_name( 'testEmp1' ) + AND resource_name_id = _get_string( 'testResource1' ); + INSERT INTO verse.resource_providers ( planet_id , resource_name_id , + resprov_quantity_max , resprov_quantity , + resprov_difficulty , resprov_recovery ) + VALUES ( + _get_map_name( 'testPlanet1' ) , _get_string( 'testResource1' ) , + 100 , 50 , + 0.5 , 0.5 + ); + + /* + * Add a resource provider to the second planet; create an empire which + * owns the planet and has planet-specific settings for the resource + * provider. + * + * This empire also has settings for the testPlanet1 planet. + */ + INSERT INTO verse.resource_providers ( planet_id , resource_name_id , + resprov_quantity_max , resprov_quantity , + resprov_difficulty , resprov_recovery ) + VALUES ( + _get_map_name( 'testPlanet2' ) , _get_string( 'testResource1' ) , + 100 , 50 , + 0.5 , 0.5 + ); + SELECT emp.create_empire( _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) , 1 ); + INSERT INTO emp.planet_mining_settings( + empire_id , planet_id , resource_name_id , emppmset_weight + ) VALUES ( + _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet2' ) , + _get_string( 'testResource1' ) , 0 + ) , ( + _get_emp_name( 'testEmp2' ) , _get_map_name( 'testPlanet1' ) , + _get_string( 'testResource1' ) , 0 + ); + + /* + * While the last planet is not owned by any empire, make sure it has a + * resource provider. + */ + INSERT INTO verse.resource_providers ( planet_id , resource_name_id , + resprov_quantity_max , resprov_quantity , + resprov_difficulty , resprov_recovery ) + VALUES ( + _get_map_name( 'testPlanet3' ) , _get_string( 'testResource1' ) , + 100 , 50 , + 0.5 , 0.5 + ); + + + /***** TESTS BEGIN HERE *****/ + SELECT plan( 6 ); + + SELECT diag_test_name( 'emp.mining_settings_view - Ignore neutral planets' ); + SELECT is( COUNT(*)::INT , 0 ) + FROM emp.mining_settings_view + WHERE planet_id = _get_map_name( 'testPlanet3' ); + + SELECT diag_test_name( 'emp.mining_settings_view - Only display provided resources' ); + SELECT is( COUNT(*)::INT , 1 ) + FROM emp.mining_settings_view + WHERE planet_id = _get_map_name( 'testPlanet1' ); + SELECT diag_test_name( 'emp.mining_settings_view - Settings from empire-wide table' ); + SELECT ok( NOT mset_specific ) + FROM emp.mining_settings_view + WHERE planet_id = _get_map_name( 'testPlanet1' ); + SELECT diag_test_name( 'emp.mining_settings_view - Value from empire-wide table' ); + SELECT is( mset_weight , 3 ) + FROM emp.mining_settings_view + WHERE planet_id = _get_map_name( 'testPlanet1' ); + + SELECT diag_test_name( 'emp.mining_settings_view - Settings from planet-specific table' ); + SELECT ok( mset_specific ) + FROM emp.mining_settings_view + WHERE planet_id = _get_map_name( 'testPlanet2' ); + SELECT diag_test_name( 'emp.mining_settings_view - Value from planet-specific table' ); + SELECT is( mset_weight , 0 ) + FROM emp.mining_settings_view + WHERE planet_id = _get_map_name( 'testPlanet2' ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/145-resource-providers/020-mining-settings-view.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/145-resource-providers/020-mining-settings-view.sql new file mode 100644 index 0000000..6540213 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/user/040-functions/145-resource-providers/020-mining-settings-view.sql @@ -0,0 +1,11 @@ +/* + * Test privileges on emp.mining_settings_view + */ +BEGIN; + SELECT plan( 1 ); + + SELECT diag_test_name( 'emp.mining_settings_view - Privileges' ); + SELECT lives_ok( 'SELECT * FROM emp.mining_settings_view' ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file