Sign In Register

How can we help you today?

Start a new topic
Answered

GetLeaderboardEntriesRequest - Get Current Player's Rank - Returning NULL

 Hi,


So I have a leaderboard with the short code: highScoreLeaderboard and I want to get the current player's rank.


According to the documentation if I leave "player" empty, then it uses the current player, but It always returns null.


Under test harness I tried the following but always returns null:

 

{
 "@class": ".GetLeaderboardEntriesRequest",
 "leaderboards": [
  "highScoreLeaderboard"
 ]
}

 

And in code I tried the following:

 

    string[] leaderBoards = new string[1]{"highScoreLeaderboard"};

    new GetLeaderboardEntriesRequest()
        .SetLeaderboards(leaderBoards)
        .Send((response) => {
          var results = response.Results; 
          var scriptData = response.ScriptData; 

          // Not sure what to put here:
          Debug.Log(scriptData.GetString("rank"));
        });

 

Thank you,


Best Answer
Hi Beppi,

Sorry for the double post, thought I could edit. So I tested your snippet and keep getting the wrong rank. Here is what I used:

 

    List<string> leaderboardCodes = new List<string>();
    leaderboardCodes.Add("highScoreLeaderboard");

    GetLeaderboardEntriesRequest leaderboard = new GetLeaderboardEntriesRequest();
    leaderboard.SetLeaderboards(leaderboardCodes);
    leaderboard.Send ((response) => {
      if (!response.HasErrors) {
          string json = response.JSONString;
          var N = SimpleJSON.JSON.Parse(json);
        var value = N["highScoreLeaderboard"]["rank"].AsInt;
        Debug.Log("Player rank: " + value);
      }
      else
        Debug.Log ("Get Player Score Leaderboard: FAIL with error " + response.Errors.JSON);
    });

 

 


Hi,
The request will work fine as long as the current authenticated user has an entry in the leaderboard, if they don't it will return null.

In the test harness when you authenticate the user, the player details including the user id will show in the inspector.


You can check in noSQL if the user is in the leaderboard.

Select highScoreLeaderboard as the collection 

In query put {"userId" : "the id you got from the test harness goes here"}

If the player has a score in the leaderboard then it will show up in the results.


Thanks

Katie




Personally I couldn't get anything from response.Result, while instead I managed to receive the information I needed using JSON:


 

		GetLeaderboardEntriesRequest leaderboard = new GetLeaderboardEntriesRequest();
		leaderboard.SetLeaderboards(shortCodes);
		leaderboard.Send ((response) => {
			if (!response.HasErrors) {
				string json = response.JSONString;
				var N = SimpleJSON.JSON.Parse(json);
				value = N["PScoreLeaderboard"]["S"].AsInt;
			}
			else
				Debug.Log ("Get Player Score Leaderboard: FAIL with error " + response.Errors.JSON);

 

Hey Beppi,
I will test your snippet (have to dl simplejson etc). In the meantime what is the PScoreLeaderboard and "S"? .

* So I looked in my NoSQL and the player is in fact in the leader boards.
1. I got the ID from NoSQL
2. Facebook authenticated
3. TestHarness GetLeaderboardEntriesRequest with the new ID
4. Get NotAuthorized , NULL.
5. Is my syntax correct for the C# snippet?

Thank you,

 

Answer
Hi Beppi,

Sorry for the double post, thought I could edit. So I tested your snippet and keep getting the wrong rank. Here is what I used:

 

    List<string> leaderboardCodes = new List<string>();
    leaderboardCodes.Add("highScoreLeaderboard");

    GetLeaderboardEntriesRequest leaderboard = new GetLeaderboardEntriesRequest();
    leaderboard.SetLeaderboards(leaderboardCodes);
    leaderboard.Send ((response) => {
      if (!response.HasErrors) {
          string json = response.JSONString;
          var N = SimpleJSON.JSON.Parse(json);
        var value = N["highScoreLeaderboard"]["rank"].AsInt;
        Debug.Log("Player rank: " + value);
      }
      else
        Debug.Log ("Get Player Score Leaderboard: FAIL with error " + response.Errors.JSON);
    });

 

 

It seems equal to mine, that appearantly works, at this point I think that it's better if I leave the solution to the customer service who might look if there is something wrong in the leaderboard config...

I also find odd that is necessary to pass through a json object instead of getting the value directly, but what is important is to get the work done :)

BTW, did you authenticate before the request?

Yes, I authenticate via device and facebook, both of which work and retrieve the correct leaderboard data.

 

I would appreciate if customer service could help me out with this issue, The game launch is being stalled because of it.

 

Hi Lawrence
It seems like Facebook authenticated is not working for you.
In the Test Harness when you run GetLeaderboardEntriesRequest  you have  to be authenticated.
In the connection box the authToken has to be filled in.
 Run a FacebookConnectRequest in the test Harness with your accessToken

{
"@class": ".FacebookConnectRequest",
"accessToken": "",
"code": "",
"doNotLinkToCurrentPlayer": false,
"errorOnSwitch": false,
"segments": {},
"switchIfPossible": false,
"syncDisplayName": false
}

Once you are authenticated you will be able to run requests.
Thanks
Katie

 

Hi Katie,

Like I previously mentioned, I am fully authenticated, yet I cant make this request. This is my entire issue, is that I'm in-fact authenticated but the request returns NULL.

 

Thank you,

Hi Lawrence
Including the userId in the Player Field in the Test Harness will give you  the leaderboard entries.

 {
"@class": ".GetLeaderboardEntriesRequest",
"leaderboards": [
"highScoreLeaderboard"
],
"player": "player userId here"
}

Thanks

Katie

 Hi Katie,

So I finally got it to work with the ID, but according to the documentation if left blank it should use the current ID. How do I go about doing this in C# for unity? I have the following:

  public void GetPlayerRank(){
    List<string> leaderboardCodes = new List<string>();
    leaderboardCodes.Add("highScoreLeaderboard");
    
    new GetLeaderboardEntriesRequest()
      .SetLeaderboards(leaderboardCodes)
        .SetPlayer(AccessToken.CurrentAccessToken.UserId)
        .Send((response) => {
          if(response.HasErrors)
            {
              playerRank.text = "ERROR!"; 
              Debug.LogError("Couldnt get player rank for some reason");
          }else{
            var results = response.Results; 
            var scriptData = response.ScriptData; 
            
            // Not sure what to put here:
            playerRank.text = "RANK: " + results.GetString("rank");
            Debug.Log("RANK Result: " + results.GetString("rank"));
            Debug.Log("RANK Script: " + scriptData.GetString("rank"));
          }
        });
  }

 Is this the correct way to get the ID in code? because its returning nothing so far =/ . Under the test harness it is working though.

Hi Lawrence

When you call a FacebookConnectRequest you are returned  the user Id in the response

 

new FacebookConnectRequest()
    .SetAccessToken(accessToken)
    .SetCode(code)
    .SetDoNotLinkToCurrentPlayer(doNotLinkToCurrentPlayer)
    .SetErrorOnSwitch(errorOnSwitch)
    .SetSegments(segments)
    .SetSwitchIfPossible(switchIfPossible)
    .SetSyncDisplayName(syncDisplayName)
    .Send((response) => {
        String authToken = response.AuthToken; 
        String displayName = response.DisplayName; 
        bool? newPlayer = response.NewPlayer; 
        GSData scriptData = response.ScriptData; 
        var switchSummary = response.SwitchSummary; 
        String userId = response.UserId; 
    });


or you use AccountDetailsRequest

 

new AccountDetailsRequest()
    .Send((response) => {
        String userId = response.UserId;   
    });

 

To get an leaderboard entry

 

new GetLeaderboardEntriesRequest()
    .SetChallenges(challenges)
    .SetInverseSocial(inverseSocial)
    .SetLeaderboards(leaderboards)
    .SetPlayer(player)
    .SetSocial(social)
    .SetTeamTypes(teamTypes)
    .Send((response) => {
        GSData results = response.Results; 
        GSData scriptData = response.ScriptData; 
    });

Rank can be accessed by scriptData

You can find the  c# client api documents here

Thanks

Katie

 



Hi Katie,

I'm sorry to be a pain but I have read the documentation and as you can see from my first post I have what is in the documentation. The issue is that I do not know what to do with the GSDATA scriptData in code, and nothing in the documentation tells me what to do with it.

My question is simple, and I need help with it in the code it self.

Given a leaderboard short code named: "highScoreLeaderBoard"
And ID "56103bc5dd3049eb" (example id)
how do I get the rank in c#? The test harness still returns scriptData as null ( it even returns null in code). This is what i see in the test harness:
{
 "@class": ".GetLeaderboardEntriesResponse",
 "scriptData": null,
 "highScoreLeaderboard": {
  "userId": "56103bc5dd3049eb",
  "score": 3865,
  "when": "2015-10-04T05:40Z",
  "city": "city",
  "country": "US",
  "userName": "myName",
  "externalIds": {
   "FB": "2837232"
  },
  "rank": 2
 }
}

 as you can see the scriptData is in fact null.


And here is my c# code:

 

  public void GetPlayerRank(){
    List<string> leaderboardCodes = new List<string>();
    leaderboardCodes.Add("highScoreLeaderboard");
    
    new GetLeaderboardEntriesRequest()
      .SetLeaderboards(leaderboardCodes)
        .SetPlayer("56103bc5dd3049eb")
        .Send((response) => {
          if(response.HasErrors)
            {
              Debug.LogError("Couldnt get player rank for some reason");
          }else{
            var results = response.Results; 
            var scriptData = response.ScriptData; 

            if(scriptData != null){ // ALWAYS NULL
              int rank = int.Parse(scriptData.GetString("rank"));
              Debug.Log("Rank is: " + rank);
            }

            // Not sure what to put here:
			
          }
        });
  }

 

Feel free to look under my account to see this for your self. I have been stuck on this for too long and would appreciate it, if someone could help me with the c# portion. I even tried messing around with the rest of the data under response, but couldnt come up with the rank.


Thank you,