Sign In Register

How can we help you today?

Start a new topic

Create challenge in cloud code with different user


I would like to implement bots in my game. I've read other threads with helpful information and I have an idea how to implement it, but in my case there is a problem using cloud code. I have already created a few test users which will be used as bots and I am trying to create a challenge if there is no other available at the moment.

The thing which I am doing is that I am looking in FindChallengeResponse in cloud code and if Spark.getData().challengeInstances is undefined or has no challenges at all I want a bot user to create a challenge. The code which I am using is like that:

var challengeInstances = Spark.getData().challengeInstances;

if(challengeInstances === undefined || challengeInstances.length == 0){

    Spark.getLog().debug("CREATE NEW CHALLENGE");


    var request = new SparkRequests.CreateChallengeRequest();

    request.challengeShortCode = "ticTacToeRequest";

    request.accessType = "PUBLIC";

    request.autoStartJoinedChallengeOnMaxPlayers = true;

    request.maxPlayers = 2;

    var endTime = new Date();

    endTime.setTime(endTime.getTime() + (24*60*60*1000));

    request.endTime = endTime;

    var expiryTime = new Date();

    expiryTime.setTime(expiryTime.getTime() + (10*60*1000));

    request.expiryTime = expiryTime;


    var kevin = null;

    var stage = Spark.getConfig().getStage();

    if(stage === "preview"){

        kevin = "5929609786972904ea924718";

    } else {

        kevin = "592816aa849ac404f693d262";



    var player = Spark.loadPlayer(kevin);


    var response = request.SendAs(kevin); // Kevin

    var chalInstanceId = response.challengeInstanceId;

    Spark.getLog().debug("challenge Id: "+ chalInstanceId);


And the problem is this code doesn't create a new challenge and doesn't throw any errors.There is no challenge and chalInstanceId is always undefined.

Any ideas how I can achieve this?

Hi Iliq,

The issue here is you are trying to set the startTime and endTime as timestamps. They need to be set as an isoString. If you send a CreateChallengeRequest from the Test Harness and use the date picker to set your times you will see the format that is required."2017-05-27T13:45Z" is what the challenge is expecting. You can find an example of how to do this below in Cloud Code.


//sets the start date to 10 minutes from now
var startDate = new Date();

//sets the end date to 5 days from now.
var endDate = new Date();

var chalReq = new SparkRequests.CreateChallengeRequest();
chalReq.accessType = "PUBLIC";
chalReq.autoStartJoinedChallengeOnMaxPlayers = "true";
chalReq.challengeMessage = "Challenge";
chalReq.challengeShortCode = "testChallenge";
chalReq.startTime = getDateString(startDate);
chalReq.endTime = getDateString(endDate);
chalReq.maxPlayers = 2;
chalReq.minPlayers = 2;
var res = chalReq.Send();

//shaves off the milliseconds from the dates passed through and formats correctly
function getDateString(date){
    var dateString = date.toISOString();
    return  dateString.substr(0, dateString.lastIndexOf(".")) + "Z";




Thanks for the help, didn't notice that!

Another think which I want to ask. Is there a way I can edit the response of script. For example in FindChallengeResponse if there is no challenges I can create a few and attach them to Spark.getData().challengeInstances ? 

I didn't find anything like that in the documentation.

Thanks in advance!


any news on the second question that I had?


If you use Spark.setScriptData() in the request then it will carry over to the response.

So something like (In FindChallengeRequest):




and use the following to retrieve it from the response:




challengeName = "exampleString"


Hope that helps,


Login to post a comment