Provoke conflict when using Android snapshots (game services)

I have implemented functions to save and load snapshots using Game API from Google Play Services. The next step I'm working on is handling conflicts when there is more than one snapshot. And this is where the problem arises.

In my understanding, the conflict should occur when performing the following actions:

  • Save snapshot after reaching gameplay.
  • Sign out (Google Play Services)
  • Delete all app data
  • Start the game again and make some progress.
  • Sign in to (Google Play Services).
  • Save new snapshot
  • Upload Snapshot

Unfortunately, in my case, there is no conflict. Instead, the current gameplay is saved (step 6) and the download (step 7) simply returns that snapshot. There is no indicator that the snapshot (step 1) was overwritten, resulting in the loss of gameplay.

Save code:

private void executeSave() {
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            GoogleApiClient googleApiClient = App.getGoogleApiHelper().getmGoogleApiClient();
            Snapshots.OpenSnapshotResult openResult = Games.Snapshots.open(googleApiClient, getSavegameFilename(), true).await();
            Status resultStatus = openResult.getStatus();

            Log.d(TAG, "openstatus is: " + resultStatus.getStatusMessage());

            if(resultStatus.isSuccess()) {
                byte[] localSavegame = getPersistingManager().readBytes();
                if(localSavegame != null)  {
                    Log.d(TAG, "Going to save:" + getPersistingManager().read());
                    createSnapshot(openResult.getSnapshot(), localSavegame).await();
                }
            }
        }
    });
}


private PendingResult<Snapshots.CommitSnapshotResult> createSnapshot(Snapshot snapshot, byte[] data) {
    GoogleApiClient googleApiClient = App.getGoogleApiHelper().getmGoogleApiClient();
    snapshot.getSnapshotContents().writeBytes(data);
    SnapshotMetadataChange metadataChange = new SnapshotMetadataChange.Builder().build();
    return Games.Snapshots.commitAndClose(googleApiClient, snapshot, metadataChange);
}

      

Download code:

private void executeLoad() {
    AsyncTask.execute(new Runnable() {
        @Override
        public void run() {
            GoogleApiClient googleApiClient = App.getGoogleApiHelper().getmGoogleApiClient();
            Snapshots.OpenSnapshotResult result = Games.Snapshots.open(googleApiClient, getSavegameFilename(), true).await();
            processResult(result, 0);
        }
    });
}

private void processResult(Snapshots.OpenSnapshotResult result, int retryCount) {
    Status resultStatus = result.getStatus();
    retryCount++;

    if(resultStatus.isSuccess()) {
        Log.d(TAG, "No conflict, thats great!");
        handleSuccess(result);
    } else if (resultStatus.getStatusCode() == GamesStatusCodes.STATUS_SNAPSHOT_CONFLICT) {
        Log.d(TAG, "Aww... a conflict!");
        handleConflict(result, retryCount);
    } else {
        Log.e(TAG, "Error while getting savegame, status message: " + resultStatus.getStatusMessage());
    }
}

      

When following steps from above, this happens:

1) Save a snapshot after reaching gameplay.

absRemotePersistMgmt: openstatus is: STATUS_OK
absRemotePersistMgmt: Going to save:{"solvedQuestions":{"1":[],"2":[1]}}

      

6) Save new snapshot

absRemotePersistMgmt: openstatus is: STATUS_OK
absRemotePersistMgmt: Going to save:{"solvedQuestions":{"1":[1,2],"2":[]}}

      

7) Upload snapshot

absRemotePersistMgmt: No conflict, thats great!

      

What am I missing?

Sources I have used:

+3


source to share





All Articles