android-basic-samples
android-basic-samples copied to clipboard
why getting current player score is so hard?
i wanna get users get last score in playstore but i research 2 days and i got nothing. Finally, i wrote the code (i'm not sure it'll be wrong code) but always get failure.
Games.getLeaderboardsClient(this, GoogleSignIn.getLastSignedInAccount(this))
.loadCurrentPlayerLeaderboardScore(
"YOUR_KEY", LeaderboardVariant.TIME_SPAN_ALL_TIME,
LeaderboardVariant.COLLECTION_PUBLIC)
.addOnSuccessListener(this, new OnSuccessListener<AnnotatedData<LeaderboardScore>>(){
@Override
public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
if (leaderboardScoreAnnotatedData != null) {
long score = 0;
if (leaderboardScoreAnnotatedData.get() != null) {
score = leaderboardScoreAnnotatedData.get().getRawScore();
Toast.makeText(MenuActivity.this, Long.toString(score), Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MenuActivity.this, "null 2", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MenuActivity.this, "null 1", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MenuActivity.this, "addOnFailureListener", Toast.LENGTH_SHORT).show();
}
});
and i'm asking a question. why getting current player score is so hard? submitscore is very easy, why getting score is not like submitting score?
please add getScore function like submitScore
Hi @kadirboz, what error are you seeing?
Try printing out more details of the failure exception like in the Type A Number Sample.
Without more information, just by looking at your code, getLastSignedInAccount might not be returning a valid account.
Ah yes! Well it seems there is a quota restriction bug, never addressed with loadCurrentPlayerLeaderboardScore. You are restricted per user to 3 requests. No more per session/activity.
I would suggest another approach I have discovered as there seems to be no other
User loadPlayerCenteredScores. Limit the scores to 1. The resultant buffer will return only the player score. Fixed
leaderboardName = 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, 1)
.addOnSuccessListener(new OnSuccessListener<AnnotatedData<LeaderboardsClient.LeaderboardScores>>() {
@Override
public void onSuccess(AnnotatedData<LeaderboardsClient.LeaderboardScores> leaderboardScoreAnnotatedData) {
LeaderboardsClient.LeaderboardScores scoresResult = leaderboardScoreAnnotatedData.get();
LeaderboardScore scoreResult = (scoresResult != null ? scoresResult.getScores().get(0) : null);
long score = 0;
if(scoreResult != null) score = scoreResult.getRawScore();
gameStatsOutbox.setLeaderboard(score, leaderboardNumber); // if using example / change this to set the result score
Log.i(TAG, "GPGuS:Leader:" + leaderboardNumber + " score:" + score);
leaderboardScoreAnnotatedData = null;
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.e(TAG, "Failure:getLeaderboardScoreUnScoped GPG:Leader:" + leaderboardNumber + " Ex:" + e.getMessage());
}