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

Players actions: yourgamename.action.php: Разлика между версии

От Board Game Arena
Направо към навигацията Направо към търсенето
(Created page with "=== APP_GameAction class (<gamename>.action.php) === ; function isArg( $argName ) : Is this argument filled ? ; function getArg( $argName, $argType, $mandatory=false, $defaul...")
 
Редакция без резюме
Ред 1: Ред 1:
=== APP_GameAction class (<gamename>.action.php) ===


; function isArg( $argName )
== Purpose of this file ==
: Is this argument filled ?
 
With this file, you define all the players entry points (ie: possible game actions) of your game.
 
This file is a sort of "bridge" between the AJAX calls you are doing from your Javascript client side, and your main PHP code in "yourgame.game.php".
 
The role of the methods defined in this file is to filter the arguments, eventually to format them a little bit, and then to call a corresponding PHP method from your main game logic ("yourgame.game.php" file).
 
Methods in this file must be short: no game logic must be introduced here.
 
== Example of typical action method ==
 
(from Reversi example)
 
<pre>
    public function playDisc()
    {
        self::setAjaxMode();   
        $x = self::getArg( "x", AT_posint, true );
        $y = self::getArg( "y", AT_posint, true );
        $result = $this->game->playDisc( $x, $y );
        self::ajaxResponse( );
    }
</pre>
 
== Methods to use in action methods ==
 
; function setAjaxMode
: Must be use at the beginning of each action method.
 
; function ajaxResponse
: Must be use at the end of each action method.
 
; function getArg( $argName, $argType, $mandatory=false, $default=NULL, $argTypeDetails=array(), $bCanFail=false  )
; function getArg( $argName, $argType, $mandatory=false, $default=NULL, $argTypeDetails=array(), $bCanFail=false  )
: Get script argument with the correct type
: This method must be used to retrieve the arguments sent with your AJAX query.
: You must NOT use "_GET", "_POST" or equivalent PHP variables to do this, as it is unsafe.
: This method use the following arguments:
: * argName: the name of the argument to retrieve.
: * argType: the type of the argument. You should use one of the following:
:  'AT_int' for an integer
:  'AT_posint' for a positive integer
:  'AT_float' for a float
:  'AT_bool' for 1/0/true/false
:  'AT_enum' for an enumeration (argTypeDetails list the possible values as an array)
:  'AT_alphanum' for a string with 0-9a-zA-Z_ and space
 
 
 
: bCanFail means than a validation failure is possible (user input)
: bCanFail means than a validation failure is possible (user input)
: The main argType values are as follows.
: The main argType values are as follows.
<pre>
<pre>
define( 'AT_int', 0 );        //  an integer
 
define( 'AT_posint', 1 );    //  a positive integer
 
define( 'AT_float', 2 );     //  a float
; function isArg( $argName )
define( 'AT_email', 3 );      //  an email 
: Return "true" or "false" whether "argName" has been specified as an argument of the AJAX request or not.
define( 'AT_url', 4 );        //  a URL
 
define( 'AT_bool', 5 );      //  1/0/true/false
define( 'AT_enum', 6 );      //  argTypeDetails list the possible values
define( 'AT_alphanum', 7 );  //  only 0-9a-zA-Z_ and space
</pre>
</pre>

Версия от 15:56, 11 януари 2013

Purpose of this file

With this file, you define all the players entry points (ie: possible game actions) of your game.

This file is a sort of "bridge" between the AJAX calls you are doing from your Javascript client side, and your main PHP code in "yourgame.game.php".

The role of the methods defined in this file is to filter the arguments, eventually to format them a little bit, and then to call a corresponding PHP method from your main game logic ("yourgame.game.php" file).

Methods in this file must be short: no game logic must be introduced here.

Example of typical action method

(from Reversi example)

    public function playDisc()
    {
        self::setAjaxMode();     
        $x = self::getArg( "x", AT_posint, true );
        $y = self::getArg( "y", AT_posint, true );
        $result = $this->game->playDisc( $x, $y );
        self::ajaxResponse( );
    }

Methods to use in action methods

function setAjaxMode
Must be use at the beginning of each action method.
function ajaxResponse
Must be use at the end of each action method.
function getArg( $argName, $argType, $mandatory=false, $default=NULL, $argTypeDetails=array(), $bCanFail=false )
This method must be used to retrieve the arguments sent with your AJAX query.
You must NOT use "_GET", "_POST" or equivalent PHP variables to do this, as it is unsafe.
This method use the following arguments:
* argName: the name of the argument to retrieve.
* argType: the type of the argument. You should use one of the following:
'AT_int' for an integer
'AT_posint' for a positive integer
'AT_float' for a float
'AT_bool' for 1/0/true/false
'AT_enum' for an enumeration (argTypeDetails list the possible values as an array)
'AT_alphanum' for a string with 0-9a-zA-Z_ and space


bCanFail means than a validation failure is possible (user input)
The main argType values are as follows.


; function isArg( $argName )
: Return "true" or "false" whether "argName" has been specified as an argument of the AJAX request or not.