Java Code Examples for com.google.android.gms.games.snapshot.Snapshots#OpenSnapshotResult

The following examples show how to use com.google.android.gms.games.snapshot.Snapshots#OpenSnapshotResult . 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: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 5 votes vote down vote up
public boolean deleteGameStateSync(String fileId, ISaveGameStateResponseListener success) {
    if (!isSessionActive()) {
        if (success != null)
            success.onGameStateSaved(false, "NO_CONNECTION");
        return false;
    }

    // Open the snapshot, creating if necessary
    Snapshots.OpenSnapshotResult open = Games.Snapshots.open(
            mGoogleApiClient, fileId, false).await();

    Snapshot snapshot = processSnapshotOpenResult(open, 0);

    if (snapshot == null) {
        Gdx.app.log(GAMESERVICE_ID, "Could not delete game state " + fileId + ": " +
                open.getStatus().getStatusMessage());
        if (success != null)
            success.onGameStateSaved(false, open.getStatus().getStatusMessage());
        return false;
    }

    Snapshots.DeleteSnapshotResult deleteResult = Games.Snapshots.delete(mGoogleApiClient,
            snapshot.getMetadata()).await();

    boolean deletionDone = deleteResult.getStatus().isSuccess();

    Gdx.app.log(GAMESERVICE_ID, "Delete game state " + fileId + ": " + deletionDone +
            " - " + open.getStatus().getStatusMessage());

    if (success != null) {

        success.onGameStateSaved(deletionDone,
                deleteResult.getStatus().getStatusMessage());
    }

    return deletionDone;
}
 
Example 2
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 5 votes vote down vote up
public boolean loadGameStateSync(String id, ILoadGameStateResponseListener listener) {
    if (!isSessionActive()) {
        listener.gsGameStateLoaded(null);
        return false;
    }

    try {
        // Open the snapshot, creating if necessary
        Snapshots.OpenSnapshotResult open = Games.Snapshots.open(
                mGoogleApiClient, id, true).await();

        Snapshot snapshot = processSnapshotOpenResult(open, 0);

        if (snapshot == null) {
            Gdx.app.log(GAMESERVICE_ID, "Could not open Snapshot.");
            listener.gsGameStateLoaded(null);
            return false;
        }

        // Read
        byte[] mSaveGameData = snapshot.getSnapshotContents().readFully();
        listener.gsGameStateLoaded(mSaveGameData);
        return true;
    } catch (Throwable t) {
        Gdx.app.error(GAMESERVICE_ID, "Error while reading Snapshot.", t);
        listener.gsGameStateLoaded(null);
        return false;
    }

}
 
Example 3
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 5 votes vote down vote up
/**
 * Conflict resolution for when Snapshots are opened.  Must be run in an AsyncTask or in a
 * background thread,
 */
public Snapshot processSnapshotOpenResult(Snapshots.OpenSnapshotResult result, int retryCount) {
    Snapshot mResolvedSnapshot = null;
    retryCount++;

    int status = result.getStatus().getStatusCode();
    Gdx.app.log(GAMESERVICE_ID, "Open Snapshot Result status: " + result.getStatus().getStatusMessage());

    if (status == GamesStatusCodes.STATUS_OK) {
        return result.getSnapshot();
    } else if (status == GamesStatusCodes.STATUS_SNAPSHOT_CONFLICT) {
        Snapshot snapshot = result.getSnapshot();
        Snapshot conflictSnapshot = result.getConflictingSnapshot();

        // Resolve between conflicts by selecting the highest progress or, if equal, newest of the conflicting
        // snapshots.
        mResolvedSnapshot = snapshot;

        if (snapshot.getMetadata().getProgressValue() < conflictSnapshot.getMetadata().getProgressValue()
                || snapshot.getMetadata().getProgressValue() == conflictSnapshot.getMetadata().getProgressValue()
                && snapshot.getMetadata().getLastModifiedTimestamp() <
                conflictSnapshot.getMetadata().getLastModifiedTimestamp()) {
            mResolvedSnapshot = conflictSnapshot;
        }

        Snapshots.OpenSnapshotResult resolveResult = Games.Snapshots.resolveConflict(
                mGoogleApiClient, result.getConflictId(), mResolvedSnapshot).await();

        if (retryCount < MAX_SNAPSHOT_RESOLVE_RETRIES) {
            // Recursively attempt again
            return processSnapshotOpenResult(resolveResult, retryCount);
        }

    }

    // Fail, return null.
    return null;
}
 
Example 4
Source File: GpgsClient.java    From gdx-gamesvcs with Apache License 2.0 4 votes vote down vote up
@NonNull
public Boolean saveGameStateSync(String id, byte[] gameState, long progressValue,
                                 ISaveGameStateResponseListener listener) {
    if (!isSessionActive()) {
        if (listener != null)
            listener.onGameStateSaved(false, "NOT_CONNECTED");
        return false;
    }

    try {
        // Open the snapshot, creating if necessary
        Snapshots.OpenSnapshotResult open = Games.Snapshots.open(
                mGoogleApiClient, id, true).await();

        Snapshot snapshot = processSnapshotOpenResult(open, 0);

        if (snapshot == null) {
            Gdx.app.log(GAMESERVICE_ID, "Could not open Snapshot.");
            if (listener != null)
                listener.onGameStateSaved(false, "Could not open Snapshot.");
            return false;
        }

        if (progressValue < snapshot.getMetadata().getProgressValue()) {
            Gdx.app.error(GAMESERVICE_ID, "Progress of saved game state higher than current one. Did not save.");
            if (listener != null)
                listener.onGameStateSaved(true, null);
            return false;
        }

        // Write the new data to the snapshot
        snapshot.getSnapshotContents().writeBytes(gameState);

        // Change metadata
        SnapshotMetadataChange.Builder metaDataBuilder = new SnapshotMetadataChange.Builder()
                .fromMetadata(snapshot.getMetadata());
        metaDataBuilder = setSaveGameMetaData(metaDataBuilder, id, gameState, progressValue);
        SnapshotMetadataChange metadataChange = metaDataBuilder.build();

        Snapshots.CommitSnapshotResult commit = Games.Snapshots.commitAndClose(
                mGoogleApiClient, snapshot, metadataChange).await();

        if (!commit.getStatus().isSuccess())
            throw new RuntimeException(commit.getStatus().getStatusMessage());

        // No failures
        Gdx.app.log(GAMESERVICE_ID, "Successfully saved gamestate with " + gameState.length + "B");
        if (listener != null)
            listener.onGameStateSaved(true, null);
        return true;

    } catch (Throwable t) {
        Gdx.app.error(GAMESERVICE_ID, "Failed to commit snapshot:" + t.getMessage());
        if (listener != null)
            listener.onGameStateSaved(false, t.getMessage());
        return false;
    }
}