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 75d3a9c..23bdad8 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 @@ -94,7 +94,7 @@ public class ConstantsRegistrarBean cDesc = "Empire size limit (relative to the ideal size) beyond which things get tough."; defs.add( new ConstantDefinition( hcNames[ 7 ] , cat , cDesc , 2.0 , 1.1 , true ) ); cDesc = "Happiness level below which strikes begin."; - defs.add( new ConstantDefinition( hcNames[ 8 ] , cat , cDesc , 0.25 , 0.0 , 1.0 ) ); + defs.add( new ConstantDefinition( hcNames[ 8 ] , cat , cDesc , 0.5 , 0.0 , 1.0 ) ); cDesc = "Happiness change at each update, relative to the size of the population."; defs.add( new ConstantDefinition( hcNames[ 9 ] , cat , cDesc , 0.001 , 0.00001 , 0.99999 ) ); cDesc = "Maximal population units for which happiness will change."; diff --git a/legacyworlds-server-data/db-structure/parts/040-functions/050-computation.sql b/legacyworlds-server-data/db-structure/parts/040-functions/050-computation.sql index ebc8ed6..0d6c1bb 100644 --- a/legacyworlds-server-data/db-structure/parts/040-functions/050-computation.sql +++ b/legacyworlds-server-data/db-structure/parts/040-functions/050-computation.sql @@ -301,21 +301,45 @@ $$ LANGUAGE plpgsql; --- --- Production adjustment --- -CREATE OR REPLACE FUNCTION verse.adjust_production( prod REAL , happiness REAL ) +/* + * Production adjustment + * ---------------------- + * + * Adjust productions depending on a planet's happiness ratio. When the + * happiness ratio is greater than the game.happiness.strike constant, the + * production is not affected. However, under that threshold, the production + * decreases proportionally, until it reaches 0. + * + * Parameters: + * _prod The production's valule + * _h_ratio The happiness ratio + * + * Returns: + * ? The adjusted production value + */ +DROP FUNCTION IF EXISTS verse.adjust_production( REAL , REAL ) CASCADE; +CREATE OR REPLACE FUNCTION verse.adjust_production( _prod REAL , _h_ratio REAL ) RETURNS REAL + LANGUAGE SQL STRICT IMMUTABLE SECURITY INVOKER -AS $$ +AS $adjust_production$ + SELECT ( CASE WHEN $2 < sys.get_constant( 'game.happiness.strike' ) THEN - ( $1 * ( 1 - ( $2 / sys.get_constant( 'game.happiness.strike' ) ) ) )::REAL + ( $1 * $2 / sys.get_constant( 'game.happiness.strike' ) )::REAL ELSE $1 END ); -$$ LANGUAGE SQL; + +$adjust_production$; + +REVOKE EXECUTE + ON FUNCTION verse.adjust_production( REAL , REAL ) + FROM PUBLIC; +GRANT EXECUTE + ON FUNCTION verse.adjust_production( REAL , REAL ) + TO :dbuser; -- diff --git a/legacyworlds-server-data/db-structure/tests/admin/040-functions/050-computation/020-adjust-production.sql b/legacyworlds-server-data/db-structure/tests/admin/040-functions/050-computation/020-adjust-production.sql new file mode 100644 index 0000000..7059de1 --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/admin/040-functions/050-computation/020-adjust-production.sql @@ -0,0 +1,51 @@ +/* + * Test the verse.adjust_production() function + */ +BEGIN; + -- Create a table of input values + CREATE TABLE test_input( + prod REAL , + happ REAL + ); + + -- Set the happiness strike level to 50% + SELECT sys.uoc_constant( 'game.happiness.strike' , '(test)' , 'Happiness' , 0.5 ); + + -- ***** TESTS BEGIN HERE ***** + SELECT plan( 3 ); + + INSERT INTO test_input VALUES + ( 10 , 1 ) , ( 100 , 1 ) , + ( 10 , 0.9 ) , ( 100 , 0.9 ) , + ( 10 , 0.8 ) , ( 100 , 0.8 ) , + ( 10 , 0.7 ) , ( 100 , 0.7 ) , + ( 10 , 0.6 ) , ( 100 , 0.6 ) , + ( 10 , 0.5 ) , ( 100 , 0.5 ); + SELECT diag_test_name( 'verse.adjust_production() - Production is unaffected when happiness >= game.happiness.strike' ); + SELECT is_empty( + $$ SELECT * FROM test_input WHERE prod <> verse.adjust_production( prod , happ ); $$ + ); + DELETE FROM test_input; + + INSERT INTO test_input VALUES + ( 10 , 0.5 ) , ( 10 , 0.4 ) , + ( 10 , 0.3 ) , ( 10 , 0.2 ) , + ( 10 , 0.1 ) , ( 10 , 0 ); + SELECT diag_test_name( 'verse.adjust_production() - Production is unaffected when happiness >= game.happiness.strike' ); + SELECT set_eq( + $$ SELECT rank( ) OVER( ORDER BY happ ) AS r1 , + rank( ) OVER ( ORDER BY verse.adjust_production( prod , happ ) ) AS r2 + FROM test_input; $$ , + $$ VALUES ( 1 , 1 ) , ( 2 , 2 ) , ( 3 , 3 ) , ( 4 , 4 ) , ( 5 , 5 ) , ( 6 , 6 ) $$ + ); + DELETE FROM test_input; + + SELECT diag_test_name( 'verse.adjust_production() - Happiness = 0 => production = 0' ); + INSERT INTO test_input VALUES + ( 1 , 0 ) , ( 10 , 0 ) , ( 100 , 0 ) , ( 1000 , 0 ) , ( 10000 , 0 ); + SELECT is_empty( + $$ SELECT * FROM test_input WHERE verse.adjust_production( prod , happ ) <> 0; $$ + ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file diff --git a/legacyworlds-server-data/db-structure/tests/user/040-functions/050-computation/020-adjust-production.sql b/legacyworlds-server-data/db-structure/tests/user/040-functions/050-computation/020-adjust-production.sql new file mode 100644 index 0000000..95ecf3e --- /dev/null +++ b/legacyworlds-server-data/db-structure/tests/user/040-functions/050-computation/020-adjust-production.sql @@ -0,0 +1,12 @@ +/* + * Test privileges on verse.adjust_production() + */ +BEGIN; + + SELECT plan( 1 ); + + SELECT diag_test_name( 'verse.adjust_production( ) - EXECUTE privilege' ); + SELECT lives_ok( $$ SELECT verse.adjust_production( 1 , 1 ) $$ ); + + SELECT * FROM finish( ); +ROLLBACK; \ No newline at end of file