如何从Google Play游戏服务的排行榜中获取当前玩家的分数?

10

目前我正在开发一个与Google Play游戏服务集成的游戏,我想将分数与最高得分进行比较,以便轻松通知玩家他们已经创造了新的最高得分。但是我不知道如何从谷歌游戏服务排行榜中获取得分,有人可以请指导一下吗?

我能够显示排行榜,但找不到为用户游戏得分的方式。

我的显示排行榜的代码:

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

我想从用户正在使用的设备中获取分数,请帮忙。

4个回答

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

6

四年之后,我遇到了与这种情况类似的问题。一些东西已经被弃用了,而一些东西不再起作用。因此,对于那些想要知道如何在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(社交集合)。


getRawScore() 可能会产生 nullPointerException。所以最好将其从 leaderboardScoreAnnotatedData.get().getRawScore(); 改为 Objects.requireNonNull(leaderboardScoreAnnotatedData.get()).getRawScore(); - Farkas Antal

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();



                    }
                });

0
对于 Android:不要使用 loadCurrentPlayerLeaderboardScore。使用作用域请求,否则您只能检索最多3个排行榜,有时甚至一个也无法检索。多年来,许多人都遭遇了这个未解决的问题的可怕命运。
请改用以下代码:使用 loadPlayerCenteredScores:
long limitResultsTo = 1;
String leaderboardID = getString(R.string.leaderboard_name); // or string of ID
Games.getLeaderboardsClient(this, GoogleSignIn.getLastSignedInAccount(this))
    .loadPlayerCenteredScores(leaderboardName, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, limitResultsTo)
    .addOnSuccessListener(new OnSuccessListener<AnnotatedData<LeaderboardsClient.LeaderboardScores>>() {
        @Override
        public void onSuccess(AnnotatedData<LeaderboardsClient.LeaderboardScores> leaderboardScoreAnnotatedData) { // big ups Danoli3.com for the fix for loadCurrentPlayerLeaderboardScore
            LeaderboardsClient.LeaderboardScores scoresResult =  leaderboardScoreAnnotatedData.get();
            LeaderboardScore scoreResult = (scoresResult != null ? scoresResult.getScores().get(0) : null);
            long score = 0;
            if(scoreResult != null) score = scoreResult.getRawScore();

            // use the score in your own code here
            // Log.i(TAG, "loadPlayerCenteredScores:" + leaderboardID + " score:" + score);

            leaderboardScoreAnnotatedData = null; 
        }
    }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {
        Log.e(TAG, "Failure:loadPlayerCenteredScores  GPG:Leader:" + leaderboardID + " Ex:" + e.getMessage());
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接