Planet mining settings interface
* Modified owned planet view to include a field which indicates whether mining settings are specific to the planet or come from the empire; modified "simple" game components accordingly * Modified stored procedures to only allow planet-specific mining settings updates when the planet actually uses planet-specific settings, and added a stored procedure which toggles the source of a planet's settings * Added corresponding parts to mining settings DAO and resources controller * Added session commands and server command handlers that toggle the source of the settings and that upload the settings * Split planet page template into multiple files for clarity and added the mining priorities form to the natural resources tab
This commit is contained in:
parent
51b529a09f
commit
e64f847ec3
25 changed files with 1009 additions and 152 deletions
legacyworlds-server-data/db-structure
parts/040-functions
tests
admin/040-functions/045-empire-mining
user/040-functions/045-empire-mining
|
@ -69,8 +69,9 @@ GRANT EXECUTE
|
|||
* _planet_id The planet's identifier
|
||||
*
|
||||
* Returns:
|
||||
* ? True if the empire exists and owns the planet, false
|
||||
* otherwise
|
||||
* ? True if the empire exists and owns the planet and if
|
||||
* the planet is using planet-specific settings,
|
||||
* false otherwise
|
||||
*/
|
||||
DROP FUNCTION IF EXISTS emp.mset_update_start( INT , INT );
|
||||
CREATE FUNCTION emp.mset_update_start( _empire_id INT , _planet_id INT )
|
||||
|
@ -95,9 +96,14 @@ BEGIN
|
|||
ON _planet.name_id = _emp_planet.planet_id
|
||||
INNER JOIN verse.resource_providers _resprov
|
||||
ON _resprov.planet_id = _planet.name_id
|
||||
INNER JOIN emp.planet_mining_settings _mset
|
||||
ON _mset.planet_id = _planet.name_id
|
||||
AND _mset.empire_id = _empire.name_id
|
||||
AND _mset.resource_name_id = _resprov.resource_name_id
|
||||
WHERE _empire.name_id = _empire_id
|
||||
AND _planet.name_id = _planet_id
|
||||
FOR SHARE OF _empire, _emp_planet , _planet , _resprov;
|
||||
FOR SHARE OF _empire, _emp_planet , _planet , _resprov
|
||||
FOR UPDATE OF _mset;
|
||||
IF NOT FOUND THEN
|
||||
RETURN FALSE;
|
||||
END IF;
|
||||
|
@ -239,4 +245,71 @@ REVOKE EXECUTE
|
|||
FROM PUBLIC;
|
||||
GRANT EXECUTE
|
||||
ON FUNCTION emp.mset_update_apply( )
|
||||
TO :dbuser;
|
||||
|
||||
|
||||
/*
|
||||
* Toggle the source of a planet's mining settings
|
||||
* ------------------------------------------------
|
||||
*
|
||||
* This function causes a planet to be switched between empire-wide and
|
||||
* planet-specific mining settings.
|
||||
*
|
||||
* Parameters:
|
||||
* _empire The empire's identifier
|
||||
* _planet The planet's identifier
|
||||
*
|
||||
* Returns:
|
||||
* ? TRUE if the operation succeeded, FALSE if the planet
|
||||
* didn't exist or wasn't owned by the specified empire.
|
||||
*/
|
||||
DROP FUNCTION IF EXISTS emp.mset_toggle_source( INT , INT );
|
||||
CREATE FUNCTION emp.mset_toggle_source( _empire INT , _planet INT )
|
||||
RETURNS BOOLEAN
|
||||
LANGUAGE PLPGSQL
|
||||
STRICT VOLATILE
|
||||
SECURITY DEFINER
|
||||
AS $mset_toggle_source$
|
||||
BEGIN
|
||||
PERFORM 1
|
||||
FROM emp.planets _ep
|
||||
INNER JOIN verse.resource_providers _rp
|
||||
USING ( planet_id )
|
||||
LEFT OUTER JOIN (
|
||||
SELECT * FROM emp.planet_mining_settings _pms
|
||||
WHERE planet_id = _planet
|
||||
AND empire_id = _empire
|
||||
FOR UPDATE
|
||||
) _pms USING ( planet_id , empire_id , resource_name_id )
|
||||
WHERE _ep.empire_id = _empire
|
||||
AND _ep.planet_id = _planet
|
||||
FOR UPDATE OF _ep;
|
||||
IF NOT FOUND THEN
|
||||
RETURN FALSE;
|
||||
END IF;
|
||||
|
||||
PERFORM 1 FROM emp.planet_mining_settings _pms
|
||||
WHERE planet_id = _planet AND empire_id = _empire;
|
||||
IF FOUND THEN
|
||||
DELETE FROM emp.planet_mining_settings
|
||||
WHERE planet_id = _planet AND empire_id = _empire;
|
||||
ELSE
|
||||
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 verse.resource_providers _rp
|
||||
INNER JOIN emp.mining_settings
|
||||
USING ( resource_name_id )
|
||||
WHERE planet_id = _planet AND empire_id = _empire;
|
||||
END IF;
|
||||
|
||||
RETURN TRUE;
|
||||
END;
|
||||
$mset_toggle_source$;
|
||||
|
||||
REVOKE EXECUTE
|
||||
ON FUNCTION emp.mset_toggle_source( INT , INT )
|
||||
FROM PUBLIC;
|
||||
GRANT EXECUTE
|
||||
ON FUNCTION emp.mset_toggle_source( INT , INT )
|
||||
TO :dbuser;
|
|
@ -36,14 +36,16 @@ CREATE TYPE planet_orbital_data AS (
|
|||
|
||||
|
||||
-- Planet owner view
|
||||
CREATE TYPE planet_owner_data AS (
|
||||
happiness INT ,
|
||||
h_change INT ,
|
||||
income BIGINT ,
|
||||
upkeep BIGINT ,
|
||||
can_rename BOOLEAN ,
|
||||
can_abandon BOOLEAN ,
|
||||
abandon_time INT
|
||||
DROP TYPE IF EXISTS emp.planet_owner_data;
|
||||
CREATE TYPE emp.planet_owner_data AS (
|
||||
happiness INT ,
|
||||
h_change INT ,
|
||||
income BIGINT ,
|
||||
upkeep BIGINT ,
|
||||
specific_mining_settings BOOLEAN ,
|
||||
can_rename BOOLEAN ,
|
||||
can_abandon BOOLEAN ,
|
||||
abandon_time INT
|
||||
);
|
||||
|
||||
|
||||
|
@ -243,26 +245,33 @@ GRANT EXECUTE ON FUNCTION verse.get_orbital_view( INT , INT ) TO :dbuser;
|
|||
-- an owner planet view entry
|
||||
--
|
||||
|
||||
DROP FUNCTION IF EXISTS verse.get_owner_view( INT , INT ) CASCADE;
|
||||
CREATE OR REPLACE FUNCTION verse.get_owner_view( e_id INT , p_id INT )
|
||||
RETURNS planet_owner_data
|
||||
RETURNS emp.planet_owner_data
|
||||
STRICT STABLE
|
||||
SECURITY DEFINER
|
||||
AS $$
|
||||
DECLARE
|
||||
rv planet_owner_data;
|
||||
rv emp.planet_owner_data;
|
||||
t_happ INT;
|
||||
h_chg INT;
|
||||
mdelay BIGINT;
|
||||
r_time INTERVAL;
|
||||
BEGIN
|
||||
-- Get income, upkeep, current and target happiness
|
||||
SELECT INTO rv.income , rv.upkeep , rv.happiness , t_happ
|
||||
SELECT INTO rv.income , rv.upkeep , rv.happiness , t_happ , rv.specific_mining_settings
|
||||
floor( pm.income )::INT , floor( pm.upkeep )::INT ,
|
||||
floor( 100 * ph.current / p.population )::INT ,
|
||||
floor( 100 * ph.target )::INT
|
||||
floor( 100 * ph.target )::INT ,
|
||||
_count.settings_exist
|
||||
FROM verse.planets p
|
||||
INNER JOIN verse.planet_happiness ph ON ph.planet_id = p.name_id
|
||||
INNER JOIN verse.planet_money pm ON pm.planet_id = p.name_id
|
||||
CROSS JOIN (
|
||||
SELECT ( COUNT( * ) > 0 ) AS settings_exist
|
||||
FROM emp.planet_mining_settings
|
||||
WHERE planet_id = p_id AND empire_id = e_id
|
||||
) _count
|
||||
WHERE p.name_id = p_id;
|
||||
|
||||
-- Compute happiness change indicator
|
||||
|
@ -306,7 +315,12 @@ BEGIN
|
|||
END;
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
GRANT EXECUTE ON FUNCTION verse.get_owner_view( INT , INT ) TO :dbuser;
|
||||
REVOKE EXECUTE
|
||||
ON FUNCTION verse.get_owner_view( INT , INT )
|
||||
FROM PUBLIC;
|
||||
GRANT EXECUTE
|
||||
ON FUNCTION verse.get_owner_view( INT , INT )
|
||||
TO :dbuser;
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -22,7 +22,7 @@ BEGIN;
|
|||
VALUES ( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet3' ) );
|
||||
|
||||
/***** TESTS BEGIN HERE *****/
|
||||
SELECT plan( 14 );
|
||||
SELECT plan( 16 );
|
||||
|
||||
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' ) ) );
|
||||
|
@ -55,7 +55,17 @@ BEGIN;
|
|||
DROP TABLE IF EXISTS mset_update;
|
||||
|
||||
|
||||
SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Return value on valid identifiers' );
|
||||
SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Return value on valid identifiers but no existing settings' );
|
||||
SELECT ok( NOT 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 despite lack of settings' );
|
||||
SELECT has_table( 'mset_update' );
|
||||
DROP TABLE IF EXISTS mset_update;
|
||||
|
||||
INSERT INTO emp.planet_mining_settings( empire_id , planet_id , resource_name_id )
|
||||
SELECT _get_emp_name( 'testEmp1' ) , planet_id , resource_name_id
|
||||
FROM verse.resource_providers
|
||||
WHERE planet_id = _get_map_name( 'testPlanet1' );
|
||||
SELECT diag_test_name( 'emp.mset_update_start( INT , INT ) - Return value on valid identifiers and existing settings' );
|
||||
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' );
|
||||
|
|
|
@ -0,0 +1,61 @@
|
|||
/*
|
||||
* Test the emp.mset_toggle_source( INT , INT ) function
|
||||
*/
|
||||
BEGIN;
|
||||
/*
|
||||
* Create a pair of natural resources, three planets, an empire owning two
|
||||
* of the planets, and resource providers for one of the resources on the
|
||||
* neutral planet and on one of the owned 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 , 'resource' );
|
||||
SELECT _create_raw_planets( 3 , 'planet' );
|
||||
SELECT _create_emp_names( 1 , 'empire' );
|
||||
SELECT emp.create_empire( _get_emp_name( 'empire1' ) ,
|
||||
_get_map_name( 'planet1' ) ,
|
||||
200.0 );
|
||||
SELECT _create_resource_provider( 'planet1' , 'resource1' );
|
||||
SELECT _create_resource_provider( 'planet2' , 'resource1' );
|
||||
INSERT INTO emp.planets ( empire_id , planet_id )
|
||||
VALUES ( _get_emp_name( 'empire1' ) , _get_map_name( 'planet3' ) );
|
||||
|
||||
|
||||
-- ***** TESTS BEGIN HERE *****
|
||||
SELECT plan( 8 );
|
||||
|
||||
SELECT diag_test_name( 'emp.mset_toggle_source() - Return value on bad empire identifier' );
|
||||
SELECT ok( NOT emp.mset_toggle_source( _get_bad_emp_name( ) , _get_map_name( 'planet1' ) ) );
|
||||
SELECT diag_test_name( 'emp.mset_toggle_source() - Return value on bad planbet identifier' );
|
||||
SELECT ok( NOT emp.mset_toggle_source( _get_emp_name( 'empire1' ) , _get_bad_map_name( ) ) );
|
||||
|
||||
SELECT diag_test_name( 'emp.mset_toggle_source() - Return value when the empire does not own the planet' );
|
||||
SELECT ok( NOT emp.mset_toggle_source( _get_emp_name( 'empire1' ) , _get_map_name( 'planet2' ) ) );
|
||||
SELECT diag_test_name( 'emp.mset_toggle_source() - Return value when the planet has no resource providers' );
|
||||
SELECT ok( NOT emp.mset_toggle_source( _get_emp_name( 'empire1' ) , _get_map_name( 'planet3' ) ) );
|
||||
|
||||
DELETE FROM emp.planet_mining_settings;
|
||||
UPDATE emp.mining_settings
|
||||
SET empmset_weight = 0;
|
||||
SELECT diag_test_name( 'emp.mset_toggle_source() - Return value when activating' );
|
||||
SELECT ok( emp.mset_toggle_source( _get_emp_name( 'empire1' ) , _get_map_name( 'planet1' ) ) );
|
||||
SELECT diag_test_name( 'emp.mset_toggle_source() - Mining settings inserted after activation' );
|
||||
SELECT set_eq(
|
||||
$$ SELECT * FROM emp.planet_mining_settings $$ ,
|
||||
$$ VALUES ( _get_emp_name( 'empire1' ) , _get_map_name( 'planet1' ) , _get_string( 'resource1' ) , 0 ) $$
|
||||
);
|
||||
|
||||
DELETE FROM emp.planet_mining_settings;
|
||||
INSERT INTO emp.planet_mining_settings VALUES (
|
||||
_get_emp_name( 'empire1' ) , _get_map_name( 'planet1' ) , _get_string( 'resource1' ) , 2
|
||||
);
|
||||
SELECT diag_test_name( 'emp.mset_toggle_source() - Return value when de-activating' );
|
||||
SELECT ok( emp.mset_toggle_source( _get_emp_name( 'empire1' ) , _get_map_name( 'planet1' ) ) );
|
||||
SELECT diag_test_name( 'emp.mset_toggle_source() - Mining settings deleted after de-activation' );
|
||||
SELECT is_empty( $$ SELECT * FROM emp.planet_mining_settings $$ );
|
||||
|
||||
SELECT * FROM finish( );
|
||||
ROLLBACK;
|
|
@ -0,0 +1,15 @@
|
|||
/*
|
||||
* Test privileges on emp.mset_toggle_source( INT , INT )
|
||||
*/
|
||||
BEGIN;
|
||||
SELECT emp.mset_update_start( 1 );
|
||||
|
||||
SELECT plan( 1 );
|
||||
|
||||
SELECT diag_test_name( 'emp.mset_toggle_source() - Privileges' );
|
||||
SELECT lives_ok( $$
|
||||
SELECT emp.mset_toggle_source( 1 , 1 )
|
||||
$$ );
|
||||
|
||||
SELECT * FROM finish( );
|
||||
ROLLBACK;
|
Reference in a new issue