SopheakVirak
SopheakVirak

Reputation: 961

How to get score from google play game service's leaderboard of current player?

Currently I am doing game that integrated with Google Game Play Service and I want to compression score between score and best of score, so I easy inform player that they are getting New High Score. But I don't how to getscore from google game service leaderboard, can anybody please guide me on how to do it?

I am able to display leaderboard but i can't find the way how to get score for user playing.

my code that showing leaderboard:

if (isSignedIn())
    {
        if(inScore<=50)
        {
            Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_easy), inScore);
        }
        else
        {
            Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_hard), inScore);
        }
    } else {
        Log.d("not signed", "Not signed in");
    }

I want to get score from user that are playing on their device, help me please.

Upvotes: 10

Views: 8493

Answers (4)

Danoli3
Danoli3

Reputation: 3233

For Android: Do Not Use loadCurrentPlayerLeaderboardScore. Use a scoped request else you will only be able to retrieve up to 3 leaderboards and sometimes not even one. So many people over the years have met the grim fate of this unsolved problem

Use the following code instead: Use loadPlayerCenteredScores:

long limitResultsTo = 1;
String leaderboardID = getString(R.string.leaderboard_name); // or string of ID
Games.getLeaderboardsClient(this, GoogleSignIn.getLastSignedInAccount(this))
    .loadPlayerCenteredScores(leaderboardName, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, limitResultsTo)
    .addOnSuccessListener(new OnSuccessListener<AnnotatedData<LeaderboardsClient.LeaderboardScores>>() {
        @Override
        public void onSuccess(AnnotatedData<LeaderboardsClient.LeaderboardScores> leaderboardScoreAnnotatedData) { // big ups Danoli3.com for the fix for loadCurrentPlayerLeaderboardScore
            LeaderboardsClient.LeaderboardScores scoresResult =  leaderboardScoreAnnotatedData.get();
            LeaderboardScore scoreResult = (scoresResult != null ? scoresResult.getScores().get(0) : null);
            long score = 0;
            if(scoreResult != null) score = scoreResult.getRawScore();

            // use the score in your own code here
            // Log.i(TAG, "loadPlayerCenteredScores:" + leaderboardID + " score:" + score);

            leaderboardScoreAnnotatedData = null; 
        }
    }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Log.e(TAG, "Failure:loadPlayerCenteredScores  GPG:Leader:" + leaderboardID + " Ex:" + e.getMessage());
    }

Upvotes: 0

Yo Apps
Yo Apps

Reputation: 536

4 years later I was having a similar problem with this situation. Some stuff has been deprecated and stuff no longer works. So for those of you who want to know how to do it now, in 2018... check this answer-

First you have to get the LeaderBoardClient with

mLeaderboardsClient = Games.getLeaderboardsClient(MainActivity.this, googleSignInAccount);

Next you can the score

mLeaderboardsClient.loadCurrentPlayerLeaderboardScore(getString(R.string.leaderboard_id), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
          .addOnSuccessListener(this, new OnSuccessListener<AnnotatedData<LeaderboardScore>>() {
              @Override
              public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
                  long score = 0L;
                  if (leaderboardScoreAnnotatedData != null) {
                      if (leaderboardScoreAnnotatedData.get() != null) {
                          score = leaderboardScoreAnnotatedData.get().getRawScore();
                          Toast.makeText(MainActivity.this, Long.toString(score), Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "LeaderBoard: " + Long.toString(score));
                      } else {
                          Toast.makeText(MainActivity.this, "no data at .get()", Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "LeaderBoard: .get() is null");
                      }
                  } else {
                      Toast.makeText(MainActivity.this, "no data...", Toast.LENGTH_SHORT).show();
                      Log.d(TAG, "LeaderBoard: " + Long.toString(score));
                  }
              }
          })
          .addOnFailureListener(new OnFailureListener() {
              @Override
              public void onFailure(@NonNull Exception e) {
                  Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
                  Log.d(TAG, "LeaderBoard: FAILURE");
              }
  });

The 3 parameters to .loadCurrentPlayerLeaderboardScore are as follows

ID of the leaderboard to load the score from.

Time span to retrieve data for. Valid values are TIME_SPAN_DAILY, TIME_SPAN_WEEKLY, or TIME_SPAN_ALL_TIME.

The leaderboard collection to retrieve scores for. Valid values are either COLLECTION_PUBLIC or COLLECTION_SOCIAL.

Upvotes: 6

doğanay
doğanay

Reputation: 11

I could not reach inside of onResult program passing directly. After submit inside of onActivityResult I'm calling it. Is it wrong to call in onActivityResult.

Games.Leaderboards.submitScore(googleApiConnection.mGoogleApiClient, String.valueOf(R.string.leaderboard_winner), Long.valueOf(120));

Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(R.string.leaderboard_winner), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
                    @Override
                    public void onResult(final Leaderboards.LoadPlayerScoreResult scoreResult) {
                        // if (isScoreResultValid(scoreResult)) {
                        LeaderboardScore c = scoreResult.getScore();
                        // here you can get the score like this
                        //    Log.e("doganay","LeaderBoardLoadSCore :"+c.getDisplayScore());
                        //us.setCoin(60);
                        mPoints = c.getRawScore();



                    }
                });

Upvotes: 0

tobi_b
tobi_b

Reputation: 1238

This is how I'm fetching the score of the current player:

private void loadScoreOfLeaderBoard() {
    Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(R.string.your_leaderboard_id), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
        @Override
        public void onResult(final Leaderboards.LoadPlayerScoreResult scoreResult) {
            if (isScoreResultValid(scoreResult)) {
                // here you can get the score like this
                mPoints = scoreResult.getScore().getRawScore();
            }
        }
    });
}

private boolean isScoreResultValid(final Leaderboards.LoadPlayerScoreResult scoreResult) {
    return scoreResult != null && GamesStatusCodes.STATUS_OK == scoreResult.getStatus().getStatusCode() && scoreResult.getScore() != null;
}

Upvotes: 13

Related Questions