From a14601df377df096091f8dc0ee950c0cef2f3567 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Thu, 1 Mar 2012 12:42:05 +0100 Subject: [PATCH] Research weight computations * Added game.research.weightBase constant * Added view which computes the weights for each in-progress research * Added view which computes total weights per empire --- .../lw/beans/sys/ConstantsRegistrarBean.java | 6 ++- .../040-functions/045-empire-research.sql | 38 +++++++++++++++++ .../070-research-weights-view.sql | 42 +++++++++++++++++++ .../080-research-total-weights-view.sql | 35 ++++++++++++++++ ...ies-view.sql => 090-technologies-view.sql} | 0 .../070-research-weights-view.sql | 15 +++++++ .../080-research-total-weights-view.sql | 15 +++++++ ...ies-view.sql => 090-technologies-view.sql} | 0 8 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/070-research-weights-view.sql create mode 100644 legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/080-research-total-weights-view.sql rename legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/{070-technologies-view.sql => 090-technologies-view.sql} (100%) create mode 100644 legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/070-research-weights-view.sql create mode 100644 legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/080-research-total-weights-view.sql rename legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/{070-technologies-view.sql => 090-technologies-view.sql} (100%) diff --git a/legacyworlds-server-beans-system/src/main/java/com/deepclone/lw/beans/sys/ConstantsRegistrarBean.java b/legacyworlds-server-beans-system/src/main/java/com/deepclone/lw/beans/sys/ConstantsRegistrarBean.java index 5fd56cc..fe2fbd9 100644 --- a/legacyworlds-server-beans-system/src/main/java/com/deepclone/lw/beans/sys/ConstantsRegistrarBean.java +++ b/legacyworlds-server-beans-system/src/main/java/com/deepclone/lw/beans/sys/ConstantsRegistrarBean.java @@ -128,7 +128,7 @@ public class ConstantsRegistrarBean // Research String[] rcNames = { - "basePoints" , "visibility.points" , "visibility.ratio" + "basePoints" , "visibility.points" , "visibility.ratio" , "weightBase" }; for ( int i = 0 ; i < wcNames.length ; i++ ) { rcNames[ i ] = "game.research." + rcNames[ i ]; @@ -140,6 +140,10 @@ public class ConstantsRegistrarBean defs.add( new ConstantDefinition( rcNames[ 1 ] , cat , cDesc , 2500.0 , 0.01 , true ) ); cDesc = "Completion ratio above which a technology becomes visible."; defs.add( new ConstantDefinition( rcNames[ 2 ] , cat , cDesc , 0.10 , 0.01 , 0.99 ) ); + cDesc = "Technology weight base value. This value is taken to the Xth power (where X is a priority) " + + "to compute the actual weight when determining how research points are distributed between " + + "an empire's in-progress research."; + defs.add( new ConstantDefinition( rcNames[ 3 ] , cat , cDesc , 10.0 , 1.0 , true ) ); // Vacation mode cDesc = "Initial vacation credits."; diff --git a/legacyworlds-server-data/db-structure/parts/040-functions/045-empire-research.sql b/legacyworlds-server-data/db-structure/parts/040-functions/045-empire-research.sql index 0c32b71..2cf07f9 100644 --- a/legacyworlds-server-data/db-structure/parts/040-functions/045-empire-research.sql +++ b/legacyworlds-server-data/db-structure/parts/040-functions/045-empire-research.sql @@ -319,6 +319,44 @@ CREATE VIEW emp.technology_visibility_view +/* + * Research weights + * ----------------- + * + * This view computes weights based on priorities for each in-progress + * research. + * + * Columns: + * empire_id The empire's identifier + * technology_name_id The technology's identifier + * emptech_weight The weight + */ +DROP VIEW IF EXISTS emp.research_weights_view CASCADE; +CREATE VIEW emp.research_weights_view + AS SELECT empire_id , technology_name_id , + POW( sys.get_constant( 'game.research.weightBase' ) , + emptech_priority ) AS emptech_weight + FROM emp.technologies_v2 + WHERE emptech_state = 'RESEARCH'; + +/* + * Total research weights + * ----------------------- + * + * This view computes total research weights for each empire with in-progress + * research. + * + * Columns: + * empire_id The empire's identifier + * emptech_total_weight The total research weight + */ +DROP VIEW IF EXISTS emp.research_total_weights_view CASCADE; +CREATE VIEW emp.research_total_weights_view + AS SELECT empire_id , SUM( emptech_weight ) AS emptech_total_weight + FROM emp.research_weights_view + GROUP BY empire_id; + + /* * Empire research and technologies * --------------------------------- diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/070-research-weights-view.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/070-research-weights-view.sql new file mode 100644 index 0000000..37f9dac --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/070-research-weights-view.sql @@ -0,0 +1,42 @@ +/* + * Test emp.research_weights_view + */ +BEGIN; + /* Remove foreign keys from the empire research table */ + ALTER TABLE emp.technologies_v2 + DROP CONSTRAINT fk_emptech_empire , + DROP CONSTRAINT fk_emptech_technology; + + /* Insert a few records */ + DELETE FROM emp.technologies_v2; + INSERT INTO emp.technologies_v2 ( + empire_id , technology_name_id , + emptech_state , emptech_points , emptech_priority + ) VALUES + ( 1 , 1 , 'RESEARCH' , 0 , 0 ) , + ( 1 , 2 , 'RESEARCH' , 0 , 1 ) , + ( 1 , 3 , 'RESEARCH' , 0 , 2 ) , + ( 1 , 4 , 'RESEARCH' , 0 , 3 ) , + ( 1 , 5 , 'RESEARCH' , 0 , 4 ) , + ( 1 , 6 , 'PENDING' , NULL , NULL ) , + ( 1 , 7 , 'KNOWN' , NULL , NULL ); + + /* Set the constant */ + SELECT sys.uoc_constant( 'game.research.weightBase' , '(test)' , 'Test' , 10.0 ); + + /***** TESTS BEGIN HERE *****/ + SELECT plan( 2 ); + + SELECT diag_test_name( 'emp.research_weights_view - Rows present' ); + SELECT is( COUNT(*)::INT , 5 ) + FROM emp.research_weights_view; + + SELECT diag_test_name( 'emp.research_weights_view - weight = game.research.weightBase ^ priority' ); + SELECT is_empty( $$ + SELECT * FROM emp.research_weights_view + WHERE emptech_weight IS NULL + OR emptech_weight <> POW( 10 , technology_name_id - 1 ) + $$ ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/080-research-total-weights-view.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/080-research-total-weights-view.sql new file mode 100644 index 0000000..6e05a66 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/080-research-total-weights-view.sql @@ -0,0 +1,35 @@ +/* + * Test emp.total_mining_weights_view + */ +BEGIN; + /* Create a table which will server as an alternate source for + * emp.total_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_weights( + empire_id INT , + technology_name_id INT , + emptech_weight DOUBLE PRECISION + ); + + CREATE OR REPLACE VIEW emp.research_weights_view + AS SELECT * FROM fake_weights; + + /* Insert fake records for two different empires */ + INSERT INTO fake_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 emp.research_total_weights_view $$ , + $$ VALUES ( 1 , 3.0 ) , ( 2 , 9.0 ) $$ + ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/070-technologies-view.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/090-technologies-view.sql similarity index 100% rename from legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/070-technologies-view.sql rename to legacyworlds-server-data/db-structure/tests/admin/040-functions/045-empire-research/090-technologies-view.sql diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/070-research-weights-view.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/070-research-weights-view.sql new file mode 100644 index 0000000..807548c --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/070-research-weights-view.sql @@ -0,0 +1,15 @@ +/* + * Test privileges on emp.research_weights_view + */ +BEGIN; + \i utils/strings.sql + + SELECT plan( 1 ); + + SELECT diag_test_name( 'emp.research_weights_view - No SELECT privilege' ); + SELECT throws_ok( $$ + SELECT * FROM emp.research_weights_view; + $$ , 42501 ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/080-research-total-weights-view.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/080-research-total-weights-view.sql new file mode 100644 index 0000000..d2767f6 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/080-research-total-weights-view.sql @@ -0,0 +1,15 @@ +/* + * Test privileges on emp.research_total_weights_view + */ +BEGIN; + \i utils/strings.sql + + SELECT plan( 1 ); + + SELECT diag_test_name( 'emp.research_total_weights_view - No SELECT privilege' ); + SELECT throws_ok( $$ + SELECT * FROM emp.research_total_weights_view; + $$ , 42501 ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/070-technologies-view.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/090-technologies-view.sql similarity index 100% rename from legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/070-technologies-view.sql rename to legacyworlds-server-data/db-structure/tests/user/040-functions/045-empire-research/090-technologies-view.sql