java - 向 Google Play 游戏排行榜提交分数并显示新排名

标签 java android google-play google-play-games

我正在开发一款游戏,其中将分数提交到 Activity 中的排行榜,并在 fragment 中显示新的高分和排名。我有一些(有点)功能,但成功率是 ~10%。

流程如下:

方法 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。 (在每个排行榜调用中检查错误)

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;

        }
    }
}

方法设置SecondHighs

此方法向用户显示错误或新排名+分数

public void setSecondHighs() {
  if(highsError)
    // display an error to the user
  else
    // display ranks+score to user
}

问题是这里有很多 API 调用,并且提交卡在调用的不同点。我知道必须有更好的方法来做到这一点。任何帮助将不胜感激。

干杯!

最佳答案

我在尝试增加排行榜分数时遇到了同样的问题,谷歌限制了您在未记录/未确认的时间段内可以提出的请求数量。通常 3 个连续的检索排行榜数据的请求将会通过,其余的将返回与网络相关的错误。可以在此处查看面临相同问题的其他用户的更多详细信息:Android - Google play service : Leaderboard, limited number of requests

关于java - 向 Google Play 游戏排行榜提交分数并显示新排名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37012975/

相关文章:

java - 有什么方法可以自动将 Javascript 库桥接到 GWT?

java - 如何在char数组中存储一个空格?

web-scraping - 3rd 方应用商店如何知道新应用何时添加到 Google Play?

android - 谷歌应用商店 : "This app is incompatible with your device"

java - 将默认应用程序上下文匿名传递到远程 EJB

java - Json解析listview自定义适配器

android - Titanium Studio for Android : Where does Ti. API.info() 打印消息?

Android sdk 管理器我没有 Android 支持库

android - 在 Android 上使用 MapDB 的最佳实践

android - 基于相同代码但不同包名称发布 2 个应用程序