Top 5 scores from google leaderboard
Asked Answered
N

4

4

My requirement is to get top 5 scores from leaderboard and display it in my app.

There is a method loadTopScores but it shows the scores in its own UI i guess.

mGamesClint.loadTopScores(new OnLeaderboardScoresLoadedListener() {

            public void onLeaderboardScoresLoaded(int arg0, LeaderboardBuffer arg1,
                    LeaderboardScoreBuffer arg2) {
                // TODO Auto-generated method stub




            }
        }, LEADERBOARD_ID,LeaderboardVariant.TIME_SPAN_ALL_TIME  , LeaderboardVariant.COLLECTION_PUBLIC, 5, true);

So is there any way I can get individual data like Name and score..?

Name 1 : Name of the top scorer 1 score 1 : score of the top scorer 1

Name 2 : Name of the top scorer 2 score 2 : score of the top scorer 2

......and so on

I just want name string and score integer so that I can use it in my game.

Please suggest me some ideas

Natishanative answered 5/6, 2014 at 10:23 Comment(1)
can you please post your solution here? I am searching the same thing but I am not able to get a solution yet.Sternick
N
2

My Solution which worked is as follows which is for the latest game play services.

  Games.Leaderboards.loadTopScores(mGamesClint,LEADERBOARD_ID, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, 5).setResultCallback(new ResultCallback<Leaderboards.LoadScoresResult>() {

        public void onResult(LoadScoresResult arg0) {
            // TODO Auto-generated method stub

            int size = arg0.getScores().getCount();



            for ( int i = 0; i < 3; i++ )  {

                LeaderboardScore lbs = arg0.getScores().get(i);

                String name = lbs.getScoreHolderDisplayName();

                String score = lbs.getDisplayScore();

                Uri urlimage = lbs.getScoreHolderHiResImageUri();

                 }

 }

you can get all the data from leaderboard users here including high resolution image ie. profile picture. Hope you get the idea here.

Natishanative answered 8/6, 2015 at 11:48 Comment(1)
looks fine but you should close the scorebuffer after you're done (according to the reference): arg0.getScores().close();Mithgarthr
A
3

You are on the right track in your example, you just need to extract the information once it is loaded. It is fairly confusing to get it working but I will point you to this answer of mine, which shows how to do it for achievements - doing it for leaderboards works the same way, except that you will use the leaderboard interfaces instead of those used for achievements.

Basically, you will access arg2 to get the data, and it should look something like this:

mGamesClint.loadTopScores(new OnLeaderboardScoresLoadedListener() {

   public void onLeaderboardScoresLoaded(int arg0, LeaderboardBuffer arg1, LeaderboardScoreBuffer arg2) {

      // iterate through the list of returned scores for the leaderboard
      int size = arg2.getCount();
      for ( int i = 0; i < size; i++ )  {
         LeaderboardScore lbs = arg2.get( i );

         // access the leaderboard data
         int rank = i + 1;         // Rank/Position (#1..#2...#n)
         String name = lbs.getScoreHolderDisplayName();
         String scoreStr = lbs.getDisplayScore();
         long score = lbs.getRawScore();

         // now display or cache these values, or do whatever with them :)
      }

      arg2.close();
      arg1.close();
   }
}, LEADERBOARD_ID,LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, 5, true);

I did not actually test this implementation, but it should work (or at least show you enough so you can fix any mistakes I might have made).

Just remember that this will be done asynchronously, so the contents of the method will not execute immediately, but rather once the scores have been loaded.

Akene answered 5/6, 2014 at 21:46 Comment(5)
with this I can get score and names from leaderboard and create my own UI for leaderboard..Natishanative
My pleasure, I'm glad to help. And since there were no examples I could find anywhere it is good to have it written down somewhere for future reference.Akene
any chance you could post the working code? this doesn't seem to work as it isPetal
@Petal I can't write your code for you, I can only point you in the right direction - this was never meant to be working code, just an example. Consult the documentation if you need further explanation.Akene
I cant find OnLeaderboardScoresLoadedListener listener anywhere, I have searched the docs and every thing I can, Can you please help?Sternick
S
3

Here is how I have achieved it.

PendingResult<Leaderboards.LoadScoresResult> topScores = 
            Games.Leaderboards.loadTopScores(getApiClient(),LEADERBOARD_ID,LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, 1, true);
            topScores.setResultCallback(new ResultCallback<Leaderboards.LoadScoresResult>() {

                @Override
                public void onResult(LoadScoresResult scoreResults) {

                    if(scoreResults != null) {
                        if (scoreResults.getStatus().getStatusCode() == GamesStatusCodes.STATUS_OK) {
                            scoreStringBuilder = new StringBuilder();
                            LeaderboardScoreBuffer scoreBuffer = scoreResults.getScores();
                            Iterator<LeaderboardScore> it = scoreBuffer.iterator();
                            while(it.hasNext()){
                                 LeaderboardScore temp = it.next();
                                 Log.d("PlayGames", "player"+temp.getScoreHolderDisplayName()+" id:"+temp.getRawScore() + " Rank: "+temp.getRank());
                                 scoreStringBuilder.append("|"+temp.getScoreHolderDisplayName()+"*"+temp.getRawScore());
                            }
                            UnityPlayer.UnitySendMessage(handlerName, "onGetScoreSucceededEventListener", "");
                             Log.d("PlayGames", "Call Sent for getHighScorewithPlayerNameSuccess ::: "+handlerName);


                        }
                    }

                }});

You can use it as you want I created a String and returned it to Unity and then parsed it. But this code is working with Latest Google Play Services.

Thanks

Sternick answered 8/6, 2015 at 12:5 Comment(0)
N
2

My Solution which worked is as follows which is for the latest game play services.

  Games.Leaderboards.loadTopScores(mGamesClint,LEADERBOARD_ID, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, 5).setResultCallback(new ResultCallback<Leaderboards.LoadScoresResult>() {

        public void onResult(LoadScoresResult arg0) {
            // TODO Auto-generated method stub

            int size = arg0.getScores().getCount();



            for ( int i = 0; i < 3; i++ )  {

                LeaderboardScore lbs = arg0.getScores().get(i);

                String name = lbs.getScoreHolderDisplayName();

                String score = lbs.getDisplayScore();

                Uri urlimage = lbs.getScoreHolderHiResImageUri();

                 }

 }

you can get all the data from leaderboard users here including high resolution image ie. profile picture. Hope you get the idea here.

Natishanative answered 8/6, 2015 at 11:48 Comment(1)
looks fine but you should close the scorebuffer after you're done (according to the reference): arg0.getScores().close();Mithgarthr
I
2

This code gets you the top 5 scores on your leaderboard.

private LeaderboardsClient mLeaderboardsClient; 

    mLeaderboardsClient.loadTopScores("HgkF9bOSF_UPAAILKE", LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC,5,true).addOnSuccessListener(new OnSuccessListener<AnnotatedData<LeaderboardsClient.LeaderboardScores>>() {
                        @Override
                        public void onSuccess(AnnotatedData<LeaderboardsClient.LeaderboardScores> leaderboardScoresAnnotatedData) {

                            LeaderboardScoreBuffer scoreBuffer = leaderboardScoresAnnotatedData.get().getScores();
                            Iterator<LeaderboardScore> it = scoreBuffer.iterator();
                            while(it.hasNext()){
                                LeaderboardScore temp = it.next();
                                Log.d("PlayGames", "player"+temp.getScoreHolderDisplayName()+" id:"+temp.getRawScore() + " Rank: "+temp.getRank());
                            }

                        }
                    });
Ivette answered 21/8, 2018 at 13:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.