简体   繁体   中英

How to load scores for current user from multiple leaderboard using Google Play Games services?

On load of my game I want to receive all scores for all levels (I have 22 currently) of my game for the current player. I am doing this:

@Override
public void onSignInSucceeded() {
    new Thread(new Runnable() {
        @Override
        public void run() {
            for (int i = 1; i < count; ++i)
                score = getScoreGPGS(LEVEL_LIST(i).leaderBoardID);
            // somehow use this score
        }
    }).start();
}

And where

public long getScoreGPGS(String leaderBoardID) {
    try {
        PendingResult<Leaderboards.LoadPlayerScoreResult> result;
        result = Games.Leaderboards.loadCurrentPlayerLeaderboardScore(gameHelper.getApiClient(),
                leaderBoardID, LeaderboardVariant.TIME_SPAN_ALL_TIME,
                LeaderboardVariant.COLLECTION_PUBLIC);
        LeaderboardScore score = result.await().getScore();
        if (score != null)
            return score.getRawScore();
        else
            return 0;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}

I understand that result.await().getScore(); is not best way to do it, but other ways haven't worked also (I tried to use callbacks as well as to create PendingResults for all my leaderboards and then in a loop to await it to complete, it is not the case here, this one is more convenient for me).

The problem is that I am able to receive scores for only 3 first levels (on any device on any internet connection). if I do result.await().getStatus() on failed levels it would not be Succeded or Cancelled or Interrupted, what specifically it returns I do not know because I can't debug it.

UPD : It seems like server blocks my calls. I tried to pause thread for 1 second between each call, it didn't work, but when I tried to pause thread for 10 seconds it did work, but only for 6 levels, maybe increasing of this time will help, but even 10 second was big time to wait. Maybe there is a different approach to this issue?

UPD2 : In Google API Console these calls are shown as "client errors" on API games.scores.get , but I am pretty sure that I am not exceeding my quota of 500 queries per user per 100 second.

You are experiencing rate limiting by the server. For more information read https://developers.google.com/games/services/quota . There are is a limit on the number of calls per user per 100 seconds, you can look at the API on the developer console and see what the values are. You'll need to keep your requests under that limit.

You can apply to increase the limit which is one way of solving the problem, the other approach might be to cache the results and be smart about any updates that need to be made.

Alternatively, it sounds like you might want to look at keeping a database of scores yourself, vs. using leaderboards as a datastore. Something like Firebase Realtime Database might be more appropriate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM