Skip to content

Commit

Permalink
Only require settings where necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
dukeofsussex committed Apr 23, 2023
1 parent 1eb9d44 commit e5bd5b2
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Assistant.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 10 additions & 10 deletions Brain/Brain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,27 +145,27 @@ private string ProcessGlobal(string command, Bomb bomb)

private string ProcessModule(string command, Bomb bomb)
{
if (!bomb.Ready)
{
return "Bomb not set up.";
}

string module = command[(command.IndexOf(' ') + 1) ..].ToUpperInvariant();

if (bomb.ActiveModule != null)
{
this.ResetProcessors();
}

SpeechProcessor speechModule = this.processors[module];
BombModule bombModule = this.processors[module] as BombModule;

if (!speechModule.IsGlobal)
if (!bombModule.IsGlobal)
{
speechModule = Activator.CreateInstance(this.processors[module].GetType()) as SpeechProcessor;
this.processors[module] = speechModule;
bombModule = Activator.CreateInstance(this.processors[module].GetType()) as BombModule;
this.processors[module] = bombModule;

if (!bombModule.TryDefuse(bomb, out string response))
{
return response;
}
}

bomb.ActiveModule = speechModule as BombModule;
bomb.ActiveModule = bombModule;
this.Recognition.Grammars.Where(g => g.Name == module)
.First()
.Enabled = true;
Expand Down
11 changes: 7 additions & 4 deletions Game/Bomb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ namespace KTANE.Game
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Speech.Recognition;
using KTANE.Brain;
Expand All @@ -25,16 +26,22 @@ internal class Bomb : SpeechProcessor

public bool AutoReset { get; set; }

[DisplayName("Batteries")]
public int? Batteries { get; set; }

[DisplayName("Parallel Port")]
public bool? HasParallelPort { get; set; }

[DisplayName("FRK")]
public bool? HasLitFRK { get; set; }

[DisplayName("CAR")]
public bool? HasLitCAR { get; set; }

[DisplayName("Vowel")]
public bool? HasVowel { get; set; }

[DisplayName("Last Digit")]
public bool? LastDigitEven { get; set; }

public int Strikes { get; set; }
Expand Down Expand Up @@ -83,10 +90,6 @@ public override GrammarBuilder Grammar
}
}

public bool Ready => typeof(Bomb).GetProperties()
.Where(p => p.PropertyType.IsGenericType && p.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
.All(p => p.GetValue(this) != null);

public static Bomb CreateRandom()
{
Random rng = new ();
Expand Down
27 changes: 27 additions & 0 deletions Game/BombModule.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,36 @@
namespace KTANE.Game
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using KTANE.Brain;

internal abstract class BombModule : SpeechProcessor
{
public abstract string PreInfo { get; }

public bool TryDefuse(Bomb bomb, out string response)
{
response = null;

if (this.GetType()
.GetMethod(nameof(this.Process))
.GetCustomAttribute(typeof(RequiredBombSettingAttribute), true) is RequiredBombSettingAttribute attr)
{
List<string> missing = typeof(Bomb).GetProperties()
.Where(p => attr.Settings.Contains(p.Name)
&& p.GetValue(bomb) == null)
.Select(p => (p.GetCustomAttribute(typeof(DisplayNameAttribute), false) as DisplayNameAttribute).DisplayName)
.ToList();

if (missing.Count > 0)
{
response = $"Set {string.Join(", ", missing)}!";
}
}

return response == null;
}
}
}
1 change: 1 addition & 0 deletions Game/Modules/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public override GrammarBuilder Grammar

public override string PreInfo => string.Empty;

[RequiredBombSetting(nameof(Bomb.Batteries), nameof(Bomb.HasLitCAR), nameof(Bomb.HasLitFRK))]
public override string Process(string command, Bomb bomb)
{
string[] parts = command.Split(' ');
Expand Down
1 change: 1 addition & 0 deletions Game/Modules/ComplexWires.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public override GrammarBuilder Grammar
}
}

[RequiredBombSetting(nameof(Bomb.Batteries), nameof(Bomb.HasParallelPort), nameof(Bomb.LastDigitEven))]
public override string Process(string command, Bomb bomb)
{
bool red = command.Contains("red");
Expand Down
1 change: 1 addition & 0 deletions Game/Modules/Wires.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public override GrammarBuilder Grammar
}
}

[RequiredBombSetting(nameof(Bomb.LastDigitEven))]
public override string Process(string command, Bomb bomb)
{
string[] wires = command.Split(' ');
Expand Down
17 changes: 17 additions & 0 deletions Game/RequiredBombSettingAttribute.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace KTANE.Game
{
using System;
using System.Collections.Generic;
using System.Linq;

[AttributeUsage(AttributeTargets.Method)]
internal class RequiredBombSettingAttribute : Attribute
{
public RequiredBombSettingAttribute(params string[] settings)
{
this.Settings = settings.ToList();
}

public List<string> Settings { get; private set; }
}
}

0 comments on commit e5bd5b2

Please sign in to comment.