Page 1 of 1

How to implement a common step where I need to roll 2 dice

Posted: 16 October 2020, 07:41
by stefano
Hello,

where can I read an example of creating a common initial step where I need to roll 2 dice and position 2 tokens depending on the result of the dice?
The roll must be common to all the players, un-repeatable (e.g. if I press F5) and the result must be applied to all players.

I'm trying to implement the game snowtime.

Thanks

Re: How to implement a common step where I need to roll 2 dice

Posted: 16 October 2020, 08:19
by Tisaac
stefano wrote: 16 October 2020, 07:41 Hello,

where can I read an example of creating a common initial step where I need to roll 2 dice and position 2 tokens depending on the result of the dice?
The roll must be common to all the players, un-repeatable (e.g. if I press F5) and the result must be applied to all players.

I'm trying to implement the game snowtime.

Thanks
This is quite simple actually, you just need a gamestate that will do the roll on the backend by taking a random value, and then dispatch notifications to all players to do the animations on the client of players.

Re: How to implement a common step where I need to roll 2 dice

Posted: 16 October 2020, 09:33
by stefano
Would you mind giving me some snippets of code?

Re: How to implement a common step where I need to roll 2 dice

Posted: 16 October 2020, 10:49
by stefano
I'm using this member, but I'm getting different results for the players:

function argPlaceFruit()
{
$result1 = rand(1, 6);
$result2 = rand(1, 6);
return array(
'diceresult1' => $result1,
'diceresult2' => $result2,
);
}


2 => array(
"name" => "PlaceFruit",
"description" => clienttranslate("Rolling the dice"),
"type" => "game",
"action" => "stPlaceFruit",
"args" => "argPlaceFruit",
"updateGameProgression" => true,
"transitions" => array( "PlayersTurn" => 3 )
),

Re: How to implement a common step where I need to roll 2 dice

Posted: 16 October 2020, 10:56
by Tisaac
stefano wrote: 16 October 2020, 09:33 Would you mind giving me some snippets of code?
Something like this :

Code: Select all

function stRollDice(){
		$face = bga_rand(1, 6);
 		$this->setGameStateValue('diceFace', $face);
		$this->gamestate->nextState('');
	}

function argMyState(){
  return [
     'dice' => this->getGameStateValue('diceFace')
   ]
}
With a state machine :

Code: Select all

    ST_ROLL_DICE => [
      "name" => "rollDice",
      "description" => '',
      "type" => "game",
      "action" => "stRollDice",
      "transitions" => ['' => ST_MY_STATE]
    ],
    
    ST_MY_STATE => [
       ....
       "arg" => "argMyState"
   ]

Re: How to implement a common step where I need to roll 2 dice

Posted: 16 October 2020, 11:40
by stefano
Working now... the trick was to do it into the "st" function and not in the "arg" one... I used one single state in the machine, passing the value from st to arg function using the global labels