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

Posted

技术标签:

【中文标题】向 Google Play 游戏排行榜提交分数并显示新排名【英文标题】:Submitting scores to Google Play Games leaderboards and displaying new rank(s) 【发布时间】:2016-08-29 00:29:40 【问题描述】:

我正在开发一个游戏,其中分数被提交到活动中的排行榜,新的高分显示在片段中的排名。我有一些(有点)实用的东西,但成功率约为 10%。

流程如下:

方法句柄领导者

此方法获取每个排行榜的当前分数,如果新分数更好,则提交并使用分数创建一个新的 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 调用,并且提交的内容挂在调用的不同点。我知道必须有更好的方法来做到这一点。任何帮助将不胜感激。

干杯!

【问题讨论】:

看起来你正在实现比我在使用排行榜时通常做的更多的低级东西。根据文档, submitScore() 是火并且忘记了。如果您在使用这种形式的 API 时遇到问题,请查看 submitScoreImmediate()。我注意到在后台,使用 submitScore(),GoogleApiClient 有时会缓存提交并将它们作为 Scores.submitMultiple() 提交。 成功率约为 10% 经过更多调试,无论我的 Internet 连接如何,许多 onResult 调用都会返回一个空 LoadPlayerScoreResult 对象。这也不是每次。有时它是空的,有时它不是。这个问题一般出现在setHighs方法中,偶尔也会出现在handleLeaders方法中 【参考方案1】:

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

【讨论】:

以上是关于向 Google Play 游戏排行榜提交分数并显示新排名的主要内容,如果未能解决你的问题,请参考以下文章

Google Play 排行榜 - 无法提交分数

如何限制到最大值。在 Google Play 游戏服务排行榜 API 中提交 1x 分数?

Google Play 排行榜未显示

Google Play 游戏服务 - 管理成就

写入 Google Play 排行榜 API 回调成功返回,但未记录分数

在 Google Play 游戏中更新(递增/递减)排行榜得分