Creating a Command without Sub-Commands

To create a command without sub-commands is much simpler than creating commands with sub-commands. Instead of implementing a constructor and the Execute() function, you implement the Execute() function and the OnRollBack() function.

Since you don’t have to execute the logic for each sub-command, the only logic that needs to be in the Execute() function is the logic for the task you are trying to complete. I’ll use the CAddWeaponsCmd() class for an example.

<?
require_once( “DB.php” );
require_once( “CCommand.php” );

require_once( “settings.php” );

class CAddWeaponsCmd extends CCommand
{
    // information needed to roll back
    var $m_sPlayerName;
    var $m_nWaterBalloons;   
    var $m_nPlasticBats;
    var $m_nSlingshots;

    function Execute( $sPlayerName, $nWaterBalloons,
        $nPlasticBats, $nSlingshots )
    {
        // make sure that the input are integer values
        $nWaterBalloons = (int)$nWaterBalloons;
        $nPlasticBats = (int)$nPlasticBats;
        $nSlingshots = (int)$nSlingshots;

        // connect to the db
        $db = DB::connect( $GLOBALS[‘g_PearDBDSN’] );
        if (DB::isError( $db ))
        {
            // let everyone know that there has been a problem
            $this->OnError(“Failed to connect to the
database using ” . $GLOBALS[‘g_PearDBDSN’]);

            // return failure
            return CMD_ERROR;
        }

        $query = “UPDATE ” . $GLOBALS[‘g_DBTable’] . “ SET WaterBalloons
            = WaterBalloons + $nWaterBalloons, PlasticBats = PlasticBats +
            $nPlasticBats, Slingshots = Slingshots + $nSlingshots WHERE
            PlayerName = ‘$sPlayerName’”;
        $result = $db->Query( $query );
        if (DB::isError( $result ))
        {
            // let everyone know there is a problem
            $this->OnError(“Failed to update ”.$GLOBALS[‘g_DBTable’]);

            // return failure
            return CMD_ERROR;
        } else {

            // set up member variables
            $this->m_sPlayerName = $sPlayerName;
            $this->m_nWaterBalloons = $nWaterBalloons;               
            $this->m_nPlasticBats = $nPlasticBats;
            $this->m_nSlingshots = $nSlingshots;

            return CMD_FINISHED;
        }
    }

    function OnRollBack()
    {
        // since there is no command to deduct weapons,
            // this rollback must be implemented here
        // connect to the db
        $db = DB::connect( $GLOBALS[‘g_PearDBDSN’] );
        if (DB::isError( $db ))
        {
            // let everyone know that there has been a problem
            $this->OnError(“Failed to connect to the database using ”
                . $GLOBALS[‘g_PearDBDSN’]);

            // return failure
            return CMD_ERROR;
        }

        $result = $db->Query( “UPDATE ”.$GLOBALS[‘g_DBTable’].“ SET ” .
            “WaterBalloons = WaterBalloons + $this->m_nWaterBalloons, ” .
            “PlasticBats = PlasticBats + $this->m_nPlasticBats, ” .
            “Slingshots = Slingshots + $this->m_nSlingshots ” .
            “WHERE PlayerName = ‘$this->m_sPlayerName’” );
        if (DB::isError( $result ))
        {
            // there is nothing we can do if the
                // rollback failed, so just let it go
        }

        return;
    }
}

?>

This command is much more straightforward than a class with sub-commands. The Execute() function takes the arguments that you want to update. In this case, since you are adding weapons to your player, it needs to take a player name, the number of water bal-loons to add, the number of plastic bats to add, and the number of slingshots to add.

The Execute() function starts off by making sure that the variables are integers. Then it tries to connect to the database. If the Execute() function cannot connect to the database then an error has occurred and you need to let the user know what has happened. If the Execute() function connected to the database successfully then you can update the record. The $db variable is using the PEAR API to connect to the database. The PEAR API contains a member function called Query() that will execute the query you specified. If an error occurs during execution you will need to call the OnRollBack() function. Otherwise you update the players’ stats and return control back to the game so it can re-render the page.

The OnRollBack() function attempts to connect to the database. After the OnRollBack() function has successfully connected to the database it attempts to update the database back to the original values that the player had before the query failed. If anything in the OnRollBack() function fails there isn’t too much you can do.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset