/// <summary> /// Intercept any changes from the view or model. /// </summary> /// <param name="observable">The observable that was changed</param> /// <param name="command">The actual data that got changed + some metadata.</param> public override void ObservableChanged(Observable observable, ObservableArgs args) { //We received an update from the model. if (args is ObservableModelArgs) { //Convert ModelArgs to an actual command, using the command factory, // The CommandFactory determines which command we want to send.s ObservableModelArgs modelArgs = args as ObservableModelArgs; Command networkCommand = CommandFactory.GetNetCommandFromModel(modelArgs); //To let the view know we have a new command, we need to notify the view. // The command we just made, we convert it to a string we want to send of the network. // And the string we get we pass as arguments. ObservableArgs commandArgs = new ObservableArgs { Content = networkCommand.ToNet() }; //Let the view know we have changed! Notify(commandArgs); } //We received an update from the view. //if(args is ObservableViewArgs) }
//TODO: A less weird way of converting ModelArgs -> NetworkCommand... public static Command GetNetCommandFromModel(ObservableModelArgs args) { //Handle all world commands if (args.Model == "world") { if (args.Field == "entities") { return(HandleEntityEvents(args)); } } return(default(Command)); }
private static Command HandleEntityEvents(ObservableModelArgs args) { Command cmd = null; Entity e = Entity.FromJson(args.Content); if (args.Action == "add") { cmd = new CommandCreateEntity(e.Id, e.Type, e.Position, e.Rotation); } else if (args.Action == "modified") { cmd = new CommandUpdateEntity(e.Id, e.Position, e.Rotation); } else if (args.Action == "remove") { cmd = new CommandDeleteEntity(e.Id); } return(cmd); }
/// <summary> /// A listener for when any of the observed observables are changed. /// This method will proxy the request through to the controller (who is observing the model). /// </summary> /// <param name="payload"></param> public void ObservableChanged(Observable observable, ObservableArgs args) { bool success = observables.TryGetValue(observable, out string name); if (success) { ObservableModelArgs modelArgs = new ObservableModelArgs { Model = modelName, Field = name, Action = args.Action, Content = args.Content }; Notify(modelArgs); } else { //Something really bad went on here, should logically not be possible. throw new Exception("The model is observing a observable but is not registered to the model, huh!?"); } }