Reputation: 117
I used the following code to submit the user's score to Google Play Games:
if(getApiClient().isConnected()){
Games.Leaderboards.submitScore(getApiClient(), getString(R.string.number_guesses_leaderboard),newScore);
}
but this doesn't increment the score of leaderboard, it just replaces the score. The same value is shown the all the time. newScore
is the amount that I want to increment the current score by.
Upvotes: 5
Views: 1107
Reputation: 51
Here is a method I use to increment a google play services leaderboard. If the score returned is null, score of 1 is posted. Otherwise, current score is incremented by 1. This is useful for keeping track of daily scores, for example. Note the use of TIME_SPAN_DAILY to indicate the time span desired. This will retrieve the highest score posted for the day, assuming "Higher is better" was indicated when setting up the leaderboard.
private void incrementDailyLeaderboard(final LeaderboardsClient leaderboardsClient, final String leaderboardId) {
leaderboardsClient.loadCurrentPlayerLeaderboardScore(
leaderboardId,
LeaderboardVariant.TIME_SPAN_DAILY,
LeaderboardVariant.COLLECTION_PUBLIC
).addOnSuccessListener(this, new OnSuccessListener<AnnotatedData<LeaderboardScore>>(){
@Override
public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
//Log.v("onSuccess","onSuccess");
if (leaderboardScoreAnnotatedData != null) {
long score = 0;
if (leaderboardScoreAnnotatedData.get() != null) {
score = leaderboardScoreAnnotatedData.get().getRawScore();
//Log.v("Your score is", Long.toString(score));
}
leaderboardsClient.submitScore(leaderboardId, ++score);
}
}
});
}
Upvotes: 3
Reputation: 229
You will need to save the current score locally using SharedPreferences
, for example, and then submit the new total score to the Google Play Games service. Have a look a this sample by the Google Play Games team, specifically their use of an AccomplishmentsOutbox
where they are storing the score locally until it is transmitted to the API.
Upvotes: 3