将分数提交到Google Play游戏排行榜并显示新排名

时间:2016-05-03 19:39:08

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

我正在制作一项游戏,其中分数被提交到活动中的排行榜,并且新的高分显示在片段中的排名。我有一些(有点)功能,但成功率约为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;

        }
    }
}

方法setSecondHighs

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

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

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

干杯!

1 个答案:

答案 0 :(得分:0)

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