4

I'm working on a multi level Android game and I want to pull the highscores after sign in. I'm doing this:

Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(LEADER_BOARDS[i]),
                LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
                .setResultCallback(this);

But the callback doesn't give me the leaderboard id:

@Override
public void onResult(LoadPlayerScoreResult res) {

}

I only get the score (res.getScore()). So do I have to create callbacks for each of my 25 levels or is there a better solution?

This doesn't work as well:

for (int i = 0; i < 25; i++)
    Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(LEADER_BOARDS[i]),
                LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
                .setResultCallback(new ResultCallback<LoadPlayerScoreResult>() {

                    @Override
                    public void onResult(LoadPlayerScoreResult res) {
                        // can't use variable i here
                    }
    });
lukas1994
  • 1,799
  • 1
  • 13
  • 18
  • I think your approach is off. Why do you need the leaderboard id if you already have it. You are inputting it into the method why do you need it back. – Ogen Apr 02 '14 at 12:15
  • Because I have to know which result I get back (for the first approach with one onResult listener). Sure if I copy it 25 times I know the leaderboard id but there should be a better solution than that. – lukas1994 Apr 02 '14 at 12:20
  • So what is your goal exactly, you want to get the scores for a player from 25 different leaderboards in the most efficient way? – Ogen Apr 02 '14 at 12:41
  • Exactly and without copying the same statement 25 times and just change the leaderboard id index ;) – lukas1994 Apr 02 '14 at 12:54
  • In that case just use that for loop that you have typed. What is wrong with it? – Ogen Apr 02 '14 at 12:56
  • Oh wait you want to know which score belonged to which board right? – Ogen Apr 02 '14 at 12:59
  • I can't use `i` in the `onResult()` function since it's not final and I obviously I can't make it final. – lukas1994 Apr 02 '14 at 12:59
  • Ok I think I know what to do, just gimme sum time – Ogen Apr 02 '14 at 13:03

1 Answers1

2

Define this inner class in your java file:

public class Leaderboard implements ResultCallback<Leaderboards.LoadPlayerScoreResult> {

        private Leaderboard(String id) {
            leaderboardId = id;
        }

        private String leaderboardId = "";

        @Override
        public void onResult(LoadPlayerScoreResult arg0) {
            // TODO Auto-generated method stub
            System.out.println(leaderboardId);
        }

}

Now use this code:

for (int i = 0; i < 25; i++) { 
    Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(LEADER_BOARDS[i]),
    LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
    .setResultCallback(new Leaderboard(getString(LEADER_BOARDS[i])));
}

Now let me explain how this works a little. The Leaderboard class implements ResultCallback so it's used in the setResultCallback method after you get the score for the player. Now, in the for loop, each call to get a score is having a different object of class leaderboard being set as the result callback, and each object is being given the unique leaderboard id which is being saved in the class using the constructor, and as you can see in the class, there is a method that will give you the leaderboardid back in the onresult.

Ogen
  • 6,499
  • 7
  • 58
  • 124
  • Haha, I didn't test it yet but it should work ;) I'll let you know. – lukas1994 Apr 02 '14 at 13:38
  • Just curious. i've tried similiar code and it works for 2-3 (the first) leaderboards of 9. Some kind of limit i think. Did it actually work for more? – Aksel Willgert Dec 16 '16 at 18:52
  • @AkselWillgert same here, I can only get the first several leaderboards in a for loop. Any solution to handle this? – jdleung Mar 18 '23 at 02:26