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

时间:2014-04-23 15:00:31

标签: 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");
    }

我希望得到用户在设备上播放的分数,请帮助我。

3 个答案:

答案 0 :(得分:12)

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

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;
}

答案 1 :(得分:2)

4年后,我在这种情况下遇到了类似的问题。有些东西已被弃用,而这些东西不再起作用。因此,对于那些想立即知道如何做的人,请在2018年...查看此答案-

首先,您必须通过以下方式获得LeaderBoardClient

mLeaderboardsClient = Games.getLeaderboardsClient(MainActivity.this, googleSignInAccount);

接下来您可以得分

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");
              }
  });

.loadCurrentPlayerLeaderboardScore 的3个参数如下

  

排行榜的ID ,从中加载得分。

     

时间跨度以获取数据。有效值为TIME_SPAN_DAILY,TIME_SPAN_WEEKLY或TIME_SPAN_ALL_TIME。

     

排行榜收藏以获取分数。有效值为COLLECTION_PUBLIC或COLLECTION_SOCIAL。

答案 2 :(得分:0)

我无法直接进入onResult程序内部。在onActivityResult内部提交之后我会调用它。调用onActivityResult是不对的。

Games.Leaderboards.submitScore(googleApiConnection.mGoogleApiClient, String.valueOf(R.string.leaderboard_winner), Long.valueOf(120));

Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(R.string.leaderboard_winner), LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<Leaderboards.LoadPlayerScoreResult>() {
                    @Override
                    public void onResult(final Leaderboards.LoadPlayerScoreResult scoreResult) {
                        // if (isScoreResultValid(scoreResult)) {
                        LeaderboardScore c = scoreResult.getScore();
                        // here you can get the score like this
                        //    Log.e("doganay","LeaderBoardLoadSCore :"+c.getDisplayScore());
                        //us.setCoin(60);
                        mPoints = c.getRawScore();



                    }
                });