This is a documentation for Board Game Arena: play board games online !
State classes: State directory
State classes allow to create a PHP class for each game state. It allows to split the code in multiple files, without using Traits. The advantage is that the IDE understands the structure and can provide auto-completion and error highlights, that are lost in Traits.
Structure
The State class in modules/php/States/PlayerTurn.php will have this structure:
namespace Bga\Games\<MyGameName>\States;
use Bga\GameFramework\StateType;
use Bga\GameFramework\States\GameState;
use Bga\GameFramework\States\PossibleAction;
use Bga\Games\<MyGameName>\Game;
class PlayerTurn extends GameState
{
function __construct(
protected Game $game,
) {
parent::__construct($game,
id: 2,
type: StateType::ACTIVE_PLAYER,
// optional
description: clienttranslate('${actplayer} must play a card or pass'),
descriptionMyTurn: clienttranslate('${you} must play a card or pass'),
transitions: [],
updateGameProgression: false,
initialPrivate: null,
);
}
public function getArgs(): array
{
// the data sent to the front when entering the state
return [];
}
function onEnteringState(int $activePlayerId) {
// the code to run when entering the state
}
#[PossibleAction]
public function actPlayCard(int $card_id, int $activePlayerId, array $args): string
{
// the code to run when the player triggers actPlayCard with bgaPerformAction
}
function zombie(int $playerId): string {
// the code to run when the player is a Zombie
}
}
The state must extends Bga\GameFramework\States\GameState and follow the same __construct function as the example. Only $game, id and type are mandatory, other parameters are optional. In non GAME states, the zombie function is also mandatory.
Differences with states declared in states.inc.php
The function getArgs replaces the function that was declared as "args" => "argXXX" on states.inc.php. Same for the function onEnteringState that was "action" => "stXXX" on states.inc.php. The zombie function doesn't have the state as first parameter anymore, because it's not needed in this context.
The possible actions for this states don't need to be declared as an array, they will be found with the tag #[PossibleAction] over each possible action.