public void LoadPathProfile(string pathFilename) { StopBot(); if (InitialiseFromFile(SelectedClassFilename, pathFilename)) { SelectedPathFilename = pathFilename; } ProfileLoaded?.Invoke(this, EventArgs.Empty); }
// handle the ProfileLoaded event that the AppController // will publish from its InitApplication method when the App starts up public void Handle(ProfileLoaded evt) { if (_model != null && _model.Id == evt.TrustedSystemId) { // we already have this client model with the event history // for the correct TrustedSystemId all loaded up so just: return; } // ok, need to create a new client model // and load the event history for the selected TrustedSystemId _model = new ClientModel(_queue, evt.TrustedSystemId); // replay all events for the relevant TrustedSystemId and load // it into this Client Model to represent its current state // (since we don't have a snapshot for now) // then this read model will be up to date var eventStream = _eventStore.LoadEventStream(evt.TrustedSystemId.ToStreamId()); foreach (var e in eventStream.Events) { try { ((dynamic)this).Handle((dynamic)e); } catch (RuntimeBinderException ex) { throw new InvalidOperationException("Failed to exec " + e.GetType().Name, ex); } } // ok, our state is up to date from latest history, // now enable sending new events when we update // let's tell the ClientModel read Model that the Loading and replay of history // is completed. LoadingCompleted means two things: // (see more details inside of the ClientModel.LoadingCompleted method) // 1) whenever a new change event comes in from the Domain Model // then the ClientModel instance will not only Handle the Domain Event to change // its own internal state to stay current, // but it will ALSO publish UI change event messages like "StuffAddedToInbox", etc. // 2) ClientModel publishes the ClientModelLoaded event _model.LoadingCompleted(); // switch our provider to this new model _provider.SwitchToModel(_model); }
public static void LoadProfile(string name) { var path = Path.Combine(GetProfilesDirectory(), name); var backupPath = $"{path}.backup"; if (!File.Exists(path) && !File.Exists(backupPath)) { throw new FileNotFoundException($"Couldn't find profile at {path}.", path); } Profile loadedProfile = null; try { loadedProfile = DeserializeProfile(path); } catch (Exception e0) { if (File.Exists(backupPath)) { L.Warn($"Trying to load backup after failed to load profile {name}"); try { if (File.Exists(backupPath)) { loadedProfile = DeserializeProfile(backupPath); } } catch (Exception e1) { throw new GameException(3, e1, "Failed to deserialize profile and its backup."); } } else { throw new GameException(2, e0, "Failed to deserialize profile."); } } lock (opLock) { Profile = loadedProfile; currentProfileName = name; } ProfileLoaded?.Invoke(); }
public static void LoadProfile(Profile profile, string name) { if (profile == null) { throw new ArgumentNullException(nameof(profile)); } if (String.IsNullOrEmpty(name)) { throw new ArgumentException($"Invalid profile name: {name}", nameof(name)); } lock (opLock) { Profile = profile; currentProfileName = name; } ProfileLoaded?.Invoke(); }
private void WebsocketHandlerOnMessageReceived(object sender, string message) { try { dynamic data = JsonConvert.DeserializeObject(message); if (data.code >= 400) { Invoke(ApiError, data); return; } if (data.meta is JObject meta && meta.TryGetValue("event", out var metaEvent)) { var eventName = metaEvent.Value <string>(); log.Debug($"Received ws message with event '{eventName}', posting to eventbus"); switch (eventName) { case ApiEventNames.Connection: if (data.result?.attributes is JObject welcome && welcome.TryGetValue("version", out var versionString)) { Version.TryParse(versionString.Value <string>(), out var version); Invoke(ConnectionEstablished, version); } break; case ApiEventNames.StreamSubscribe: break; case ApiEventNames.UserProfile: var user = JsonApi.Deserialize <User>(message); ProfileLoaded?.Invoke(this, user); break; case ApiEventNames.RescueCreated: var rescues = JsonApi.Deserialize <Rescue[]>(message); foreach (var rescue in rescues) { Invoke(RescueCreated, rescue); } break; case ApiEventNames.RescueUpdated: rescues = JsonApi.Deserialize <Rescue[]>(message); foreach (var rescue in rescues) { if (rescue.Status == RescueState.Closed) { Invoke(RescueClosed, rescue); return; } Invoke(RescueUpdated, rescue); } break; case ApiEventNames.RescueRead: rescues = JsonApi.Deserialize <Rescue[]>(message); Invoke(RescuesReloaded, rescues); break; default: log.Debug($"Received unmapped message: event '{eventName}', message '{message}'"); break; } } } catch (Exception ex) { log.Fatal("Exception in WSClient_MessageReceived: " + ex.Message, ex); } }