Mining computation update

* Added various views and helper functions used by the mining
computation but which may be re-used in other parts.

* Added mining computation update type and associated update function

* New constants: game.resources.weightBase (the value used to compute
weights from mining settings) and game.resources.extraction (the
quantity extracted in a day from a full provider at difficulty 0)
This commit is contained in:
Emmanuel BENOîT 2012-01-16 12:35:20 +01:00
parent 038bba896a
commit 74b6f2ab09
20 changed files with 1139 additions and 2 deletions

View file

@ -0,0 +1,89 @@
/*
* Test the verse.get_extraction_factor() function
*/
BEGIN;
/* Drop foreign keys on resource providers, then fill the table with
* values which can be used to test some fundamental properties of
* the computation, then create a view that returns computation results.
*/
ALTER TABLE verse.resource_providers
DROP CONSTRAINT fk_resprov_planet ,
DROP CONSTRAINT fk_resprov_resource;
CREATE FUNCTION _fill_providers( )
RETURNS VOID
AS $$
DECLARE
i INT;
j INT;
BEGIN
FOR i IN 0 .. 10
LOOP
FOR j IN 0 .. 10
LOOP
INSERT INTO verse.resource_providers(
planet_id , resource_name_id ,
resprov_quantity_max , resprov_quantity ,
resprov_difficulty , resprov_recovery
) VALUES (
i , j , 100 , j * 10 , i * 0.1 , 0.5
);
END LOOP;
END LOOP;
END;
$$ LANGUAGE PLPGSQL;
SELECT _fill_providers( );
DROP FUNCTION _fill_providers( );
CREATE VIEW extraction_factor_view
AS SELECT planet_id , resource_name_id ,
verse.get_extraction_factor(
resprov_quantity / resprov_quantity_max ,
resprov_difficulty
) AS resprov_extraction
FROM verse.resource_providers;
/***** TESTS BEGIN HERE *****/
SELECT plan( 6 );
SELECT diag_test_name( 'verse.get_extraction_factor() - Range');
SELECT is_empty($$
SELECT * FROM extraction_factor_view
WHERE resprov_extraction NOT BETWEEN 0 AND 1
$$);
SELECT diag_test_name( 'verse.get_extraction_factor() - Full provider with difficulty 0 => extraction is 1');
SELECT is( resprov_extraction , 1.0::DOUBLE PRECISION )
FROM extraction_factor_view
WHERE planet_id = 0 AND resource_name_id = 10;
SELECT diag_test_name( 'verse.get_extraction_factor() - Full provider with difficulty 1 => extraction is 0.5');
SELECT is( resprov_extraction , 0.5::DOUBLE PRECISION )
FROM extraction_factor_view
WHERE planet_id = 10 AND resource_name_id = 10;
SELECT diag_test_name( 'verse.get_extraction_factor() - Empty provider => extraction is 0');
SELECT is_empty($$
SELECT * FROM extraction_factor_view
WHERE resource_name_id = 0 AND resprov_extraction > 0
$$);
SELECT diag_test_name( 'verse.get_extraction_factor() - At same quantity ratio, higher difficulty => lower extraction');
SELECT is_empty($$
SELECT * FROM extraction_factor_view v1
INNER JOIN extraction_factor_view v2
ON v1.resource_name_id = v2.resource_name_id
WHERE v1.planet_id > v2.planet_id
AND v1.resprov_extraction > v2.resprov_extraction
$$);
SELECT diag_test_name( 'verse.get_extraction_factor() - At same difficulty, higher quantity ratio => higher extraction');
SELECT is_empty($$
SELECT * FROM extraction_factor_view v1
INNER JOIN extraction_factor_view v2
ON v1.planet_id = v2.planet_id
WHERE v1.resource_name_id > v2.resource_name_id
AND v1.resprov_extraction < v2.resprov_extraction
$$);
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,44 @@
/*
* 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

@ -0,0 +1,35 @@
/*
* 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.gu_pmc_get_data() function
*/
BEGIN;
\i utils/common-setup/setup-gu-pmc-get-data-test.sql
/* Select results into a temporary table */
CREATE TEMPORARY TABLE test_results
AS SELECT * FROM sys.gu_pmc_get_data( 0 );
/***** TESTS BEGIN HERE *****/
SELECT no_plan( );
SELECT diag_test_name( 'sys.gu_pmc_get_data() - Neutral planet without resource providers not included' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE planet = _get_map_name ( 'planet1' );
$$ );
SELECT diag_test_name( 'sys.gu_pmc_get_data() - 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( 'sys.gu_pmc_get_data() - Neutral planet with resource providers - No extra rows' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE planet = _get_map_name ( 'planet2' )
AND NOT ( difficulty = 0.2 AND empire IS NULL
AND happiness IS NULL AND weight IS NULL
AND total_weight IS NULL );
$$ );
SELECT diag_test_name( 'sys.gu_pmc_get_data() - 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( 'sys.gu_pmc_get_data() - Planet using empire settings - No extra rows' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE planet = _get_map_name ( 'planet3' )
AND NOT ( 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( 'sys.gu_pmc_get_data() - 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( 'sys.gu_pmc_get_data() - Planet using specific settings - No extra rows' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE planet = _get_map_name ( 'planet4' )
AND NOT ( 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 ) ) );
$$ );
SELECT diag_test_name( 'sys.gu_pmc_get_data() - Owned planet without resource providers not included' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE planet = _get_map_name ( 'planet5' );
$$ );
SELECT diag_test_name( 'sys.gu_pmc_get_data() - Planet matching all criterias but marked as processed not included' );
SELECT is_empty( $$
SELECT * FROM test_results
WHERE planet = _get_map_name ( 'planet6' );
$$ );
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,91 @@
/*
* 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.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( ) - Planet resources income udpated' );
SELECT is( pres_income , 1.0::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 , 0.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 , 1000.0::DOUBLE PRECISION )
FROM verse.planet_resources
WHERE planet_id = 1 AND resource_name_id = 1;
SELECT * FROM finish( );
ROLLBACK;

View file

@ -0,0 +1,58 @@
/*
* 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 no_plan( );
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;