Sign In Register

How can we help you today?

Start a new topic
Answered

Cloud code to know if users in a leaderboard has challenges with current player.

Hi, I need some help with cloud code. I want to make a Leaderboard request that also include data for each person if they have or not a running challenge (turn based) session with current player. Some tips? Thanks!

Best Answer

Cool Patrick, works really good! I had to extend it a little to fit my needs just adding this:


  

var accepted = challengeInstances[challenge].accepted;
                //For every player 
                for(player in accepted){
                    //If the leaderboard player exists inside the accepted players of the challenge
                    if(challengeInstances[challenge].accepted[player].id === leaderBoardPlayers[userId]){
                        //Push the challengeInstanceId to the array
                        challengesWithThisPlayer.push(challengeInstances[challenge].challengeId);
                    }
                }
                
                var challenged = challengeInstances[challenge].challenged;
                //For every player 
                for(player in challenged){
                    //If the leaderboard player exists inside the challenged players of the challenge
                    if(challengeInstances[challenge].challenged[player].id === leaderBoardPlayers[userId]){
                        //Push the challengeInstanceId to the array
                        challengesWithThisPlayer.push(challengeInstances[challenge].challengeId);
                    }
                }
                
                var challenger = challengeInstances[challenge].challenger;
                //For every player 
                for(player in challenger){
                    //If the leaderboard player exists inside the challenger players of the challenge
                    if(challengeInstances[challenge].challenger[player].id === leaderBoardPlayers[userId]){
                        //Push the challengeInstanceId to the array
                        challengesWithThisPlayer.push(challengeInstances[challenge].challengeId);
                    }
                }

  

Thanks you and the team for the help.


Hi Esteban, 

I'm going to test a work around for you, this shouldn't be a problem. I will get back to you as soon as I can. 

Regards, Patrick. 

Here is our method for achieving the behavior you wanted. It is added to leader board data responses in cloud code.  It can be refactored or changed in any way to suit your needs as you see fit.This should have everything you need. It's heavily commented but feel free to ask questions. 

The code is as follows:  

// ====================================================================================================
//
// Cloud Code for LeaderboardDataResponse, write your code here to customise the GameSparks platform.
//
// For details of the GameSparks Cloud Code API see https://portal.gamesparks.net/docs.htm			
//
// ====================================================================================================

//Cache the attributes passed in to the request
var leaderBoardData = Spark.getData().data;
//An array we will use to cache the list of players from the leaderboard we will use 
var leaderBoardPlayers= [];

//For every entry in the leaderboardData Response, push the userId to the leaderboardPlayers array 
for(entry in leaderBoardData)
{
    leaderBoardPlayers.push(leaderBoardData[entry].userId);
}

//Perform a listChallengeRequest in cloudCode
var listChallenegeREQ = new SparkRequests.ListChallengeRequest();

//Set all the attributes of this call
listChallenegeREQ.entryCount = 100;
listChallenegeREQ.offset = 0;
listChallenegeREQ.shortCode = "TEST";
//These are the following states of a game that is waiting to happen or is active
listChallenegeREQ.states = ["WAITING",
                              "RUNNING",
                              "ISSUED",
                              "RECEIVED"];

//Send the request and cache the response
var listChallengeResp = listChallenegeREQ.Send();

//Cache the challengeInstances Object for clarity in the future
var challengeInstances = listChallengeResp.challengeInstances;

//Create an empty JS Object to hold the challenges, we will be using the userId as the key;
var challengesWithPlayers = {};

//For every user id present in the leaderBoardPlayers array (we pushed id's from leaderboard data) 
for(userId in leaderBoardPlayers){
    //First of all make sure it isn't our id, which is always present in a listChallengeResponse as it gets only challenges we are involved in
    if(leaderBoardPlayers[userId] != Spark.getPlayer().getPlayerId()){
        //We'll use this array to store challenges that involve the player from the leaderboard
        var challengesWithThisPlayer = [];
        //For each challenge in the list of challengeInstance
        for(challenge in challengeInstances){
            //Cache the list of accepted players from the challenge for better clarity in the future
            var accepted = challengeInstances[challenge].accepted;
            //For every player 
            for(player in accepted){
                //If the leaderboard player exists inside the accepted players of the challenge
                if(challengeInstances[challenge].accepted[player].id === leaderBoardPlayers[userId]){
                    //Push the challengeInstanceId to the array
                    challengesWithThisPlayer.push(challengeInstances[challenge].challengeId);
                }
            }
        }
        //Finally, create key in the challengesWithPlayers array
        //Assign the list of challengeInstanceId's as the value to the key.
        challengesWithPlayers[leaderBoardPlayers[userId]] = challengesWithThisPlayer;
        
        //You could also make challengesWithPlayers an array and structure the data as follows:
        // playerWithChallenges = {};
        // playerWithChallenges.playerId = leaderBoardPlayers[userId];
        // playerWithChallenges.challenges = challengesWithThisPlayer;
        //This would give you the structure of something like:
        //{
        //["playerId" : "5698c2eee4b0470646a54045", 
        //"challenges" : [
        //      "5698c7a2e4b0504a7fc37c83"
        //  ]
        //]
        //}
        
    }
}
//Set the challengesWithPlayers object to the scriptData of the response so you can process and make use of this data in the client
Spark.setScriptData("challenges", challengesWithPlayers);

 

Now here is the request in the test harness inspector:  

*Note that it still matches a typical data request in it's appearance. 

{
 "@class": ".LeaderboardDataRequest",
 "entryCount": 10,
 "leaderboardShortCode": "Test_Board",
 "offset": 0
}

 

However the response we get is different in tat now it includes the challenge id's you wanted. 
Here is the response: 

 

{
 "@class": ".LeaderboardDataResponse",
 "data": [
  {
   "userId": "5698c2d6e4b0470646a53c86",
   "SCORE": 100,
   "when": "2016-01-15T10:04Z",
   "city": null,
   "country": "IE",
   "userName": "p1",
   "externalIds": {},
   "rank": 1
  },
  {
   "userId": "5698c2eee4b0470646a54045",
   "SCORE": 90,
   "when": "2016-01-15T10:05Z",
   "city": null,
   "country": "IE",
   "userName": "p2",
   "externalIds": {},
   "rank": 2
  },
  {
   "userId": "5698c2ffe4b0470646a540a1",
   "SCORE": 80,
   "when": "2016-01-15T10:10Z",
   "city": null,
   "country": "IE",
   "userName": "p3",
   "externalIds": {},
   "rank": 3
  },
  {
   "userId": "5698c5b7e4b0470646a573e1",
   "SCORE": 70,
   "when": "2016-01-15T10:11Z",
   "city": null,
   "country": "IE",
   "userName": "p4",
   "externalIds": {},
   "rank": 4
  }
 ],
 "leaderboardShortCode": "Test_Board",
 "scriptData": {
  "challenges": {
   "5698c2eee4b0470646a54045": [
    "5698c7a2e4b0504a7fc37c83"
   ],
   "5698c2ffe4b0470646a540a1": [],
   "5698c5b7e4b0470646a573e1": []
  }
 }
}

 


The following is a portion of the response above. This is the additional information we requested (relevant players challenge id's.id's. Relevance being challenge requests sent received accepted or challenges ongoing ): 


 

"leaderboardShortCode": "Test_Board",
 "scriptData": {
  "challenges": {
   "5698c2eee4b0470646a54045": [
    "5698c7a2e4b0504a7fc37c83"
   ],
   "5698c2ffe4b0470646a540a1": [],
   "5698c5b7e4b0470646a573e1": []
  }

 

Hopefully this satisfies your query. This method has many other applications. Checking for the presence of certain achievements for example. 
Regards, Patrick. 

Answer

Cool Patrick, works really good! I had to extend it a little to fit my needs just adding this:


  

var accepted = challengeInstances[challenge].accepted;
                //For every player 
                for(player in accepted){
                    //If the leaderboard player exists inside the accepted players of the challenge
                    if(challengeInstances[challenge].accepted[player].id === leaderBoardPlayers[userId]){
                        //Push the challengeInstanceId to the array
                        challengesWithThisPlayer.push(challengeInstances[challenge].challengeId);
                    }
                }
                
                var challenged = challengeInstances[challenge].challenged;
                //For every player 
                for(player in challenged){
                    //If the leaderboard player exists inside the challenged players of the challenge
                    if(challengeInstances[challenge].challenged[player].id === leaderBoardPlayers[userId]){
                        //Push the challengeInstanceId to the array
                        challengesWithThisPlayer.push(challengeInstances[challenge].challengeId);
                    }
                }
                
                var challenger = challengeInstances[challenge].challenger;
                //For every player 
                for(player in challenger){
                    //If the leaderboard player exists inside the challenger players of the challenge
                    if(challengeInstances[challenge].challenger[player].id === leaderBoardPlayers[userId]){
                        //Push the challengeInstanceId to the array
                        challengesWithThisPlayer.push(challengeInstances[challenge].challengeId);
                    }
                }

  

Thanks you and the team for the help.

We are always glad help. If you have any further questions feel free to get back to us. 

Regards, Patrick. 

Login to post a comment