This is my code:
State Machine:
Code: Select all
1 => array(
"name" => "gameSetup",
"description" => clienttranslate("Game setup"),
"type" => "manager",
"action" => "stGameSetup",
"transitions" => array( "" => 2 )
),
2 => array(
"name" => "PlaceFruit",
"description" => clienttranslate("Rolling the dice"),
"type" => "game",
"action" => "stPlaceFruit",
"args" => "argPlaceFruit",
"possibleactions" => array( "PlaceFruitDone"),
"updateGameProgression" => true,
"transitions" => array( "PlayersTurn" => 3 )
),
3 => array(
"name" => "Playacard",
"description" => clienttranslate("Play one card"),
"type" => "multipleactiveplayer",
"action" => "stPlayacard",
//"updateGameProgression" => true,
"possibleactions" => array( 'Playacard' ),
"transitions" => array( "Applyaction" => 4 )
),Code: Select all
function gPlaceFruitDone()
{
$this->gamestate->nextState( 'PlayersTurn' );
}
function stPlaceFruit()
{
$result1 = bga_rand(1, 6);
$result2 = bga_rand(1, 6);
self::setGameStateValue('diceFace1', $result1 );
self::setGameStateValue('diceFace2', $result2 );
$sql = "UPDATE fruits SET numbers=numbers+1 WHERE treelevel = '$result1'";
self::DbQuery( $sql );
$sql = "UPDATE fruits SET numbers=numbers+1 WHERE treelevel = '$result2'";
self::DbQuery( $sql );
}
function argPlaceFruit()
{
$result1 = self::getGameStateValue('diceFace1');
$result2 = self::getGameStateValue('diceFace2');
return array(
'diceresult1' => $result1,
'diceresult2' => $result2,
);
}
Code: Select all
onEnteringState: function( stateName, args )
{
console.log( 'Entering state: '+stateName );
switch( stateName )
{
case 'PlaceFruit':
console.log( "Dice result: " + args.args.diceresult1 + " " + args.args.diceresult2);
//replace all of this kind with dojo.byId() at BGA Studio
dice1=document.getElementById("dice1");
dice2=document.getElementById("dice2");
diceresult1=document.getElementById("diceresult1");
diceresult2=document.getElementById("diceresult2");
message=document.getElementById("message");
message.value="the result is "+args.args.diceresult1 + " " + args.args.diceresult2;
dice1.className ="rolled1";
dice2.className ="rolled2";
diceresult1.className ="num1"+args.args.diceresult1;
diceresult2.className ="num2"+args.args.diceresult2;
//this.addFruitBoard( args.args.diceresult1, 1, 2337413 );
//this.addFruitBoard( args.args.diceresult2, 2, 2337413 );
this.onDiceCompleted();
break;
}
},
onDiceCompleted: function( evt )
{
console.log( 'PlaceFruitDone' );
// Preventing default browser reaction
//dojo.stopEvent( evt );
// Check that this action is possible (see "possibleactions" in states.inc.php)
if( ! this.checkAction( 'PlaceFruitDone' ) )
{
console.log('Not PlaceFruitDone action!');
//return;
}
this.ajaxcall( "/snowtime/snowtime/PlaceFruitDone.html", { },
this, function( result ) {
// What to do after the server call if it succeeded
// (most of the time: nothing)
}, function( is_error) {
// What to do after the server call in anyway (success or failure)
// (most of the time: nothing)
} );
},