Sign In Register

How can we help you today?

Start a new topic
Answered

Requesting players current rank

Hi Guys

Im finishing setting up a Gamesparks leader board for my game, got most of it sorted, have it pulling down the user data for the top 10 players.

What Im having trouble with is pulling down the rank for the current player.

The Code I have is

new GetLeaderboardEntriesRequest().SetPlayer("").Send((response2) =>
{              
      Debug.Log("results" + response2.Results);
});

Im getting a null response in the debug loh

I havnt had much luck with the help files, or the search feature.


Im working in Unity and C#


Best Answer

I've recreated your scenario in test harness. The problem with your code is that the player id field is null. A player ID must be given to find THAT players leader board score. Furthermore you must specify a leaderboard to pull from. My usual practice is to first test all behaviors in the test ahrness. This confirms for me whether the desired behavior will work, or if not, will demonstrate whether I'm making a mistake, or it is on GameSparks end. 


Here is what the request looks like in the harness. Note: the player id was copied and pasted by me from a leaderboardDataRequest. High_Score_Leaderboard is the shortcode for that leaderboard.  

 

{
 "@class": ".LeaderboardsEntriesRequest",
 
 "leaderboards": [
  "High_Score_Leaderboard"
 ],
 "player": "5687f439e4b04444878e257b"
}

  

The unity implementation is the following:     

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class AuthLeaderBoardEntry : MonoBehaviour
{

    public Text userNameInuput, userPassInput;

    public string userID; 
    private bool pressedUIButton = false;

    public List<string> myLeaderBoard = new List<string>();
    public void pressedLoginButton()
    {
        pressedUIButton = true;
    }

    
    public void authorizePlayerBttn()
    {
        new GameSparks.Api.Requests.AuthenticationRequest().SetUserName("Test")
            .SetPassword("Test")
            .Send((response) => {
                if (!response.HasErrors)
                {
                    Debug.Log("Player Authenticated...");
                    userID = response.UserId;
                }
                else
                {
                    Debug.Log("Error Authenticating Player...");
                }
            });


        myLeaderBoard.Add("High_Score_Leaderboard");

        new GameSparks.Api.Requests.GetLeaderboardEntriesRequest().SetPlayer(userID)
                                                                  .SetLeaderboards(myLeaderBoard)
                                                                  .Send((response) => {
                                                                      if (!response.HasErrors)
                                                                      {
                                                                          Debug.Log("results" + response.Results);

                                                                      }
                                                                      else
                                                                      {
                                                                          Debug.Log("Error Authenticating Player...");
                                                                      }
                                                                  });
    }
    void Update()
    {
        if (pressedUIButton)
        {
            authorizePlayerBttn();
            pressedUIButton = false;
        }
    }
}

  

Note: I set the value of userID =  response.UserID in my auth request. Then I pass it as a argument into .SetPlayer(). Also, I create a List of type string and using list.add() I pass in the leaderboard shortcode. Then I pass the list as  an argument for .SetLeaderBoards()

Hope this clarifies everything for you. Feel free to get back to me. 

Thanks, Patrick. 

1 Comment

Answer

I've recreated your scenario in test harness. The problem with your code is that the player id field is null. A player ID must be given to find THAT players leader board score. Furthermore you must specify a leaderboard to pull from. My usual practice is to first test all behaviors in the test ahrness. This confirms for me whether the desired behavior will work, or if not, will demonstrate whether I'm making a mistake, or it is on GameSparks end. 


Here is what the request looks like in the harness. Note: the player id was copied and pasted by me from a leaderboardDataRequest. High_Score_Leaderboard is the shortcode for that leaderboard.  

 

{
 "@class": ".LeaderboardsEntriesRequest",
 
 "leaderboards": [
  "High_Score_Leaderboard"
 ],
 "player": "5687f439e4b04444878e257b"
}

  

The unity implementation is the following:     

using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;

public class AuthLeaderBoardEntry : MonoBehaviour
{

    public Text userNameInuput, userPassInput;

    public string userID; 
    private bool pressedUIButton = false;

    public List<string> myLeaderBoard = new List<string>();
    public void pressedLoginButton()
    {
        pressedUIButton = true;
    }

    
    public void authorizePlayerBttn()
    {
        new GameSparks.Api.Requests.AuthenticationRequest().SetUserName("Test")
            .SetPassword("Test")
            .Send((response) => {
                if (!response.HasErrors)
                {
                    Debug.Log("Player Authenticated...");
                    userID = response.UserId;
                }
                else
                {
                    Debug.Log("Error Authenticating Player...");
                }
            });


        myLeaderBoard.Add("High_Score_Leaderboard");

        new GameSparks.Api.Requests.GetLeaderboardEntriesRequest().SetPlayer(userID)
                                                                  .SetLeaderboards(myLeaderBoard)
                                                                  .Send((response) => {
                                                                      if (!response.HasErrors)
                                                                      {
                                                                          Debug.Log("results" + response.Results);

                                                                      }
                                                                      else
                                                                      {
                                                                          Debug.Log("Error Authenticating Player...");
                                                                      }
                                                                  });
    }
    void Update()
    {
        if (pressedUIButton)
        {
            authorizePlayerBttn();
            pressedUIButton = false;
        }
    }
}

  

Note: I set the value of userID =  response.UserID in my auth request. Then I pass it as a argument into .SetPlayer(). Also, I create a List of type string and using list.add() I pass in the leaderboard shortcode. Then I pass the list as  an argument for .SetLeaderBoards()

Hope this clarifies everything for you. Feel free to get back to me. 

Thanks, Patrick. 

Login to post a comment