This is a documentation for Board Game Arena: play board games online !
Main game logic: yourgamename.game.php : diforc'h etre ar stummoù
Mont d’ar merdeerezh
Mont d’ar c’hlask
Linenn 19: | Linenn 19: | ||
The main game logic should be the only point from where you should access to the game database. You access your database using SQL queries with the following methods: | The main game logic should be the only point from where you should access to the game database. You access your database using SQL queries with the following methods: | ||
; | ; DbQuery( $sql ) | ||
: This is the generic method to access the database. | : This is the generic method to access the database. | ||
: It can execute any type of SELECT/UPDATE/DELETE/REPLACE query on the database. | : It can execute any type of SELECT/UPDATE/DELETE/REPLACE query on the database. | ||
: You should use it for UPDATE/DELETE/REPLACE query. For SELECT queries, the specialized methods above are much better. | : You should use it for UPDATE/DELETE/REPLACE query. For SELECT queries, the specialized methods above are much better. | ||
; | ; getUniqueValueFromDB( $sql ) | ||
: Returns a unique value from DB or null if no value is found. | : Returns a unique value from DB or null if no value is found. | ||
: $sql must be a SELECT query. | : $sql must be a SELECT query. | ||
: Raise an exception if more than 1 row is returned. | : Raise an exception if more than 1 row is returned. | ||
; | ; getCollectionFromDB( $sql, $bSingleValue=false ) | ||
: Returns an associative array of rows for a sql SELECT query. | : 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 key of the resulting associative array is the first field specified in the SELECT query. | ||
Linenn 53: | Linenn 53: | ||
Result: | Result: | ||
array( | array( | ||
1234 => 'myuser0' | 1234 => 'myuser0', | ||
1235 => 'myuser1' | 1235 => 'myuser1' | ||
) | ) | ||
</pre> | </pre> | ||
; | ; getNonEmptyCollectionFromDB( $sql ) | ||
: Idem, but raise an exception if the collection is empty | : Idem than previous one, but raise an exception if the collection is empty | ||
; function getObjectFromDB( $sql ) | ; function getObjectFromDB( $sql ) | ||
: Returns one row for the sql query as an associative array or null if there is no result | : Returns one row for the sql SELECT query as an associative array or null if there is no result | ||
: Raise an exception if the query return more than one row | : Raise an exception if the query return more than one row | ||
<pre> | |||
self::getObjectFromDB( "SELECT player_id id, player_name name, player_color color FROM player WHERE player_id='1234'" ); | |||
Result: | |||
array( | |||
'id' => 1234, | |||
'name' => 'myuser1', | |||
'color' => 'ff0000' | |||
) | |||
</pre> | |||
; function getNonEmptyObjectFromDB( $sql ) | ; function getNonEmptyObjectFromDB( $sql ) | ||
: Idem, but raise an exception if the query doesn't return exactly one row | : Idem, but raise an exception if the query doesn't return exactly one row |
Stumm eus an 5 Gen 2013 da 12:10
This file is the main file for your game logic. Here you initialize the game, persist data, implement the rules and notify changes to the client interface.
File Structure
The details on how the file is structured is described directly with comments on the code skeleton provided to you. Basically, here's this structure:
- EmptyGame (constructor): where you define global variables.
- setupNewGame: initial setup of the game.
- getAllDatas: where you retrieve all game data during a complete reload of the game.
- getGameProgression: where you compute the game progression indicator.
- Utility functions: your utility functions.
- Player actions: the entry points for players actions.
- Game state arguments: methods to return additional data on specific game states.
- Game state actions: the logic to run when entering a new game state.
- zombieTurn: what to do it's the turn of a zombie player.
Accessing database
The main game logic should be the only point from where you should access to the game database. You access your database using SQL queries with the following methods:
- DbQuery( $sql )
- This is the generic method to access the database.
- It can execute any type of SELECT/UPDATE/DELETE/REPLACE query on the database.
- You should use it for UPDATE/DELETE/REPLACE query. For SELECT queries, the specialized methods above are much better.
- getUniqueValueFromDB( $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( $sql, $bSingleValue=false )
- 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 if an associative array with all the field specified in the SELECT query and associated values.
- First column must be a primary or alternate key.
- The resulting collection can be empty.
- If you specified $bSingleValue=true and if your SQL query request 2 fields A and B, the method returns an associative array "A=>B"
Example 1:
self::getCollectionFromDB( "SELECT player_id id, player_name name, player_score score FROM player" ); Result: array( 1234 => array( 'id'=>1234, 'name'=>'myuser0', 'score'=>1 ), 1235 => array( 'id'=>1235, 'name'=>'myuser1', 'score'=>0 ) )
Example 2:
self::getCollectionFromDB( "SELECT player_id id, player_name name FROM player", true ); Result: array( 1234 => 'myuser0', 1235 => 'myuser1' )
- getNonEmptyCollectionFromDB( $sql )
- Idem than previous one, but raise an exception if the collection is empty
- function getObjectFromDB( $sql )
- Returns one row for the sql SELECT query as an associative array or null if there is no result
- Raise an exception if the query return more than one row
self::getObjectFromDB( "SELECT player_id id, player_name name, player_color color FROM player WHERE player_id='1234'" ); Result: array( 'id' => 1234, 'name' => 'myuser1', 'color' => 'ff0000' )
- function getNonEmptyObjectFromDB( $sql )
- Idem, but raise an exception if the query doesn't return exactly one row
Note: see Editing Game database model: dbmodel.sql to know how to define your database model.
Table class (<gamename>.game.php)
- function _( $text )
- Transparent function, used to mark strings to be translated on the server side (ex: error message)
- function clienttranslate( $string )
- Transparent function: used to mark string to be translated on client side (ex: notification message)
- function getPlayersNumber()
- Returns the number of players playing at the table
- function checkAction( $actionName, $bThrowException=true )
- Check if action is valid regarding current game state (exception if fails)
- if "bThrowException" is set to "false", the function return false in case of failure instead of throwing and exception
- function 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"
- function getActivePlayerName()
- Get the "active_player" name
- function getCurrentPlayerId()
- Get the "current_player". The current player is the one from which the action originated. It is not always the active player.
- function getCurrentPlayerName()
- Get the "current_player" name
- function getCurrentPlayerColor()
- Get the "current_player" color
- function isCurrentPlayerZombie()
- Check the "current_player" zombie status
- function activeNextPlayer()
- Make the next player active
- function activePrevPlayer()
- Make the previous player active
- function giveExtraTime( $player_id, $specific_time=null )
- Give standard extra time to this player (standard extra time is a game option)
- function initStat( $table_or_player, $name, $value, $player_id=null )
- Create a statistic entry for the specified statistics with a default value
- In case of a "player" entry, if player_id is not specified, all players are set to the same value
- function setStat( $value, $name, $player_id = null )
- Set statistic value
- function incStat( $delta, $name, $player_id = null )
- Increment (or decrement) specified value
Exceptions you can throw
- throw new BgaUserException ( $error_message)
- Base class to notify a user error
- 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.
- throw new BgaSystemVisibleException ( $error_message)
- Same as previous, except that the message is visible by the user. You can use this if the message is understandable by the user.