我正在开发一款涉及到排行榜的游戏,游戏中玩家提交分数,新的高分会在游戏界面的碎片中显示出来,同时附带排名。目前我的方案已经初步实现,但是成功率只有大约10%。
整个流程如下:
方法handleLeaders
该方法获取每个排行榜的当前得分,并检查新的得分是否更高。如果更高,则会将新的得分提交并创建一个newHigh对象,并添加到ArrayList中。在处理完3个排行榜后,会调用setHighs方法。(每个排行榜调用时都会进行错误检查)
问题在于这里有很多API调用,而提交会在调用的不同阶段挂起。我知道肯定有更好的方法来解决这个问题。非常感谢您的帮助。
干杯!
整个流程如下:
方法handleLeaders
该方法获取每个排行榜的当前得分,并检查新的得分是否更高。如果更高,则会将新的得分提交并创建一个newHigh对象,并添加到ArrayList中。在处理完3个排行榜后,会调用setHighs方法。(每个排行榜调用时都会进行错误检查)
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.TIME_SPAN_ALL_TIME,
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.TIME_SPAN_ALL_TIME,
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, getResources().getString(R.string.leaderboard_fastest_apprentice), 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();
}
});
}
}
方法:setHighs
该方法获取每个对应的newHigh的排名,并将新的排名存储在对象中。当所有排名都收集完成后,将调用setSecondHighs方法。(在leaderboard的每个调用中都会检查错误)
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,
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) {
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,
getString(R.string.leaderboard_apprentice_wins),
LeaderboardVariant.TIME_SPAN_ALL_TIME,
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,
getString(R.string.leaderboard_fastest_apprentice),
LeaderboardVariant.TIME_SPAN_ALL_TIME,
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;
}
}
}
setSecondHighs方法
该方法会向用户显示错误信息或新的排名和分数。
public void setSecondHighs() {
if(highsError)
// display an error to the user
else
// display ranks+score to user
}
问题在于这里有很多API调用,而提交会在调用的不同阶段挂起。我知道肯定有更好的方法来解决这个问题。非常感谢您的帮助。
干杯!