playgameservices / android-basic-samples

Google Play game services - Android samples
Apache License 2.0
971 stars 973 forks source link

why getting current player score is so hard? #265

Open kadirboz opened 6 years ago

kadirboz commented 6 years ago

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

mwgray commented 6 years ago

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.

danoli3 commented 3 years ago

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());

        }