【问题标题】:How to get score from google play game service's leaderboard of current player?如何从 google play 游戏服务的当前玩家排行榜中获取分数?
【发布时间】:2014-06-08 12:20:09
【问题描述】:

目前我正在做与 Google Game Play Service 集成的游戏,我想在分数和最佳分数之间压缩分数,所以我很容易通知玩家他们正在获得新的高分。但是我不知道如何从谷歌游戏服务排行榜中获取分数,谁能指导我如何做到这一点?

我可以显示排行榜,但我找不到如何为用户玩游戏获得分数的方法。

我显示排行榜的代码:

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

我想从正在他们设备上玩游戏的用户那里获得分数,请帮助我。

【问题讨论】:

    标签: android google-play-games


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      我无法直接通过 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();
      
      
      
                          }
                      });
      

      【讨论】:

        【解决方案3】:

        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。

        【讨论】:

          【解决方案4】:

          对于 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());
              }
          

          【讨论】:

            猜你喜欢
            • 2016-10-03
            • 1970-01-01
            • 1970-01-01
            • 2015-09-30
            • 1970-01-01
            • 2015-05-31
            • 1970-01-01
            • 2014-02-09
            • 2016-02-11
            相关资源
            最近更新 更多