/// <summary> /// Continues the story when the first connection selector `When` has not selected an connection, and when this selector does select a connection. Effectively performs an exclusive or, but in non formal language `Or` is most of the time exclusive. /// </summary> /// <param name="connectionName">the name of the connection to be selected</param> /// <returns>the connectionstate</returns> /// <exception cref="LogicException">When the connection selected upon is not present.</exception> public Logic OrWhen(string connectionName) { var selectedConnection = Connections.FirstOrDefault(c => c.Name == connectionName); if (selectedConnection == null) { throw new LogicException("Connection this rule should fire (" + connectionName + ") on is not listed in the connections (" + Connections.Aggregate("", ((s, c) => s + "," + c.Name)) + ")."); } ShouldContinue = (Connection.Name == connectionName) ^ ShouldContinue; return(this); }
/// <summary> /// Returns a func that can be called to return the StateChangeDirectives that are a result from the story. /// </summary> /// <param name="which">a selector to select </param> /// <returns>the Func that can be called.</returns> public Logic Turn(string which) { var selectedConnection = Connections.FirstOrDefault(c => c.Name == which); if (selectedConnection == null) { throw new LogicException("Connection this rule should fire (" + which + ") on is not listed in the connections (" + Connections.Aggregate("", ((s, c) => s + c.Name + ",")) + ")."); } AffectedConnection = selectedConnection; return(this); }
/// <summary> /// The start of a rule story. /// </summary> /// <param name="connectionName">the name of the input you want it to react on.</param> /// <returns>the ConnectionState</returns> /// <exception cref="LogicException">Throws when the selected connection is not present in the list of connections.</exception> public Logic When(string connectionName) { var selectedConnection = Connections.FirstOrDefault(c => c.Name == connectionName); if (selectedConnection == null) { throw new LogicException("Connection this rule should fire (" + connectionName + ") on is not listed in the connections (" + Connections.Aggregate("", ((s, c) => s + c.Name + ",")) + ")."); } ShouldContinue = Connection.Name == connectionName; Debug.WriteLineIf(Connection.Name == connectionName, "Connection: " + Connection.Name + " looking for: " + connectionName + " (" + (Connection.Name == connectionName) + ")"); return(this); }
/// <summary> /// Adds a statechangedirective to increase the value of the state with the given /// amout. Until it reaches max and then cylces to min. /// </summary> /// <param name="which">Which connections state it should increase</param> /// <param name="with">with howmuch the intstate should be increased</param> /// <param name="max">after which it should cycle min</param> /// <param name="min">to which level it should cycle after passing max</param> /// <returns>the current state.</returns> public Logic Increase(string which, int with, int max, int min) { if (ShouldContinue && Input == "in") { var selectedConnection = Connections.FirstOrDefault(c => c.Name == which); if (selectedConnection == null) { throw new LogicException("Connection this rule should fire (" + which + ") on is not listed in the connections (" + Connections.Aggregate("", ((s, c) => s + c.Name + ",")) + ")."); } var cur = int.Parse(selectedConnection.CurrentState.Name); CollectedStateChanges.Add(new StateChangeDirective { Connection = selectedConnection, NewState = (cur + with > max ? min : (cur + with)).ToString(CultureInfo.InvariantCulture) }); } return(this); }