Sign In Register

How can we help you today?

Start a new topic
Answered

How to receive the message on opponent's client side when player calls createChallengeRequest in JavaScript SDK?

Hello.


I have an client with JavaScript SDK and so far I managed to successfully log two players (different browsers) into accounts, start matchmaking and find each other. From that point each of them gets the opponent's id and one of them (string comparison on ids, but it really doesn't matter which one for me) want to start a challenge. For it I use:


var expiryTime = getGameSparksTimeFormat(addMinutes(new Date(), 1)); //my function to format time for GameSparks

var endTime = getGameSparksTimeFormat(addMinutes(new Date(), 60*8)); //it's turn-based game without time limit, they should end max 30 minutes but let them have 8 hours 


gamesparks.createChallengeRequest("PRIVATE", "Can you beat my army in normal game?", "normal", 0, 0, 0, 0, 0, 0, endTime, expiryTime, null, 2, 2, false, null, opponentId, createChallengeResponse );


I receive the response for the player who send createChallengeRequest, with the challenge id.


The problem is, his opponent is not aware that he was challenged and cannot react (accept/decline - I want to accept the challenge automatically). How to receive the message on opponent's client side? Should I call some GameSparks SDK function to await incoming messages (that are not responses to my direct requests)?


Best Answer

Hi,


I have recently implemented this in my game and it is working very well. Here is what I did:


Matches in my game are always 1v1 so I automatically accept the challenge by the challenged player in CloudCode of CreateChallengeResponse. 

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

var challengedPlayers = challenge.getChallengedPlayerIds();
var player2ID = challengedPlayers[0];

// Accept the challenge as if the request is sent by the challenged player
Spark.sendRequestAs( {
        "@class" : ".AcceptChallengeRequest",
        "challengeInstanceId" : challenge.getId(),
    },
    player2ID
);

I am also setting "autoStartJoinedChallengeOnMaxPlayers" to true so that both players receive ChallengeStartedMessage right after this.

Note this works for challenges created after both players are matched using GameSpark's Matches (i.e. both are willing to play). If you were to introduce optional challenges where player can decline the challenge then you would need some conditional check but that should not be too difficult.


If you wanted to accept the challenge "manually" by the client then you would need to setup a message handler on your client and listen to ChallengeIssuedMessage. After that, simply send AcceptChallengeRequest. But the CloudCode version seems simpler to me and does not require further client-side logic.


Marcel


Answer

Hi,


I have recently implemented this in my game and it is working very well. Here is what I did:


Matches in my game are always 1v1 so I automatically accept the challenge by the challenged player in CloudCode of CreateChallengeResponse. 

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

var challengedPlayers = challenge.getChallengedPlayerIds();
var player2ID = challengedPlayers[0];

// Accept the challenge as if the request is sent by the challenged player
Spark.sendRequestAs( {
        "@class" : ".AcceptChallengeRequest",
        "challengeInstanceId" : challenge.getId(),
    },
    player2ID
);

I am also setting "autoStartJoinedChallengeOnMaxPlayers" to true so that both players receive ChallengeStartedMessage right after this.

Note this works for challenges created after both players are matched using GameSpark's Matches (i.e. both are willing to play). If you were to introduce optional challenges where player can decline the challenge then you would need some conditional check but that should not be too difficult.


If you wanted to accept the challenge "manually" by the client then you would need to setup a message handler on your client and listen to ChallengeIssuedMessage. After that, simply send AcceptChallengeRequest. But the CloudCode version seems simpler to me and does not require further client-side logic.


Marcel

Thank you! That's quite a nice solution. I was trying with AcceptChallangeRequest but the CloudCode version you presented looks better to me.

Login to post a comment