Sign In Register

How can we help you today?

Start a new topic
Answered

Multiple requests at once? Or way to get challenges with 2 states in the same request?

So when building my friends list that also has a challenge button on it, I'm also trying to disable the challenge button if an ISSUED challenge or RUNNING challenge already exists.


Right now I have this whole thing working, however I am waiting on 3 requests before any logic happens.  It goes like this...


ChallengeListRequest(ISSUED) ---> ChallengeListRequest(RUNNING) ----> GetFriends ---> logic to build friends list with options as mentioned above, disabling challenge button if ISSUED or RUNNING challenges in progress, and giving a message in the friend entry depending which one.


So I'm waiting for all 3 requests to come back before I start doing any of that logic.  And it's definitely not optimal and somewhat slow, waiting on 3 requests to come back.


Is there anyway to either make this all just 1 request to the server, or anyway to get both ISSUED and RUNNING challenges in the same request, to at least make my requests go down to 2 from 3?


Thanks.


Best Answer

Here is what I did. Hope this helps.


I created a cloud script and fetching all on cloud and returning the results. You can add/remove which status you want in this script.

the below script i attached to ListChallengeResponse. While making request for ListChallengeRequest I will send "ALL" as the status.


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 inviteRequest = new SparkRequests.ListChallengeRequest();

       inviteRequest.state = eachState;

       inviteRequest.shortCode = shortCode;

       

       //Send the request

       var response = inviteRequest.Send();

       if(response.challengeInstances)

       {

           Spark.setScriptData(eachState, response.challengeInstances);

       }

   }

}


Answer

Here is what I did. Hope this helps.


I created a cloud script and fetching all on cloud and returning the results. You can add/remove which status you want in this script.

the below script i attached to ListChallengeResponse. While making request for ListChallengeRequest I will send "ALL" as the status.


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 inviteRequest = new SparkRequests.ListChallengeRequest();

       inviteRequest.state = eachState;

       inviteRequest.shortCode = shortCode;

       

       //Send the request

       var response = inviteRequest.Send();

       if(response.challengeInstances)

       {

           Spark.setScriptData(eachState, response.challengeInstances);

       }

   }

}

Cool thanks for that suggestion.  That does look like it could be exactly what I need, and I could tailor it to include all 3 calls that I need.


So now my question is, and this might be a dumb question because I'm used to groovy code where on the backend, you wouldn't send another request, you would just call the service to do the logic you need to do, but while I know you are already on the backend with this call, does each one of those "Send()" calls actually make any sort of roundtrip to ANOTHER backend server?  Or will this all be done on the same server since you are on the backend at this point?


The whole calling of a function "Send()" made me think it would be sending another request and waiting for another response, which would in turn, mean while this would simplify to 1 backend call on my client, it would still be calling 3 more external calls, which would in turn be 4 total external calls, but at the benefit of making the front end code a little cleaner maybe with 1 call to a server.  Although, then that would mean I need to add a lot more parsing logic on the front end.


But ... if it is not making any other server to server calls, and is just doing everything on the backend, then this sounds like the solution I'm looking for 100%.


One more quick question too - how do you call this cloud code?  The only cloud code I've used thus far is when creating challenges and updating them and stuff, so they are automatically called with the API.

Probably exact answer can be from the GameSparks Dev team. 

To my knowledge, its not roundtrip and it should be like a direct fetch from its database. so nothing to worry on that.



One more quick question too - how do you call this cloud code?  The only cloud code I've used thus far is when creating challenges and updating them and stuff, so they are automatically called with the API.


Just open the configurator in Dash board and go to Cloud Code -> Responses -> ListChallengeResponse.  paste the above code.


PS : pls rename  var inviteRequest to var request :P . I just quickly wrote that code ^_^


-Ayyappa.