com.google.android.gms.games.leaderboard.LeaderboardVariant Java Examples

The following examples show how to use com.google.android.gms.games.leaderboard.LeaderboardVariant. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Request.java    From play_games with MIT License 6 votes vote down vote up
public void submitScore(String leaderboardId, Long score) {
    Log.i(TAG, "Submitting leaderboard, id: " + leaderboardId + "; score: " + score);
    LeaderboardsClient leaderboardsClient = Games.getLeaderboardsClient(this.registrar.activity(), currentAccount);
    leaderboardsClient.submitScoreImmediate(leaderboardId, score).addOnSuccessListener(new OnSuccessListener<ScoreSubmissionData>() {
        @Override
        public void onSuccess(ScoreSubmissionData data) {
            Map<String, Object> successMap = new HashMap<>();
            successMap.put("type", "SUCCESS");
            successMap.put("leaderboardId", data.getLeaderboardId());
            successMap.put("playerId", data.getPlayerId());
            successMap.put("scoreResultDaily", resultToMap(data.getScoreResult(LeaderboardVariant.TIME_SPAN_DAILY)));
            successMap.put("scoreResultWeekly", resultToMap(data.getScoreResult(LeaderboardVariant.TIME_SPAN_WEEKLY)));
            successMap.put("scoreResultAllTime", resultToMap(data.getScoreResult(LeaderboardVariant.TIME_SPAN_ALL_TIME)));
            result(successMap);
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception e) {
            Log.e(TAG, "Could not submit leadderboard", e);
            error("LEADERBOARD_SUBMIT_FAILURE", e);
        }
    });
}
 
Example #2
Source File: GameServicesHelper.java    From dice-heroes with GNU General Public License v3.0 6 votes vote down vote up
@Override public IFuture<Boolean> incrementScore(final String leaderboardId, final int by) {
    final Future<Boolean> future = Future.make();
    activity.getMainHandler().post(new Runnable() {
        @Override public void run() {
            Games.Leaderboards
                .loadCurrentPlayerLeaderboardScore(
                    helper.getApiClient(),
                    leaderboardId,
                    LeaderboardVariant.TIME_SPAN_WEEKLY,
                    LeaderboardVariant.COLLECTION_PUBLIC
                )
                .setResultCallback(new ResultCallback<LoadPlayerScoreResult>() {
                    @Override public void onResult(LoadPlayerScoreResult result) {
                        processLoadScoreResult(result, leaderboardId, by, future);
                    }
                });
        }
    });
    return future;
}
 
Example #3
Source File: SoomlaGooglePlus.java    From android-profile with Apache License 2.0 6 votes vote down vote up
@Override
public void submitScore(String leaderboardId, long value, final GameServicesCallbacks.SuccessWithScoreListener submitScoreListener) {
    if (enableGameServices) {
        Games.Leaderboards.submitScoreImmediate(googleApiClient, leaderboardId, value).setResultCallback(new ResultCallback<Leaderboards.SubmitScoreResult>() {
            @Override
            public void onResult(Leaderboards.SubmitScoreResult submitScoreResult) {
                if (submitScoreResult.getStatus().isSuccess()) {
                    submitScoreListener.success(
                            new Score(
                                    new Leaderboard(submitScoreResult.getScoreData().getLeaderboardId(), getProvider(), "", ""), // here is no iconURL and title at this moment
                                    0, // rank is undefined here
                                    SoomlaProfile.getInstance().getStoredUserProfile(getProvider()),
                                    submitScoreResult.getScoreData().getScoreResult(LeaderboardVariant.TIME_SPAN_ALL_TIME).rawScore
                            )
                    );
                } else {
                    submitScoreListener.fail(submitScoreResult.getStatus().getStatusMessage());
                }
            }
        });
    } else {
        submitScoreListener.fail("To use GPGS features, please set `enableGameServices = true` in Google provider initialization parameters.");
    }
}
 
Example #4
Source File: BaseGooglePlayServicesActivity.java    From Onesearch with MIT License 6 votes vote down vote up
private void loadScoreOfLeaderBoardIfLarger(final String leaderboardId, final int currentScore, final String gameDifficulty) {
    Games.Leaderboards.loadCurrentPlayerLeaderboardScore(mGoogleApiClient, leaderboardId, 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
                int score = (int) scoreResult.getScore().getRawScore();
                if (score > currentScore) {
                    SharedPreferences.Editor editor = getSharedPreferences(ResultsActivity.PREF_NAME, MODE_PRIVATE).edit();
                    editor.putInt(ResultsActivity.SCORE_PREFIX + gameDifficulty, score);
                    editor.apply();
                }
            }
        }
    });
}
 
Example #5
Source File: Request.java    From play_games with MIT License 5 votes vote down vote up
private int convertTimeSpan(String timeSpan) {
    switch (timeSpan) {
        case "TimeSpan.TIME_SPAN_DAILY":
            return LeaderboardVariant.TIME_SPAN_DAILY;
        case "TimeSpan.TIME_SPAN_WEEKLY":
            return LeaderboardVariant.TIME_SPAN_WEEKLY;
        case "TimeSpan.TIME_SPAN_ALL_TIME":
            return LeaderboardVariant.TIME_SPAN_ALL_TIME;
    }
    throw new RuntimeException("Unknown time span...");
}
 
Example #6
Source File: Leaderboards.java    From godot-gpgs with MIT License 5 votes vote down vote up
public void showLeaderList(final String id) {
     if (googleApiClient == null || !googleApiClient.isConnected()) return;
     activity.runOnUiThread(new Runnable() {
	@Override
         public void run() {
		if (googleApiClient != null && googleApiClient.isConnected()) {
			activity.startActivityForResult(Games.Leaderboards.getLeaderboardIntent(googleApiClient, id, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC), REQUEST_LEADERBOARD);
                 Log.d(TAG, "GPGS: showLeaderList.");
		}
	}
});
 }
 
Example #7
Source File: Leaderboards.java    From godot-gpgs with MIT License 5 votes vote down vote up
public void getLeaderboardValue(final String id) {
     if (googleApiClient == null || !googleApiClient.isConnected()) return;
     activity.runOnUiThread(new Runnable() {
	@Override
         public void run() {
             Games.Leaderboards.loadCurrentPlayerLeaderboardScore(googleApiClient, id, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC).setResultCallback(new ResultCallback<LoadPlayerScoreResult>() {
                 @Override
                 public void onResult(LoadPlayerScoreResult result) {
                     Status status = result.getStatus();
                     if (status.getStatusCode() == GamesStatusCodes.STATUS_OK) {
                         LeaderboardScore score = result.getScore();
                         if (score != null) {
                             int scoreValue = (int) score.getRawScore();
                             Log.d(TAG, "GPGS: Leaderboard values is " + score.getDisplayScore());
                             GodotLib.calldeferred(instance_id, "_on_leaderboard_get_value", new Object[]{ scoreValue, id });
                         } else {
                             Log.d(TAG, "GPGS: getLeaderboardValue STATUS_OK but is NULL -> Request again...");
                             getLeaderboardValue(id);
                         }
                     } else if (status.getStatusCode() == GamesStatusCodes.STATUS_CLIENT_RECONNECT_REQUIRED) {
                         Log.d(TAG, "GPGS: getLeaderboardValue reconnect required -> reconnecting...");
                         googleApiClient.reconnect();
                     } else {
                         Log.d(TAG, "GPGS: getLeaderboardValue connection error -> " + status.getStatusMessage());
                         GodotLib.calldeferred(instance_id, "_on_leaderboard_get_value_error", new Object[]{ id });
                     }
                 }
             });
             Log.d(TAG, "GPGS: getLeaderboardValue '" + id + "'.");
	}
});
 }
 
Example #8
Source File: GameServicesHelper.java    From dice-heroes with GNU General Public License v3.0 5 votes vote down vote up
@Override public void showLeaderboard(final String leaderboardId) {
    activity.getMainHandler().post(new Runnable() {
        @Override public void run() {
            Intent intent = Games.Leaderboards.getLeaderboardIntent(
                helper.getApiClient(),
                leaderboardId,
                LeaderboardVariant.TIME_SPAN_WEEKLY
            );
            activity.startActivityForResult(intent, LEADERBOARD_REQUEST_CODE);
        }
    });
}
 
Example #9
Source File: ExtensionContext.java    From ANE-Google-Play-Game-Services with Apache License 2.0 5 votes vote down vote up
public void getLeaderboard( String leaderboardId ) {

		Games.Leaderboards.loadTopScores(
				getApiClient(),
				leaderboardId,
				LeaderboardVariant.TIME_SPAN_ALL_TIME,
				LeaderboardVariant.COLLECTION_SOCIAL,
				25,
				true
		).setResultCallback(new ScoresLoadedListener());
    }
 
Example #10
Source File: Request.java    From play_games with MIT License 4 votes vote down vote up
private int convertCollection(String collection) {
    if ("CollectionType.COLLECTION_PUBLIC".equals(collection)) {
        return LeaderboardVariant.COLLECTION_PUBLIC;
    }
    throw new RuntimeException("Unknown time span...");
}
 
Example #11
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 4 votes vote down vote up
private boolean fetchLeaderboardEntriesSync(String leaderBoardId, int limit, boolean relatedToPlayer,
                                            IFetchLeaderBoardEntriesResponseListener callback) {
    if (!isSessionActive())
        return false;

    if (gpgsLeaderboardIdMapper != null)
        leaderBoardId = gpgsLeaderboardIdMapper.mapToGsId(leaderBoardId);

    Leaderboards.LoadScoresResult scoresResult =
            (relatedToPlayer ?
                    Games.Leaderboards.loadPlayerCenteredScores(mGoogleApiClient, leaderBoardId,
                            LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC,
                            MathUtils.clamp(limit, 1, 25), forceRefresh).await()
                    :
                    Games.Leaderboards.loadTopScores(mGoogleApiClient, leaderBoardId,
                            LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC,
                            MathUtils.clamp(limit, 1, 25), forceRefresh).await());

    if (!scoresResult.getStatus().isSuccess()) {
        Gdx.app.log(GAMESERVICE_ID, "Failed to fetch leaderboard entries:" +
                scoresResult.getStatus().getStatusMessage());
        callback.onLeaderBoardResponse(null);
        return false;
    }

    LeaderboardScoreBuffer scores = scoresResult.getScores();

    Array<ILeaderBoardEntry> gpgsLbEs = new Array<ILeaderBoardEntry>(scores.getCount());
    String playerDisplayName = getPlayerDisplayName();

    for (LeaderboardScore score : scores) {
        GpgsLeaderBoardEntry gpgsLbE = new GpgsLeaderBoardEntry();

        gpgsLbE.userDisplayName = score.getScoreHolderDisplayName();
        gpgsLbE.currentPlayer = gpgsLbE.userDisplayName.equalsIgnoreCase(playerDisplayName);
        gpgsLbE.formattedValue = score.getDisplayScore();
        gpgsLbE.scoreRank = score.getDisplayRank();
        gpgsLbE.userId = score.getScoreHolder().getPlayerId();
        gpgsLbE.sortValue = score.getRawScore();
        gpgsLbE.scoreTag = score.getScoreTag();

        gpgsLbEs.add(gpgsLbE);
    }

    scores.release();

    callback.onLeaderBoardResponse(gpgsLbEs);

    return true;
}