From 3659e357f27122accde94d07394a06d605fb8268 Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 11 Feb 2024 23:38:04 +0100 Subject: [PATCH] Dependency Update Less convoluted json. --- OpenCS2hock/Configuration.cs | 2 +- OpenCS2hock/OpenCS2hock.cs | 2 +- OpenCS2hock/OpenCS2hock.csproj | 2 +- OpenCS2hock/Setup.cs | 53 ++++++++++++---------------------- OpenCS2hock/ShockerAction.cs | 32 +++++++++++++------- 5 files changed, 43 insertions(+), 48 deletions(-) diff --git a/OpenCS2hock/Configuration.cs b/OpenCS2hock/Configuration.cs index 7b8feec..91deb82 100644 --- a/OpenCS2hock/Configuration.cs +++ b/OpenCS2hock/Configuration.cs @@ -15,7 +15,7 @@ public struct Configuration public List Apis { get; init; } - public List Shockers { get; init; } + public Dictionary Shockers { get; init; } public Configuration() { diff --git a/OpenCS2hock/OpenCS2hock.cs b/OpenCS2hock/OpenCS2hock.cs index f2d8356..1cfaef5 100644 --- a/OpenCS2hock/OpenCS2hock.cs +++ b/OpenCS2hock/OpenCS2hock.cs @@ -71,6 +71,6 @@ public class OpenCS2hock private void EventHandler(CS2EventArgs cs2EventArgs, ShockerAction shockerAction) { this._logger?.Log(LogLevel.Information, $"Action {shockerAction}\nEventArgs: {cs2EventArgs}"); - shockerAction.Execute(cs2EventArgs); + shockerAction.Execute(_configuration.Shockers, cs2EventArgs); } } \ No newline at end of file diff --git a/OpenCS2hock/OpenCS2hock.csproj b/OpenCS2hock/OpenCS2hock.csproj index 0086674..4c46f2c 100644 --- a/OpenCS2hock/OpenCS2hock.csproj +++ b/OpenCS2hock/OpenCS2hock.csproj @@ -11,7 +11,7 @@ - + diff --git a/OpenCS2hock/Setup.cs b/OpenCS2hock/Setup.cs index 40b9803..717160c 100644 --- a/OpenCS2hock/Setup.cs +++ b/OpenCS2hock/Setup.cs @@ -63,7 +63,8 @@ public static class Setup foreach (OpenShockApi api in c.Apis.Where(a => a is OpenShockApi)) { Configuration configuration = c; - c.Shockers.AddRange(api.GetShockers().Where(s => !configuration.Shockers.Contains(s))); + foreach(OpenShockShocker s in api.GetShockers().Where(s => !configuration.Shockers.ContainsValue(s))) + c.Shockers.Add(c.Shockers.Any() ? c.Shockers.Keys.Max() + 1 : 0, s); } break; @@ -118,28 +119,22 @@ public static class Setup string apiUri, apiKey; Api? api = null; - DurationRange durationRange; - IntensityRange intensityRange; switch (selected) { case 1: //OpenShock (HTTP) apiUri = QueryString("OpenShock API-Endpoint (https://api.shocklink.net):", "https://api.shocklink.net"); apiKey = QueryString("OpenShock API-Key:",""); - intensityRange = GetIntensityRange(); - durationRange = GetDurationRange(); - api = new OpenShockHttp(intensityRange, durationRange, apiKey, apiUri); + api = new OpenShockHttp(apiKey, apiUri); foreach(OpenShockShocker shocker in ((OpenShockHttp)api).GetShockers()) - c.Shockers.Add(shocker); + c.Shockers.Add(c.Shockers.Any() ? c.Shockers.Keys.Max() + 1 : 0, shocker); goto default; case 2: //OpenShock (Serial) apiUri = QueryString("OpenShock API-Endpoint (https://api.shocklink.net):", "https://api.shocklink.net"); apiKey = QueryString("OpenShock API-Key:",""); - intensityRange = GetIntensityRange(); - durationRange = GetDurationRange(); SerialPortInfo serialPort = SelectSerialPort(); - api = new OpenShockSerial(intensityRange, durationRange, serialPort, apiKey, apiUri); + api = new OpenShockSerial(serialPort, apiKey, apiUri); foreach (OpenShockShocker shocker in ((OpenShockSerial)api).GetShockers()) - c.Shockers.Add(shocker); + c.Shockers.Add(c.Shockers.Any() ? c.Shockers.Keys.Max() + 1 : 0, shocker); goto default; case 3: //PiShock (HTTP) case 4: //PiShock (Serial) @@ -174,27 +169,30 @@ public static class Setup private static void AddAction(ref Configuration c) { CS2Event triggerEvent = GetTrigger(); - Shocker shocker = GetShocker(c.Shockers); + int shockerId = GetShockerId(c.Shockers); ControlAction action = GetControlAction(); bool useEventArgsValue = QueryBool("Try using EventArgs to control Intensity (within set limits)?", false); + IntegerRange intensityRange = GetIntegerRange(0, 100, "%"); + IntegerRange durationRange = GetIntegerRange(0, 30000, "ms"); - c.ShockerActions.Add(new(triggerEvent, shocker, action, useEventArgsValue)); + c.ShockerActions.Add(new(triggerEvent, shockerId, action, useEventArgsValue, intensityRange, durationRange)); } - private static Shocker GetShocker(List shockers) + private static int GetShockerId(Dictionary shockersDict) { Console.WriteLine("Select Shocker:"); + List shockers = shockersDict.Values.ToList(); for (int i = 0; i < shockers.Count; i++) Console.WriteLine($"{i}) {shockers[i]}"); int selectedShockerIndex; - while (!int.TryParse(Console.ReadLine(), out selectedShockerIndex) || selectedShockerIndex < 0 || selectedShockerIndex > shockers.Count) + while (!int.TryParse(Console.ReadLine(), out selectedShockerIndex) || selectedShockerIndex < 0 || selectedShockerIndex > shockersDict.Count) Console.WriteLine("Select Shocker:"); Console.WriteLine();//NewLine after Input Shocker shocker = shockers[selectedShockerIndex]; - return shocker; + return shockersDict.First(s => s.Value == shocker).Key; } private static bool QueryBool(string queryString, bool defaultResult) @@ -212,26 +210,13 @@ public static class Setup return userInput?.Length > 0 ? userInput : defaultResult; } - private static IntensityRange GetIntensityRange() + private static IntegerRange GetIntegerRange(int min, int max, string? unit = null) { - Regex intensityRangeRex = new (@"([0-9]{1,3})\-(1?[0-9]{1,2})"); + Regex rangeRex = new (@"([0-9]{1,5})\-([0-9]{1,5})"); string intensityRangeStr = ""; - while(!intensityRangeRex.IsMatch(intensityRangeStr)) - intensityRangeStr = QueryString("Intensity Range (0-100) in %:", "0-100"); - short min = short.Parse(intensityRangeRex.Match(intensityRangeStr).Groups[1].Value); - short max = short.Parse(intensityRangeRex.Match(intensityRangeStr).Groups[2].Value); - return new IntensityRange(min, max); - } - - private static DurationRange GetDurationRange() - { - Regex intensityRangeRex = new (@"([0-9]{1,4})\-([0-9]{1,5})"); - string intensityRangeStr = ""; - while(!intensityRangeRex.IsMatch(intensityRangeStr)) - intensityRangeStr = QueryString("Duration Range (500-30000) in ms:", "500-30000"); - short min = short.Parse(intensityRangeRex.Match(intensityRangeStr).Groups[1].Value); - short max = short.Parse(intensityRangeRex.Match(intensityRangeStr).Groups[2].Value); - return new DurationRange(min, max); + while(!rangeRex.IsMatch(intensityRangeStr)) + intensityRangeStr = QueryString($"Range ({min}-{max}) {(unit is null ? "" : $"in {unit}")}:", "0-100"); + return new IntegerRange(short.Parse(rangeRex.Match(intensityRangeStr).Groups[1].Value), short.Parse(rangeRex.Match(intensityRangeStr).Groups[2].Value)); } private static CS2Event GetTrigger() diff --git a/OpenCS2hock/ShockerAction.cs b/OpenCS2hock/ShockerAction.cs index 8e46327..2bff8a9 100644 --- a/OpenCS2hock/ShockerAction.cs +++ b/OpenCS2hock/ShockerAction.cs @@ -9,32 +9,42 @@ public struct ShockerAction { public CS2Event TriggerEvent; // ReSharper disable thrice FieldCanBeMadeReadOnly.Global JsonDeserializer will throw a fit - public Shocker Shocker; + public int ShockerId; public ControlAction Action; public bool ValueFromInput; + public IntegerRange IntensityRange, DurationRange; - public ShockerAction(CS2Event trigger, Shocker shocker, ControlAction action, bool valueFromInput = false) + public ShockerAction(CS2Event trigger, int shockerId, ControlAction action, bool valueFromInput, IntegerRange intensityRange, IntegerRange durationRange) { this.TriggerEvent = trigger; - this.Shocker = shocker; + this.ShockerId = shockerId; this.Action = action; this.ValueFromInput = valueFromInput; + this.IntensityRange = intensityRange; + this.DurationRange = durationRange; } - public void Execute(CS2EventArgs cs2EventArgs) + public void Execute(Dictionary shockers, CS2EventArgs cs2EventArgs) { - int intensity = ValueFromInput ? IntensityFromCS2Event(cs2EventArgs) : Shocker.Api.IntensityRange.GetRandomRangeValue(); - Shocker.Control(Action, intensity); + if (!shockers.ContainsKey(ShockerId)) + return; + int intensity = ValueFromInput ? IntensityFromCS2Event(cs2EventArgs) : RandomValueFromRange(IntensityRange); + int duration = RandomValueFromRange(DurationRange); + shockers[ShockerId].Control(Action, intensity, duration); + } + + private static int RandomValueFromRange(IntegerRange range) + { + return Random.Shared.Next(range.Min, range.Max); } private int IntensityFromCS2Event(CS2EventArgs cs2EventArgs) { - IntensityRange configuredRangeForShocker = Shocker.Api.IntensityRange; return TriggerEvent switch { - CS2Event.OnDamageTaken => MapInt(cs2EventArgs.ValueAsOrDefault(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max), - CS2Event.OnArmorChange => MapInt(cs2EventArgs.ValueAsOrDefault(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max), - _ => configuredRangeForShocker.GetRandomRangeValue() + CS2Event.OnDamageTaken => MapInt(cs2EventArgs.ValueAsOrDefault(), 0, 100, IntensityRange.Min, IntensityRange.Max), + CS2Event.OnArmorChange => MapInt(cs2EventArgs.ValueAsOrDefault(), 0, 100, IntensityRange.Min, IntensityRange.Max), + _ => RandomValueFromRange(IntensityRange) }; } @@ -47,7 +57,7 @@ public struct ShockerAction public override string ToString() { return $"Trigger Event: {Enum.GetName(typeof(CS2Event), this.TriggerEvent)}\n" + - $"Shocker: {string.Join(", ", Shocker)}\n" + + $"ShockerId: {ShockerId}\n" + $"Action: {Enum.GetName(typeof(ControlAction), this.Action)}\n" + $"ValueFromInput: {ValueFromInput}"; }