public IEnumerator WaitForState(SyncLoopState state) { float waitInterval = 1.0f; float timeOut = syncLoopLength; logger.Log(Tag, "Waiting for state=" + state + " (current=" + _syncState + ")"); float secs = 0f; while (_syncState != state && secs < syncLoopLength) { yield return(new WaitForSeconds(waitInterval)); secs += waitInterval; } logger.Log(Tag, String.Format("ModelSyncService state: {0} Waited for state={1} {2} seconds.", _syncState, state, secs)); // Finished waiting without reaching state if (_syncState != state) { _timedOut = true; logger.Log(Tag, "ModelSync - Timing out..."); promise.Dispatch(); modelSyncCompletedSignal.Dispatch(); } }
private void HandleLoginResponse(ConnectionHandler.RequestResult result, string response) { if (_timedOut) { logger.LogWarning(Tag, "HandleGetdataResponse - ModelSyncCompleted timed out"); return; } logger.Log(Tag, "HandleLoginResponse (result=" + result + ")\nresponse=" + response.SSubstring(300)); if (result != ConnectionHandler.RequestResult.Ok) { HandleErrorResponse(result, response); return; } JsonData responseJsonObject = JsonMapper.ToObject(response); string playerId = ((long)responseJsonObject [keyPlayerId]).ToString(); logger.Log(Tag, "Got playerId:" + playerId); bool isNewPlayer = TTPlayerPrefs.GetValue(keyPlayerId, "0") == "0"; TTPlayerPrefs.SetValue(keyPlayerId, playerId); //_gameDB.SaveSyncInfo (keyPlayerId, ((long)responseJsonObject [keyPlayerId]).ToString ()); logger.Log(Tag, "Got token:" + (string)responseJsonObject [keyToken]); _gameDB.SaveSyncInfo(keyToken, (string)responseJsonObject [keyToken]); _syncState = SyncLoopState.GettingSync; }
public void SyncData() { if (syncType != SyncType.OnDemand) { logger.LogWarning(Tag, "SyncData will only work when syncType is OnDemand, sync type is now " + syncType.ToString()); return; } if (!_dataSyncEnabled) { logger.LogWarning(Tag, "DataSync is disabled, will not attempt to sync player state"); return; } _timedOut = false; if (_syncState == SyncLoopState.Connected) { _coroutineFactory.StartCoroutine(TrySyncData); } else if (!_loggedIn) // If the login process wasn't completed, run it again. { _syncState = SyncLoopState.LoggingIn; _coroutineFactory.StartCoroutine(() => StartOnDemandMode(true)); } else { logger.LogWarning(Tag, "Sync Data was called before sync state is ready, listen to the ModelSyncCompletedSignal to know when it is safe to sync data. Current Sync State:" + _syncState); } }
public void ResumeConnectionAfterHold() { _syncState = SyncLoopState.LoggingIn; if (modelSyncStartSignal != null) { modelSyncStartSignal.Dispatch(); } }
public void RestartFlowAfterBackground() { _syncState = SyncLoopState.LoggingIn; if (modelSyncStartSignal != null) { modelSyncStartSignal.Dispatch(); } }
public void RestartFlow(bool sync) { _syncState = SyncLoopState.GettingSync; if (modelSyncStartSignal != null) { modelSyncStartSignal.Dispatch(); } _coroutineFactory.StartCoroutine(() => StartOnDemandMode(sync)); }
public IEnumerator WaitWhileInState(float secs = 1.0f, SyncLoopState state = SyncLoopState.Connected) { _timedOut = false; float remainingSecs = secs; while (remainingSecs > 0f) { float waitNow = remainingSecs > 1f ? 1f : remainingSecs; remainingSecs -= waitNow; yield return(new WaitForSeconds(waitNow)); if (_syncState != state) { break; } } }
public void SwitchUserAndRestartFlow(string newPlayerId) { _syncState = SyncLoopState.LoggingIn; _gameDB.SaveSyncInfo(keyToken, ""); TTPlayerPrefs.SetValue("SwitchPlayerInProgress", true); TTPlayerPrefs.SetValue(keyPlayerId, newPlayerId); //_gameDB.SaveSyncInfo(keyPlayerId, newPlayerId); _gameDB.SaveSyncInfo(keyLastStateUpdate, "2015-01-01T12:00:00Z"); _gameDB.SaveSyncInfo(keyLastSharedStateUpdate, "2015-01-01T12:00:00Z"); _timedOut = false; if (modelSyncStartSignal != null) { modelSyncStartSignal.Dispatch(); } _coroutineFactory.StartCoroutine(() => StartOnDemandMode(true)); }
private void HandleGetdataResponse(ConnectionHandler.RequestResult result, string response) { if (CheckSyncTimeout()) { return; } logger.Log(Tag, "HandleGetdataResponse (result=" + result + ")\nresponse=" + response.SSubstring(200)); if (result != ConnectionHandler.RequestResult.Ok) { HandleErrorResponse(result, response); return; } JsonData responseJsonOnject = JsonMapper.ToObject(response); if (!_editorMode) { UpdateSyncInfo(responseJsonOnject); } ICollection <string> affectedConfigTables = new List <string>(); ICollection <string> affectedSharedStateTables = new List <string>(); ICollection <string> affectedStates = new List <string>(); if (responseJsonOnject.Keys.Contains(keyGameData)) { //Debug.Log ("Got player data update " + responseJsonOnject[key].GetType()); JsonData configs = (JsonData)responseJsonOnject [keyGameData]; foreach (string config in configs.Keys) { logger.Log(Tag, "Got config updates for config: " + config); //TODO: Add the pre and post send here too. JsonData configUpdates = (JsonData)configs [config]; int updatesCount = configUpdates.Count; for (int index = 0; index < updatesCount; index++) { JsonData data = (JsonData)configUpdates [index]; string id = data ["id"].ToString(); data = PostReceive(data, id, config); logger.Log(Tag, "Saving config: table=" + config + " id=" + id + " data=" + data.ToJson().SSubstring(200)); _gameDB.SaveConfig(config, id, data.ToJson()); affectedConfigTables.Add(config); } } } // Running in editor mode would update only config tables - for syncing only the gld form the server if (_editorMode) { return; } if (responseJsonOnject.Keys.Contains(keySharedStateData)) { //Debug.Log ("Got shared data update " + responseJsonOnject[key].GetType()); JsonData sharedItems = (JsonData)responseJsonOnject [keySharedStateData]; HandleSharedStateResponse(sharedItems, affectedSharedStateTables); } if (responseJsonOnject.Keys.Contains(keyListStateData)) { //Debug.Log ("Got liststate data update " + responseJsonOnject[key].GetType()); JsonData stateItems = (JsonData)responseJsonOnject [keyListStateData]; HandleSharedStateResponse(stateItems, affectedSharedStateTables); } if (responseJsonOnject.Keys.Contains(keyPlayerData)) { JsonData states = (JsonData)responseJsonOnject [keyPlayerData]; foreach (string state in states.Keys) { // Debug.Log ("Got state update: key=" + state + " value=" + ((JsonData)states [state]).ToJson ()); JsonData decoded = PostReceive(states[state], state); string data = decoded.ToJson(); if (data.CompareTo("{}") != 0) { _gameDB.SyncState(state, data); affectedStates.Add(state); } } } if (affectedConfigTables.Count > 0) { if (syncConfigsSignal != null) { syncConfigsSignal.Dispatch(affectedConfigTables); } } if (affectedSharedStateTables.Count > 0) { if (syncSharedStateSignal != null) { syncSharedStateSignal.Dispatch(affectedSharedStateTables); } } if (affectedStates.Count > 0) { if (syncStatesSignal != null) { syncStatesSignal.Dispatch(affectedStates); } } _loggedIn = true; _syncState = SyncLoopState.Connected; if (responseJsonOnject.Keys.Contains(keyRunFullSync)) { bool shouldRunFullSync = (bool)responseJsonOnject[keyRunFullSync]; if (shouldRunFullSync) { logger.Log(Tag, "ModelSync - Running full sync"); _gameDB.SetStateDataAsOutdated(); _coroutineFactory.StartCoroutine(TrySyncData); } } logger.Log(Tag, "Sending event OnModelSyncCompleted"); if (modelSyncCompletedSignal != null) { _timedOut = false; modelSyncCompletedSignal.Dispatch(); promise.Dispatch(); } }
public void HandleErrorResponse(ConnectionHandler.RequestResult result, string response) { logger.Log(Tag.Italics(), "ModelSync.HandleErrorResponse (result=" + result + ")\nresponse: " + response.SSubstring(200)); string title = ""; string message = ""; string button = ""; bool showDialog = false; bool recoverUnidentifiedUSer = false; switch (result) { case (ConnectionHandler.RequestResult.NoInternet): _flowDisconnectReason = FlowDisconnectReason.NoInternet; title = "Connection Error"; message = "Unable to connect with server. Check you internet connection and try again"; button = "Try Again"; break; case (ConnectionHandler.RequestResult.CantReachServer): title = "Connection Error"; message = "Unable to connect with server. Please try again"; button = "Try Again"; _flowDisconnectReason = FlowDisconnectReason.CantReachServer; break; case (ConnectionHandler.RequestResult.InternalServerError): _flowDisconnectReason = FlowDisconnectReason.ServerInternalError; break; case (ConnectionHandler.RequestResult.UnAuthorized): title = "Progress Detected"; message = "You have been playing on another device. Please reload game to load your progress"; button = "Reload"; _flowDisconnectReason = FlowDisconnectReason.ExpiredToken; break; case (ConnectionHandler.RequestResult.OkWithErrorInside): JsonData responseErrorJsonOnject = JsonMapper.ToObject(response); JsonData errorObject = (JsonData)responseErrorJsonOnject [keyError]; ServerErrorData error = JsonMapper.ToObject <ServerErrorData> (errorObject.ToJson()); logger.Log(Tag, "Handling server error message: " + error.ToString()); if (error.code == (int)ConnectionHandler.RequestResult.UnAuthorized) { _flowDisconnectReason = FlowDisconnectReason.ExpiredToken; title = "Progress Detected"; message = "You have been playing on another device. Please reload game to load your progress"; button = "Reload"; break; } if (error.code == (int)ConnectionHandler.RequestResult.ClientUpdateRequired) { _flowDisconnectReason = FlowDisconnectReason.ClientUpdateStarted; title = error.title; message = "You must update your game - update initiated (url=" + error.message + ")."; button = ""; break; } if (error.code == (int)ConnectionHandler.RequestResult.InternalServerError && error.message == "User not foundPlayer not found") { logger.Log(Tag, "ModelSync.HandleErrorResponse() - PlayerId not found on server => staring new game"); recoverUnidentifiedUSer = true; //_gameDB.SaveSyncInfo(keyPlayerId, "0"); TTPlayerPrefs.SetValue(keyPlayerId, "0"); _gameDB.SaveSyncInfo(keyLastStateUpdate, ""); RestartFlowAfterUserSwitch(); } title = "Unidentified Server Error"; message = "error " + error.ToString(); button = "reload"; break; } if (!recoverUnidentifiedUSer) { _syncState = SyncLoopState.DisconnectedOnHold; } if (connectionDisconnectedSignal != null) { connectionDisconnectedSignal.Dispatch(_flowDisconnectReason); } if (showDialog) { ShowErrorDialog(title, message, button); } }