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

The following examples show how to use com.google.android.gms.games.leaderboard.Leaderboards. 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: 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 #2
Source File: SoomlaGooglePlus.java    From android-profile with Apache License 2.0 6 votes vote down vote up
@Override
public void getLeaderboards(final GameServicesCallbacks.SuccessWithListListener<Leaderboard> leaderboardsListener) {
    if (enableGameServices) {
        Games.Leaderboards.loadLeaderboardMetadata(googleApiClient, true).setResultCallback(new ResultCallback<Leaderboards.LeaderboardMetadataResult>() {
            @Override
            public void onResult(Leaderboards.LeaderboardMetadataResult leaderboardMetadataResult) {
                if (leaderboardMetadataResult.getStatus().isSuccess()) {
                    List<Leaderboard> result = new ArrayList<>();
                    for (com.google.android.gms.games.leaderboard.Leaderboard lb : leaderboardMetadataResult.getLeaderboards()) {
                        result.add(new Leaderboard(lb.getLeaderboardId(), Provider.GOOGLE, lb.getDisplayName(), lb.getIconImageUrl()));
                    }
                    leaderboardsListener.success(result, false);
                } else {
                    leaderboardsListener.fail(leaderboardMetadataResult.getStatus().getStatusMessage());
                }
            }
        });
    } else {
        leaderboardsListener.fail("To use GPGS features, please set `enableGameServices = true` in Google provider initialization parameters.");
    }
}
 
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: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 5 votes vote down vote up
@Override
public void showLeaderboards(String leaderBoardId) throws GameServiceException {
    if (isSessionActive()) {
        if (gpgsLeaderboardIdMapper != null)
            leaderBoardId = gpgsLeaderboardIdMapper.mapToGsId(leaderBoardId);

        myContext.startActivityForResult(leaderBoardId != null ?
                Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient, leaderBoardId) :
                Games.Leaderboards.getAllLeaderboardsIntent(mGoogleApiClient), RC_LEADERBOARD);
    } else
        throw new GameServiceException.NoSessionException();
}
 
Example #6
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 5 votes vote down vote up
@Override
public boolean submitToLeaderboard(String leaderboardId, long score, String tag) {
    if (gpgsLeaderboardIdMapper != null)
        leaderboardId = gpgsLeaderboardIdMapper.mapToGsId(leaderboardId);

    if (leaderboardId != null && isSessionActive()) {
        if (tag != null)
            Games.Leaderboards.submitScore(mGoogleApiClient, leaderboardId, score, tag);
        else
            Games.Leaderboards.submitScore(mGoogleApiClient, leaderboardId, score);
        return true;
    } else
        return false;
}
 
Example #7
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 #8
Source File: GameServicesHelper.java    From dice-heroes with GNU General Public License v3.0 5 votes vote down vote up
private void processLoadScoreResult(LoadPlayerScoreResult result, String leaderboardId, int by, final Future<Boolean> future) {
    if (result.getStatus().isSuccess()) {
        LeaderboardScore score = result.getScore();
        long current = score == null ? 0 : score.getRawScore();
        Games.Leaderboards
            .submitScoreImmediate(
                helper.getApiClient(),
                leaderboardId,
                current + by
            )
            .setResultCallback(new ResultCallback<Leaderboards.SubmitScoreResult>() {
                @Override public void onResult(Leaderboards.SubmitScoreResult submitResult) {
                    final boolean success = submitResult.getStatus().isSuccess();
                    Gdx.app.postRunnable(new Runnable() {
                        @Override public void run() {
                            future.happen(success);
                        }
                    });
                }
            });
    } else {
        Gdx.app.postRunnable(new Runnable() {
            @Override public void run() {
                future.happen(false);
            }
        });
    }
}
 
Example #9
Source File: SoomlaGooglePlus.java    From android-profile with Apache License 2.0 5 votes vote down vote up
@Override
public void showLeaderboards(final Activity activity) {
    if (enableGameServices) {
        SoomlaUtils.LogDebug(TAG, "showLeaderboards");
        activity.startActivityForResult(Games.Leaderboards.getAllLeaderboardsIntent(googleApiClient), 2);
    } else {
        Log.e(TAG, "To use GPGS features, please set `enableGameServices = true` in Google provider initialization parameters.");
    }
}
 
Example #10
Source File: GameOverActivity.java    From ColorPhun with Apache License 2.0 5 votes vote down vote up
public void showLeaderboard(View view) {
    if (isSignedIn()) {
        startActivityForResult(Games.Leaderboards.getLeaderboardIntent(getApiClient(),
                getString(R.string.LEADERBOARD_ID)), REQUEST_LEADERBOARD);
    } else {
        showAlert(getString(R.string.signin_help_title), getString(R.string.signin_help));
    }
}
 
Example #11
Source File: GameOverActivity.java    From ColorPhun with Apache License 2.0 5 votes vote down vote up
void pushAccomplishments() {
    if (!isSignedIn()) {
        return;
    }
    if (best > 0) {
        // submit score to play services
        Games.Leaderboards.submitScore(getApiClient(),
                getString(R.string.LEADERBOARD_ID) , best);
    }
}
 
Example #12
Source File: BaseGooglePlayServicesActivity.java    From Onesearch with MIT License 5 votes vote down vote up
@Override
    public void onConnected(Bundle bundle) {

        SharedPreferences prefs = getSharedPreferences(BGP_PREF_MAME, MODE_PRIVATE);
        boolean isFirstTime = prefs.getBoolean(FIRST_CONNECT, true);
        if (isFirstTime) {
            SharedPreferences.Editor editor = getSharedPreferences(BGP_PREF_MAME, MODE_PRIVATE).edit();
            editor.putBoolean(FIRST_CONNECT, false);
            editor.apply();

            // Push high scores
            prefs = getSharedPreferences(ResultsActivity.PREF_NAME, MODE_PRIVATE);
            String easyLeaderboardId = getString(R.string.leaderboard_highest_scores__easy);
            String mediumLeaderboardId = getString(R.string.leaderboard_highest_scores__medium);
            String hardLeaderboardId = getString(R.string.leaderboard_highest_scores__hard);
//            String advancedLeaderboardId = getString(R.string.leaderboard_highest_scores__advanced);

            int easyScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Easy, 0);
            int mediumScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Medium, 0);
            int hardScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Hard, 0);
//            int advancedScore = prefs.getInt(ResultsActivity.SCORE_PREFIX + GameDifficulty.Advanced, 0);

            if (easyScore > 0)
                Games.Leaderboards.submitScore(mGoogleApiClient, easyLeaderboardId, easyScore);
            if (mediumScore > 0)
                Games.Leaderboards.submitScore(mGoogleApiClient, mediumLeaderboardId, mediumScore);
            if (hardScore > 0)
                Games.Leaderboards.submitScore(mGoogleApiClient, hardLeaderboardId, hardScore);
//            if (advancedScore > 0)
//                Games.Leaderboards.submitScore(mGoogleApiClient, advancedLeaderboardId, advancedScore);

            loadScoreOfLeaderBoardIfLarger(easyLeaderboardId, easyScore, GameDifficulty.Easy);
            loadScoreOfLeaderBoardIfLarger(mediumLeaderboardId, mediumScore, GameDifficulty.Medium);
            loadScoreOfLeaderBoardIfLarger(hardLeaderboardId, hardScore, GameDifficulty.Hard);
        }
    }
 
Example #13
Source File: ScoresLoadedListener.java    From ANE-Google-Play-Game-Services with Apache License 2.0 5 votes vote down vote up
public void onResult( Leaderboards.LoadScoresResult scoresResult ) {


        LeaderboardScoreBuffer scores = scoresResult.getScores();

        if( scores.getCount() == currentBufferSize ) {
            Extension.context.onLeaderboardLoaded(scores);
        }
        else {
            currentBufferSize = scores.getCount();
            Games.Leaderboards.loadMoreScores( Extension.context.getApiClient(), scores, 25, PageDirection.NEXT ).setResultCallback( this );
        }

    }
 
Example #14
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;
}
 
Example #15
Source File: BaseGooglePlayServicesActivity.java    From Onesearch with MIT License 4 votes vote down vote up
private boolean isScoreResultValid(final Leaderboards.LoadPlayerScoreResult scoreResult) {
    return scoreResult != null && GamesStatusCodes.STATUS_OK == scoreResult.getStatus().getStatusCode() && scoreResult.getScore() != null;
}