This is a documentation for Board Game Arena: play board games online !
「Main game logic: yourgamename.game.php」の版間の差分
Sourisdudesert (トーク | 投稿記録) |
Sourisdudesert (トーク | 投稿記録) 編集の要約なし |
||
(3人の利用者による、間の13版が非表示) | |||
57行目: | 57行目: | ||
== Accessing database == | == 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 | 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 methods below. | ||
'''IMPORTANT''' | |||
BGA is using [http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-transactions.html database transactions]. It means that your database changes WON'T BE APPLIED to the database until your request ends normally. Using transaction is in fact very useful for you: at any time, if your game logic detects that something is wrong (ex: unallowed move), you just have to throw an exception and all the changes already performed on the game situation will be removed. | |||
; DbQuery( $sql ) | ; DbQuery( $sql ) | ||
226行目: | 230行目: | ||
: Check if action is valid regarding current game state (exception if fails) | : Check if action is valid regarding current game state (exception if fails) | ||
: The action is valid if it is listed as a "possibleactions" in the current game state (see game state description). | : The action is valid if it is listed as a "possibleactions" in the current game state (see game state description). | ||
: This method | : This method MUST be called in the first place in ALL your PHP methods that handle players action, in order to make sure a player can't do an action when the rules disallow it at this moment of the game. | ||
: if "bThrowException" is set to "false", the function return false in case of failure instead of throwing and exception. This is useful when several actions are possible in order to test each of them without throwing exceptions. | : if "bThrowException" is set to "false", the function return false in case of failure instead of throwing and exception. This is useful when several actions are possible in order to test each of them without throwing exceptions. | ||
255行目: | 259行目: | ||
; $this->gamestate->setPlayersMultiactive( $players, $next_state ) | ; $this->gamestate->setPlayersMultiactive( $players, $next_state ) | ||
: Make a specific list of players active during a multiactive gamestate. | : Make a specific list of players active during a multiactive gamestate. | ||
: Bare in mind it doesn't deactivate other previously active players. | |||
: "players" is the array of player id that should be made active. | : "players" is the array of player id that should be made active. | ||
: In case "players" is empty, the method trigger the "next_state" transition to go to the next game state. | : In case "players" is empty, the method trigger the "next_state" transition to go to the next game state. | ||
262行目: | 267行目: | ||
: Usually, you call this method during a multiactive game state after a player did his action. | : Usually, you call this method during a multiactive game state after a player did his action. | ||
: If this player was the last active player, the method trigger the "next_state" transition to go to the next game state. | : If this player was the last active player, the method trigger the "next_state" transition to go to the next game state. | ||
; $this->gamestate->checkPossibleAction( $action ) | |||
: (rarely used) | |||
: This works exactly like "checkAction", except that it do NOT check if current player is active. | |||
: This is used specifically in certain game states when you want to authorize some additional actions for players that are not active at the moment. | |||
: Example: in Libertalia game, you want to authorize players to change their mind about card played. They are of course not active at the time they change their mind, so you cannot use "checkAction" and use "checkPossibleAction" instead. | |||
== Players turn order == | == Players turn order == | ||
295行目: | 306行目: | ||
== Notify players == | == Notify players == | ||
To understand | To understand notifications, please read [http://www.slideshare.net/boardgamearena/the-bga-framework-at-a-glance The BGA Framework at a glance] first. | ||
'''IMPORTANT''' | |||
Notifications are sent at the very end of the request, when it ends normally. It means that if you throw an exception for any reason (ex: move not allowed), no notifications will be sent to players. | |||
'''notifyAllPlayers( $notification_type, $notification_log, $notification_args )''' | '''notifyAllPlayers( $notification_type, $notification_log, $notification_args )''' | ||
314行目: | 329行目: | ||
You can use arguments in your notification_log strings, that refers to values defines in the "notification_args" argument (see below). | You can use arguments in your notification_log strings, that refers to values defines in the "notification_args" argument (see below). | ||
Note: you CAN use some HTML inside your notification log, and it is working. However: | |||
_ pay attention to keep the log clear. | |||
_ try to not include some HTML tags inside the "clienttranslate" method, otherwise it will make the translators work more difficult. You can use a notification argument instead, and provide your HTML through this argument. | |||
* notification_args: | * notification_args: | ||
420行目: | 439行目: | ||
== Managing errors and exceptions == | == 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 were existing before the request is completely restored. | |||
; throw new BgaUserException ( $error_message) | ; throw new BgaUserException ( $error_message) | ||
433行目: | 454行目: | ||
; throw new | ; throw new BgaVisibleSystemException ( $error_message) | ||
: You must throw this exception when you detect something that is not supposed to happened into your code. | : You must throw this exception when you detect something that is not supposed to happened into your code. | ||
: The error message is shown to the user as an "Unexpected error", in order he can report it in the forum. | : The error message is shown to the user as an "Unexpected error", in order he can report it in the forum. | ||
441行目: | 462行目: | ||
: 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. | : 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. | : 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. | |||
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: | |||
<pre> | |||
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 ); | |||
} | |||
</pre> | |||
Note that in the example above, all corresponding game state should implement "zombiePass" as a transition. |
2013年5月22日 (水) 10:01時点における最新版
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 player informations
- getPlayersNumber()
- Returns the number of players playing at the table
- Note: doesn't work in setupNewGame so use count($players) instead
- 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.
- 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 content of each value is:
- * player_name
- * player_color (ex: ff0000)
- getCurrentPlayerId()
- Get the "current_player". The current player is the one from which the action originated (the one who send the request).
- Be careful: It is not always the active player.
- In general, you shouldn't use this method, unless you are in "multiplayer" state.
- getCurrentPlayerName()
- Get the "current_player" name
- Be careful using this method (see above).
- getCurrentPlayerColor()
- Get the "current_player" color
- Be careful using this method (see above).
- isCurrentPlayerZombie()
- Check the "current_player" zombie status. If true, player leave the game.
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 methods below.
IMPORTANT
BGA is using database transactions. It means that your database changes WON'T BE APPLIED to the database until your request ends normally. Using transaction is in fact very useful for you: at any time, if your game logic detects that something is wrong (ex: unallowed move), you just have to throw an exception and all the changes already performed on the game situation will be removed.
- 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
Example:
self::getObjectFromDB( "SELECT player_id id, player_name name, player_score score FROM player WHERE player_id='$player_id'" ); Result: array( 'id'=>1234, 'name'=>'myuser0', 'score'=>1 )
- getNonEmptyObjectFromDB( $sql )
- Idem than previous one, but raise an exception if no row is found
- getObjectListFromDB( $sql, $bUniqueValue=false )
- Return an array of rows for a sql SELECT query.
- the result if the same than "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:
self::getObjectListFromDB( "SELECT player_id id, player_name name, player_score score FROM player" ); Result: array( array( 'id'=>1234, 'name'=>'myuser0', 'score'=>1 ), array( 'id'=>1235, 'name'=>'myuser1', 'score'=>0 ) )
Example 2:
self::getObjectListFromDB( "SELECT player_id id, player_name name FROM player", true ); Result: array( 'myuser0', 'myuser1' )
- getDoubleKeyCollectionFromDB( $sql, $bSingleValue=false )
- 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()
- Return the number of row affected by the last operation
- escapeStringForDB( $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.
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.
Use globals
Sometimes, you have to keep a single integer value that is global to your game, and you don't want to create a DB table specifically for it.
Using a BGA framework "global", you can do such a thing. Your value will be stored in the "global" table in database, and you can access it with simple methods.
initGameStateLabels
This method is located at the beginning of your game logic. This is the place you defines the globals used in your game logic, by assigning them IDs.
You can define up to 89 globals, with IDs from 10 to 89. You must NOT use globals outside this range as globals are used by other components of the framework.
self::initGameStateLabels( array( "my_first_global_variable" => 10, "my_second_global_variable" => 11 ) );
setGameStateInitialValue( $value_label, $value_value )
Init your global value. Must be called before any use of your global, so you should call this method from your "setupNewGame" method.
getGameStateValue( $value_label )
Retrieve the current value of a global.
setGameStateValue( $value_label, $value_value )
Set the current value of a global.
incGameStateValue( $value_label, $increment )
Increment the current value of a global. If increment is negative, decrement the value of the global.
Return the final value of the global.
Game states and active players
- checkAction( $actionName, $bThrowException=true )
- Check if action is valid regarding current game state (exception if fails)
- The action is valid if it is listed as a "possibleactions" in the current game state (see game state description).
- This method MUST be called in the first place in ALL your PHP methods that handle players action, in order to make sure a player can't do an action when the rules disallow it at this moment of the game.
- if "bThrowException" is set to "false", the function return false in case of failure instead of throwing and exception. This is useful when several actions are possible in order to test each of them without throwing exceptions.
- activeNextPlayer()
- Make the next player active in the natural player order.
- Note: you CANT use this method in a "activeplayer" or "multipleactiveplayer" state. You must use a "game" type game state for this.
- activePrevPlayer()
- Make the previous player active (in the natural player order).
- Note: you CANT 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 CANT use this method in a "activeplayer" or "multipleactiveplayer" state. You must use a "game" type game state for this.
- $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 is the latter case.
- $this->gamestate->setAllPlayersMultiactive()
- With this method, all playing players are made active.
- Usually, you use this method at the beginning (ex: "st" action method) of a multiplayer game state when all players have to do some action.
- $this->gamestate->setPlayersMultiactive( $players, $next_state )
- Make a specific list of players active during a multiactive gamestate.
- Bare in mind it doesn't deactivate other previously active players.
- "players" is the array of player id that should be made active.
- In case "players" is empty, the method trigger the "next_state" transition to go to the next game state.
- $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.
- If this player was the last active player, the method trigger the "next_state" transition to go to the next game state.
- $this->gamestate->checkPossibleAction( $action )
- (rarely used)
- This works exactly like "checkAction", except that it do NOT check if current player is active.
- This is used specifically in certain game states when you want to authorize some additional actions for players that are not active at the moment.
- Example: in Libertalia game, you want to authorize players to change their mind about card played. They are of course not active at the time they change their mind, so you cannot use "checkAction" and use "checkPossibleAction" instead.
Players turn 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 1, 2 and 3 are around the table, in this order, the method returns:
array( 1 => 2, 2 => 3, 3 => 1, 0 => 1 );
getPrevPlayerTable()
Same as above, but the associative array associate the previous player around the table.
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.
Notify players
To understand notifications, please read The BGA Framework at a glance first.
IMPORTANT
Notifications are sent at the very end of the request, when it ends normally. It means that if you throw an exception for any reason (ex: move not allowed), no notifications will be sent to players.
notifyAllPlayers( $notification_type, $notification_log, $notification_args )
Send a notification to all players of the game.
- 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).
- notification_log:
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.
If you define a real string here, you should use "clienttranslate" method to make sure it can be translate.
You can use arguments in your notification_log strings, that refers to values defines in the "notification_args" argument (see below).
Note: you CAN use some HTML inside your notification log, and it is working. However: _ pay attention to keep the log clear. _ try to not include some HTML tags inside the "clienttranslate" method, otherwise it will make the translators work more difficult. You can use a notification argument instead, and provide your HTML through this argument.
- 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"):
self::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 date must be sent with this method, as a cheater could see it even it is not used explicitly by the game interface logic. If you want to send private information to a player, please use notifyPlayer below.
notifyPlayer( $player_id, $notification_type, $notification_log, $notification_args )
Same as above, except that the notification is sent to one player only.
This method must be used each time some private information must be transmitted to a player.
Game statistics
There are 2 types of statistics:
- a "player" statistic is a statistic associated to a player
- a "table" statistics is a statistic not associated to a player (global statistic for this game).
See Game statistics: stats.inc.php to see how you defines statistics for your game.
initStat( $table_or_player, $name, $value, $player_id=null ) Create a statistic entry for the specified statistics with a default value. This method must be called for each statistics of your game, in your setupNewGame method.
'table_or_player' must be set to "table" if this is a table statistics, or "player" if this is a player statistics.
'name' is the name of your statistics, as it has been defined in your stats.inc.php file.
'value' is the initial value of the statistics. If this is a player statistics and if the player is not specified by "player_id" argument, the value is set for ALL players.
function setStat( $value, $name, $player_id = null )
Set a statistic 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. Same behavior as above.
Translations
See Translations
Manage player scores and Tie breaker
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 self::DbQuery( "UPDATE player SET player_score=player_score+2 WHERE player_id='".self::getActivePlayerId()."'" ); // Set score of active player to 5 self::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 Constructor method like this:
$this->tie_breaker_description = self::_("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.
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).
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 were existing 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 want to do something that he is not allowed to do.
- The error message will be shown to the player as a "red message", so it must be translated.
- Throwing such an exception is NOT considered as a bug, so it is not traced in BGA error logs.
Example from Gomoku:
throw new BgaUserException( self::_("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 into your code.
- The error message is shown to the user as an "Unexpected error", in order 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.
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.