This is a documentation for Board Game Arena: play board games online !

Main game logic: Game.php

From Board Game Arena
Jump to navigation Jump to search


Game File Reference



Useful Components

Official

  • Deck: a PHP component to manage cards (deck, hands, picking cards, moving cards, shuffle deck, ...).
  • Draggable: a JS component to manage drag'n'drop actions.
  • Counter: a JS component to manage a counter that can increase/decrease (ex: player's score).
  • ExpandableSection: a JS component to manage a rectangular block of HTML than can be displayed/hidden.
  • Scrollmap: a JS component to manage a scrollable game area (useful when the game area can be infinite. Examples: Saboteur or Takenoko games).
  • Stock: a JS component to manage and display a set of game elements displayed at a position.
  • Zone: a JS component to manage a zone of the board where several game elements can come and leave, but should be well displayed together (See for example: token's places at Can't Stop).

Undocumented component (if somebody knows please help with docs)

  • Wrapper: a JS component to wrap a <div> element around its child, even if these elements are absolute positioned.

Unofficial



Game Development Process



Guides for Common Topics



Miscellaneous Resources

This is the main file for your game logic. Here you initialize the game, persist data, implement the rules and notify the client interface of changes.

This is the main class that implements the "server" callbacks. As it is a server it cannot initiate any data communicate with the game client (running in browser) and only can respond to client using notifications.

Your php class instance won't be in memory between two callbacks, every time client send a request a new class will be created, constructor will be called and eventually your callback function.

File Structure

The details of how the file is structured are described directly with comments in the code skeleton provided to you.

Here is the basic structure:

  • __construct: the game constructor, where you define global variables and initiaze class members.
  • setupNewGame: initial setup of the game. Takes an array of players, indexed by player_id. Structure of each player includes player_name, player_canal, player_avatar, and flags indicating admin/ai/premium/order/language/beginner.
  • getAllDatas: where you retrieve all game data during a complete reload of the game. Return value must be associative array. Value of 'players' is reserved for returning players data from players table, if you set it it must follow certain rules
       $result ['players'] = $this->getCollectionFromDb("SELECT player_id id, player_score score, player_no no, player_color color FROM player");
       // Returned value must include ['players'][$player_id]]['score'] for scores to populate when F5 is pressed.
  • getGameProgression: where you compute the game progression indicator. Returns a number indicating percent of progression (0-100). Used to calculate ELO changes of remaining players when a player quits, or as a conceding requirement (in non-tournament 2 player games, a player may concede if the progression is at least 50%).
  • Utility functions: your utility functions.
  • Player actions: the entry points for players actions (more info here).
  • Game state arguments: methods to return additional data on specific game states (more info here).
  • Game state actions: the logic to run when entering a new game state (more info here).
  • initTable: (not part of template) - this function is called for every php callback by the framework and it can be implement by the game (empty by default). You can use it in rare cases where you need to read database and manipulate some data before any ANY php entry functions are called (such as getAllDatas,action*,st*, etc). Note: it is not called before arg* methods
  • zombieTurn: what to do it's the turn of a zombie player.
  • upgradeTableDb: function to migrate database if you change it after release on production.
  • getGameName: returns the game name. This will be setup when you create the project. If you are copying files in from another project, make sure you keep this function intact. It must return the right game name, or lots of things will be broken.

Accessing player information

Important: In the following methods, be mindful of the difference between the "active" player and the "current" player. The active player is the player whose turn it is - not necessarily the player who sent a request! The current player is the player who sent the request and will see the results returned by your methods: not necessarily the player whose turn it is!


getPlayersNumber()
Returns the number of players playing at the table
Note: doesn't work in the beggining of setupNewGame (use count($players) instead). It will work after initialization of player table.
getActivePlayerId()
Get the "active_player", whatever what is the current state type.
Note: it does NOT mean that this player is active right now, because state type could be "game" or "multiplayer"
Note: avoid using this method in a "multiplayer" state because it does not mean anything.
getActivePlayerName()
Get the "active_player" name
Note: avoid using this method in a "multiplayer" state because it does not mean anything.
getPlayerNameById($player_id)
Get the name by id
getPlayerColorById($player_id)
Get the color by id
getPlayerNoById($player_id)
Get 'player_no' (number) by id


loadPlayersBasicInfos()
Get an associative array with generic data about players (ie: not game specific data).
The key of the associative array is the player id. The returned table is cached, so ok to call multiple times without performance concerns.
The content of each value is:
* player_name - the name of the player
* player_color (ex: ff0000) - the color code of the player (as string)
* player_no - the position of the player at the start of the game in natural table order, i.e. 1,2,3
                $players = $this->loadPlayersBasicInfos();
                foreach ($players as $player_id => $info) {
                    $player_color = $info['player_color'];
                    ...
                }

Note: if you want array of player ids only you can do this:

   $player_ids =  array_keys($this->loadPlayersBasicInfos());  
getCurrentPlayerId(bool $bReturnNullIfNotLogged = false) int
Get the "current_player". The current player is the one from which the action originated (the one who sent the request).
Be careful: This is not necessarily the active player!
In general, you shouldn't use this method, unless you are in "multiplayer" state.
Very important: in your setupNewGame and zombieTurn function, you must never use getCurrentPlayerId() or getCurrentPlayerName(),
otherwise it will fail with a "Not logged" error message (these actions are triggered from the main site and propagated to the gameserver from a server, not from a browser. As a consequence, there is no current player associated to these actions).
getCurrentPlayerName(bool $bReturnEmptyIfNotLogged = false) string
Get the "current_player" name.
Note: this will throw an exception if current player is not at the table, i.e. spectator
Be careful using this method (see above).
getCurrentPlayerColor()
Get the "current_player" color.
Note: this will throw an exception if current player is not at the table, i.e. spectator
Be careful using this method (see above).
isCurrentPlayerZombie()
Check the "current_player" zombie status. If true, player is zombie, i.e. left or was kicked out of the game.
Note: this will throw an exception if current player is not at the table, i.e. spectator
isSpectator()
Check the "current_player" spectator status. If true, the user accessing the game is a spectator (not part of the game). For this user, the interface should display all public information, and no private information (like a friend sitting at the same table as players and just spectating the game).
getActivePlayerColor()
This function does not seems to exist in API, if you need it here is implementation
     function getActivePlayerColor() {
       $player_id = $this->getActivePlayerId();
       $players = $this->loadPlayersBasicInfos();
       if (isset($players[$player_id]))
           return $players[$player_id]['player_color'];
       else
           return null;
   }
isPlayerZombie($player_id)
This method does not exists, but if you need it it looks like this
   protected function isPlayerZombie($player_id) {
       $players = $this->loadPlayersBasicInfos();
       if (! isset($players[$player_id]))
           throw new BgaSystemException("Player $player_id is not playing here");
       
       return ($players[$player_id]['player_zombie'] == 1);
   }

Accessing the database

The main game logic should be the only point from which you should access the game database. You access your database using SQL queries with the methods below.

IMPORTANT

BGA uses database transactions. This means that your database changes WON'T BE APPLIED to the database until your request ends normally (web request, not database request). Using transactions is in fact very useful for you; at any time, if your game logic detects that something is wrong (example: a disallowed move), you just have to throw an exception and all changes to the game situation will be removed. This also means that you need not (and in fact cannot) use your own transactions for multiple related database operations.

However there are sets of database operation that will do implicit commit (most common mistake is to use "TRUNCATE"), you cannot use these operations during the game, it breaks the unrolling of transactions and will lead to nasty issues (https://mariadb.com/kb/en/sql-statements-that-cause-an-implicit-commit).

All methods below are part of game class (and view class) and can be accessed using $this->

DbQuery( string $sql )
This is the generic method to access the database.
It can execute any type of SELECT/UPDATE/DELETE/REPLACE/INSERT query on the database. Returns result of the query.
For SELECT queries, the specialized methods below are much better.
Do not use method for TRUNCATE, DROP and other table altering operations. See disclamer above about implicit commits. If you really need TRUNCATE use DELETE FROM xxx instead.
getUniqueValueFromDB( string $sql )
Returns a unique value from DB or null if no value is found.
$sql must be a SELECT query.
Raise an exception if more than 1 row is returned.
getCollectionFromDB( string $sql, bool $bSingleValue=false ) array
Returns an associative array of rows for a sql SELECT query.
The key of the resulting associative array is the first field specified in the SELECT query.
The value of the resulting associative array is an associative array with all the field specified in the SELECT query and associated values.
First column must be a primary or alternate key (semantically, it does not actually have to declared in sql as such).
The resulting collection can be empty (it won't be null).
If you specified $bSingleValue=true and if your SQL query requests 2 fields A and B, the method returns an associative array "A=>B", otherwise its A=>[A,B]
Note: The name a bit misleading, it really return associative array, i.e. map and NOT a collection. You cannot use it to get list of values which may have duplicates (hence primary key requirement on first column). If you need simple array use getObjectListFromDB() method.

Example 1:

$result = $this->getCollectionFromDB( "SELECT player_id id, player_name name, player_score score FROM player" );

Result:
[
 1234 => [ 'id'=>1234, 'name'=>'myuser0', 'score'=>1 ],
 1235 => [ 'id'=>1235, 'name'=>'myuser1', 'score'=>0 ]
]

Example 2:

$result = $this->getCollectionFromDB( "SELECT player_id id, player_name name FROM player", true );

Result:
[
 1234 => 'myuser0',
 1235 => 'myuser1'
]

getNonEmptyCollectionFromDB(string $sql) array
Same as getCollectionFromDB($sdl), but raise an exception if the collection is empty. Note: this function does NOT have 2nd argument as previous one does.
getObjectFromDB(string $sql) array
Returns one row for the sql SELECT query as an associative array or null if there is no result (where fields are keys mapped to values)
Raise an exception if the query return more than one row (you can use LIMIT 1 in the query to avoid the exception)

Example:

$result = $this->getObjectFromDB( "SELECT player_id id, player_name name, player_score score FROM player WHERE player_id='$player_id'" );

Result:
[
  'id'=>1234, 'name'=>'myuser0', 'score'=>1 
]
getNonEmptyObjectFromDB(string $sql) array
Similar to previous one, but raise an exception if no row is found
getObjectListFromDB(string $sql, bool $bUniqueValue=false) array
Return an array of rows for a sql SELECT query.
The result is the same as "getCollectionFromDB" except that the result is a simple array (and not an associative array).
The result can be empty.
If you specified $bUniqueValue=true and if your SQL query request 1 field, the method returns directly an array of values.

Example 1:

$result = $this->getObjectListFromDB( "SELECT player_id id, player_name name, player_score score FROM player" );

Result:
[
 [ 'id'=>1234, 'name'=>'myuser0', 'score'=>1 ],
 [ 'id'=>1235, 'name'=>'myuser1', 'score'=>0 ]
]

Example 2:

$result = $this->getObjectListFromDB( "SELECT player_name name FROM player", true );

Result:
[
 'myuser0',
 'myuser1'
]

getDoubleKeyCollectionFromDB(string $sql, bool $bSingleValue=false) array
Return an associative array of associative array, from a SQL SELECT query.
First array level correspond to first column specified in SQL query.
Second array level correspond to second column specified in SQL query.
If $bSingleValue = true, keep only third column on result


DbGetLastId()
Return the PRIMARY key of the last inserted row (see PHP mysql_insert_id function).
DbAffectedRow() int
Return the number of row affected by the last operation
escapeStringForDB(string $string) string
You must use this function on every string type data in your database that contains unsafe data.
(unsafe = can be modified by a player).
This method makes sure that no SQL injection will be done through the string used.
Note: if you using standard types in ajax actions, like AT_alphanum it is sanitized before arrival,
this is only needed if you manage to get unchecked string, like in the games where user has to enter text as a response.


Note: see Editing Game database model: dbmodel.sql to know how to define your database model.

Use globals

Sometimes, you want a single global integer value for your game, and you don't want to create a DB table specifically for it.

You can do this with the BGA framework "global". Your value will be stored in the "global" table in the database, and you can access it with simple methods.

All methods below are members of the game class and should be accessed via $this->

initGameStateLabels(array $labelsMap): void

This method should be located at the beginning of constructor of yourgamename.game.php. This is where you define the globals used in your game logic, by assigning them IDs.

You can define up to 80 globals, with IDs from 10 to 89 (inclusive, there can be gaps). Also you must use this method to access value of game options Game_options_and_preferences:_gameoptions.inc.php, in that case, IDs need to be between 100 and 199. You must not use globals outside the range defined above, as those values are used by other components of the framework.

   function __construct() {
        parent::__construct();
        $this->initGameStateLabels([ 
                "my_first_global_variable" => 10,
                "my_second_global_variable" => 11,
                "my_game_variant" => 100
        ]);
         // other code ...
   }

NOTE: The methods below WILL throw an exception if label is not defined using the call above.

setGameStateInitialValue( string $label, int $value ): void

Initialize global value. This is not required if you ok with default value if 0. This should be called from setupNewGame function.


getGameStateValue( string $label, int $default = 0): int

Retrieve the value of a global. Returns $default if global is not been initialized (by setGameStateInitialValue).

NOTE: this method use globals "cache" if you directly manipulated globals table OR call this function after undoRestorePoint() - it won't work as expected.

  $value = $this->getGameStateValue('my_first_global_variable');

For debugging purposes, you can have labels and value pairs send to client side by inserting that code in your "getAllDatas":

$labels = array_keys($this->mygamestatelabels);
$result['myglobals'] = array_combine($labels, array_map([$this,'getGameStateValue'],$labels));

That assumes you stored your label mapping in $this->mygamestatelabels in constructor

  $this->mygamestatelabels=["my_first_global_variable" => 10, ...];
  $this->initGameStateLabels($this->mygamestatelabels);

setGameStateValue( string $label, int $value ): void

Set the current value of a global.

  $this->setGameStateValue('my_first_global_variable', 42);

incGameStateValue( string $label, int $increment ): int

Increment the current value of a global. If increment is negative, decrement the value of the global.

Return the final value of the global. If global was not initialized it will initialize it as 0.

NOTE: this method use globals "cache" if you directly manipulated globals table OR call this function after undoRestorePoint() - it won't work as expected.

  $value = $this->incGameStateValue('my_first_global_variable', 1);


BGA predefined globals

BGA already defines some globals in the global database table. You should not change them directly but it can be useful to know what they mean when debugging:

global_id label Meaning
1 Current state
2 Active player id
3 next_move_id Next move number
4 Game id
5 Table creator id
6 playerturn_nbr Player turn number
7 gameprogression Game progression
8 initial_reflexion_time Initial reflection time
9 additional_reflexion_time Additional reflection time
200 reflexion_time_profile Reflexion time profile
201 bgaranking_mode BGA ranking mode
207 game_language GAMESTATE_GAME_LANG
300 game_db_version GAMESTATE_GAMEVERSION: Current version of the game (when in production)
301 game_result_neutralized GAMESTATE_GAME_RESULT_NEUTRALIZED
302 neutralized_player_id GAMESTATE_NEUTRALIZED_PLAYER_ID
304 undo_moves_stored GAMESTATE_UNDO_MOVES_STORED
305 undo_moves_player GAMESTATE_UNDO_MOVES_PLAYER
306 lock_screen_timestamp GAMESTATE_LOCK_TIMESTAMP

Game states and active players

Activate player handling

$this->activeNextPlayer()
Make the next player active in the natural player order.
Note: you CANNOT use this method in a "activeplayer" or "multipleactiveplayer" state. You must use a "game" type game state for this.
$this->activePrevPlayer()
Make the previous player active (in the natural player order).
Note: you CANNOT use this method in a "activeplayer" or "multipleactiveplayer" state. You must use a "game" type game state for this.
$this->gamestate->changeActivePlayer( $player_id )
You can call this method to make any player active.
Note: you CANNOT use this method in a "activeplayer" or "multipleactiveplayer" state. You must use a "game" type game state for this.
$this->getActivePlayerId()
Return the "active_player" id
Note: it does NOT mean that this player is active right now, because state type could be "game" or "multipleactiveplayer"
Note: avoid using this method in a "multipleactiveplayer" state because it does not mean anything.

Multiple activate player handling

$this->gamestate->setAllPlayersMultiactive()
All playing players are made active. Update notification is sent to all players (this will trigger onUpdateActionButtons).
Usually, you use this method at the beginning of a game state (e.g., "stGameState") which transitions to a multipleactiveplayer state in which multiple players have to perform some action. Do not use this method if you going to make some more changes in the active player list. (I.e., if you want to take away multipleactiveplayer status immediately afterwards, use setPlayersMultiactive instead.)

Example of usage:

    function st_MultiPlayerInit() {
        $this->gamestate->setAllPlayersMultiactive();
    }
    
$this->stMakeEveryoneActive()
this method can be used in state machine to make everybody active as "st" method of multiplayeractive state, it just calls $this->gamestate->setAllPlayersMultiactive()

This is to be used in state declaration:

    2 => array(
    		"name" => "playerTurnPlace",
    		"description" => clienttranslate('Other player must place ships'),
    		"descriptionmyturn" => clienttranslate('${you} must place ships (click on YOUR SHIPS board to place)'),
    		"type" => "multipleactiveplayer",
                'action' => 'stMakeEveryoneActive',
                'args' => 'arg_playerTurnPlace',
    	     	"possibleactions" => array( "actionBla" ),
                "transitions" => array( "next" => 4, "last" => 99)
    ),
    
$this->gamestate->setAllPlayersNonMultiactive( $next_state )
All playing players are made inactive. Transition to next state
$this->gamestate->setPlayersMultiactive( $players, $next_state, $bExclusive = false )
Make a specific list of players active during a multiactive gamestate. Update notification is sent to all players whose state changed.
"players" is the array of player id that should be made active. If "players" is not empty the value of "next_state" will be ignored (you can put whatever you want)
If "bExclusive" parameter is not set or false it doesn't deactivate other previously active players. If its set to true, the players who will be multiactive at the end are only these in "$players" array
In case "players" is empty, the method trigger the "next_state" transition to go to the next game state.
returns true if state transition happened, false otherwise
$this->gamestate->setPlayerNonMultiactive( $player_id, $next_state )
During a multiactive game state, make the specified player inactive.
Usually, you call this method during a multiactive game state after a player did his action. It is also possible to call it directly from multiplayer action handler.
If this player was the last active player, the method trigger the "next_state" transition to go to the next game state.
returns true if state transition happened, false otherwise

Example of usage (see state declaration of playerTurnPlace above):

    function actionBla($args) {
        $this->checkAction('actionBla');
        // handle the action using $this->getCurrentPlayerId()
        $this->gamestate->setPlayerNonMultiactive( $this->getCurrentPlayerId(), 'next');
    }
$this->gamestate->getActivePlayerList()
With this method you can retrieve the list of the active player at any time.
During a "game" type gamestate, it will return a void array.
During a "activeplayer" type gamestate, it will return an array with one value (the active player id).
During a "multipleactiveplayer" type gamestate, it will return an array of the active players id.
Note: you should only use this method in the latter case.


$this->gamestate->updateMultiactiveOrNextState( $next_state_if_none )
Sends update notification about multiplayer changes. All multiactive set* functions above do that, however if you want to change state manually using db queries for complex calculations, you have to call this yourself after. Do not call this if you calling one of the other setters above.

Example: you have player teams and you want to activate all players in one team

        $sql = "UPDATE player SET player_is_multiactive='0'";
        $this->DbQuery( $sql );
        $sql = "UPDATE player SET player_is_multiactive='1' WHERE player_id='$player_id' AND player_team='$team_no'";
        $this->DbQuery( $sql );
        
        $this->gamestate->updateMultiactiveOrNextState( 'error' );
updating database manually
Use this helper function to change multiactive state without sending notification
    /**
     * Changes values of multiactivity in db, does not sent notifications.
     * To send notifications after use updateMultiactiveOrNextState
     * @param number $player_id, player id <=0 or null - means ALL
     * @param number $value - 1 multiactive, 0 non multiactive
     */
    function dbSetPlayerMultiactive($player_id = -1, $value = 1) {
        if (! $value)
            $value = 0;
        else
            $value = 1;
        $sql = "UPDATE player SET player_is_multiactive = '$value' WHERE player_zombie = 0 and player_eliminated = 0";
        if ($player_id > 0) {
            $sql .= " AND player_id = $player_id";
        }
        $this->DbQuery($sql);
    }
$this->gamestate->isPlayerActive($player_id)
Return true if specified player is active right now.
This method take into account game state type, ie nobody is active if game state is "game" and several players can be active if game state is "multiplayer"
$this->bIndependantMultiactiveTable
This flag can be set to true in constructor of game.php to force creation of second table to handle multiplayer states (normally these are in player table), this is very advanced feature.
ONLY use it after you deploy you game to production if you receive unusual amount of bug report with dead lock symptoms DURING multiactiveplayer states
   function __construct() {
     ...
     $this->bIndependantMultiactiveTable=true;
   }

States functions

$this->gamestate->nextState( $transition )
Change current state to a new state. Important: the $transition parameter is the name of the transition, and NOT the name of the target game state, see Your game state machine: states.inc.php for more information about states.

$this->gamestate->jumpToState($stateNum)

Change current state to a new state. Important: the $stateNum parameter is the key of the state. See Your game state machine: states.inc.php for more information about states.
Note: this is very advanced method, it should not be used in normal cases. Specific advanced cases include - jumping to specific state from "do_anytime" actions, jumping to dispatcher state or jumping to recovery state from zombie player function
$this->checkAction( $actionName, $bThrowException=true )
Check if the current player can perform a specific action in the current game state, and optionally throw an exception if they can't.
The action is valid if it is listed in the "possibleactions" array for the current game state (see game state description).
This method MUST be the first one called in ALL your PHP methods that handle player actions, in order to make sure a player doesn't perform an action not allowed by the rules at the point in the game. It should not be called from methods where the current player is not necessarily the active player, otherwise it may fail with an "It is not your turn" exception.
If "bThrowException" is set to "false", the function returns false in case of failure instead of throwing an exception. This is useful when several actions are possible, in order to test each of them without throwing exceptions.
$this->gamestate->checkPossibleAction( $action )
(rarely used)
This works exactly like "checkAction" (above), except that it does NOT check if the current player is active.
Note: This does NOT check either spectator or eliminated status, so those checks must be done manually.
This is used specifically in certain game states when you want to authorize additional actions for players that are not active at the moment.
Example: in Libertalia, you want to authorize players to change their mind about the card played. They are of course not active at the time they change their mind, so you cannot use "checkAction"; use "checkPossibleAction" instead.

This is how PHP action looks that returns player to active state (only for multiplayeractive states). To be able to execute this on client do not call checkAction on js side for this specific action.

  function actionUnpass() {
       $this->gamestate->checkPossibleAction('actionUnpass'); // player changed mind about passing while others were thinking
       $this->gamestate->setPlayersMultiactive(array ($this->getCurrentPlayerId() ), 'error', false);
   }
$this->gamestate->state()
Get an associative array of current game state attributes, see Your game state machine: states.inc.php for state attributes.
 $state=$this->gamestate->state(); if( $state['name'] == 'myGameState' ) {...}

I suggest to define and use this function in your php class to access state name:

   public function getStateName() {
       $state = $this->gamestate->state();
       return $state['name'];
   }
$this->gamestate->state_id()
Get the id of the current game state (rarely useful, its best to use name, unless you use constants for state ids)
$this->gamestate->isMutiactiveState()
Return true if we are in multipleactiveplayer state, false otherwise

Private parallel states

See the overview of private parallel states here.

$this->gamestate->initializePrivateStateForAllActivePlayers()
All active players in a multiactive state are entering a first private state defined in the master state's initialprivate parameter.
Every time you need to start a private parallel states you need to call this or similar methods below.
Note: at least one player needs to be active (see above) and current game state must be a multiactive state with initialprivate parameter defined
Note: initialprivate parameter of master state should be set to the id of the first private state. This private state needs to be defined in states.php with the type set to 'private'.
Note: this method is usually preceded with activating some or all players
Note: initializing private state can run action or args methods of the initial private state

Example of usage:

    function stStartPlayerTurn() {
        // This is usually done in master state action method
        
        $this->gamestate->setAllPlayersMultiactive();
        $this->gamestate->initializePrivateStateForAllActivePlayers();

        // in some cases you can move immediately some or all players to different private states
        if ($someCondition) {
            //move all players to different state 
            $this->gamestate->nextPrivateStateForAllActivePlayers("some_transition");
        }

        if ($other condition) {
            //move single player to different state
            $this->gamestate->nextPrivateState($specificPlayerId, "some_transition");
        }
    }
    
$this->gamestate->initializePrivateStateForPlayers($playerIds)
Players with specified ids are entering a first private state defined in the master state initialprivate parameter.
Same considerations apply as for the method above.
$this->gamestate->initializePrivateState($playerId)
Player with the specified id is entering a first private state defined in the master state initialprivate parameter.
Everytime you need to start a private parallel states you need to call this or similar methods above
Note: player needs to be active (see above) and current game state must be a multiactive state with initialprivate parameter defined
Note: initialprivate parameter of master state should be set to the id of the first private state. This private state needs to be defined in states.php with the type set to 'private'.
Note: this method is usually preceded with activating that player
Note: initializing private state can run action or args methods of the initial private state

Example of usage:

    function st_ChangeMind() {
        // This player finished his move before, but now decides change something while other players are still active
        // We activate the player and initialize his private state
        $this->gamestate->setPlayersMultiactive([$this->getCurrentPlayerId()], "");
        $this->gamestate->initializePrivateState(this->getCurrentPlayerId());

        // It is also possible to move the player to some other specific state immediately
        $this->gamestate->nextPrivateState($this->getCurrentPlayerId(), "some_transition");
    }
    
$this->gamestate->nextPrivateStateForAllActivePlayers($transition)
All active players will transition to next private state by specified transition
Note: game needs to be in a master state which allows private parallel states
Note: transition should lead to another private state (i.e. a state with type defined as 'private'
Note: transition should be defined in private state in which the players currently are.
Note: this method can run action or args methods of the target state
Note: this is usually used after initializing the private state to move players to specific private state according to the game logic

Example of usage:

    function stStartPlayerTurn() {
        // This is usually done in master state action method
        $this->gamestate->setAllPlayersMultiactive();
        $this->gamestate->initializePrivateStateForAllActivePlayers();

        if ($specificOption) {
            //move all players to different state 
            $this->gamestate->nextPrivateStateForAllActivePlayers("some_transition");
        }
    }
    
$this->gamestate->nextPrivateStateForPlayers($playerIds, $transition)
Players with specified ids will transition to next private state specified by provided transition.
Same considerations apply as for the method above.
$this->gamestate->nextPrivateState($playerId, $transition)
Player with specified id will transition to next private state specified by provided transition
Note: game needs to be in a master state which allows private parallel states
Note: transition should lead to another private state (i.e. a state with type defined as 'private'
Note: transition should be defined in private state in which the players currently are.
Note: this method can run action or args methods of the target state for specified player
Note: this is usually used after some player actions to move to next private state

Example of usage:

    function someAction() {
        $this->checkAction("someAction"); //needs to be defined in the current state
        $this->gamestate->nextPrivateState($this->getCurrentPlayerId(), "some_transition");
    }
    
$this->gamestate->unsetPrivateStateForAllPlayers()
All players private state will be reset to null, which means they will get out of private parallel states and be in a master state like the private states are not used
Note: game needs to be in a master state which allows private parallel states
Note: this is usually used to clean up after leaving a master state in which private states were used, but can be used in other cases when we want to exit private parallel states and use a regular multiactive state for all players
Note: After unseting private state only actions on master state are possible
Note: Usually it is not necessary to unset private states as they will be initialized to first private state when private states are needed again. Nevertheless it is generally better to clean private state after exiting private parallel states to avoid bugs.

Example of usage:

    function stNextRound() {
        $this->gamestate->unsetPrivateStateForAllPlayers();
    }
    
$this->gamestate->unsetPrivateStateForPlayers($playerIds, $transition)
For players with specified ids private state will be reset to null, which means they will get out of private parallel states and be in a master state like the private states are not used.
Same considerations apply as for the method above.
$this->gamestate->unsetPrivateState($playerId)
For player with specified id private state will be reset to null, which means they will get out of private parallel states and be in a master state like the private states are not used
Note: game needs to be in a master state which allows private parallel states
Note: this is usually used when deactivating player to clean up their parallel state
Note: After unseting private state only actions on master state are possible
Note: Usually it is not necessary to unset private state as it will be initialized to first private state when private states are needed again. Nevertheless it is generally better to clean private state when not needed to avoid bugs.

Example of usage:

    function done() {
        $this->gamestate->setPlayerNonMultiactive( $this->getCurrentPlayerId(), "newTurn" );
        $this->gamestate->unsetPrivateState($this->getCurrentPlayerId());
    }
    
$this->gamestate->setPrivateState($playerId, $newStateId)
For player with specified id a new private state would be set
Note: game needs to be in a master state which allows private parallel states
Note: this should be rarely used as it doesn't check if the transition is allowed (it doesn't even specifies transition). This can be useful in very complex cases when standard state machine is not adequate (i.e. specific cards can lead to some micro action in various states where defining transitions back and forth can become very tedious.)
Note: this method can run action or args methods of the target state for specified player

Example of usage:

    function someAction() {
        $this->checkAction("someAction"); //needs to be defined in the current state

        if ($playerHaveSpecificCard)
            return $this->gamestate->setPrivateState($this->getCurrentPlayerId(), 35);

        $this->gamestate->nextPrivateState($this->getCurrentPlayerId(), "some_transition");        
    }
    
$this->gamestate->getPrivateState($playerId)
This return the private state or null if not initialized or not in private state

State Arguments in Private parallel states

The args method called for private states will have the player_id passed to it, allowing you to customise the arguments returned for that player.

Example of usage:

    function argMyPrivateState($player_id) {
        return array(
          'my_data' => $this->getPlayerSpecificData($player_id)
        );
    }
    

Inactive Players

During Private Parallel State, active players will be managed by the private state that is current assigned to them.

Inactive players will be managed by the master multipleactiveplayer state, so your client should respond to that state in order to display any status message advising players that they are waiting for others to have their turn, or to add any buttons that allow players to potentially "break in" and become active.

Players turn order

When table is created the "natural" player order is assigned to player at random, and stored in "read-only" field "player_no". If you need to create a custom order you should never change natural order but have a separate data structure. For example you can alter the players table to add another "custom_order" field, you can use state globals or you can use your natural board database, to store meeple_color/position_location pair. BGA currently does not provide any API to create/store custom player order.

getNextPlayerTable()

Return an associative array which associate each player with the next player around the table.

In addition, key 0 is associated to the first player to play.

Example: if three player with ID 1000, 2000 and 3000 are around the table, in this order, the method returns:

   array( 
    1000 => 2000, 
    2000 => 3000, 
    3000 => 1000, 
    0 => 1000 
   );

getPrevPlayerTable()

Same as above, but the associative array associate the previous player around the table. However there no 0 index here.

getPlayerAfter( $player_id )

Get player playing after given player in natural playing order.

getPlayerBefore( $player_id )

Get player playing before given player in natural playing order.

Note: There is no API to modify this order, if you have custom player order you have to maintain it in your database and have custom function to access it.

createNextPlayerTable( $players, $bLoop=true )

Using $players array creates a map of current => next as in example from getNextPlayerTable(), however you can use custom order here. If parmeter $bLoop is set to true then last player will points to first (creaing a loop), false otherwise. In any case index 0 points to first player (first element of $players array). $players is array of player ids in desired order.

Note: This function DOES NOT change the order in database, it only creates a map using key/values as descibed.

Example of usage:

    function getNextPlayerTableCustom() {
        $starting = $this->getStartingPlayer(); // custom function to get starting player
        $player_ids = $this->getPlayerIdsInOrder($starting); // custom function to create players array starting from starting player
        return $this->createNextPlayerTable($player_ids, false); // create next player table in custom order
    }


$table = $this->createNextPlayerTable([3000,2000,1000], false);

will return:
   [ 
    3000 => 2000, 
    2000 => 1000, 
    1000 => null,
    0 => 3000 
   ]

Notifications

To understand notifications, please read The BGA Framework at a glance first.

IMPORTANT

Notifications are crutial part of BGA framework. Everything players see, all changes which happen on frontend (including setup) are done via notfications. Some of the are framework notifications and some of them are custom (game specific). Generally you should not send or handle stanard notifications, except exposed APIs. Example of standard notifications:

  • game setup (first time client connects to game)
  • game state change
  • active player change

Example of custom notification

  • game piece moved
  • player scored points

Notifications are queued, i.e. sent at the very end of the action, when it ends normally. It means that if you throw an exception for any reason (i.e: move is not allowed), no notifications will be sent to players.

Notification can be sent from the following functions (and transive calls obviosly):

  • action handler (action* in game.php). It is a MUST actually. At least one notificaton must be sent (but it includes state transition which sends it)
  • game state transition (st* in game.php)
  • setupNewGame (after player tables setup is finished)

Notification cannot be sent from the following:

  • .view.php
  • .action.php
  • material.inc.php
  • constructor of .game.php
  • arg* methods of .game.php

There are conceptually two types of notification:

  • public - sent to all client who are listening - that includes all players in the table and spectators
  • private - that can only be sent to a player

The bundle of notifications is sent at the end action considered a single "move" and move counter increases (if you mix private and public notification - its separate bundles). In rare case if you want to change this behavior, you can apply some hackery described in BGA_Studio_Cookbook


The notifications are handled on JS side by subscribing to notifications (only do it for custom notifications!). See [Game_interface_logic:_yourgamename.js#Notifications]

NotifyAllPlayers

notifyAllPlayers(string $notification_type,string $message,array $notification_args )

Send a notification to all players of the game and spectators (public).

  • notification_type: A string that defines the type of your notification.

Your game interface Javascript logic will use this to know what is the type of the received notification (and to trigger the corresponding method).

  • message: A string that defines what is to be displayed in the game log.

You can use an empty string here (). In this case, nothing is displayed in the game log.

Unless its empty, use "clienttranslate" method to make sure string is translated.

You can use arguments in your $notification_log string, that refers to values defines in the "$notification_args" argument (see below). Note: Make sure you only use single quotes ('), otherwise PHP will try to interpolate the variable and will ignore the values in the args array.

  • notification_args: The arguments of your notifications, as an associative array.

This array will be transmitted to the game interface logic, in order the game interface can be updated.

Complete notifyAllPlayers example (from "Reversi"):

$this->notifyAllPlayers( "playDisc", clienttranslate( '${player_name} plays a disc and turns over ${returned_nbr} disc(s)' ),
 array(
        'player_id' => $player_id,
        'player_name' => self::getActivePlayerName(),
        'returned_nbr' => count( $turnedOverDiscs ),
        'x' => $x,
        'y' => $y
     ) );

You can see in the example above the use of the "clienttranslate" method, and the use of 2 arguments "player_name" and "returned_nbr" in the notification log.

Important: NO private data must be sent with this method, as a cheater could see it even if it is not used explicitly by the game interface logic. If you want to send private information to a player, please use notifyPlayer below.

Important: this array is serialized to be sent to the browsers, and will be saved with the notification to be able to replay the game later. If it is too big, it can make notifications slower / less reliable, and replay archives very big (to the point of failing). So as a general rule, you should send only the minimum of information necessary to update the client interface with no overhead in order to keep the notifications as light as possible.

Important: When the game page is reloaded (i.e. F5 or when loading turn based game) all previous notifications are replayed as history notifications. These notifications do not trigger notification handlers and are used basically to build the game log. Because of that most of the notification arguments (except i18n, player_id and all arguments referenced in the message), are removed from these history notifications. If you need additional arguments in history notifications you can add special field preserve to notification arguments, like this:

$this->notifyAllPlayers( "playDisc", clienttranslate( '${player_name} plays a disc and turns over ${returned_nbr} disc(s)' ),
 array(
        'player_id' => $player_id,
        'player_name' => self::getActivePlayerName(),
        'returned_nbr' => count( $turnedOverDiscs ),
        'x' => $x,
        'y' => $y,
        'preserve' => [ 'x', 'y' ]
     ) );

In this example, fields x and y will be preserved when replaying history notification at the game load.

NOTE: The ONLY reason 'preserve' is useful if you have custom method to render notifications in game log which changes some text arguments into html (i.e. to insert the images instead of plain text). Do not use preserve "just in case" - it will only bloat the logs and make game load VERY slow.

Important: If both public and private notifications are sent to the same player in the same action (AJAX call), they will initially appear in the log in the order in which they were called, but they are placed into the game log in the following order: All private notifications first, then all public notifications. This means that when the page is refreshed, or when a player loads an asynchronous game, if you have called any public notifications before the last private notification, they will appear out of order in the log.

HTML in Notifications

You CAN use some HTML inside your notification log, however it not recommended for many reasons:

  • Its bad architecture, ui elements leak into server now you have to manage ui in many places
  • If you decided to change something in ui in a future version, old games replay and tutorials may not work, since they use stored notifications (for example if the change occured during the game and later this game is used for tutorial)
  • When you read log preview for old games its unreadable (this is log before you enter the game replay, useful for troubleshooting or game analysis)
  • Its more data to transfer and store in db
  • Its nightmare for translators, at least don't put HTML tags inside the "clienttranslate" method. You can use a notification argument instead, and provide your HTML through this argument.

If you still want to have pretty pictures in the log check this BGA_Studio_Cookbook#Inject_images_and_styled_html_in_the_log.

Recursive Notifications

If your notification contains some phrases that build programmatically you may need to use recursive notifications. In this case the argument can be not only the string but an array itself, which contains 'log' and 'args', i.e.

 $this->notifyAllPlayers('message',clienttranslate('Game moves ${token_name_rec}'),
                  ['token_name_rec'=>['log'=>'${token_name} #${token_number}',
                                      'args'=> ['token_name'=>clienttranslate('Boo'), 'token_number'=>$number, 'i18n'=>['token_name'] ]
                                     ]
                  ]);


Excluding some players

Sometimes you want to notify all players of a message but not have it appear in the log of specific players (for example, have every player see "Player X draws a card" but have Player X will get a private notification "You draw the Ace of Spades", so you want them not to see the public one).

To send a notification to all players but have some clients ignore it, send it as normal from the server, but implement setIgnoreNotificationCheck on the client to ignore the message under given conditions. See the Game_interface_logic:_yourgamename.js#Ignoring_notifications documentation for more details. Note: do not sent private info with such notification, hiding something on client side is not a hacker safe.

Using player names

The variable for player name must be ${player_name} in order to be highlighted with the player color in the game log, it has to have macthing 'player_id' argument in $notification_args. If you want a second player name in the log, name the variable ${player_name2} and $player_id2, etc.

Special handling of arguments:

  • ${player_name} - this will be wrapped in html and text shown using color of the corresponding player, some colors also have reserved background. This will apply recursively as well.
  • ${player_name1}, ${player_name2}, ${player_name3}, etc. - same

In order for this to work you must pass corresponding player_id in args i.e.

   $this->notifyAllPlayers(
     'buyAndPassCard',
     clienttranslate('${player_name} buys ${card_name} and pass it to ${player_name2}'),
     [
       'player_id' => $player,
       'player_id2' => $nextPlayer,
       ...
     ]
   );

Note: player_name is not translatable, never add it to i18n array

NotifyPlayer

notifyPlayer(int $player_id, string $notification_type, string $message, $notification_args )

Same as above, except that the notification is sent to one player only. The player must be the player at the game table.

This method must be used each time some private information must be transmitted to a player.

Important: the variable for player name must be ${player_name} in order to be highlighted with the player color in the game log. Since its a private notification it may be more appropriate to use "you" insted


 $this->notifyPlayer($player_id,'message',clienttranslate('You draw ${card_name}'), [ ... ]);

or

 $this->notifyPlayer($player_id,'message',clienttranslate('${player_name} draws ${card_name}'), [ 'player_name' => $this->getPlayerNameById($player_id), ... ]);


Note: Spectators cannot be notified using this method, because their player ID is not available via loadPlayersBasicInfos() or otherwise. You must use notifyAllPlayers() for any notification that spectators should get.

Randomization

A large number of board games rely on random, most often based on dice, cards shuffling, picking some item in a bag, and so on. This is very important to ensure a high level of randomness for each of these situations.

Here's are a list of techniques you should use in these situations, from the best to the worst.

Dice and bga_rand

bga_rand( min, max ) This is a BGA framework function that provides you a random number between "min" and "max" (inclusive), using the best available random method available on the system.

This is the preferred function you should use, because we are updating it when a better method is introduced.

As of now, bga_rand is based on the PHP function "random_int", which ensures a cryptographic level of randomness.

In particular, it is mandatory to use it for all dice throw (ie: games using other methods for dice throwing will be rejected by BGA during review).

Note: rand() and mt_rand() are deprecated on BGA and should not be used anymore, as their randomness is not as good as "bga_rand".

Arrays

Although PHP's "shuffle()" is generally considered good enough (see below, BGA's own Deck component uses this), the following PHP code based on "random_int" provides a cryptographically-secure method to choose a random key, value, or slice of an array. (the slice preserves keys)

    private function getRandomKey(array &$array)
    {
        $size = count($array);
        if ($size == 0) {
            trigger_error("getRandomKey(): Array is empty", E_USER_WARNING);
            return null;
        }
        $rand = random_int(0, $size - 1);
        $slice = array_slice($array, $rand, 1, true);
        foreach ($slice as $key => $value) {
            return $key;
        }
    }

    private function getRandomValue(array &$array)
    {
        $size = count($array);
        if ($size == 0) {
            trigger_error("getRandomValue(): Array is empty", E_USER_WARNING);
            return null;
        }
        $rand = random_int(0, $size - 1);
        $slice = array_slice($array, $rand, 1, true);
        foreach ($slice as $key => $value) {
            return $value;
        }
    }

    private function getRandomSlice(array &$array, int $count)
    {
        $size = count($array);
        if ($size == 0) {
            trigger_error("getRandomSlice(): Array is empty", E_USER_WARNING);
            return null;
        }
        if ($count < 1 || $count > $size) {
            trigger_error("getRandomSlice(): Invalid count $count for array with size $size", E_USER_WARNING);
            return null;
        }
        $slice = [];
        $randUnique = [];
        while (count($randUnique) < $count) {
            $rand = random_int(0, $size - 1);
            if (array_key_exists($rand, $randUnique)) {
                continue;
            }
            $randUnique[$rand] = true;
            $slice += array_slice($array, $rand, 1, true);
        }
        return $slice;
    }

shuffle and cards shuffling

To shuffle items, like a pile of cards, the best way is to use the BGA PHP Deck component and to use "shuffle" method. This ensures that the best available shuffling method is used, and that if in the future we improve it your game will be up to date.

As of now, the Deck component shuffle method is based on PHP "shuffle" method, which has quite good randomness (even if not as good as bga_rand). In consequence, we accept other shuffling methods during reviews, as long as they are based on PHP "shuffle" function (or similar, like "array_rand").

Other methods

Mysql "RAND()" function has not enough randomness to be a valid method to get a random element on BGA. This function has been used in some existing games and has given acceptable results, but now it should be avoided and you should use other methods instead.

Game statistics

There are 2 types of statistics:

  • a "player" statistic is a statistic associated to a player
  • a "table" statistic is a statistic not associated to a player (global statistic for this game).

See Game statistics: stats.inc.php to see how you define statistics for your game.


initStat( $table_or_player, $name, $value, $player_id = null )

Create a statistic entry with a default value.

This method must be called for each statistic of your game, in your setupNewGame method. If you neglect to call this for a statistic, and also do not update the value during the course of a certain game using setStat or incStat, the value of the stat will be undefined rather than 0. This will result in it being ignored at the end of the game, as if it didn't apply to that particular game, and excluded from cumulative statistics. As a consequence - if do not want statistic to be applied, do not init it, or call set or inc on it.


'$table_or_player' must be set to "table" if this is a table statistic, or "player" if this is a player statistic.

'$name' is the name of your statistic, as it has been defined in your stats.inc.php file.

'$value' is the initial value of the statistic. If this is a player statistic and if the player is not specified by "$player_id" argument, the value is set for ALL players.


setStat( $value, $name, $player_id = null )

Set a statistic $name to $value.

If "$player_id" is not specified, setStat consider it is a TABLE statistic.

If "$player_id" is specified, setStat consider it is a PLAYER statistic.


incStat( $delta, $name, $player_id = null )

Increment (or decrement) specified statistic value by $delta value. Same behavior as setStat function.


getStat( $name, $player_id = null )

Return the value of statistic specified by $name. Useful when creating derivative statistics such as average.

Translations

See Translations

Manage player scores and Tie breaker

Normal scoring

At the end of the game, players automatically get a rank depending on their score: the player with the biggest score is #1, the player with the second biggest score is #2, and so on...

During the game, you update player's score directly by updating "player_score" field of "player" table in database.

Examples:


  // +2 points to active player
  $this->DbQuery( "UPDATE player SET player_score=player_score+2 WHERE player_id='".self::getActivePlayerId()."'" );

  // Set score of active player to 5
  $this->DbQuery( "UPDATE player SET player_score=5 WHERE player_id='".self::getActivePlayerId()."'" );

Note: don't forget to notify the client side in order the score control can be updated accordingly.

Tie breaker

Tie breaker is used when two players get the same score at the end of a game.

Tie breaker is using "player_score_aux" field of "player" table. It is updated exactly like the "player_score" field.

Tie breaker score is displayed only for players who are tied at the end of the game. Most of the time, it is not supposed to be displayed explicitly during the game.

When you are using "player_score_aux" functionality, you must describe the formula to use in your gameinfos.inc.php file like this:

         'tie_breaker_description' => totranslate("Describe here your tie breaker formula"),

This description will be used as a tooltip to explain to players how this auxiliary score has been calculated.

See also Multiple Tie Breaker Management.

Co-operative game

To make everyone win/lose together in a full-coop game:

Add the following in gameinfos.inc.php : 'is_coop' => 1, // full cooperative

Assign a score of zero to everyone if it's a loss. Assign the same score > 0 to everyone if it's a win.

Semi-coop

If the game is not full-coop, then everyone loses = everyone is tied. I.e. set score to 0 to everybody.

Only "winners" and "losers"

For some games, there is only a group (or a single) "winner", and everyone else is a "loser", with no "end of game rank" (1st, 2nd, 3rd...).

Examples:

  • Coup
  • Not Alone
  • Werewolves
  • Quantum

In this case:

  • Set the scores so that the winner has the best score, and the other players have the same (lower) score.
  • Add the following lines to gameinfos.inc.php:
// If in the game, all losers are equal (no score to rank them or explicit in the rules that losers are not ranked between them), set this to true 
// The game end result will display "Winner" for the 1st player and "Loser" for all other players
'losers_not_ranked' => true,

Werewolves and Coup are implemented like this, as you can see here:

Adding this has the following effects:

  • On game results for this game, "Winner" or "Loser" is going to appear instead of the usual "1st, 2nd, 3rd, ...".
  • When a game is over, the result of the game will be "End of game: Victory" or "End of game: Defeat" depending on the result of the CURRENT player (instead of the usual "Victory of XXX").
  • When calculating ELO points, if there is at least one "Loser", no "victorious" player can lose ELO points, and no "losing" player can win ELO point. Usually it may happened because being tie with many players with a low rank is considered as a tie and may cost you points. If losers_not_ranked is set, we prevent this behavior and make sure you only gain/loss ELO when you get the corresponding results.

Important: this SHOULD NOT be used for cooperative games (see is_coop parameter), or for 2 players games (it makes no sense in this case).

Solo

If game supports solo variant, a negative or zero score means defeat, a positive score means victory.

Player elimination

In some games, this is useful to eliminate a player from the game in order he/she can start another game without waiting for the current game end.

This case should be rare. Please don't use player elimination feature if some player just has to wait the last 10% of the game for game end. This feature should be used only in games where players are eliminated all along the game (typical examples: "Perudo" or "The Werewolves of Miller's Hollow").

Usage:

  • Player to eliminate should NOT be active anymore (preferably use the feature in a "game" type game state).
  • In your PHP code:
 $this->eliminatePlayer( <player_to_eliminate_id> );
  • the player is informed in a dialog box that he no longer have to play and can start another game if he/she wants too (with buttons "stay at this table" "quit table and back to main site"). In any case, the player is free to start & join another table from now.
  • When your game is over, all players who have been eliminated before receive a "notification" (the small "!" icon on the top right of the BGA interface) that indicate them that "the game has ended" and invite them to review the game results.

Important: this should not be used on a player who has already left the game ("zombie") as leaving/being kicked of the game (outside of the scope of the rules) is not the same as being eliminated from the game (according to the rules), except if in the course of the game, the zombie player is eliminated according to the rules.

Important: When all surviving players are eliminated at the same time BGA framework causes the game to be abandoned automatically. To circumvent this, the game should leave 1 player not eliminated but change final scores accordingly and end the game.

Scoring Helper functions

These functions should have been API but they are not, just add them to your php game and use for every game.

   // get score
   function dbGetScore($player_id) {
       return $this->getUniqueValueFromDB("SELECT player_score FROM player WHERE player_id='$player_id'");
   }
   // set score
   function dbSetScore($player_id, $count) {
       $this->DbQuery("UPDATE player SET player_score='$count' WHERE player_id='$player_id'");
   }
   // set aux score (tie breaker)
   function dbSetAuxScore($player_id, $score) {
       $this->DbQuery("UPDATE player SET player_score_aux=$score WHERE player_id='$player_id'");
   }
   // increment score (can be negative too)
   function dbIncScore($player_id, $inc) {
       $count = $this->dbGetScore($player_id);
       if ($inc != 0) {
           $count += $inc;
           $this->dbSetScore($player_id, $count);
       }
       return $count;
   }

Reflexion time

function giveExtraTime( $player_id, $specific_time=null )
Give standard extra time to this player.
Standard extra time depends on the speed of the game (small with "slow" game option, bigger with other options).
You can also specify an exact time to add, in seconds, with the "specified_time" argument (rarely used).
function isAsync()
Returns true if game is turn based, false if it is realtime

Undo moves

Please read our BGA Undo policy before.


Important: Before using these methods, you must also add the following to your "gameinfos.inc.php" file, otherwise these methods are ineffective:

 'db_undo_support' => true

Note: if you deploy undo support after game is in production this will take into effect for new games only, old games will give user an error if user choses Undo action, but otherwise it should not affect them.


function undoSavepoint( )
Save the whole game situation inside an "Undo save point".
There is only ONE undo save point available (see BGA Undo policy). Cannot use in multiactivate state or in game state where next state is multiactive.
Note: this function does not actually do anything when it is called, it only raises the flag to store the database AFTER transaction is over. So the actual state will be saved when you exit the function calling it (technically before first queued notification is sent, which matters if you transition to game state not to user state after), this may affect what you end up saving.


function undoRestorePoint()
Restore the situation previously saved as an "Undo save point".
You must make sure that the active player is the same after and before the undoRestorePoint (ie: this is your responsibility to ensure that the player that is active when this method is called is exactly the same than the player that was active when the undoSavePoint method has been called).
   function actionUndo() {
       $this->checkAction('actionUndo');
       $this->undoRestorePoint();
       $this->gamestate->nextState('next'); // transition to single player state (i.e. beginning of player actions for this turn)
   }

Important note: if you are reading game state variable right after restore (without changing state first) it won't work properly as the global table cache is not automatically refreshed after undoRestorePoint(). So you should either change state immediately to refresh game state values, or use $this->gamestate->reloadState() to refresh the state. If you choose to do the latest, be aware that this will bring the state machine back to the state during which the save point snapshot has been taken using undoSavepoint() (which means your transition you do after has to declared in the state which was saved, not in the state which was active for your actionUndo())

Managing errors and exceptions

Note: when you throw an exception, all database changes and all notifications are cancelled immediately. This way, the game situation that existed before the request is completely restored.

throw new BgaUserException ( $error_message)
Base class to notify a user error
You must throw this exception when a player wants to do something that they are not allowed to do.
The error message will be shown to the player as a "red message".
The error message must be translated, make sure you use self::_() or $this->_() here and NOT clientranslate()
Throwing such an exception is NOT considered a bug, so it is not traced in BGA error logs.

Example from Gomoku:

     throw new BgaUserException( $this->_("There is already a stone on this intersection, you can't play there") );


throw new BgaVisibleSystemException ( $error_message)
You must throw this exception when you detect something that is not supposed to happened in your code.
The error message is shown to the user as an "Unexpected error", in order that he can report it in the forum.
The error message is logged in BGA error logs. If it happens regularly, we will report it to you.
throw new BgaSystemException ( $error_message)
Base class to notify a system exception. The message will be hidden from the user, but show in the logs. Use this if the message contains technical information.
You shouldn't use this type of exception except if you think the information shown could be critical. Indeed: a generic error message will be shown to the user, so it's going to be difficult for you to see what happened.

Zombie mode

When a player leaves a game for any reason (expelled, quit), he becomes a "zombie player". In this case, the results of the game won't count for statistics, but this is cool if the other players can finish the game anyway. That's why zombie mode exists: allow the other player to finish the game, even if the situation is not ideal.

While developing your zombie mode, keep in mind that:

  • Do not refer to the rules, because this situation is not planned by the rules.
  • Try to figure that you are playing with your friends and one of them has to leave: how can we finish the game without killing the spirit of the game?
  • The idea is NOT to develop an artificial intelligence for the game.
  • Do not try to end the game early, even in a two-player game. The zombie is there to allow the game to continue, not to end it. Trying to end the game is not supported by the framework and will likely cause unexpected errors.

Most of the time, the best thing to do when it is zombie player turn is to jump immediately to a state where he is not active anymore. For example, if he is in a game state where he has a choice between playing A and playing B, the best thing to do is NOT to choose A or B, but to pass. So, even if there's no "pass" action in the rules, add a "zombiepass" transitition in your game state and use it.

Each time a zombie player must play, your "zombieTurn" method is called.

Parameters:

  • $state: the name of the current game state.
  • $active_player: the id of the active player.

Most of the time, your zombieTurn method looks like this:

    function zombieTurn( $state, $active_player )
    {
    	$statename = $state['name'];

        if( $statename == 'myFirstGameState'
             ||  $statename == 'my2ndGameState'
             ||  $statename == 'my3rdGameState'
               ....
           )
        {
            $this->gamestate->nextState( "zombiePass" );
        }
        else
            throw new BgaVisibleSystemException( "Zombie mode not supported at this game state: ".$statename );
    }

Note that in the example above, all corresponding game state should implement "zombiePass" as a transition.

Very important: your zombie code will be called when the player leaves the game. This action is triggered from the main site and propagated to the gameserver from a server, not from a browser. As a consequence, there is no current player associated to this action. In your zombieTurn function, you must never use getCurrentPlayerId() or getCurrentPlayerName(), otherwise it will fail with a "Not logged" error message.

Player color preferences

BGA premium users may choose their preferred color for playing. For example, if they are used to play green for every board game, they can select "green" in their BGA preferences page.

Making your game compatible with colors preferences is very easy and requires only 1 line of configuration change:

On your gameinfos.inc.php file, add the following lines :

 // Favorite colors support: if set to "true", support attribution of favorite colors based on player's preferences (see reattributeColorsBasedOnPreferences PHP method)
 // NB: this parameter is used only to flag games supporting this feature; you must use (or not use) reattributeColorsBasedOnPreferences PHP method to actually enable or disable the feature.
 'favorite_colors_support' => true,

Then, on your main <your_game>.game.php file check the code of "setupNewGame". New template already have correct code, but if you editing very old game and it may be absent.

       $gameinfos = $this->getGameinfos();
       ...
       if ($gameinfos['favorite_colors_support'])
           $this->reattributeColorsBasedOnPreferences($players, $gameinfos['player_colors']); // this should be above reloadPlayersBasicInfos()
       $this->reloadPlayersBasicInfos();


The "reattributeColorsBasedOnPreferences" method reattributes all colors, taking into account players color preferences and available colors.

Note that you must update the colors to indicate the colors available for your game.

Some important remarks:

  • for some games (i.e. Chess), the color has an influence on a mechanism of the game, most of the time by giving a special advantage to a player (i.e. Starting the game). Color preference mechanism must NOT be used in such a case.
  • your logic should NEVER consider that the first player has the color X, that the second player has the color Y, and so on. If this is the case, your game will NOT be compatible with reattributeColorsBasedOnPreferences as this method attribute colors to players based on their preferences and not based as their order at the table.

Custom color assignments

Some colors don't play nicely with BGA's color difference algorithm. If you receive feedback that colors are not well chosen, you can bypass the BGA algorithm by specifying a map from user preference colors to game colors.

For example, you may wish to assign BGA's blue to your game's baby blue: "0000ff" /* Blue */ => "89CFF0", whereas otherwise, a deep purple might be chosen instead. Just be sure that the assigned colors are also present in the player_colors array passed to reattributeColorsBasedOnPreferences, otherwise the assignment will be ignored.

To do this, implement this method in your X.game.php class.

Note: the user preference colors (the keys in the returned array) should not be modified, or the code may not work as expected. These are the colors players can choose between in their profile.

    /**
     * Returns an array of user preference colors to game colors.
     * Game colors must be among those which are passed to reattributeColorsBasedOnPreferences()
     * Each game color can be an array of suitable colors, or a single color:
     * [
     *    // The first available color chosen:
     *    'ff0000' => ['990000', 'aa1122'],
     *    // This color is chosen, if available
     *    '0000ff' => '000099',
     * ]
     * If no color can be matched from this array, then the default implementation is used.
     */
    function getSpecificColorPairings(): array {
        return array(
            "ff0000" /* Red */         => null,
            "008000" /* Green */       => null,
            "0000ff" /* Blue */        => null,
            "ffa500" /* Yellow */      => null,
            "000000" /* Black */       => null,
            "ffffff" /* White */       => null,
            "e94190" /* Pink */        => null,
            "982fff" /* Purple */      => null,
            "72c3b1" /* Cyan */        => null,
            "f07f16" /* Orange */      => null,
            "bdd002" /* Khaki green */ => null,
            "7b7b7b" /* Gray */        => null,
        );
    }

Legacy games API

For some very specific games ("legacy", "campaign"), you need to keep some informations from a game to another.

This should be an exceptional situation: the legacy API is costing resources on Board Game Arena databases, and is slowing down the game setup process + game end of game process. Please do not use it for things like:

  • keeping a player preference/settings (=> player preferences and game options should be used instead)
  • keeping a statistics, a score, or a ranking, while it is not planned in the physical board game, or while there is no added value compared to BGA statistics / rankings.

You should use it for:

  • legacy games: when some components of the game has been altered in a previous game and should be kept as it is.
  • "campaign style" games: when a player is getting a "reward" at the end of a game, and should be able to use it in further games.

Important: you cannot store more than 64k of data (serialized as JSON) per player per game. If you go over 64k, storeLegacyData function is going to FAIL, and there is a risk to create a major bug (= players blocked) in your game. You MUST make sure that no more than 64k of data is used for each player for your game. For example, if you are implementing a "campaign style" game and if you allow a player to start multiple campaign, you must LIMIT the number of different campaign so that the total data size to not go over the limit. We strongly recommend you to use this:

 try 
 {
 	$this->storeLegacyTeamData( $my_data );
 }
 catch( feException $e ) // feException is a base class of BgaSystemException and others...
 {
 	if( $e->getCode() == FEX_legacy_size_exceeded )
 	{
 		// Do something here to free some space in Legacy data (ex: by removing some variables)
 	}
 	else
 		throw $e;
 }

The keys may only contain letters and numbers, underscore seems not to be allowed.

function storeLegacyData( $player_id, $key, $data, $ttl = 365 )
Store some data associated with $key for the given user / current game
In the opposite of all other game data, this data will PERSIST after the end of this table, and can be re-used
in a future table with the same game.
IMPORTANT: The only possible place where you can use this method is when the game is over at your table (last game action). Otherwise, there is a risk of conflicts between ongoing games.
TTL is a time-to-live: the maximum, and default, is 365 days.
In any way, the total data (= all keys) you can store for a given user+game is 64k (note: data is store serialized as JSON data)
NOTICE: You can store some persistant data across all tables from your game using the specific player_id 0 which is unused. In such case, it's even more important to manage correctly the size of your data to avoid any exception or issue while storing updated data (ie. you can use this for some kind of leaderbord for solo game or contest)
Note: This function cannot be called during game setup (will throw an error).
function retrieveLegacyData( $player_id, $key )
Get data associated with $key for the current game
This data is common to ALL tables from the same game for this player, and persist from one table to another.
Note: calling this function has an important cost => please call it few times (possibly: only ONCE) for each player for 1 game if possible
Note: you can use '%' in $key to retrieve all keys matching the given patterns
function removeLegacyData( $player_id, $key )
Remove some legacy data with the given key
(useful to free some data to avoid going over 64k)


function storeLegacyTeamData( $data, $ttl = 365 )
Same as storeLegacyData, except that it stores some data for the whole team within the current table and does not use a key
Ie: if players A, B and C are at a table, the legacy data will be saved for future table with (exactly) A, B and C on the table.
This is useful for games which are intended to be played several time by the same team.
Note: the data total size is still limited, so you must implement catch the FEX_legacy_size_exceeded exception if it happens
function retrieveLegacyTeamData()
Same as retrieveLegacyData, except that it retrieves some data for the whole team within the current table (set by storeLegacyTeamData)
function removeLegacyTeamData()
Same as removeLegacyData, except that it retrieves some data for the whole team within the current table (set by storeLegacyTeamData)

Players text input and moderation

This section concerns only games where the players have to write some words to play: games based on words, like "Just one" or "Codenames".

Some players will use your game to write insults or profanities. As this is part of the game and not in the game chat, these words cannot be reported by players and moderated.

If you met the following situation:

  • You are asking a player to type a text (word(s) or sentence)
  • The player can enter any text (this is not a pre-selection or anything you can control)
  • This text is visible by at least one other player

Then, you must use the following method:

function logTextForModeration( $player_id, $text )

player_id = player who write the text

text = text that has been written


This function will have no visible consequence for your game, but will allow players to report the text to moderators if something happens.

Language dependent games API

This API is used for games that are heavily language dependent. Two most common use cases are:

  • Games that have a language dependent component that are not necessarily translatable, typically a list of words. (Think of games like Codenames, Decrypto, Just One...)
  • Games with massive communication where players would like to ensure that all participants speak the same language. (Think of games like Werewolf, The Resistance, maybe even dixit...)

If this option is used, the table created will be limited only to users that have specific language in their profile. Player starting the game would be able to chose one of the languages they speak.

There is a new property language_dependency in gameinfos.inc.php which can be set like this:

 'language_dependency' => false,  //or if the property is missing, the game is not language dependent
 'language_dependency' => true, //all players at the table must speak the same language
 'language_dependency' => array( 1 => 'en', 2 => 'fr', 3 => 'it' ), //1-based list of supported languages

In the gamename.game.php file, you can get the id of selected language with the method getGameLanguage.

function getGameLanguage()
Returns an index of the selected language as defined in gameinfos.inc.php.

Languages currently available on BGA are:

 'ar' => array( 'name' => "العربية", 'code' => 'ar_AE' ),             // Arabic
 'be' => array( 'name' => "беларуская мова", 'code' => 'be_BY' ),     // Belarusian
 'bn' => array( 'name' => "বাংলা", 'code' => 'bn_BD' ),                // Bengali
 'bg' => array( 'name' => "български език", 'code' => 'bg_BG' ),      // Bulgarian
 'ca' => array( 'name' => "català", 'code' => 'ca_ES' ),              // Catalan
 'cs' => array( 'name' => "čeština", 'code' => 'cs_CZ' ),             // Czech
 'da' => array( 'name' => "dansk", 'code' => 'da_DK' ),               // Danish
 'de' => array( 'name' => "deutsch", 'code' => 'de_DE' ),             // German
 'el' => array( 'name' => "Ελληνικά", 'code' => 'el_GR' ),            // Greek
 'en' => array( 'name' => "English", 'code' => 'en_US' ),             // English
 'es' => array( 'name' => "español", 'code' => 'es_ES' ),             // Spanish
 'et' => array( 'name' => "eesti keel", 'code' => 'et_EE' ),          // Estonian       
 'fi' => array( 'name' => "suomi", 'code' => 'fi_FI' ),               // Finnish
 'fr' => array( 'name' => "français", 'code' => 'fr_FR' ),            // French
 'he' => array( 'name' => "עברית", 'code' => 'he_IL' ),               // Hebrew       
 'hi' => array( 'name' => "हिन्दी", 'code' => 'hi_IN' ),                 // Hindi
 'hr' => array( 'name' => "Hrvatski", 'code' => 'hr_HR' ),            // Croatian
 'hu' => array( 'name' => "magyar", 'code' => 'hu_HU' ),              // Hungarian
 'id' => array( 'name' => "Bahasa Indonesia", 'code' => 'id_ID' ),    // Indonesian
 'ms' => array( 'name' => "Bahasa Malaysia", 'code' => 'ms_MY' ),     // Malaysian
 'it' => array( 'name' => "italiano", 'code' => 'it_IT' ),            // Italian
 'ja' => array( 'name' => "日本語", 'code' => 'ja_JP' ),               // Japanese
 'jv' => array( 'name' => "Basa Jawa", 'code' => 'jv_JV' ),           // Javanese                       
 'ko' => array( 'name' => "한국어", 'code' => 'ko_KR' ),               // Korean
 'lt' => array( 'name' => "lietuvių", 'code' => 'lt_LT' ),            // Lithuanian
 'lv' => array( 'name' => "latviešu", 'code' => 'lv_LV' ),            // Latvian
 'nl' => array( 'name' => "nederlands", 'code' => 'nl_NL' ),          // Dutch
 'no' => array( 'name' => "norsk", 'code' => 'nb_NO' ),               // Norwegian
 'oc' => array( 'name' => "occitan", 'code' => 'oc_FR' ),             // Occitan
 'pl' => array( 'name' => "polski", 'code' => 'pl_PL' ),              // Polish
 'pt' => array( 'name' => "português",  'code' => 'pt_PT' ),          // Portuguese
 'ro' => array( 'name' => "română",  'code' => 'ro_RO'  ),            // Romanian
 'ru' => array( 'name' => "Русский язык", 'code' => 'ru_RU' ),        // Russian
 'sk' => array( 'name' => "slovenčina", 'code' => 'sk_SK' ),          // Slovak
 'sl' => array( 'name' => "slovenščina", 'code' => 'sl_SI' ),         // Slovenian       
 'sr' => array( 'name' => "Српски", 'code' => 'sr_RS' ),              // Serbian       
 'sv' => array( 'name' => "svenska", 'code' => 'sv_SE' ),             // Swedish
 'tr' => array( 'name' => "Türkçe", 'code' => 'tr_TR' ),              // Turkish       
 'uk' => array( 'name' => "Українська мова", 'code' => 'uk_UA' ),     // Ukrainian
 'zh' => array( 'name' => "中文 (漢)",  'code' => 'zh_TW' ),           // Traditional Chinese (Hong Kong, Macau, Taiwan)
 'zh-cn' => array( 'name' => "中文 (汉)", 'code' => 'zh_CN' ),         // Simplified Chinese (Mainland China, Singapore)

Debugging and Tracing

To debug php code you can use some tracing functions available from the parent class such as debug, trace, error, warn, dump.

 $this->debug("Ahh!");
 $this->dump('my_var',$my_var);

See Practical_debugging section for complete information about debugging interfaces and where to find logs.

PHP 8 upgrade

See PHP 8 upgrade