【问题标题】:Getting the user Rank in Google Leaderboards (latest library)在 Google 排行榜中获取用户排名(最新库)
【发布时间】:2018-08-01 12:46:54
【问题描述】:

我想检索当前用户在排行榜中的位置。

我已经实现了登录和排行榜 UI,但我真的无法获得这个排名。

我尝试遵循一些旧解决方案,但无法使用新版本的谷歌库实现。

例如我试过solution,没有运气。

【问题讨论】:

  • 尝试检查getRank(),如相关SO post 中所述。在LeaderboardScore 中,可以使用getDisplayRank()getDisplayRank() 或`getRank()`。
  • 仍然找不到更新的例子来使用它。

标签: android google-play-services


【解决方案1】:

更新玩家的分数 游戏通过调用 LeaderboardsClient.submitScore() 并传入排行榜 ID 和原始得分值来更新他们在排行榜上的得分。

以下代码 sn-p 作为示例显示了您的应用如何更新玩家的分数:

Games.getLeaderboardsClient(this, GoogleSignIn.getLastSignedInAccount(this))
    .submitScore(getString(R.string.leaderboard_id), 1337);

一个好的做法是在您的 strings.xml 文件中定义排行榜 ID,以便您的游戏可以通过资源 ID 引用排行榜。在调用更新和加载玩家分数时,请务必遵循这些最佳做法以避免超出您的 API 配额。

显示排行榜 要显示排行榜,请致电LeaderboardsClient.getLeaderboardIntent() 以获取创建默认排行榜用户界面的意图。然后,您的游戏可以通过调用 startActivityForResult 来调出 UI。

以下代码 sn-p 作为示例显示了您的应用如何更新玩家的分数。在代码 sn-p 中,RC_LEADERBOARD_UI 是请求代码的任意整数。

private static final int RC_LEADERBOARD_UI = 9004;

private void showLeaderboard() {
  Games.getLeaderboardsClient(this, GoogleSignIn.getLastSignedInAccount(this))
      .getLeaderboardIntent(getString(R.string.leaderboard_id))
      .addOnSuccessListener(new OnSuccessListener<Intent>() {
        @Override
        public void onSuccess(Intent intent) {
          startActivityForResult(intent, RC_LEADERBOARD_UI);
        }
      });
}
Notice that even though no result is returned, you have to use startActivityForResult so that the API can obtain the identity of the calling package.

实施后 -

要检索当前登录玩家的玩家统计数据,请按以下步骤操作:

调用 PlayerStatsClient.loadPlayerStats() 方法。 如果调用成功,Google Play 游戏服务会返回一个异步加载 PlayerStats 对象的 Task 对象。使用此对象的方法来检索有关已登录玩家在您的应用中的活动的数据。

示例: 公共无效checkPlayerStats(){ Games.getPlayerStatsClient(this, GoogleSignIn.getLastSignedInAccount(this)) .loadPlayerStats(真) .addOnCompleteListener(new OnCompleteListener>() { @覆盖 公共无效 onComplete(@NonNull 任务> 任务) { if (task.isSuccessful()) { // 检查缓存数据。 if (task.getResult().isStale()) { Log.d(TAG, "使用缓存数据"); } PlayerStats stats = task.getResult().get(); 如果(统计!= null){ Log.d(TAG, "已加载玩家数据"); if (stats.getDaysSinceLastPlayed() > 7) { Log.d(TAG, "已经超过一周"); } 如果 (stats.getNumberOfSessions() > 1000) { Log.d(TAG, "老玩家"); } if (stats.getChurnProbability() == 1) { Log.d(TAG, "玩家流失的风险很高"); } } } 别的 { int status = CommonStatusCodes.DEVELOPER_ERROR; if (task.getException() instanceof ApiException) { status = ((ApiException) task.getException()).getStatusCode(); } Log.d(TAG, "获取统计数据状态失败:" + 状态 + ":" + task.getException()); } } }); }

【讨论】:

  • 你的代码解释了如何实现排行榜,我问的是如何检索用户的排名。
  • 仍然不认为是正确的答案,我想要不同的东西
猜你喜欢
  • 2021-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-24
  • 1970-01-01
  • 2015-01-29
  • 2021-07-03
  • 1970-01-01
相关资源
最近更新 更多