Mining settings

* Changed the way mining settings work: use a priority value (between 0
and 4) as the weight. Leaving them as they were before would have caused
numerous problems (and a lot of unnecessary code to work around them)

* Empire mining settings will be created along with the empire's own
record. By default all natural resources will have weight = 2.

* Added a set of four stored procedures which can be used to update an
empire's mining settings, including planet-specific settings. The
emp.mset_update_start() function can be used to start an update (on an
empire's settings if there is only one parameter, or on a planet's
settings if there are two parameters); the emp.mset_update_set() and
emp.mset_update_apply() functions are then used to modify the settings
and apply the changes, respectively.
This commit is contained in:
Emmanuel BENOîT 2012-01-10 10:17:47 +01:00
parent 3e109b13bc
commit afc66166e0
15 changed files with 745 additions and 55 deletions

View file

@ -135,11 +135,12 @@ CREATE TABLE emp.mining_settings(
resource_name_id INT NOT NULL , resource_name_id INT NOT NULL ,
/* Weight to give to this type of resource when there are no planet- /* 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 empmset_weight INT NOT NULL
DEFAULT 1 DEFAULT 2
CHECK( empmset_weight >= 0 ) , CHECK( empmset_weight BETWEEN 0 AND 4 ) ,
/* Primary key on (empire,resource type) pairs */ /* Primary key on (empire,resource type) pairs */
PRIMARY KEY( empire_id , resource_name_id ) PRIMARY KEY( empire_id , resource_name_id )
@ -176,10 +177,12 @@ CREATE TABLE emp.planet_mining_settings(
/* Identifier of the type of resources */ /* Identifier of the type of resources */
resource_name_id INT NOT NULL , 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 emppmset_weight INT NOT NULL
DEFAULT 1 DEFAULT 2
CHECK( emppmset_weight >= 0 ) , CHECK( emppmset_weight BETWEEN 0 AND 4 ) ,
/* Primary key on (empire,resource type) pairs */ /* Primary key on (empire,resource type) pairs */
PRIMARY KEY( empire_id , planet_id , resource_name_id ) PRIMARY KEY( empire_id , planet_id , resource_name_id )

View file

@ -7,43 +7,60 @@
-- -------------------------------------------------------- -- --------------------------------------------------------
-- /*
-- Empire creation * Empire creation
-- *
-- Parameters: * This function inserts the rows that represent an empire and its settings.
-- nid Empire name identifier * It also initialises the empire's updates.
-- pid Planet identifier *
-- icash Initial cash * Parameters:
-- * _name_id Empire name identifier
CREATE OR REPLACE FUNCTION emp.create_empire( nid INT , pid INT , icash REAL ) * _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 RETURNS VOID
STRICT STRICT VOLATILE
VOLATILE
SECURITY INVOKER SECURITY INVOKER
AS $$ AS $$
DECLARE DECLARE
uid BIGINT; _update BIGINT;
utp update_type; _update_type update_type;
BEGIN BEGIN
-- Add empire and give initial planet -- Add empire and give initial planet
INSERT INTO emp.empires ( name_id , cash ) INSERT INTO emp.empires ( name_id , cash )
VALUES ( nid , icash ); VALUES ( _name_id , _initial_cash );
INSERT INTO emp.planets ( planet_id , empire_id ) 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 -- Add empire update records
FOR utp IN SELECT x FROM unnest( enum_range( NULL::update_type ) ) AS x FOR _update_type IN SELECT _type
WHERE x::text LIKE 'EMPIRE_%' FROM unnest( enum_range( NULL::update_type ) ) AS _type
WHERE _type::text LIKE 'EMPIRE_%'
LOOP LOOP
INSERT INTO sys.updates( gu_type ) INSERT INTO sys.updates( gu_type )
VALUES ( utp ) VALUES ( _update_type )
RETURNING id INTO uid; RETURNING id INTO _update;
INSERT INTO emp.updates ( update_id , empire_id ) INSERT INTO emp.updates ( update_id , empire_id )
VALUES ( uid , nid ); VALUES ( _update , _name_id );
END LOOP; END LOOP;
END; END;
$$ LANGUAGE plpgsql; $$ LANGUAGE plpgsql;
REVOKE EXECUTE
ON FUNCTION emp.create_empire( INT , INT , REAL )
FROM PUBLIC;
-- --
-- Returns a planet owner's empire size -- Returns a planet owner's empire size

View file

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

View file

@ -14,10 +14,10 @@ BEGIN;
SELECT id , 0 FROM naming.empire_names; SELECT id , 0 FROM naming.empire_names;
/****** TESTS BEGIN HERE ******/ /****** 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 PREPARE _test_this AS
INSERT INTO emp.mining_settings( INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight empire_id , resource_name_id , empmset_weight
@ -26,11 +26,11 @@ BEGIN;
); );
SELECT lives_ok( '_test_this' ); 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 ); SELECT throws_ok( '_test_this' , 23505 );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.mining_settings( INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight empire_id , resource_name_id , empmset_weight
@ -40,7 +40,7 @@ BEGIN;
SELECT lives_ok( '_test_this' ); SELECT lives_ok( '_test_this' );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.mining_settings( INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight empire_id , resource_name_id , empmset_weight
@ -51,8 +51,21 @@ BEGIN;
DEALLOCATE ALL; DEALLOCATE ALL;
DELETE FROM emp.mining_settings; 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 PREPARE _test_this AS
INSERT INTO emp.mining_settings( INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight empire_id , resource_name_id , empmset_weight
@ -62,7 +75,7 @@ BEGIN;
SELECT throws_ok( '_test_this' , 23502 ); SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.mining_settings( INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight empire_id , resource_name_id , empmset_weight
@ -73,7 +86,7 @@ BEGIN;
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.mining_settings( INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight empire_id , resource_name_id , empmset_weight
@ -83,7 +96,7 @@ BEGIN;
SELECT throws_ok( '_test_this' , 23502 ); SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.mining_settings( INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight empire_id , resource_name_id , empmset_weight
@ -94,7 +107,7 @@ BEGIN;
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.mining_settings( INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight empire_id , resource_name_id , empmset_weight
@ -104,7 +117,7 @@ BEGIN;
SELECT throws_ok( '_test_this' , 23502 ); SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.mining_settings( INSERT INTO emp.mining_settings(
empire_id , resource_name_id , empmset_weight empire_id , resource_name_id , empmset_weight
@ -114,6 +127,16 @@ BEGIN;
SELECT throws_ok( '_test_this' , 23514 ); SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL; 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( ); SELECT * FROM finish( );
ROLLBACK; ROLLBACK;

View file

@ -1,5 +1,5 @@
/* /*
* Test constraints and foreign keys on emp.mining_settings * Test constraints and foreign keys on emp.planet_mining_settings
*/ */
BEGIN; BEGIN;
@ -20,10 +20,10 @@ BEGIN;
-- No provider for testResource2 on testPlanet2 -- No provider for testResource2 on testPlanet2
/****** TESTS BEGIN HERE ******/ /****** 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -34,11 +34,11 @@ BEGIN;
); );
SELECT lives_ok( '_test_this' ); 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 ); SELECT throws_ok( '_test_this' , 23505 );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -50,7 +50,7 @@ BEGIN;
SELECT lives_ok( '_test_this' ); SELECT lives_ok( '_test_this' );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -62,7 +62,7 @@ BEGIN;
SELECT lives_ok( '_test_this' ); SELECT lives_ok( '_test_this' );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -73,10 +73,25 @@ BEGIN;
); );
SELECT lives_ok( '_test_this' ); SELECT lives_ok( '_test_this' );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -88,7 +103,7 @@ BEGIN;
SELECT throws_ok( '_test_this' , 23502 ); SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -101,7 +116,7 @@ BEGIN;
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -113,7 +128,7 @@ BEGIN;
SELECT throws_ok( '_test_this' , 23502 ); SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -126,7 +141,7 @@ BEGIN;
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -138,7 +153,7 @@ BEGIN;
SELECT throws_ok( '_test_this' , 23502 ); SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -151,7 +166,7 @@ BEGIN;
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -164,7 +179,7 @@ BEGIN;
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -176,7 +191,7 @@ BEGIN;
SELECT throws_ok( '_test_this' , 23502 ); SELECT throws_ok( '_test_this' , 23502 );
DEALLOCATE ALL; 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 PREPARE _test_this AS
INSERT INTO emp.planet_mining_settings( INSERT INTO emp.planet_mining_settings(
empire_id , planet_id , empire_id , planet_id ,
@ -188,6 +203,18 @@ BEGIN;
SELECT throws_ok( '_test_this' , 23514 ); SELECT throws_ok( '_test_this' , 23514 );
DEALLOCATE ALL; 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( ); SELECT * FROM finish( );
ROLLBACK; ROLLBACK;

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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