使用Android快照(游戏玩法服务)时引发冲突

时间:2017-04-16 20:22:19

标签: android google-play-services snapshot

我已经实现了使用Google Play服务的Game API保存和加载快照的功能。我正在进行的下一步是在有多个快照时处理冲突。这就是问题所在。

根据我的理解,执行以下操作时应发生冲突:

  1. 在取得游戏进度后保存快照
  2. 退出(Google Play服务)
  3. 删除所有应用数据
  4. 再次开始游戏并取得一些进展
  5. 登录(Google Play服务)
  6. 保存新的snapshop
  7. 加载snapshop
  8. 不幸的是,在我的情况下没有发生冲突。而是保存当前游戏进度(步骤6)并且加载(步骤7)仅返回该快照商店。没有指示快照(步骤1)被覆盖 - 导致游戏进度失去。

    保存代码:

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

    加载代码:

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

    执行上述步骤时,会发生这种情况:

    1)在完成游戏进度后保存快照

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

    6)保存新快照

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

    7)加载snapshop

    absRemotePersistMgmt: No conflict, thats great!
    

    我错过了什么?

    我使用的来源:

0 个答案:

没有答案