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

Diferenzas entre revisións de «Players actions: yourgamename.action.php»

De Board Game Arena
Saltar ata a navegación Saltar á procura
(Created page with "=== APP_GameAction class (<gamename>.action.php) === ; function isArg( $argName ) : Is this argument filled ? ; function getArg( $argName, $argType, $mandatory=false, $defaul...")
 
 
(Non se amosan 7 revisións do historial feitas por 3 usuarios.)
Liña 1: Liña 1:
=== APP_GameAction class (<gamename>.action.php) ===


; function isArg( $argName )
== Purpose of this file ==
: Is this argument filled ?
 
; function getArg( $argName, $argType, $mandatory=false, $default=NULL, $argTypeDetails=array(), $bCanFail=false  )
With this file, you define all the player entry points (i.e., possible game actions) for your game.
: Get script argument with the correct type
 
: bCanFail means than a validation failure is possible (user input)
This file is a sort of "bridge" between the AJAX calls you perform from the Javascript client side, and your main PHP code in "yourgame.game.php".
: The main argType values are as follows.
 
The role of the methods defined in this file is to filter the arguments, format them a bit, and then call a corresponding PHP method from your main game logic ("yourgame.game.php" file).
 
Methods in this file should be short: no game logic must be introduced here.
 
== Example of typical action method ==
 
(from Reversi example)
 
<pre>
<pre>
define( 'AT_int', 0 );       //  an integer
    public function playDisc()
define( 'AT_posint', 1 );    // a positive integer  
    {
define( 'AT_float', 2 );      //  a float
        self::setAjaxMode();   
define( 'AT_email', 3 );      // an email 
        $x = self::getArg( "x", AT_posint, true );
define( 'AT_url', 4 );        //  a URL
        $y = self::getArg( "y", AT_posint, true );
define( 'AT_bool', 5 );       // 1/0/true/false
        $result = $this->game->playDisc( $x, $y );
define( 'AT_enum', 6 );       //  argTypeDetails list the possible values
        self::ajaxResponse( );
define( 'AT_alphanum', 7 );   //  only 0-9a-zA-Z_ and space
     }
</pre>
 
== Methods to use in action methods ==
 
'''function setAjaxMode()'''
 
Must be used at the beginning of each action method.
 
'''function ajaxResponse()'''
 
Must be used 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 uses 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 lists the possible values as an array)
  'AT_alphanum' for a string with 0-9a-zA-Z_ and space
  'AT_numberlist' for a list of numbers separated with "," or ";" (example: 1,4;2,3;-1,2).
* mandatory: specify "true" if the argument is mandatory.
* default: if mandatory=false, you can specify here a default value in case the argument is not present.
* argTypeDetails: see AT_enum above.
* bCanFail: if true, specify that it may be possible that the argument won't be of the type specified by argType (and then do not log this as a fatal error in the system, and return a standard exception to the player).
 
 
'''function isArg( $argName )'''
 
This is a useful method when you only want to check if an argument is present or not present in your AJAX request (and don't care about the value).
 
It returns "true" or "false" according to whether "argName" has been specified as an argument of the AJAX request or not.
 
== Useful tip: retrieve a list of numbers ==
 
If your Javascript sends a list of integers separated by ";" (example: "1;2;3;4") as an argument, you can transform them into a PHP array with the following:
 
<pre>
    public function playCards()
    {
        self::setAjaxMode();   
 
        $card_ids_raw = self::getArg( "card_ids", AT_numberlist, true );
       
        // Removing last ';' if exists
        if( substr( $card_ids_raw, -1 ) == ';' )
            $card_ids_raw = substr( $card_ids_raw, 0, -1 );
        if( $card_ids_raw == '' )
            $card_ids = array();
        else
            $card_ids = explode( ';', $card_ids_raw );
 
        $this->game->playCards( $card_ids );
        self::ajaxResponse( );
    }
</pre>
</pre>

Revisión actual feita o 16 de setembro de 2018 ás 17:46

Purpose of this file

With this file, you define all the player entry points (i.e., possible game actions) for your game.

This file is a sort of "bridge" between the AJAX calls you perform from the 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, format them a bit, and then call a corresponding PHP method from your main game logic ("yourgame.game.php" file).

Methods in this file should 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 used at the beginning of each action method.

function ajaxResponse()

Must be used 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 uses 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 lists the possible values as an array)
 'AT_alphanum' for a string with 0-9a-zA-Z_ and space
 'AT_numberlist' for a list of numbers separated with "," or ";" (example: 1,4;2,3;-1,2).
  • mandatory: specify "true" if the argument is mandatory.
  • default: if mandatory=false, you can specify here a default value in case the argument is not present.
  • argTypeDetails: see AT_enum above.
  • bCanFail: if true, specify that it may be possible that the argument won't be of the type specified by argType (and then do not log this as a fatal error in the system, and return a standard exception to the player).


function isArg( $argName )

This is a useful method when you only want to check if an argument is present or not present in your AJAX request (and don't care about the value).

It returns "true" or "false" according to whether "argName" has been specified as an argument of the AJAX request or not.

Useful tip: retrieve a list of numbers

If your Javascript sends a list of integers separated by ";" (example: "1;2;3;4") as an argument, you can transform them into a PHP array with the following:

    public function playCards()
    {
        self::setAjaxMode();     

        $card_ids_raw = self::getArg( "card_ids", AT_numberlist, true );
        
        // Removing last ';' if exists
        if( substr( $card_ids_raw, -1 ) == ';' )
            $card_ids_raw = substr( $card_ids_raw, 0, -1 );
        if( $card_ids_raw == '' )
            $card_ids = array();
        else
            $card_ids = explode( ';', $card_ids_raw );

        $this->game->playCards( $card_ids );
        self::ajaxResponse( );
    }