Java – submit scores to Google play game leaderboard and display new rankings
I'm working on a game in which scores are submitted to the leaderboard in the activity, and new scores are displayed in the ranking in the clip I have some features, but the success rate is about 10%
The process is as follows:
Method handleleaders
This method gets the current score of each leaderboard. If the new score is better, submit it and use the score to create a new newhigh object and add it to ArrayList After processing all 3 charts, call the method setHighs.. (check for errors in each leaderboard call)
public void handleLeaders(boolean win,int size,double t,final int toupees) { if(win) { final long time = (long) t; // Toupees Games.Leaderboards.loadCurrentPlayerLeaderboardscore(mGoogleApiClient,getString(R.string.leaderboard_trumps_toupeed),LeaderboardVariant.TIME_SPAN_ALL_TIME,LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback( new ResultCallback<Leaderboards.LoadPlayerscoreResult>() { @Override public void onResult(Leaderboards.LoadPlayerscoreResult arg0) { Leaderboardscore c = arg0.getscore(); int old; if (c != null) old = (int) c.getRawscore(); else old = 0; Games.Leaderboards.submitscore(mGoogleApiClient,getResources().getString(R.string.leaderboard_trumps_toupeed),old + toupees); GameEndOverlay.newHighs.add(new newHigh("Trumps Toupee'd",old + toupees)); Status status = arg0.getStatus(); int statusCode = status.getStatusCode(); if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) GameEndOverlay.highsError = true; if(++GameEndOverlay.leaderboardsCompleted == 3) ((GameEndOverlay) gameEndOverlayFrag).setHighs(); } }); if (size == getResources().getInteger(R.integer.size_apprentice)) { // Wins Games.Leaderboards.loadCurrentPlayerLeaderboardscore(mGoogleApiClient,getString(R.string.leaderboard_apprentice_wins),LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback( new ResultCallback<Leaderboards.LoadPlayerscoreResult>() { @Override public void onResult(Leaderboards.LoadPlayerscoreResult arg0) { Leaderboardscore c = arg0.getscore(); int wins; if (c != null) wins = (int) c.getRawscore(); else wins = 0; Games.Leaderboards.submitscore(mGoogleApiClient,getResources().getString(R.string.leaderboard_apprentice_wins),wins + 1); GameEndOverlay.newHighs.add(new newHigh("Apprentice Wins",wins + 1)); Status status = arg0.getStatus(); int statusCode = status.getStatusCode(); if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) GameEndOverlay.highsError = true; if(++GameEndOverlay.leaderboardsCompleted == 3) ((GameEndOverlay) gameEndOverlayFrag).setHighs(); } }); // Speed Games.Leaderboards.loadCurrentPlayerLeaderboardscore(mGoogleApiClient,getString(R.string.leaderboard_fastest_apprentice),LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback( new ResultCallback<Leaderboards.LoadPlayerscoreResult>() { @Override public void onResult(Leaderboards.LoadPlayerscoreResult arg0) { Leaderboardscore c = arg0.getscore(); long old_time; if(c != null) { old_time = c.getRawscore(); Log.d("time",old_time + ""); if(time < old_time) { Games.Leaderboards.submitscore(mGoogleApiClient,getResources().getString(R.string.leaderboard_fastest_apprentice),time); GameEndOverlay.newHighs.add(new newHigh("Fastest Apprentice",time)); } } else { Games.Leaderboards.submitscore(mGoogleApiClient,time); GameEndOverlay.newHighs.add(new newHigh("Fastest Apprentice",time)); } Status status = arg0.getStatus(); int statusCode = status.getStatusCode(); if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) GameEndOverlay.highsError = true; if(++GameEndOverlay.leaderboardsCompleted == 3) ((GameEndOverlay) gameEndOverlayFrag).setHighs(); } }); } }
Method sethighs
This method obtains the level of each corresponding newhigh and stores the new level in the object After collecting all the rankings, call the method setSecondHighs. (check for errors in each leaderboard call)
public void setHighs() { if(getActivity() == null) return; ranksComputed = 0; for(newHigh highRaw : newHighs) { final newHigh high = highRaw; switch(high.getName()) { case "Trumps Toupee'd": Games.Leaderboards.loadCurrentPlayerLeaderboardscore(mGoogleApiClient,LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback( new ResultCallback<Leaderboards.LoadPlayerscoreResult>() { @Override public void onResult(Leaderboards.LoadPlayerscoreResult arg0) { if(arg0.getscore() == null) { highsError = true; ranksComputed++; if(ranksComputed >= newHighs.size()) setSecondHighs(); return; } high.setRank(arg0.getscore().getRank()); Status status = arg0.getStatus(); int statusCode = status.getStatusCode(); if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) GameEndOverlay.highsError = true; ranksComputed++; if(ranksComputed >= newHighs.size()) setSecondHighs(); } }); break; case "Apprentice Wins": Games.Leaderboards.loadCurrentPlayerLeaderboardscore(mGoogleApiClient,LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback( new ResultCallback<Leaderboards.LoadPlayerscoreResult>() { @Override public void onResult(Leaderboards.LoadPlayerscoreResult arg0) { if(arg0.getscore() == null) { highsError = true; ranksComputed++; if(ranksComputed >= newHighs.size()) setSecondHighs(); return; } high.setRank(arg0.getscore().getRank()); Status status = arg0.getStatus(); int statusCode = status.getStatusCode(); if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) GameEndOverlay.highsError = true; ranksComputed++; if(ranksComputed >= newHighs.size()) setSecondHighs(); } }); break; case "Fastest Apprentice": Games.Leaderboards.loadCurrentPlayerLeaderboardscore(mGoogleApiClient,LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback( new ResultCallback<Leaderboards.LoadPlayerscoreResult>() { @Override public void onResult(Leaderboards.LoadPlayerscoreResult arg0) { if(arg0.getscore() == null) { highsError = true; ranksComputed++; if(ranksComputed >= newHighs.size()) setSecondHighs(); return; } high.setRank(arg0.getscore().getRank()); Status status = arg0.getStatus(); int statusCode = status.getStatusCode(); if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) GameEndOverlay.highsError = true; ranksComputed++; if(ranksComputed >= newHighs.size()) setSecondHighs(); } }); break; } } }
Method setsecondhigh
This method displays an error or a new ranking score to the user
public void setSecondHighs() { if(highsError) // display an error to the user else // display ranks+score to user }
The problem is that there are many API calls, and the submitted content hangs at different points of the call I know there must be a better way to do this Any help would be appreciated
Cheers!
Solution
I encountered the same problem when trying to increase the ranking score. Google has limited the number of requests you can make in unrecorded / unconfirmed time periods Generally, three consecutive requests to retrieve leaderboard data will pass, and the rest will return network related errors For more details about other users of the same question, see here: Android – Google play service: leaderboard, limited number of requests