Skip to content

Instantly share code, notes, and snippets.

@manwithsteelnerves
Last active August 29, 2015 14:17
Show Gist options
  • Save manwithsteelnerves/601acda9967c0008c3d4 to your computer and use it in GitHub Desktop.
Save manwithsteelnerves/601acda9967c0008c3d4 to your computer and use it in GitHub Desktop.
// ====================================================================================================
//
// Cloud Code for CreateChallengeResponse, write your code here to customise the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://portal.gamesparks.net/docs.htm
//
// ====================================================================================================
require("DEFINES");
require("CONFIG_CHALLENGES");
var scriptInfo = Spark.getData().scriptData;
if(scriptInfo)
{
var scores = scriptInfo[SCORES_KEY];
if(scores !== undefined)
{
//Just add these scores to the current created challenge.
var instanceId =Spark.getData().challengeInstanceId;
var challengeInfo = Spark.getChallenge(instanceId);
//Get short code to request for challenge config detials.
var challengeShortCode = challengeInfo.getShortCode();
var challengeConfigDetails = getChallengeDetails(challengeShortCode);
//This extra info will have event and event attribute detials.
var eventInfo = challengeConfigDetails[EXTRA_INFO_KEY];
var eventName = eventInfo[EVENT_KEY];
var eventAttributeName = eventInfo[EVENT_ATTRIBUTE0_KEY];
//Populate the details we got above in the request.
var challengeEventRequest = new SparkRequests.LogChallengeEventRequest();
challengeEventRequest.challengeInstanceId = instanceId;
challengeEventRequest.eventKey = eventName;
if(ASYNC_CHALLENGE_EVENT_ATTRIBUTE_KEY === eventAttributeName)
{
challengeEventRequest.SCORES_PER_TURN_ATTRIBUTE = JSON.stringify(scriptInfo);
}
//Send the request.
var response = Spark.sendRequest(challengeEventRequest);
response.error;
}
}
Passing values as JSON
"SCORES_PER_TURN_ATTRIBUTE": "{\"SCORES\" : [1000,3000,6000]}"
// ====================================================================================================
//
// Cloud Code for ListChallengeResponse, write your code here to customise the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://portal.gamesparks.net/docs.htm
//
// ====================================================================================================
//This is for fetchign all states listed in states variable. This will be handy if we are populating the challenges with all states instead fo individual calls to each state.
var state = Spark.getData().scriptData["state"];
var shortCode = Spark.getData().scriptData["shortCode"];
if("ALL".localeCompare(state) === 0)
{
var states = ["RUNNING","WAITING","ISSUED","COMPLETE"];
var results = [];
//ALL is not yet ready in gamesparks so sending this requests data in script data
var eachState;
for(var i =0 ; i < states.length ; i++)
{
var eachState = states[i];
var request = new SparkRequests.ListChallengeRequest();
request.state = eachState;
request.shortCode = shortCode;
//Send the request
var response = request.Send();
if(response.challengeInstances)
{
Spark.setScriptData(eachState, response.challengeInstances);
}
}
}
////For passing data from Request to Response cloud code//////
//Spark.setScriptData can be used to pass information from request on to response and it will be set in response client receives as well.
var SCORES_KEY = "SCORES";
var scriptDataFromRequest = Spark.getData().scriptData;
if(scriptDataFromRequest)
{
//For passing this info to Response Cloud code.
//If scores list is passed, this means we need
var scores = scriptDataFromRequest[SCORES_KEY];
if(scores !== undefined)
{
Spark.setScriptData(SCORES_KEY, scores);
}
}
/////////////
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment