Sign In Register

How can we help you today?

Start a new topic
Answered

Skipping turn or removing a player from challenge

Hi,


I am making a turn based last man standing game which means that I will have to skip turns of the players who are no longer part of challenge. How can I skip turn at server side ?


Thanks 

Abhishek Bansal


Best Answer

Hi Abhishek


If you call SparkChallenge.consumeTurn(String playerId) the turn token will be moved to the next player. 


You could use this along with your own logic to skip the players who are no longer "standing"


Hope thats useful


Gabriel


Answer

Hi Abhishek


If you call SparkChallenge.consumeTurn(String playerId) the turn token will be moved to the next player. 


You could use this along with your own logic to skip the players who are no longer "standing"


Hope thats useful


Gabriel

Hi Gabriel,


Will try this !


Thanks

Hi Gabriel !


I am confused with where to call this ?

in challenge events ?

How can I know the who is the next turn.. if I have this information i can simply use my logic to know if that player is still in game.


Thanks

Abhishek

If you write the script in ChallengeTurnTakenMessage you can query the message for the next player using


Spark.getData().nextPlayer


Gabriel

okay thanks !

Hi,

I'm having a problem doing consumeTurn on ChallengeTurnTakenMessage

I have two players, and on some instances, when the player 1 takes a turn of event A, he sends the flag next_is_local in order to enable the next turn to skip and go back to that player 1 so he can take a turn of event B.

 

I previously set a flag next_is_local on the challenge event A to see if it should skip the turn of the next player 

Spark.setScriptData("next_is_local", next_is_local);

 Then on ChallengeTurnTakenMessage

var next_is_local = Spark.getScriptData("next_is_local");
if(next_is_local==1){
    SparkChallenge.consumeTurn(Spark.getData().nextPlayer);
}

 But after sending the Event A when I send an event B request it says it's not player 1's turn, so It seems this lines of code are not running well.

Hi Pedro,


At first glance it looks like you are setting the Responses scriptData and not the challenge instance's scriptData.


Spark.setScriptData will only set the scriptData of the next Response to that request and wont store it. When ChallengeTurnTakenMessage goes looking for "next_is_local" it's not going to find anything as the message happens after and separately to the LogChallengeEventRequest.


To set the scriptData of the challenge you can do the following:


  

var challenge = Spark.getChallenge(Spark.data.challengeInstanceId);

challenge.setScriptData("next_is_local", next_is_local); //This is setting the scriptData of the challenge instance and saving it.

  

Then in ChallengeTurnTakenMessage you need to also get the challengeId, load the challenge and get the scriptData:


  

var challenge = Spark.getChallenge(Spark.getData().challenge.challengeId);

var next_is_local = challenge.getScriptData("next_is_local");
if(next_is_local==1){
    challenge.consumeTurn(Spark.getData().nextPlayer);
}

  

Shane

Thanks Shane,

that solves part of the problem but It still not consuming the next player's turn, seems like Spark.getData().nextPlayer is returning "undefined"

challenge.setScriptData("TEST", Spark.getData().nextPlayer);

 Tested this line both on the LogChallengeEvent A and then on ChallengeTakenTurnMessage both set TEST:"undefined" on challenge script data.

Hi Pedro,


As nextPlayer is stored in the challenge Object on the ChallengeTakenTurnMessage this line of code actually needs to be:


 

challenge.setScriptData("TEST", Spark.getData().challenge.nextPlayer);

 

Shane

None of these examples are working for me.  I'm in a normal event (not a challenge event).  I am using Spark.getChallenge("somechallengeid") to get a challenge object, and then nextPlayer is undefined.  How do I get nextPlayer?


The API doesn't seem to give access to nextPlayer or Turns properties of a challenge, but I need both of those.

Hi Rich,


As you pointed out, the SparkChallenge object in CloudCode does not expose the nextPlayer property. You will need to store this value manually in the challenge's scriptData. I do it myself like this in both ChallengeStartedMessage and ChallengeTurnTakenMessage:

var challenge = Spark.getChallenge( Spark.getData().challenge.challengeId );
challenge.setScriptData( "nextPlayer", Spark.getData().challenge.nextPlayer );

This makes it possible to get the value even in normal events, provided that you have the challenge's ID.


I am not exactly sure what you mean by "turns properties" but I imagine you could use the same approach as above for other data.


Marcel

Ahh, that's a good idea. Thanks!

Login to post a comment