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

@ -1,44 +0,0 @@
/*
* Test sys.gu_pmc_weights_view
*/
BEGIN;
/* Create a table which will server as an alternate source for
* emp.mining_settings_view ; the table is not temporary (PostgreSQL
* won't allow replacing the view otherwise), but will be dropped
* on rollback anyway.
*/
CREATE TABLE fake_mining_settings(
planet_id INT ,
resource_name_id INT ,
mset_weight INT ,
mset_specific BOOLEAN
);
CREATE OR REPLACE VIEW emp.mining_settings_view
AS SELECT * FROM fake_mining_settings;
/* Insert fake records for each possible mining setting */
INSERT INTO fake_mining_settings VALUES
( 1 , 0 , 0 , FALSE ) ,
( 1 , 1 , 1 , FALSE ) ,
( 1 , 2 , 2 , FALSE ) ,
( 1 , 3 , 3 , FALSE ) ,
( 1 , 4 , 4 , FALSE );
/***** TESTS BEGIN HERE *****/
SELECT plan( 2 );
SELECT diag_test_name( 'sys.gu_pmc_weights_view - Rows present' );
SELECT isnt( COUNT(*)::INT , 0 )
FROM sys.gu_pmc_weights_view;
SELECT diag_test_name( 'sys.gu_pmc_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
WHERE pmc_weight IS NULL
OR pmc_weight <> POW( 10 , resource_name_id )
$$ );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -1,35 +0,0 @@
/*
* Test sys.gu_pmc_totals_view
*/
BEGIN;
/* Create a table which will server as an alternate source for
* sys.gu_pmc_weights_view ; the table is not temporary (PostgreSQL
* won't allow replacing the view otherwise), but will be dropped
* on rollback anyway.
*/
CREATE TABLE fake_mining_weights(
planet_id INT ,
resource_name_id INT ,
pmc_weight DOUBLE PRECISION
);
CREATE OR REPLACE VIEW sys.gu_pmc_weights_view
AS SELECT * FROM fake_mining_weights;
/* Insert fake records for two different planets */
INSERT INTO fake_mining_weights VALUES
( 1 , 0 , 1 ) ,
( 1 , 1 , 2 ) ,
( 2 , 0 , 4 ) ,
( 2 , 1 , 5 );
/***** TESTS BEGIN HERE *****/
SELECT plan( 1 );
SELECT set_eq(
$$ SELECT * FROM sys.gu_pmc_totals_view $$ ,
$$ VALUES ( 1 , 3.0 ) , ( 2 , 9.0 ) $$
);
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;