如何从谷歌玩游戏服务的当前玩家的排行榜获得分数?

Sop*_*rak 10 android google-play-games

目前我正在做与Google游戏服务集成的游戏,我希望在得分和最佳得分之间压缩得分,所以我很容易告知玩家他们正在获得新的高分.但我不知道如何从谷歌游戏服务排行榜获取,任何人都可以指导我如何做到这一点?

我能够显示排行榜,但我找不到如何获得用户游戏得分的方式.

我的代码显示排行榜:

if (isSignedIn())
    {
        if(inScore<=50)
        {
            Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_easy), inScore);
        }
        else
        {
            Games.Leaderboards.submitScore(getApiClient(), getString(R.string.leaderboard_hard), inScore);
        }
    } else {
        Log.d("not signed", "Not signed in");
    }
Run Code Online (Sandbox Code Playgroud)

我想得到用户在设备上玩的分数,请帮帮我.

tob*_*i_b 13

这就是我获取当前玩家得分的方式:

private void loadScoreOfLeaderBoard() {
    Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(R.string.your_leaderboard_id), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
        @Override
        public void onResult(final Leaderboards.LoadPlayerScoreResult scoreResult) {
            if (isScoreResultValid(scoreResult)) {
                // here you can get the score like this
                mPoints = scoreResult.getScore().getRawScore();
            }
        }
    });
}

private boolean isScoreResultValid(final Leaderboards.LoadPlayerScoreResult scoreResult) {
    return scoreResult != null && GamesStatusCodes.STATUS_OK == scoreResult.getStatus().getStatusCode() && scoreResult.getScore() != null;
}
Run Code Online (Sandbox Code Playgroud)


Yo *_*pps 6

4 年后,我在这种情况下遇到了类似的问题。一些东西已被弃用,东西不再有效。所以对于那些想知道现在如何做的人,在 2018 年......检查这个答案 -

首先,您必须获得 LeaderBoardClient

mLeaderboardsClient = Games.getLeaderboardsClient(MainActivity.this, googleSignInAccount);
Run Code Online (Sandbox Code Playgroud)

接下来就可以打分了

mLeaderboardsClient.loadCurrentPlayerLeaderboardScore(getString(R.string.leaderboard_id), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
          .addOnSuccessListener(this, new OnSuccessListener<AnnotatedData<LeaderboardScore>>() {
              @Override
              public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
                  long score = 0L;
                  if (leaderboardScoreAnnotatedData != null) {
                      if (leaderboardScoreAnnotatedData.get() != null) {
                          score = leaderboardScoreAnnotatedData.get().getRawScore();
                          Toast.makeText(MainActivity.this, Long.toString(score), Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "LeaderBoard: " + Long.toString(score));
                      } else {
                          Toast.makeText(MainActivity.this, "no data at .get()", Toast.LENGTH_SHORT).show();
                          Log.d(TAG, "LeaderBoard: .get() is null");
                      }
                  } else {
                      Toast.makeText(MainActivity.this, "no data...", Toast.LENGTH_SHORT).show();
                      Log.d(TAG, "LeaderBoard: " + Long.toString(score));
                  }
              }
          })
          .addOnFailureListener(new OnFailureListener() {
              @Override
              public void onFailure(@NonNull Exception e) {
                  Toast.makeText(MainActivity.this, "Failure", Toast.LENGTH_SHORT).show();
                  Log.d(TAG, "LeaderBoard: FAILURE");
              }
  });
Run Code Online (Sandbox Code Playgroud)

.loadCurrentPlayerLeaderboardScore的3个参数如下

要从中加载分数的排行榜的 ID

检索数据的时间跨度。有效值为 TIME_SPAN_DAILY、TIME_SPAN_WEEKLY 或 TIME_SPAN_ALL_TIME。

用于检索分数的排行榜集合。有效值为 COLLECTION_PUBLIC 或 COLLECTION_SOCIAL。