【问题标题】:How can I get the cached leaderboard score, when offline?离线时如何获取缓存的排行榜分数?
【发布时间】:2014-07-23 14:26:56
【问题描述】:

在初次登录后,GPGS 似乎可以让用户保持“已登录”状态,即使在离线时也是如此。因此,我可以像已连接一样玩游戏,然后,当我有实际连接时,我的分数/成就会同步。

这很好,但在离线时尝试从排行榜检索分数时似乎不起作用:

Games.Leaderboards.loadCurrentPlayerLeaderboardScore(
        gameHelper.getApiClient(), 
        LD_ID, 
        LeaderboardVariant.TIME_SPAN_ALL_TIME,
        LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<LoadPlayerScoreResult>(){

                @Override
                public void onResult(LoadPlayerScoreResult arg0) {
                        LeaderboardScore c = arg0.getScore();
                        Player.bestScore = (int)c.getRawScore();
                }

});

在线时,c.getRawScore() 按预期工作,但离线时,c 为空(令我惊讶的是,我认为如果离线,它只会回退到缓存的分数)。

有没有办法获取缓存的排行榜分数?

【问题讨论】:

  • 我没有尝试过,也不知道离线时它不起作用。如果确实如此,那么最好的方法是保留最佳排行榜分数的本地副本(在线时),并在您的代码返回 null 时使用该值。基本上只需将其缓存在您的应用程序中(并将其保存到持久存储中)。

标签: google-play-games


【解决方案1】:
Games.Leaderboards.loadCurrentPlayerLeaderboardScore(
        gameHelper.getApiClient(), 
        LD_ID, 
        LeaderboardVariant.TIME_SPAN_ALL_TIME,
        LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>(){

                @Override
                public void onResult(LoadPlayerScoreResult res) {
                        Status status = res.getStatus();
                        int statusCode = status.getStatusCode();

                        if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_NO_DATA) {
                            System.out.println("A network error occurred while attempting to retrieve fresh data, and no data was available locally.");
                        } else if (statusCode == GamesStatusCodes.STATUS_NETWORK_ERROR_STALE_DATA) {
                            System.out.println("A network error occurred while attempting to retrieve fresh data, but some locally cached data was available. The data returned may be stale and/or incomplete.");
                        } else {
                            System.out.println(status.getStatusMessage());
                        }
                }

});

我还没有测试过这段代码,但它应该可以工作。这是一些调试代码来帮助您解决问题。在线和离线运行此程序,看看在每种情况下你会得到什么。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    相关资源
    最近更新 更多