Page 1 of 1

Changing Values in $game_options

Posted: 26 June 2020, 20:44
by MikeIsHere
What is the best practice for changing the values of $game_options

I would like to change this

Code: Select all

    100 => array(
            'name' => totranslate('Game length'),
            'values' => array(                    
                    1 => array( 'name' => totranslate( 'Standard game (121 points)' ) ),
                    2 => array( 'name' => totranslate( 'Quick game (61 points)' ) ),
                    3 => array( 'name' => totranslate( 'Debug game (31 points)' ) ),
            ),
            'default' => 1
        ),
Change the 1->121, 2->61, 3->31 It would take away some gnarly if statements. My game is in Alpha, and I know that it doesn't matter much, but I am also trying to learn what would happen if I was in production and how to handle this change with grace.

Re: Changing Values in $game_options

Posted: 26 June 2020, 21:08
by RicardoRix
how about a lookup array. 1=> 121, 2 => 61, 3 => 31.
or more simply (121,121,61,31);

Re: Changing Values in $game_options

Posted: 26 June 2020, 22:01
by Brainchild
You can also replace the hard-coded values in gameoptions.inc with PHP defines.

Code: Select all

// Variant IDs
if (!defined("OPTION_GAME_LENGTH")) // ensure this block is only invoked once, since it is included multiple times
{
    define("OPTION_GAME_LENGTH", 100);
        define("GAME_LENGTH_STANDARD", 1);
        define("GAME_LENGTH_QUICK", 2);
        define("GAME_LENGTH_DEBUG", 3);
}
And wrap it in a method in your.game.php.

Code: Select all

    function getNumPointsToWinGame()
    {
        $num_points_to_win_game = 121;

        switch ($this->gamestate->table_globals[OPTION_GAME_LENGTH])
        {
            case GAME_LENGTH_QUICK:
                $num_points_to_win_game = 61;
                break;

            case GAME_LENGTH_DEBUG:
                $num_points_to_win_game = 31;
                break;
        }
        
        return $num_points_to_win_game;
    }
No gnarly if-statements!

Re: Changing Values in $game_options

Posted: 26 June 2020, 23:27
by Tisaac
I can only emphasize the advice from Brainchild : you should use a lot of defines, that makes the code much easier to read.
Use defiines for gameoption, for states, for other enums, ...

Re: Changing Values in $game_options

Posted: 28 June 2020, 08:47
by LaszloK
Like others said, leverage associative arrays and set the key to be the value you want. In Tarokk I used an option like this:

Code: Select all

        'name' => totranslate( 'Game length' ),
        'values' => [
            4 => [ 'name' => totranslate( 'Very short - 4 hands' ) ],
            8 => [ 'name' => totranslate( 'Short - 8 hands' ) ],
            12 => [ 'name' => totranslate( 'Medium - 12 hands' ) ],
            20 => [ 'name' => totranslate( 'Long - 20 hands' ) ],
            32 => [ 'name' => totranslate( 'Very long - 32 hands' ) ]
        ],
Then get the value of the option and use it directly as the number of hands to play (or in your case, points to win).

Re: Changing Values in $game_options

Posted: 28 June 2020, 13:36
by MikeIsHere
Then get the value of the option and use it directly as the number of hands to play (or in your case, points to win).
This is what I want to move towards but since I already implemented as 1 2 3. So my question is how can I change my array and still support backwards compatibility

Re: Changing Values in $game_options

Posted: 28 June 2020, 21:36
by Tisaac
The easiest would probably be to define a custom method with a lookup array inside :

Code: Select all

public function getGameLength(){
  $option = self::getGameStateValue('gameLength');
  $tab = [1 => 121, 2 => 61, 3 => 31];
   return $tab[$option]; // Maybe add some check here
 }