This is a documentation for Board Game Arena: play board games online !
State classes: State directory: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
{{Studio_Framework_Navigation}} | {{Studio_Framework_Navigation}}<big>'''🚧 Work in Progress 🚧'''</big> | ||
''You can test it on the Studio, but not yet deploy it to production.'' | |||
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. | 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 == | ==Structure == | ||
The State class in <code>modules/php/States/PlayerTurn.php</code> will have this structure: | The State class in <code>modules/php/States/PlayerTurn.php</code> will have this structure: | ||
| Line 60: | Line 59: | ||
The state must extends <code>Bga\GameFramework\States\GameState</code> and follow the same <code>__construct</code> function as the example. Only $game, id and type are mandatory, other parameters are optional. In non GAME states, the <code>zombie</code> function is also mandatory. | The state must extends <code>Bga\GameFramework\States\GameState</code> and follow the same <code>__construct</code> function as the example. Only $game, id and type are mandatory, other parameters are optional. In non GAME states, the <code>zombie</code> function is also mandatory. | ||
== Differences with states declared in states.inc.php == | ==Differences with states declared in states.inc.php == | ||
=== Moved elements === | ===Moved elements === | ||
The function <code>getArgs</code> replaces the function that was declared as <code>"args" => "argXXX"</code> on states.inc.php. Same for the function <code>onEnteringState</code> that was <code>"action" => "stXXX"</code> on states.inc.php. The <code>zombie</code> function doesn't have the state as first parameter anymore, because it's not needed in this context. | The function <code>getArgs</code> replaces the function that was declared as <code>"args" => "argXXX"</code> on states.inc.php. Same for the function <code>onEnteringState</code> that was <code>"action" => "stXXX"</code> on states.inc.php. The <code>zombie</code> function doesn't have the state as first parameter anymore, because it's not needed in this context. | ||
| Line 72: | Line 71: | ||
The <code>getArgs</code> function can set some predefined parameters that will be automatically filled: | The <code>getArgs</code> function can set some predefined parameters that will be automatically filled: | ||
* <code>int $activePlayerId/$active_player_id</code> will be filled by the result of getActivePlayerId. To be used on ACTIVE_PLAYER states only. | *<code>int $activePlayerId/$active_player_id</code> will be filled by the result of getActivePlayerId. To be used on ACTIVE_PLAYER states only. | ||
* <code>int $playerId/$player_id/$currentPlayerId/$current_player_id</code> will be filled by the player id of the current PRIVATE state. To be used on PRIVATE states only. | *<code>int $playerId/$player_id/$currentPlayerId/$current_player_id</code> will be filled by the player id of the current PRIVATE state. To be used on PRIVATE states only. | ||
The <code>onEnteringState</code> function can set some predefined parameters that will be automatically filled: | The <code>onEnteringState</code> function can set some predefined parameters that will be automatically filled: | ||
* <code>array $args</code> will be filled by the result of <code>$this->getArgs()</code>. | *<code>array $args</code> will be filled by the result of <code>$this->getArgs()</code>. | ||
* <code>int $activePlayerId/$active_player_id</code> will be filled by the result of getActivePlayerId. To be used on ACTIVE_PLAYER states only. | *<code>int $activePlayerId/$active_player_id</code> will be filled by the result of getActivePlayerId. To be used on ACTIVE_PLAYER states only. | ||
* <code>int $playerId/$player_id/$currentPlayerId/$current_player_id</code> will be filled by the player id of the current PRIVATE state. To be used on PRIVATE states only. | *<code>int $playerId/$player_id/$currentPlayerId/$current_player_id</code> will be filled by the player id of the current PRIVATE state. To be used on PRIVATE states only. | ||
The <code>actXXX</code> functions (auto-wired functions) can set some predefined parameters that will be automatically filled: | The <code>actXXX</code> functions (auto-wired functions) can set some predefined parameters that will be automatically filled: | ||
* <code>array $args</code> will be filled by the result of <code>$this->getArgs()</code>. | *<code>array $args</code> will be filled by the result of <code>$this->getArgs()</code>. | ||
* <code>int $activePlayerId/$active_player_id</code> will be filled by the result of getActivePlayerId. To be used on ACTIVE_PLAYER states only. | *<code>int $activePlayerId/$active_player_id</code> will be filled by the result of getActivePlayerId. To be used on ACTIVE_PLAYER states only. | ||
* <code>int $currentPlayerId/$current_player_id</code> will be filled by the result of getCurrentPlayerId. | *<code>int $currentPlayerId/$current_player_id</code> will be filled by the result of getCurrentPlayerId. | ||
For all those functions, and also the <code>zombie</code> function, they can now send a redirection to a game state as a returned result. The allowed writings are: | For all those functions, and also the <code>zombie</code> function, they can now send a redirection to a game state as a returned result. The allowed writings are: | ||
* <code>return NextPlayer::class</code> will redirect to the state declared in that class. | *<code>return NextPlayer::class</code> will redirect to the state declared in that class. | ||
* <code>return ST_END_GAME;</code> = <code>return 99;</code> will redirect to the state of that id. It must be typed as int, numbers in a string won't work. | *<code>return ST_END_GAME;</code> = <code>return 99;</code> will redirect to the state of that id. It must be typed as int, numbers in a string won't work. | ||
* <code>return 'nextPlayer';</code> will redirect to the transition of that name (requires <code>transitions</code> to be declared in the constructor). | *<code>return 'nextPlayer';</code> will redirect to the transition of that name (requires <code>transitions</code> to be declared in the constructor). | ||
''If you use this writing, remove <code>$this->gamestate->nexState</code> to avoid double redirection!'' | ''If you use this writing, remove <code>$this->gamestate->nexState</code> to avoid double redirection!'' | ||
[[Category:Studio]] | [[Category:Studio]] | ||
Revision as of 13:23, 16 September 2025
🚧 Work in Progress 🚧
You can test it on the Studio, but not yet deploy it to production.
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
Moved elements
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.
The functions declared in Game.php will be accessible with $this->game instead of $this. The Game sub-objects are available on the State class too, so you can write $this->notif->all without needing to pass through the game variable.
New elements
The getArgs function can set some predefined parameters that will be automatically filled:
int $activePlayerId/$active_player_idwill be filled by the result of getActivePlayerId. To be used on ACTIVE_PLAYER states only.int $playerId/$player_id/$currentPlayerId/$current_player_idwill be filled by the player id of the current PRIVATE state. To be used on PRIVATE states only.
The onEnteringState function can set some predefined parameters that will be automatically filled:
array $argswill be filled by the result of$this->getArgs().int $activePlayerId/$active_player_idwill be filled by the result of getActivePlayerId. To be used on ACTIVE_PLAYER states only.int $playerId/$player_id/$currentPlayerId/$current_player_idwill be filled by the player id of the current PRIVATE state. To be used on PRIVATE states only.
The actXXX functions (auto-wired functions) can set some predefined parameters that will be automatically filled:
array $argswill be filled by the result of$this->getArgs().int $activePlayerId/$active_player_idwill be filled by the result of getActivePlayerId. To be used on ACTIVE_PLAYER states only.int $currentPlayerId/$current_player_idwill be filled by the result of getCurrentPlayerId.
For all those functions, and also the zombie function, they can now send a redirection to a game state as a returned result. The allowed writings are:
return NextPlayer::classwill redirect to the state declared in that class.return ST_END_GAME;=return 99;will redirect to the state of that id. It must be typed as int, numbers in a string won't work.return 'nextPlayer';will redirect to the transition of that name (requirestransitionsto be declared in the constructor).
If you use this writing, remove $this->gamestate->nexState to avoid double redirection!