Sign In Register

How can we help you today?

Start a new topic
Answered

Setting challenge endtime from cloud

Im tryng to create a challenge request from cloud, but I dont know how to set the endtime to some time in the future.


 

var createChReq = new SparkRequests.CreateChallengeRequest();
    createChReq.accessType = "PUBLIC";
    createChReq.challengeShortCode = "Ch_ES";
    createChReq.endTime = new Date().getTime() + 1000 * 7* 24 * 60 * 60;
    createChReq.scriptData = {"state":"waiting"};
    var resp = createChReq.Execute();
    
    if( resp.error !== null ){
 
        Spark.setScriptError( "create challenge request error", resp.error );
    }

This gives me parsing date error. 

Also, repassing the error on the SetScriptError as Im doing there doesnt work, it shows as "undefined" on the test harness..I dont get why. How can I do it?


Best Answer

Hi Giuliano,


You're using a timestamp here while the challenges themselves require an ISO string date. To get it in the correct format you could use the following in Cloud Code.


  

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

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

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

Spark.setScriptData("response", res.challengeInstanceId);

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

 

Try that and let us know how you get on.


Regards,

Liam

1 Comment

Answer

Hi Giuliano,


You're using a timestamp here while the challenges themselves require an ISO string date. To get it in the correct format you could use the following in Cloud Code.


  

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

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

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

Spark.setScriptData("response", res.challengeInstanceId);

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

 

Try that and let us know how you get on.


Regards,

Liam

Login to post a comment