Sign In Register

How can we help you today?

Start a new topic
Answered

Problem sending data across players

Let me explain the requirement first. We have a two-player game, where player1 chooses a set of 5 questions and challenges player2, who would then race against player1 in solving these questions. Now, with every challenge sent, player1 needs to send a set of questions to the CloudCode, which would then save it to the Collection as well as send it to player2, to setup the start of the game.


Now let me explain what we have already tried. We tried sending scriptData along with the challenge (tried using Test Harness, of course), and tried to read it using getScriptData in CreateChallengeResponse. This doesn't work! The only other thing we could think of was sending a challenge separately and after the challenge is accepted, logging an event with the questionData with it. So, arelogEvents and logChallengeEvents the only ways to send data or can we attach data to the requests/responses as well?


Now comes the next part. Having read the list of questions into the Collection, how do we send it to player2? Does it do with theAcceptChallengeResponse? Through the ScriptData or is there any other way?


Best Answer

Hey Jatin,

So i have a solution for you, but its kind of complicated. Im gonna do it out really simply, so it will be suitable for anyone else who comes across this post.
I've tried to package it up nice and neat for you, but im afraid it's still going to take a few calls on the receiving end client-side.


So here is my solution, lemmie know if it works for what you need.


[1] -> Get the id of the player you are challenging onto the client-side. I dunno how you intend to do this, so i wont discuss it here, but feel free to ask if you get stuck.

[2] -> You will send a LogEventRequest() from client-side, and give that event the challenged player's ID and a list of strings which will be your questions. Here is an example of how my C# would look.


List<string> questionList = new List<string>(){
			"Question 1: ..........",
			"Question 2: ..........",
			"Question 3: ..........",

		};

		new GameSparks.Api.Requests.LogEventRequest()
			.SetEventKey("SEND_QUEST")
				.SetEventAttribute("QUESTIONS", questionList)
				.SetEventAttribute("PLAYERID", "560a96c9e4b0114e9837ff03")
				.SetDurable(true)
				.Send((response) => {

					// we dont really care about the response, but you can check for errors yourself if you like //
				});

 

[3] -> You need to create a new event that takes two strings for your player id and your questions. Now, in the cloud-code for this event, i send a custom CreateChallengeRequest() and get the response.

I then get the challenge-ID from that response and with it i get the challenge object. Then i enter the array of questions into the challenge as scriptData.

And thats it! Now that challenge will have those questions stored as script data...


 

 

var questionList = Spark.getData().QUESTIONS;
var playerToChallenge = Spark.getData().PLAYERID;

// construct a CreateChallengeRequest() here //
var response = Spark.sendRequest({
    "@class": ".CreateChallengeRequest", 
    "accessType" : "PRIVATE",
    "challengeMessage" : "Here are your questions",
    "challengeShortCode" : "QUEST_CHL",
    "usersToChallenge" : playerToChallenge,
    "endTime" : "2015-12-31T12:00Z",
    });

// get the challenge you just created and add some script-data to it with the questions in there //
var challengeInstance = Spark.getChallenge(response["challengeInstanceId"]);
challengeInstance.setScriptData("questions", questionList);
// send the response data back if you need it //
Spark.setScriptData("response", response);

 

 

You can, if you want, put whatever challenge attributes in the event-request you want. Check out the doc here to see what else you can put in there.


[4] -> Now when the challenged player gets the message telling them they have been challenged, you need to grab the challenge-ID from that message.


[5] -> Use the challenge-id to call a GetChallengeRequest() event from the client-side.

         In the response you will see your questions in the script-data there.


Below is an example of the output of that response in the test-harness...

Now, you can take that directly out on the client-side response as a string-array and display it to the player before they accept the challenge if you want.
Otherwise, you will get the same script-data back if you call a AcceptChallengeRequest()


Hope that helps
-Sean




1 Comment

Answer

Hey Jatin,

So i have a solution for you, but its kind of complicated. Im gonna do it out really simply, so it will be suitable for anyone else who comes across this post.
I've tried to package it up nice and neat for you, but im afraid it's still going to take a few calls on the receiving end client-side.


So here is my solution, lemmie know if it works for what you need.


[1] -> Get the id of the player you are challenging onto the client-side. I dunno how you intend to do this, so i wont discuss it here, but feel free to ask if you get stuck.

[2] -> You will send a LogEventRequest() from client-side, and give that event the challenged player's ID and a list of strings which will be your questions. Here is an example of how my C# would look.


List<string> questionList = new List<string>(){
			"Question 1: ..........",
			"Question 2: ..........",
			"Question 3: ..........",

		};

		new GameSparks.Api.Requests.LogEventRequest()
			.SetEventKey("SEND_QUEST")
				.SetEventAttribute("QUESTIONS", questionList)
				.SetEventAttribute("PLAYERID", "560a96c9e4b0114e9837ff03")
				.SetDurable(true)
				.Send((response) => {

					// we dont really care about the response, but you can check for errors yourself if you like //
				});

 

[3] -> You need to create a new event that takes two strings for your player id and your questions. Now, in the cloud-code for this event, i send a custom CreateChallengeRequest() and get the response.

I then get the challenge-ID from that response and with it i get the challenge object. Then i enter the array of questions into the challenge as scriptData.

And thats it! Now that challenge will have those questions stored as script data...


 

 

var questionList = Spark.getData().QUESTIONS;
var playerToChallenge = Spark.getData().PLAYERID;

// construct a CreateChallengeRequest() here //
var response = Spark.sendRequest({
    "@class": ".CreateChallengeRequest", 
    "accessType" : "PRIVATE",
    "challengeMessage" : "Here are your questions",
    "challengeShortCode" : "QUEST_CHL",
    "usersToChallenge" : playerToChallenge,
    "endTime" : "2015-12-31T12:00Z",
    });

// get the challenge you just created and add some script-data to it with the questions in there //
var challengeInstance = Spark.getChallenge(response["challengeInstanceId"]);
challengeInstance.setScriptData("questions", questionList);
// send the response data back if you need it //
Spark.setScriptData("response", response);

 

 

You can, if you want, put whatever challenge attributes in the event-request you want. Check out the doc here to see what else you can put in there.


[4] -> Now when the challenged player gets the message telling them they have been challenged, you need to grab the challenge-ID from that message.


[5] -> Use the challenge-id to call a GetChallengeRequest() event from the client-side.

         In the response you will see your questions in the script-data there.


Below is an example of the output of that response in the test-harness...

Now, you can take that directly out on the client-side response as a string-array and display it to the player before they accept the challenge if you want.
Otherwise, you will get the same script-data back if you call a AcceptChallengeRequest()


Hope that helps
-Sean




Login to post a comment