From 37555841ce142d3f2fa4a0767864fff6219b85b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Tue, 10 Jan 2012 10:48:56 +0100 Subject: [PATCH] Empire resources initialisation * Empire resources will be initialised when an empire is created * When a new resource is created through the appropriate defs.uoc_... function, it will be added to all empires as well. --- .../parts/040-functions/025-resources.sql | 12 +++++++++ .../parts/040-functions/040-empire.sql | 4 +++ .../010-uoc-resource-internal.sql | 17 ++++++++++++- .../025-resources/020-uoc-resource.sql | 23 ++++++++++++++++- .../025-resources/030-uoc-natres-internal.sql | 18 ++++++++++++- .../040-uoc-natural-resource.sql | 25 ++++++++++++++++++- .../040-empire/010-create-empire.sql | 18 +++++++------ 7 files changed, 106 insertions(+), 11 deletions(-) diff --git a/legacyworlds-server-data/db-structure/parts/040-functions/025-resources.sql b/legacyworlds-server-data/db-structure/parts/040-functions/025-resources.sql index 053f1f1..0937af2 100644 --- a/legacyworlds-server-data/db-structure/parts/040-functions/025-resources.sql +++ b/legacyworlds-server-data/db-structure/parts/040-functions/025-resources.sql @@ -102,6 +102,12 @@ BEGIN ) VALUES ( _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; + RETURN 'CREATED'; EXCEPTION WHEN unique_violation THEN @@ -312,6 +318,12 @@ BEGIN _difficulty_avg , _difficulty_dev , _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; + RETURN 'CREATED'; END IF; diff --git a/legacyworlds-server-data/db-structure/parts/040-functions/040-empire.sql b/legacyworlds-server-data/db-structure/parts/040-functions/040-empire.sql index 67204c1..7963876 100644 --- a/legacyworlds-server-data/db-structure/parts/040-functions/040-empire.sql +++ b/legacyworlds-server-data/db-structure/parts/040-functions/040-empire.sql @@ -42,6 +42,10 @@ BEGIN SELECT _name_id , _resource.resource_name_id FROM defs.natural_resources _resource; + -- Add empire resources + INSERT INTO emp.resources ( empire_id , resource_name_id ) + SELECT _name_id , resource_name_id FROM defs.resources; + -- Add empire update records FOR _update_type IN SELECT _type FROM unnest( enum_range( NULL::update_type ) ) AS _type diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/010-uoc-resource-internal.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/010-uoc-resource-internal.sql index 10bee48..9dc1a03 100644 --- a/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/010-uoc-resource-internal.sql +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/010-uoc-resource-internal.sql @@ -10,7 +10,7 @@ BEGIN; SELECT _create_natural_resources( 1 , 'natRes' ); /****** TESTS BEGIN HERE ******/ - SELECT plan( 15 ); + SELECT plan( 16 ); SELECT diag_test_name( 'defs.uoc_resource_internal() - creation without category' ); SELECT is( defs.uoc_resource_internal( 'test3' , 'test4' , NULL , 1 ) , 'CREATED' ); @@ -69,6 +69,21 @@ BEGIN; SELECT diag_test_name( 'defs.uoc_resource_internal() - weight <= 0' ); SELECT is( defs.uoc_resource_internal( 'test1' , 'test2' , NULL , 0 ) , 'BAD_VALUE' ); + + /* Reset resources, create empire */ + DELETE FROM defs.natural_resources; + DELETE FROM defs.resources; + ALTER TABLE emp.empires + DROP CONSTRAINT fk_empires_name; + INSERT INTO emp.empires ( name_id , cash ) + VALUES ( 1 , 0 ); + + SELECT diag_test_name( 'defs.uoc_resource_internal() - new resources are added to empires' ); + SELECT defs.uoc_resource_internal( 'test3' , 'test4' , NULL , 1 ); + SELECT is( COUNT(*)::INT , 1 ) + FROM emp.resources + WHERE empire_id = 1 + AND resource_name_id = _get_string( 'test3' ); SELECT * FROM finish( ); ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/020-uoc-resource.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/020-uoc-resource.sql index 113634d..3b08cfd 100644 --- a/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/020-uoc-resource.sql +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/020-uoc-resource.sql @@ -10,7 +10,7 @@ BEGIN; SELECT _create_natural_resources( 1 , 'natRes' ); /****** TESTS BEGIN HERE ******/ - SELECT plan( 22 ); + SELECT plan( 24 ); SELECT diag_test_name( 'defs.uoc_resource() - NULL name (no category)' ); SELECT is( defs.uoc_resource( NULL , 'test2' , 1 ) , NULL ); @@ -86,5 +86,26 @@ BEGIN; SELECT diag_test_name( 'defs.uoc_resource() - weight <= 0' ); SELECT is( defs.uoc_resource( 'natRes1' , 'test2' , 0 ) , 'BAD_VALUE' ); + /* Reset resources, create empire */ + DELETE FROM defs.natural_resources; + DELETE FROM defs.resources; + ALTER TABLE emp.empires + DROP CONSTRAINT fk_empires_name; + INSERT INTO emp.empires ( name_id , cash ) + VALUES ( 1 , 0 ); + + SELECT diag_test_name( 'defs.uoc_resource() - new resources are added to empires (no category)' ); + SELECT defs.uoc_resource( 'test1' , 'test2' , 1 ); + SELECT is( COUNT(*)::INT , 1 ) + FROM emp.resources + WHERE empire_id = 1 + AND resource_name_id = _get_string( 'test1' ); + SELECT diag_test_name( 'defs.uoc_resource() - new resources are added to empires' ); + SELECT defs.uoc_resource( 'test3' , 'test4' , 'test5' , 1 ); + SELECT is( COUNT(*)::INT , 1 ) + FROM emp.resources + WHERE empire_id = 1 + AND resource_name_id = _get_string( 'test3' ); + SELECT * FROM finish( ); ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/030-uoc-natres-internal.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/030-uoc-natres-internal.sql index d442020..79f5a3b 100644 --- a/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/030-uoc-natres-internal.sql +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/030-uoc-natres-internal.sql @@ -10,7 +10,7 @@ BEGIN; SELECT _create_resources( 1 , 'basicRes' ); /****** TESTS BEGIN HERE ******/ - SELECT plan( 37 ); + SELECT plan( 38 ); SELECT diag_test_name( 'defs.uoc_natres_internal() - creation without category' ); SELECT is( defs.uoc_natres_internal( 'test3' , 'test4' , NULL , 1 , @@ -171,5 +171,21 @@ BEGIN; SELECT is( defs.uoc_natres_internal( 'test5' , 'test2' , NULL , 1 , 0.5 , 100 , 50 , 0.5 , 0.05 , 0.75 , 0.5 ) , 'BAD_VALUE' ); + /* Reset resources, create empire */ + DELETE FROM defs.natural_resources; + DELETE FROM defs.resources; + ALTER TABLE emp.empires + DROP CONSTRAINT fk_empires_name; + INSERT INTO emp.empires ( name_id , cash ) + VALUES ( 1 , 0 ); + + SELECT diag_test_name( 'defs.uoc_natres_internal() - new resources are added to empires' ); + SELECT defs.uoc_natres_internal( 'test1' , 'test2' , NULL , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ); + SELECT is( COUNT(*)::INT , 1 ) + FROM emp.resources + WHERE empire_id = 1 + AND resource_name_id = _get_string( 'test1' ); + SELECT * FROM finish( ); ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/040-uoc-natural-resource.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/040-uoc-natural-resource.sql index 3f8f119..87889fe 100644 --- a/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/040-uoc-natural-resource.sql +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/025-resources/040-uoc-natural-resource.sql @@ -10,7 +10,7 @@ BEGIN; SELECT _create_resources( 1 , 'basicRes' ); /****** TESTS BEGIN HERE ******/ - SELECT plan( 58 ); + SELECT plan( 60 ); SELECT diag_test_name( 'defs.uoc_natural_resource() - NULL name (no category)' ); @@ -238,5 +238,28 @@ BEGIN; SELECT is( defs.uoc_natural_resource( 'test5' , 'test2' , 1 , 0.5 , 100 , 50 , 0.5 , 0.05 , 0.75 , 0.5 ) , 'BAD_VALUE' ); + /* Reset resources, create empire */ + DELETE FROM defs.natural_resources; + DELETE FROM defs.resources; + ALTER TABLE emp.empires + DROP CONSTRAINT fk_empires_name; + INSERT INTO emp.empires ( name_id , cash ) + VALUES ( 1 , 0 ); + + SELECT diag_test_name( 'defs.uoc_natural_resource() - new resources are added to empires (no category)' ); + SELECT defs.uoc_natural_resource( 'test1' , 'test2' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ); + SELECT is( COUNT(*)::INT , 1 ) + FROM emp.resources + WHERE empire_id = 1 + AND resource_name_id = _get_string( 'test1' ); + SELECT diag_test_name( 'defs.uoc_natural_resource() - new resources are added to empires' ); + SELECT defs.uoc_natural_resource( 'test3' , 'test4' , 'test5' , 1 , + 0.5 , 100 , 50 , 0.5 , 0.05 , 0.5 , 0.05 ); + SELECT is( COUNT(*)::INT , 1 ) + FROM emp.resources + WHERE empire_id = 1 + AND resource_name_id = _get_string( 'test3' ); + SELECT * FROM finish( ); ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/040-empire/010-create-empire.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/040-empire/010-create-empire.sql index 55f1b48..72671df 100644 --- a/legacyworlds-server-data/db-structure/tests/admin/040-functions/040-empire/010-create-empire.sql +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/040-empire/010-create-empire.sql @@ -16,7 +16,7 @@ BEGIN; SELECT _create_emp_names( 1 , 'testEmp' ); /***** TESTS BEGIN HERE *****/ - SELECT plan( 7 ); + SELECT plan( 8 ); SELECT emp.create_empire( _get_emp_name( 'testEmp1' ) , _get_map_name( 'testPlanet1' ) , @@ -46,12 +46,16 @@ BEGIN; 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 mining settings are all set to 2' ); + SELECT is( COUNT( * )::INT , 0 ) + FROM emp.mining_settings + WHERE empire_id = _get_emp_name( 'testEmp1' ) + AND empmset_weight <> 2; + + SELECT diag_test_name( 'emp.create_empire() - Empire resources have been initialised' ); + SELECT is( COUNT(*)::INT , 3 ) + FROM emp.resources + WHERE empire_id = _get_emp_name( 'testEmp1' ); SELECT diag_test_name( 'emp.create_empire() - Empire update records' ); SELECT is( _eur.quantity , _utv.quantity)