Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 450
SopheakVirak Avatar asked Apr 23 '14 15:04

SopheakVirak


2 Answers

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;
}
like image 169
tobi_b Avatar answered Oct 09 '22 00:10

tobi_b


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.

like image 33
Yo Apps Avatar answered Oct 08 '22 23:10

Yo Apps