Planet resources

* Added missing table that should store a planet's resources data
(income and upkeep for each type of resource).

* Modified resource definition functions and universe generator to
initialise planet resource records as well

* Heavy clean-up in resource definition function unit tests
This commit is contained in:
Emmanuel BENOîT 2012-01-10 12:30:47 +01:00
parent 37555841ce
commit b49bc1a44f
11 changed files with 465 additions and 378 deletions
legacyworlds-server-data/db-structure/parts

View file

@ -135,6 +135,52 @@ ALTER TABLE verse.planet_money
FOREIGN KEY (planet_id) REFERENCES verse.planets;
/*
* Planet resource changes
* ------------------------
*
* This table stores the results of planet resource updates. It will then be
* used to update the owning empires' resources.
*
* This table applies to both basic and natural resources.
*/
CREATE TABLE verse.planet_resources(
/* Identifier of the planet */
planet_id INT NOT NULL ,
/* Identifier of the resource type */
resource_name_id INT NOT NULL ,
/* Quantity of that resource which was somehow gained at the last game
* update.
*/
pres_income DOUBLE PRECISION NOT NULL
DEFAULT 0
CHECK( pres_income >= 0 ) ,
/* Quantity of that resource used by the planet's various buildings at the
* last game update.
*/
pres_upkeep DOUBLE PRECISION NOT NULL
DEFAULT 0
CHECK( pres_upkeep >= 0 ) ,
/* Primary key on (planet,type of resource) */
PRIMARY KEY( planet_id , resource_name_id )
);
CREATE INDEX idx_pres_resource
ON verse.planet_resources( resource_name_id );
ALTER TABLE verse.planet_resources
ADD CONSTRAINT fk_pres_planet
FOREIGN KEY ( planet_id ) REFERENCES verse.planets ,
ADD CONSTRAINT fk_pres_resource
FOREIGN KEY ( resource_name_id ) REFERENCES defs.resources;
--
-- Buildings
--

View file

@ -38,6 +38,35 @@ CREATE TYPE defs.resource_update_result
);
/*
* Add a resource to all empire and planet records
*
* This function makes sure that all empire and planet records have a row for
* a newly-created resource.
*
* Parameters:
* _resource The identifier of the resource to add
*/
DROP FUNCTION IF EXISTS defs.add_resource_records( INT );
CREATE FUNCTION defs.add_resource_records( _resource INT )
RETURNS VOID
STRICT VOLATILE
SECURITY INVOKER
AS $$
INSERT INTO emp.resources ( empire_id , resource_name_id )
SELECT name_id , $1 FROM emp.empires;
INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
SELECT name_id , $1 FROM verse.planets;
$$ LANGUAGE SQL;
REVOKE EXECUTE
ON FUNCTION defs.add_resource_records( INT )
FROM PUBLIC;
/*
* Create or update a basic resource
@ -60,7 +89,8 @@ CREATE TYPE defs.resource_update_result
* Returns:
* ? the result code for the operation
*/
CREATE OR REPLACE FUNCTION defs.uoc_resource_internal(
DROP FUNCTION IF EXISTS defs.uoc_resource_internal( TEXT , TEXT , TEXT , INT );
CREATE FUNCTION defs.uoc_resource_internal(
_name TEXT ,
_description TEXT ,
_category TEXT ,
@ -103,11 +133,7 @@ BEGIN
_name_id , _desc_id , _cat_id , _weight
);
-- Add the resource to all empires
INSERT INTO emp.resources ( empire_id , resource_name_id )
SELECT name_id , _name_id
FROM emp.empires;
PERFORM defs.add_resource_records( _name_id );
RETURN 'CREATED';
EXCEPTION
WHEN unique_violation THEN
@ -163,7 +189,8 @@ REVOKE EXECUTE
* Returns:
* ? the result code for the operation
*/
CREATE OR REPLACE FUNCTION defs.uoc_resource(
DROP FUNCTION IF EXISTS defs.uoc_resource( TEXT , TEXT , INT );
CREATE FUNCTION defs.uoc_resource(
_name TEXT ,
_description TEXT ,
_weight INT )
@ -196,7 +223,8 @@ GRANT EXECUTE
* Returns:
* ? the result code for the operation
*/
CREATE OR REPLACE FUNCTION defs.uoc_resource(
DROP FUNCTION IF EXISTS defs.uoc_resource( TEXT , TEXT , TEXT , INT );
CREATE FUNCTION defs.uoc_resource(
_name TEXT ,
_description TEXT ,
_category TEXT ,
@ -246,7 +274,11 @@ GRANT EXECUTE
* Returns:
* ? the result code for the operation
*/
CREATE OR REPLACE FUNCTION defs.uoc_natres_internal(
DROP FUNCTION IF EXISTS defs.uoc_natres_internal( TEXT , TEXT , TEXT , INT ,
DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION ,
DOUBLE PRECISION , DOUBLE PRECISION , DOUBLE PRECISION ,
DOUBLE PRECISION );
CREATE FUNCTION defs.uoc_natres_internal(
_name TEXT ,
_description TEXT ,
_category TEXT ,
@ -319,11 +351,7 @@ BEGIN
_recovery_avg , _recovery_dev
);
-- Add the resource to all empires
INSERT INTO emp.resources ( empire_id , resource_name_id )
SELECT name_id , _name_id
FROM emp.empires;
PERFORM defs.add_resource_records( _name_id );
RETURN 'CREATED';
END IF;

View file

@ -193,6 +193,10 @@ BEGIN
verse.get_raw_production( pnid , 'CASH'::building_output_type )
) , verse.get_planet_upkeep( pnid ) );
-- FIXME: for now, just stick data about resources in the appropriate table
INSERT INTO verse.planet_resources ( planet_id , resource_name_id )
SELECT pnid , resource_name_id FROM defs.resources;
-- Add planet update records
FOR utp IN SELECT x FROM unnest( enum_range( NULL::update_type ) ) AS x
WHERE x::text LIKE 'PLANET_%'