Extracted quantities update as soon as possible

* The quantities of resources extracted from mines will now be updated
as soon as they have a reason to. This includes planet assignment,
abandonment, ownership changes, and updates to mining priorities.

* The mining update will now remove the current resource income from the
providers, and only then re-compute all extracted quantities. This is
more logical and corresponds to the way the other game updates work.

* Fixed bug in extraction computation where the size of the planet's
happy population was used instead of the happy/total ratio when
adjusting the mining production for riots.

* The following SQL scripts must be re-executed to upgrade a database:
  -> 040-functions/040-empire.sql
  -> 040-functions/145-resource-providers.sql
  -> 040-functions/147-empire-mining.sql
  -> 050-updates/120-planet-mining.sql
This commit is contained in:
Emmanuel BENOîT 2012-02-09 10:54:00 +01:00
parent f3aa563758
commit bf6bea5a79
35 changed files with 863 additions and 372 deletions

View file

@ -3,7 +3,7 @@
*/
BEGIN;
/* We need a pair of natural resources, a basic resource, a planet
* and an empire name.
* and an empire name. We add a resource provider to the planet.
*/
\i utils/strings.sql
\i utils/resources.sql
@ -14,9 +14,42 @@ BEGIN;
SELECT _create_resources( 1 , 'basicRes' );
SELECT _create_raw_planets( 1 , 'testPlanet' );
SELECT _create_emp_names( 1 , 'testEmp' );
INSERT INTO verse.planet_resources( planet_id , resource_name_id )
SELECT name_id , resource_name_id
FROM verse.planets CROSS JOIN defs.resources;
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( 'natRes1' ) , 100 ,
100 , 0.2 , 0.5
);
/* We replace the emp.mining_get_input() and emp.mining_compute_extraction()
* functions with something we control fully.
*/
CREATE OR REPLACE FUNCTION emp.mining_get_input( _empire INT )
RETURNS SETOF emp.planet_mining_type
LANGUAGE SQL
AS $$
SELECT _get_map_name( 'testPlanet1' ) AS planet ,
_get_string( 'natRes1' ) AS resource ,
NULL::DOUBLE PRECISION AS quantity , NULL::DOUBLE PRECISION AS quantity_max ,
NULL::DOUBLE PRECISION AS difficulty , NULL::INT AS empire ,
NULL::REAL AS happiness , NULL::DOUBLE PRECISION AS weight ,
NULL::DOUBLE PRECISION AS total_weight;
$$;
CREATE OR REPLACE FUNCTION emp.mining_compute_extraction( _input emp.planet_mining_type )
RETURNS DOUBLE PRECISION LANGUAGE SQL
AS $$
SELECT 42::DOUBLE PRECISION;
$$;
/***** TESTS BEGIN HERE *****/
SELECT plan( 7 );
SELECT plan( 8 );
SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) ,
_get_map_name( 'testPlanet1' ) ,
@ -25,10 +58,12 @@ BEGIN;
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 diag_test_name( 'emp.create_empire() - Empire cash set' );
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 diag_test_name( 'emp.create_empire() - Empire debt set' );
SELECT is( debt , 0.0::REAL ) FROM emp.empires
WHERE name_id = _get_emp_name( 'testEmp1' );
@ -57,5 +92,11 @@ BEGIN;
FROM emp.resources
WHERE empire_id = _get_emp_name( 'testEmp1' );
SELECT diag_test_name( 'emp.create_empire() - Resource mining has been updated' );
SELECT is( pres_income::DOUBLE PRECISION , 42::DOUBLE PRECISION )
FROM verse.planet_resources
WHERE planet_id = _get_map_name( 'testPlanet1' )
AND resource_name_id = _get_string( 'natRes1' );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -1,5 +1,5 @@
/*
* Test sys.gu_pmc_weights_view
* Test emp.scaled_mining_weights_view
*/
BEGIN;
/* Create a table which will server as an alternate source for
@ -28,14 +28,14 @@ BEGIN;
/***** TESTS BEGIN HERE *****/
SELECT plan( 2 );
SELECT diag_test_name( 'sys.gu_pmc_weights_view - Rows present' );
SELECT diag_test_name( 'emp.scaled_mining_weights_view - Rows present' );
SELECT isnt( COUNT(*)::INT , 0 )
FROM sys.gu_pmc_weights_view;
FROM emp.scaled_mining_weights_view;
SELECT diag_test_name( 'sys.gu_pmc_weights_view - weight = game.resources.weightBase ^ setting' );
SELECT diag_test_name( 'emp.scaled_mining_weights_view - weight = game.resources.weightBase ^ setting' );
SELECT sys.uoc_constant( 'game.resources.weightBase' , '(test)' , 'Resources' , 10.0 );
SELECT is_empty( $$
SELECT * FROM sys.gu_pmc_weights_view
SELECT * FROM emp.scaled_mining_weights_view
WHERE pmc_weight IS NULL
OR pmc_weight <> POW( 10 , resource_name_id )
$$ );

View file

@ -1,9 +1,9 @@
/*
* Test sys.gu_pmc_totals_view
* Test emp.total_mining_weights_view
*/
BEGIN;
/* Create a table which will server as an alternate source for
* sys.gu_pmc_weights_view ; the table is not temporary (PostgreSQL
* emp.scaled_mining_weights_view ; the table is not temporary (PostgreSQL
* won't allow replacing the view otherwise), but will be dropped
* on rollback anyway.
*/
@ -13,7 +13,7 @@ BEGIN;
pmc_weight DOUBLE PRECISION
);
CREATE OR REPLACE VIEW sys.gu_pmc_weights_view
CREATE OR REPLACE VIEW emp.scaled_mining_weights_view
AS SELECT * FROM fake_mining_weights;
/* Insert fake records for two different planets */
@ -27,7 +27,7 @@ BEGIN;
SELECT plan( 1 );
SELECT set_eq(
$$ SELECT * FROM sys.gu_pmc_totals_view $$ ,
$$ SELECT * FROM emp.total_mining_weights_view $$ ,
$$ VALUES ( 1 , 3.0 ) , ( 2 , 9.0 ) $$
);

View file

@ -0,0 +1,135 @@
/*
* Test the emp.mining_compute_extraction() function
*/
BEGIN;
/* Define the necessary constant */
SELECT sys.uoc_constant( 'game.resources.extraction' , '(test)' , 'Resources' , 10 );
SELECT sys.uoc_constant( 'game.happiness.strike' , '(test)' , 'Resources' , 0.5 );
/* Make sure the functions are not immutable during the tests */
ALTER FUNCTION sys.get_constant( TEXT ) VOLATILE;
ALTER FUNCTION emp.mining_compute_extraction( emp.planet_mining_type ) VOLATILE;
/* Replace verse.get_raw_production() with a function that returns a
* value from a "temporary" table.
*/
CREATE TABLE fake_mining_production( production REAL );
CREATE OR REPLACE FUNCTION verse.get_raw_production( pid INT , pt building_output_type )
RETURNS REAL LANGUAGE SQL VOLATILE AS 'SELECT production FROM fake_mining_production';
INSERT INTO fake_mining_production VALUES ( 100 );
/* Create a table that is used as the input for tests */
CREATE TABLE tests_input OF emp.planet_mining_type;
-- ***** TESTS BEGIN HERE *****
SELECT plan( 9 );
INSERT INTO tests_input VALUES
( 0 , 0 , 1000 , 1000 , 0 , NULL , NULL , NULL , NULL ) ,
( 1 , 1 , 10000 , 10000 , 0 , NULL , NULL , NULL , NULL ) ,
( 2 , 2 , 100000 , 100000 , 0 , NULL , NULL , NULL , NULL );
SELECT diag_test_name( 'emp.mining_compute_extraction() - No empire -> no extraction' );
SELECT is_empty( $$
SELECT *
FROM tests_input
WHERE emp.mining_compute_extraction( ROW(
planet , resource , quantity , quantity_max ,
difficulty , empire , happiness , weight ,
total_weight ) )
<> 0 $$ );
DELETE FROM tests_input;
INSERT INTO tests_input VALUES
( 0 , 0 , 1000 , 1000 , 0 , 1 , 1 , 1 , 1 ) ,
( 1 , 1 , 10000 , 10000 , 0 , 1 , 1 , 1 , 1 ) ,
( 2 , 2 , 100000 , 100000 , 0 , 1 , 1 , 1 , 1 );
SELECT diag_test_name( 'emp.mining_compute_extraction() - Extracted quantity > 0 if empire and resources are present' );
SELECT is_empty( $$
SELECT *
FROM tests_input
WHERE emp.mining_compute_extraction( ROW(
planet , resource , quantity , quantity_max ,
difficulty , empire , happiness , weight ,
total_weight ) )
= 0 $$ );
SELECT diag_test_name( 'emp.mining_compute_extraction() - Extracted quantity does not change for same initial ratio' );
SELECT is( COUNT( DISTINCT emp.mining_compute_extraction( ROW(
planet , resource , quantity , quantity_max ,
difficulty , empire , happiness , weight ,
total_weight ) )
)::INT , 1 ) FROM tests_input;
UPDATE tests_input SET weight = 1 + planet , total_weight = 3;
SELECT diag_test_name( 'emp.mining_compute_extraction() - Result increases when weight/total weight ratio increases' );
SELECT set_eq(
$$ SELECT rank() OVER ( ORDER BY weight ) AS r1 ,
rank() OVER( ORDER BY emp.mining_compute_extraction( ROW(
planet , resource , quantity , quantity_max ,
difficulty , empire , happiness , weight ,
total_weight ) ) ) AS r2
FROM tests_input $$ ,
$$ VALUES ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) $$
);
UPDATE tests_input SET weight = total_weight , happiness = 0.5 + 0.25 * planet;
SELECT diag_test_name( 'emp.mining_compute_extraction() - Happiness does not affect result when greater than strike level' );
SELECT is( COUNT( DISTINCT emp.mining_compute_extraction( ROW(
planet , resource , quantity , quantity_max ,
difficulty , empire , happiness , weight ,
total_weight ) )
)::INT , 1 ) FROM tests_input;
UPDATE tests_input SET weight = total_weight , happiness = 0.2 * planet;
SELECT diag_test_name( 'emp.mining_compute_extraction() - Result increases with happiness when lower than strike level' );
SELECT set_eq(
$$ SELECT rank() OVER ( ORDER BY happiness ) AS r1 ,
rank() OVER( ORDER BY emp.mining_compute_extraction( ROW(
planet , resource , quantity , quantity_max ,
difficulty , empire , happiness , weight ,
total_weight ) ) ) AS r2
FROM tests_input $$ ,
$$ VALUES ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) $$
);
UPDATE fake_mining_production SET production = 10000;
UPDATE tests_input SET quantity = 1 , quantity_max = 1 , happiness = 1;
SELECT diag_test_name( 'emp.mining_compute_extraction() - Result <= quantity' );
SELECT is_empty(
$$ SELECT * FROM tests_input
WHERE quantity < emp.mining_compute_extraction( ROW(
planet , resource , quantity , quantity_max ,
difficulty , empire , happiness , weight ,
total_weight ) ) $$
);
UPDATE fake_mining_production SET production = 1;
UPDATE tests_input SET quantity = 100 * planet , quantity_max = 200;
SELECT diag_test_name( 'emp.mining_compute_extraction() - Result decreases when quantity/capacity ratio decreases' );
SELECT set_eq(
$$ SELECT rank() OVER ( ORDER BY quantity ) AS r1 ,
rank() OVER( ORDER BY emp.mining_compute_extraction( ROW(
planet , resource , quantity , quantity_max ,
difficulty , empire , happiness , weight ,
total_weight ) ) ) AS r2
FROM tests_input $$ ,
$$ VALUES ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) $$
);
UPDATE tests_input SET quantity = quantity_max , difficulty = 0.33 * planet;
SELECT diag_test_name( 'emp.mining_compute_extraction() - Result decreases when difficulty increases' );
SELECT set_eq(
$$ SELECT rank() OVER ( ORDER BY difficulty DESC ) AS r1 ,
rank() OVER( ORDER BY emp.mining_compute_extraction( ROW(
planet , resource , quantity , quantity_max ,
difficulty , empire , happiness , weight ,
total_weight ) ) ) AS r2
FROM tests_input $$ ,
$$ VALUES ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) $$
);
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,88 @@
/*
* Tests for the verse.mining_get_input() function
*/
BEGIN;
\i utils/common-setup/setup-gu-pmc-get-data-test.sql
SELECT plan( 9 );
SELECT diag_test_name( 'verse.mining_get_input() - Neutral planet without resource providers -> no rows' );
SELECT is_empty( $$ SELECT * FROM verse.mining_get_input( _get_map_name( 'planet1' ) ) $$ );
CREATE TEMPORARY TABLE test_results
AS SELECT * FROM verse.mining_get_input( _get_map_name( 'planet2' ) );
SELECT diag_test_name( 'verse.mining_get_input() - Neutral planet with resource providers - Rows included' );
SELECT is( COUNT(*)::INT , 2)
FROM test_results
WHERE planet = _get_map_name ( 'planet2' )
AND difficulty = 0.2 AND empire IS NULL
AND happiness IS NULL AND weight IS NULL
AND total_weight IS NULL;
SELECT diag_test_name( 'verse.mining_get_input() - Neutral planet with resource providers - No extra rows' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE NOT ( planet = _get_map_name ( 'planet2' )
AND difficulty = 0.2 AND empire IS NULL
AND happiness IS NULL AND weight IS NULL
AND total_weight IS NULL );
$$ );
DROP TABLE test_results;
CREATE TEMPORARY TABLE test_results
AS SELECT * FROM verse.mining_get_input( _get_map_name( 'planet3' ) );
SELECT diag_test_name( 'verse.mining_get_input() - Planet using empire settings - Rows included' );
SELECT is( COUNT(*)::INT , 2)
FROM test_results
WHERE planet = _get_map_name ( 'planet3' ) AND difficulty = 0.3
AND empire = _get_emp_name( 'empire1' )
AND happiness IS NOT NULL AND weight = 100
AND total_weight = 200;
SELECT diag_test_name( 'verse.mining_get_input() - Planet using empire settings - No extra rows' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE NOT ( planet = _get_map_name ( 'planet3' )
AND difficulty = 0.3
AND empire = _get_emp_name( 'empire1' )
AND happiness IS NOT NULL
AND weight = 100 AND total_weight = 200 );
$$ );
DROP TABLE test_results;
CREATE TEMPORARY TABLE test_results
AS SELECT * FROM verse.mining_get_input( _get_map_name( 'planet4' ) );
SELECT diag_test_name( 'verse.mining_get_input() - Planet using specific settings - Rows included' );
SELECT is( COUNT(*)::INT , 2)
FROM test_results
WHERE planet = _get_map_name ( 'planet4' ) AND difficulty = 0.4
AND empire = _get_emp_name( 'empire2' )
AND happiness IS NOT NULL AND (
( resource = _get_string( 'resource1' ) AND weight = 10 )
OR ( resource = _get_string( 'resource2' ) AND weight = 1000 ) )
AND total_weight = 1010;
SELECT diag_test_name( 'verse.mining_get_input() - Planet using specific settings - No extra rows' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE NOT ( planet = _get_map_name ( 'planet4' )
AND difficulty = 0.4 AND empire = _get_emp_name( 'empire2' )
AND happiness IS NOT NULL AND total_weight = 1010
AND ( ( resource = _get_string( 'resource1' ) AND weight = 10 )
OR ( resource = _get_string( 'resource2' ) AND weight = 1000 ) ) );
$$ );
DROP TABLE test_results;
SELECT diag_test_name( 'verse.mining_get_input() - Owned planet without resource providers -> no rows' );
SELECT is_empty( $$ SELECT * FROM verse.mining_get_input( _get_map_name( 'planet5' ) ) $$ );
CREATE TEMPORARY TABLE test_results
AS SELECT * FROM verse.mining_get_input( _get_map_name( 'planet6' ) );
SELECT diag_test_name( 'verse.mining_get_input() - Selects planets independently of update state' );
SELECT is( COUNT(*)::INT , 2)
FROM test_results
WHERE planet = _get_map_name ( 'planet6' ) AND difficulty = 0.6
AND empire = _get_emp_name( 'empire4' )
AND happiness IS NOT NULL AND weight = 100
AND total_weight = 200;
DROP TABLE test_results;
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,69 @@
/*
* Tests for the emp.mining_get_input() function
*/
BEGIN;
\i utils/common-setup/setup-gu-pmc-get-data-test.sql
SELECT plan( 7 );
CREATE TEMPORARY TABLE test_results
AS SELECT * FROM emp.mining_get_input( _get_emp_name( 'empire1' ) );
SELECT diag_test_name( 'emp.mining_get_input() - Empire with a planet using empire settings - Rows included' );
SELECT is( COUNT(*)::INT , 2)
FROM test_results
WHERE planet = _get_map_name ( 'planet3' ) AND difficulty = 0.3
AND empire = _get_emp_name( 'empire1' )
AND happiness IS NOT NULL AND weight = 100
AND total_weight = 200;
SELECT diag_test_name( 'emp.mining_get_input() - Empire with a planet using empire settings - No extra rows' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE NOT ( planet = _get_map_name ( 'planet3' )
AND difficulty = 0.3
AND empire = _get_emp_name( 'empire1' )
AND happiness IS NOT NULL
AND weight = 100 AND total_weight = 200 );
$$ );
DROP TABLE test_results;
CREATE TEMPORARY TABLE test_results
AS SELECT * FROM emp.mining_get_input( _get_emp_name( 'empire2' ) );
SELECT diag_test_name( 'emp.mining_get_input() - Empire with a planet using specific settings - Rows included' );
SELECT is( COUNT(*)::INT , 2)
FROM test_results
WHERE planet = _get_map_name ( 'planet4' ) AND difficulty = 0.4
AND empire = _get_emp_name( 'empire2' )
AND happiness IS NOT NULL AND (
( resource = _get_string( 'resource1' ) AND weight = 10 )
OR ( resource = _get_string( 'resource2' ) AND weight = 1000 ) )
AND total_weight = 1010;
SELECT diag_test_name( 'emp.mining_get_input() - Empire with a planet using specific settings - No extra rows' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE NOT ( planet = _get_map_name ( 'planet4' )
AND difficulty = 0.4 AND empire = _get_emp_name( 'empire2' )
AND happiness IS NOT NULL AND total_weight = 1010
AND ( ( resource = _get_string( 'resource1' ) AND weight = 10 )
OR ( resource = _get_string( 'resource2' ) AND weight = 1000 ) ) );
$$ );
DROP TABLE test_results;
SELECT diag_test_name( 'emp.mining_get_input() - Owned planet without resource providers -> no rows' );
SELECT is_empty( $$ SELECT * FROM emp.mining_get_input( _get_emp_name( 'empire3' ) ) $$ );
CREATE TEMPORARY TABLE test_results
AS SELECT * FROM emp.mining_get_input( _get_emp_name( 'empire4' ) );
SELECT diag_test_name( 'emp.mining_get_input() - Selects planets independently of update state' );
SELECT is( COUNT(*)::INT , 2)
FROM test_results
WHERE planet = _get_map_name ( 'planet6' ) AND difficulty = 0.6
AND empire = _get_emp_name( 'empire4' )
AND happiness IS NOT NULL AND weight = 100
AND total_weight = 200;
DROP TABLE test_results;
SELECT diag_test_name( 'emp.mining_get_input() - Empire with no planets -> no rows' );
SELECT is_empty( $$ SELECT * FROM emp.mining_get_input( _get_emp_name( 'empire5' ) ) $$ );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,89 @@
/*
* Test the sys.process_planet_mining_updates() function
*/
BEGIN;
/* Disable other update types */
DELETE FROM sys.update_types
WHERE updtype_name <> 'PlanetMining';
/* We need two planets with identicals resource records and resource providers. */
\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 , 'resource' );
SELECT _create_raw_planets( 2 , 'planet' );
INSERT INTO verse.resource_providers (
planet_id , resource_name_id , resprov_quantity_max ,
resprov_quantity , resprov_difficulty , resprov_recovery
) VALUES (
_get_map_name( 'planet1' ) , _get_string( 'resource1' ) , 100 ,
100 , 0.2 , 0.5
) , (
_get_map_name( 'planet2' ) , _get_string( 'resource1' ) , 100 ,
100 , 0.2 , 0.5
);
INSERT INTO verse.planet_resources ( planet_id , resource_name_id , pres_income , pres_upkeep )
SELECT p.name_id , r.resource_name_id , 50 , 0
FROM verse.planets p CROSS JOIN defs.resources r;
/* First planet will be updated, second will not */
UPDATE sys.updates su
SET update_state = 'PROCESSING' , update_last = 0
FROM verse.planets_updates vu
WHERE vu.update_id = su.update_id
AND vu.name_id = _get_map_name( 'planet1' );
UPDATE sys.updates su
SET update_state = 'PROCESSED' , update_last = 0
FROM verse.planets_updates vu
WHERE vu.update_id = su.update_id
AND vu.name_id = _get_map_name( 'planet2' );
/* Replace sys.gu_pmc_get_data() and emp.mining_compute_extraction(); both
* functions will write to temporary tables.
*/
CREATE TABLE _get_data_calls( tick BIGINT );
CREATE OR REPLACE FUNCTION sys.gu_pmc_get_data( _tick BIGINT )
RETURNS SETOF emp.planet_mining_type LANGUAGE SQL
AS $$
INSERT INTO _get_data_calls VALUES ( $1 );
SELECT _get_map_name( 'planet1' ) AS planet ,
_get_string( 'resource1' ) AS resource ,
NULL::DOUBLE PRECISION AS quantity , NULL::DOUBLE PRECISION AS quantity_max ,
NULL::DOUBLE PRECISION AS difficulty , NULL::INT AS empire ,
NULL::REAL AS happiness , NULL::DOUBLE PRECISION AS weight ,
NULL::DOUBLE PRECISION AS total_weight;
$$;
CREATE OR REPLACE FUNCTION emp.mining_compute_extraction( _input emp.planet_mining_type )
RETURNS DOUBLE PRECISION LANGUAGE SQL
AS $$
SELECT 42::DOUBLE PRECISION;
$$;
/***** TESTS BEGIN HERE *****/
SELECT plan( 3 );
SELECT sys.process_planet_mining_updates( 0 );
SELECT diag_test_name( 'sys.process_planet_mining_updates() - Resource providers updated' );
SELECT set_eq(
$$ SELECT planet_id , resprov_quantity FROM verse.resource_providers $$ ,
$$ VALUES ( _get_map_name( 'planet1' ) , 50 ) , ( _get_map_name( 'planet2' ) , 100 ) $$
);
SELECT diag_test_name( 'sys.process_planet_mining_updates() - Planet resources updated' );
SELECT set_eq(
$$ SELECT planet_id , pres_income FROM verse.planet_resources $$ ,
$$ VALUES ( _get_map_name( 'planet1' ) , 42 ) , ( _get_map_name( 'planet2' ) , 50 ) $$
);
SELECT diag_test_name( 'sys.process_planet_mining_updates() - Two calls to gu_pmc_get_data()' );
SELECT is( COUNT(*)::INT , 2 ) FROM _get_data_calls;
SELECT * FROM finish( );
ROLLBACK;

View file

@ -1,91 +0,0 @@
/*
* Test the sys.gu_pmc_update_resource() function
*/
BEGIN;
/*
* We need to create a set of both resource providers and planet resource
* records that will be updated by the function when it is called. We
* disable foreign keys on both tables, as it makes things simpler. We
* also replace verse.get_raw_production(), verse.get_extraction_factor( )
* and verse.adjust_production() so that they return fixed values, and we
* need to set the game.resources.extraction constant.
*/
ALTER TABLE verse.resource_providers
DROP CONSTRAINT fk_resprov_planet ,
DROP CONSTRAINT fk_resprov_resource;
ALTER TABLE verse.planet_resources
DROP CONSTRAINT fk_pres_planet ,
DROP CONSTRAINT fk_pres_resource;
INSERT INTO verse.resource_providers(
planet_id , resource_name_id , resprov_quantity_max, resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
1 , 1 , 1000 , 1000 , 0 , 0.5
);
INSERT INTO verse.planet_resources(
planet_id , resource_name_id , pres_income , pres_upkeep
) VALUES (
1 , 1 , 0 , 0
);
CREATE OR REPLACE FUNCTION verse.get_raw_production( pid INT , pt building_output_type )
RETURNS REAL
AS $$
SELECT 1.0::REAL;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION verse.adjust_production( prod REAL , happiness REAL )
RETURNS REAL
AS $$
SELECT 1.0::REAL;
$$ LANGUAGE SQL;
CREATE OR REPLACE FUNCTION verse.get_extraction_factor( _fill_ratio DOUBLE PRECISION , _difficulty DOUBLE PRECISION )
RETURNS DOUBLE PRECISION
AS $$
SELECT ( CASE WHEN $1 = 1.0 AND $2 = 0 THEN 0.002 ELSE 0.0 END )::DOUBLE PRECISION;
$$ LANGUAGE SQL;
SELECT sys.uoc_constant( 'game.resources.extraction' , '(test)' , 'Resources' , 1440.0 );
ALTER FUNCTION sys.get_constant( TEXT ) VOLATILE;
/***** TESTS BEGIN HERE *****/
SELECT plan( 4 );
SELECT sys.gu_pmc_update_resource( ROW(
1 , 1 , 1000.0 , 1000.0 , 0.0 , NULL , 1.0 , 0.5 , 1.0
) );
SELECT diag_test_name( 'sys.gu_pmc_update_resource( ) - Provider udpated' );
SELECT is( resprov_quantity , 999.999::DOUBLE PRECISION )
FROM verse.resource_providers
WHERE planet_id = 1 AND resource_name_id = 1;
SELECT diag_test_name( 'sys.gu_pmc_update_resource( ) - Planet resources income udpated' );
SELECT is( pres_income , 0.001::DOUBLE PRECISION )
FROM verse.planet_resources
WHERE planet_id = 1 AND resource_name_id = 1;
UPDATE verse.resource_providers SET resprov_quantity = resprov_quantity_max;
UPDATE sys.constant_definitions
SET c_value = 14400000.0
WHERE name = 'game.resources.extraction';
SELECT sys.gu_pmc_update_resource( ROW(
1 , 1 , 1000.0 , 1000.0 , 0.0 , NULL , 1.0 , 0.5 , 1.0
) );
SELECT diag_test_name( 'sys.gu_pmc_update_resource( ) - Bounded extraction quantity (1/2)' );
SELECT is( resprov_quantity , 990.0::DOUBLE PRECISION )
FROM verse.resource_providers
WHERE planet_id = 1 AND resource_name_id = 1;
SELECT diag_test_name( 'sys.gu_pmc_update_resource( ) - Bounded extraction quantity (2/2)' );
SELECT is( pres_income , 10.0::DOUBLE PRECISION )
FROM verse.planet_resources
WHERE planet_id = 1 AND resource_name_id = 1;
SELECT * FROM finish( );
ROLLBACK;

View file

@ -1,58 +0,0 @@
/*
* Test the sys.process_planet_mining_updates() function
*/
BEGIN;
/*
* Create a fake planet resource record which will be updated by the
* function (dropping the foreign key is therefore needed).
*/
ALTER TABLE verse.planet_resources
DROP CONSTRAINT fk_pres_planet ,
DROP CONSTRAINT fk_pres_resource;
INSERT INTO verse.planet_resources(
planet_id , resource_name_id , pres_income , pres_upkeep
) VALUES (
1 , 1 , 42 , 0
);
/*
* Create a table which contains the values which will be returned by
* the fake sys.gu_pmc_get_data( ).
*/
CREATE TABLE _fake_update_data OF sys.gu_pmc_data_type;
INSERT INTO _fake_update_data VALUES (
1 , 1 , 1000.0 , 1000.0 , 0.5 , NULL , NULL , NULL , NULL ) ,
( 2 , 1 , 1000.0 , 1000.0 , 0.5 , 1 , 0.5 , 1.0 , 1.0 );
CREATE OR REPLACE FUNCTION sys.gu_pmc_get_data( _tick BIGINT )
RETURNS SETOF sys.gu_pmc_data_type
AS $$
SELECT * FROM _fake_update_data;
$$ LANGUAGE SQL;
/*
* Replace sys.gu_pmc_update_resource() so it deletes records from the
* table.
*/
CREATE OR REPLACE FUNCTION sys.gu_pmc_update_resource( _input sys.gu_pmc_data_type )
RETURNS VOID
AS $$
DELETE FROM _fake_update_data WHERE planet = $1.planet AND resource = $1.resource;
$$ LANGUAGE SQL;
/***** TESTS BEGIN HERE *****/
SELECT plan( 2 );
SELECT sys.process_planet_mining_updates( 0 );
SELECT diag_test_name( 'sys.process_planet_mining_updates() - Neutral planets updated' );
SELECT is( pres_income , 0::DOUBLE PRECISION ) FROM verse.planet_resources;
SELECT diag_test_name( 'sys.process_planet_mining_updates() - Owned planets updated' );
SELECT is_empty(
$$ SELECT * FROM _fake_update_data WHERE planet = 2 $$
);
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on emp.scaled_mining_weights_view
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'emp.scaled_mining_weights_view - No SELECT privilege' );
SELECT throws_ok( 'SELECT * FROM emp.scaled_mining_weights_view' , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,11 @@
/*
* Test privileges on emp.total_mining_weights_view
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'emp.total_mining_weights_view - No SELECT privilege' );
SELECT throws_ok( 'SELECT * FROM emp.total_mining_weights_view' , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,13 @@
/*
* Test privileges on emp.mining_compute_extraction()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'emp.mining_compute_extraction() - No EXECUTE privilege' );
SELECT throws_ok( $$
SELECT emp.mining_compute_extraction( ROW( 1 , 2 , 3 , 4 , 5 , NULL , NULL , NULL , NULL ) )
$$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,13 @@
/*
* Test privileges on verse.mining_get_input()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'verse.mining_get_input() - No EXECUTE privilege' );
SELECT throws_ok( $$
SELECT verse.mining_get_input( 1 )
$$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,13 @@
/*
* Test privileges on emp.mining_get_input()
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'emp.mining_get_input() - No EXECUTE privilege' );
SELECT throws_ok( $$
SELECT emp.mining_get_input( 1 )
$$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -1,11 +0,0 @@
/*
* Test privileges on sys.gu_pmc_weights_view
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.gu_pmc_weights_view - Privileges' );
SELECT throws_ok( 'SELECT * FROM sys.gu_pmc_weights_view' , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -1,11 +0,0 @@
/*
* Test privileges on sys.gu_pmc_totals_view
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.gu_pmc_totals_view - Privileges' );
SELECT throws_ok( 'SELECT * FROM sys.gu_pmc_totals_view' , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -1,13 +0,0 @@
/*
* Test privileges on sys.gu_pmc_update_resource( )
*/
BEGIN;
SELECT plan( 1 );
SELECT diag_test_name( 'sys.gu_pmc_update_resource - Privileges' );
SELECT throws_ok( $$
SELECT * FROM sys.gu_pmc_update_resource( ROW( NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL , NULL ) );
$$ , 42501 );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -37,7 +37,7 @@ DELETE FROM sys.update_types
SELECT sys.uoc_constant( 'game.resources.weightBase' , '(test)' , 'Resources' , 10.0 );
SELECT _create_natural_resources( 2 , 'resource' );
SELECT _create_raw_planets( 6 , 'planet' );
SELECT _create_emp_names( 4 , 'empire' );
SELECT _create_emp_names( 5 , 'empire' );
INSERT INTO emp.empires ( name_id , cash )
SELECT id , 0 FROM naming.empire_names;