Production adjustment fix

* The production adjustment function was completely off (and always has
been). It has been fixed.

* The problem with the adjustment function not being reported means that
no-one ever encountered it. As a consequence, happiness strike threshold
has been increased to 50%

/!\ Full database reset required (or at least much easier than other
options).
This commit is contained in:
Emmanuel BENOîT 2012-02-08 15:38:12 +01:00
parent c94958a058
commit afa1224391
4 changed files with 95 additions and 8 deletions

View file

@ -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.";

View file

@ -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;
--

View file

@ -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;

View file

@ -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;