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.
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;
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With