Sameer M
Sameer M

Reputation: 41

Problems syncing my game local scores from the leaderboards values

I'm getting errors when trying to get a player's scores across all leaderboards, in order to store locally.

My Android game is designed to be played offline, so I store high score and lifetime scores locally for each game mode.

Now I am working on adding leaderboards to my game. During the game play, I periodically post these scores to the leaderboards. So far so good.

Naturally, I need to cover the scenario where a user either uninstalls my game or moves to a new device, etc. In that scenario, when they sign in again to Google, I need to update the scores that are on leaderboards back into my local version.

Here's where I seem to get 26504 - NETWORK_ERROR_NO_DATA when I make the 4th call.

I have tried to wait until the 3rd call is complete and then fire the 4th one but I still get the same error. So I haven't even been able to add code for the 5-6 additional scores I will still need to retrieve.

getScoresFromLeaderboards(GAME_MODE_FIXED_QUESTIONS, SCORE_HIGH);
getScoresFromLeaderboards(GAME_MODE_FIXED_QUESTIONS, SCORE_LIFETIME);
getScoresFromLeaderboards(GAME_MODE_RAPID_FIRE, SCORE_HIGH);
getScoresFromLeaderboards(GAME_MODE_RAPID_FIRE, SCORE_LIFETIME);

Then...

public long getScoresFromLeaderboards(final String gameMode, final String scoreType) {
final int scoreHigh = 0, scoreLifetime = 0;

String leaderboardID = getLeaderboardForGameMode(gameMode, scoreType);

mLeaderboardsClient.loadCurrentPlayerLeaderboardScore(leaderboardID, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC )
.addOnCompleteListener(new OnCompleteListener<AnnotatedData<LeaderboardScore>>() {
       @Override
       public void onComplete(@NonNull Task<AnnotatedData<LeaderboardScore>> task) {
                if (task.isSuccessful()) {
                    AnnotatedData<LeaderboardScore> lbs = task.getResult();
                    long i = lbs.get().getRawScore();
                    // Do something to store the score locally
                }
                else{
                     Log.Error("Error", "Retrieval failed");
                }
      }

Upvotes: 1

Views: 186

Answers (1)

Danoli3
Danoli3

Reputation: 3233

I would suggest another approach to using "loadCurrentPlayerLeaderboardScore" which has the 3 query limit

Use loadPlayerCenteredScores instead . Limit the scores to 1. The resultant buffer will return only the player score. You are now in the user quota of 500 requests instead of 3.

long limitResultsTo = 1;
String PlayerID = "-1"; // set this from playersClient.getCurrentPlayer() ->task-> getPlayerId()
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 && scoreResult.getScoreHolder() != null &&
                PlayerID.equals(scoreResult.getScoreHolder().getPlayerId())) // else if the player has 0 score it will retrieve top player
            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: 2

Related Questions