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:
Emmanuel BENOîT 2012-02-07 17:03:55 +01:00
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
src/main/java/com/deepclone/lw/sqld/game

View file

@ -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;

View file

@ -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;

View file

@ -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' );

View file

@ -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;

View file

@ -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;

View file

@ -22,7 +22,7 @@ public final class PlanetData
public AccessType getAccess( )
{
return access;
return this.access;
}
@ -34,7 +34,7 @@ public final class PlanetData
public int getX( )
{
return x;
return this.x;
}
@ -46,7 +46,7 @@ public final class PlanetData
public int getY( )
{
return y;
return this.y;
}
@ -58,7 +58,7 @@ public final class PlanetData
public int getOrbit( )
{
return orbit;
return this.orbit;
}
@ -70,7 +70,7 @@ public final class PlanetData
public int getPicture( )
{
return picture;
return this.picture;
}
@ -82,7 +82,7 @@ public final class PlanetData
public String getName( )
{
return name;
return this.name;
}
@ -94,7 +94,7 @@ public final class PlanetData
public String getTag( )
{
return tag;
return this.tag;
}
@ -117,7 +117,7 @@ public final class PlanetData
public long getPopulation( )
{
return population;
return this.population;
}
@ -129,7 +129,7 @@ public final class PlanetData
public long getDefence( )
{
return defence;
return this.defence;
}
@ -141,7 +141,7 @@ public final class PlanetData
public long getOwnPower( )
{
return ownPower;
return this.ownPower;
}
@ -153,7 +153,7 @@ public final class PlanetData
public long getFriendlyPower( )
{
return friendlyPower;
return this.friendlyPower;
}
@ -165,7 +165,7 @@ public final class PlanetData
public long getHostilePower( )
{
return hostilePower;
return this.hostilePower;
}
@ -177,7 +177,7 @@ public final class PlanetData
public Long getBattle( )
{
return battle;
return this.battle;
}
@ -197,11 +197,12 @@ public final class PlanetData
private boolean renamePossible;
private boolean abandonPossible;
private Integer abandonTime;
private boolean ownSettings;
public int getHappiness( )
{
return happiness;
return this.happiness;
}
@ -213,7 +214,7 @@ public final class PlanetData
public int gethChange( )
{
return hChange;
return this.hChange;
}
@ -225,7 +226,7 @@ public final class PlanetData
public long getIncome( )
{
return income;
return this.income;
}
@ -237,7 +238,7 @@ public final class PlanetData
public long getUpkeep( )
{
return upkeep;
return this.upkeep;
}
@ -249,7 +250,7 @@ public final class PlanetData
public boolean isRenamePossible( )
{
return renamePossible;
return this.renamePossible;
}
@ -261,7 +262,7 @@ public final class PlanetData
public boolean isAbandonPossible( )
{
return abandonPossible;
return this.abandonPossible;
}
@ -273,7 +274,7 @@ public final class PlanetData
public Integer getAbandonTime( )
{
return abandonTime;
return this.abandonTime;
}
@ -282,6 +283,18 @@ public final class PlanetData
this.abandonTime = abandonTime;
}
public boolean isOwnSettings( )
{
return this.ownSettings;
}
public void setOwnSettings( boolean ownSettings )
{
this.ownSettings = ownSettings;
}
}