From 3871fbc76e1d9d9d414adfdd77ec895d5e586a3a Mon Sep 17 00:00:00 2001 From: glax Date: Sun, 4 Feb 2024 17:50:10 +0100 Subject: [PATCH] Update dependencies --- OpenCS2hock.sln.DotSettings.user | 6 + OpenCS2hock/Configuration.cs | 24 ++-- OpenCS2hock/Logger.cs | 65 --------- OpenCS2hock/OpenCS2hock.cs | 97 +++++-------- OpenCS2hock/OpenCS2hock.csproj | 3 +- OpenCS2hock/Program.cs | 3 +- OpenCS2hock/Setup.cs | 235 +++++++++++++------------------ OpenCS2hock/ShockerAction.cs | 35 ++++- 8 files changed, 182 insertions(+), 286 deletions(-) delete mode 100644 OpenCS2hock/Logger.cs diff --git a/OpenCS2hock.sln.DotSettings.user b/OpenCS2hock.sln.DotSettings.user index 87e528f..2887714 100644 --- a/OpenCS2hock.sln.DotSettings.user +++ b/OpenCS2hock.sln.DotSettings.user @@ -1,4 +1,10 @@  True + <AssemblyExplorer> + <Assembly Path="C:\Users\Glax\RiderProjects\CShocker\CShocker\bin\Debug\net7.0\CShocker.dll" /> +</AssemblyExplorer> + <SessionState ContinuousTestingMode="0" Name="All tests from Solution" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"> + <Solution /> +</SessionState> True \ No newline at end of file diff --git a/OpenCS2hock/Configuration.cs b/OpenCS2hock/Configuration.cs index 9dfa7cf..7b8feec 100644 --- a/OpenCS2hock/Configuration.cs +++ b/OpenCS2hock/Configuration.cs @@ -1,4 +1,7 @@ -using CShocker.Shockers.Abstract; +using CShocker.Devices.Abstract; +using CShocker.Devices.Additional; +using CShocker.Shockers.Abstract; +using CShocker.Shockers.Additional; using Microsoft.Extensions.Logging; using Newtonsoft.Json; @@ -8,20 +11,25 @@ public struct Configuration { public LogLevel LogLevel = LogLevel.Information; - public List Shockers = new(); + public List ShockerActions { get; init; } - public List ShockerActions = new (); + public List Apis { get; init; } + + public List Shockers { get; init; } public Configuration() { - + ShockerActions = new (); + Apis = new(); + Shockers = new(); } public override string ToString() { return $"Loglevel: {Enum.GetName(typeof(LogLevel), LogLevel)}\n" + - $"Shockers: {string.Join("\n---", Shockers)}\n" + - $"Actions: {string.Join("\n---", ShockerActions)}"; + $"Apis:\n{string.Join("\n---\n", Apis)}\n" + + $"Shockers:\n{string.Join("\n---\n", Shockers)}\n" + + $"Actions:\n{string.Join("\n---\n", ShockerActions)}\n"; } internal static Configuration GetConfigurationFromFile(string? path = null, ILogger? logger = null) @@ -31,11 +39,9 @@ public struct Configuration if (!File.Exists(settingsFilePath)) c = Setup.Run().SaveConfiguration(); else - c = JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath), new CShocker.Shockers.ShockerJsonConverter()); + c = JsonConvert.DeserializeObject(File.ReadAllText(settingsFilePath), new ApiJsonConverter(), new ShockerJsonConverter()); if (!c.ConfigurationValid()) throw new Exception("Configuration validation failed."); - foreach (Shocker cShocker in c.Shockers) - cShocker.SetLogger(logger); return c; } diff --git a/OpenCS2hock/Logger.cs b/OpenCS2hock/Logger.cs deleted file mode 100644 index 8a8c0da..0000000 --- a/OpenCS2hock/Logger.cs +++ /dev/null @@ -1,65 +0,0 @@ -using Microsoft.Extensions.Logging; - -namespace OpenCS2hock; - -public class Logger : ILogger -{ - private LogLevel _enabledLoglevel; - private readonly ConsoleColor _defaultForegroundColor = Console.ForegroundColor; - private readonly ConsoleColor _defaultBackgroundColor = Console.BackgroundColor; - - public Logger(LogLevel logLevel = LogLevel.Trace) - { - _enabledLoglevel = logLevel; - } - - public void UpdateLogLevel(LogLevel logLevel) - { - this._enabledLoglevel = logLevel; - } - - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) - { - if (!IsEnabled(logLevel)) - return; - Console.ForegroundColor = ForegroundColorForLogLevel(logLevel); - Console.BackgroundColor = BackgroundColorForLogLevel(logLevel); - Console.Write(logLevel.ToString()[..3].ToUpper()); - Console.ResetColor(); - // ReSharper disable once LocalizableElement - Console.Write($" [{DateTime.UtcNow:HH:mm:ss.fff}] "); - Console.WriteLine(formatter.Invoke(state, exception)); - } - - public bool IsEnabled(LogLevel logLevel) - { - return logLevel >= _enabledLoglevel; - } - - public IDisposable? BeginScope(TState state) where TState : notnull - { - return null; - } - - private ConsoleColor ForegroundColorForLogLevel(LogLevel logLevel) - { - return logLevel switch - { - LogLevel.Error or LogLevel.Critical => ConsoleColor.Black, - LogLevel.Debug => ConsoleColor.Black, - LogLevel.Information => ConsoleColor.White, - _ => _defaultForegroundColor - }; - } - - private ConsoleColor BackgroundColorForLogLevel(LogLevel logLevel) - { - return logLevel switch - { - LogLevel.Error or LogLevel.Critical => ConsoleColor.Red, - LogLevel.Debug => ConsoleColor.Yellow, - LogLevel.Information => ConsoleColor.Black, - _ => _defaultBackgroundColor - }; - } -} \ No newline at end of file diff --git a/OpenCS2hock/OpenCS2hock.cs b/OpenCS2hock/OpenCS2hock.cs index 9166ac3..f2d8356 100644 --- a/OpenCS2hock/OpenCS2hock.cs +++ b/OpenCS2hock/OpenCS2hock.cs @@ -1,7 +1,6 @@ using Microsoft.Extensions.Logging; using CS2GSI; -using CShocker.Ranges; -using CShocker.Shockers.Abstract; +using GlaxLogger; using CS2Event = CS2GSI.CS2Event; namespace OpenCS2hock; @@ -35,75 +34,43 @@ public class OpenCS2hock this._logger?.Log(LogLevel.Information, "Setting up EventHandlers..."); foreach (ShockerAction shockerAction in this._configuration.ShockerActions) { - foreach (string shockerId in shockerAction.ShockerIds) - { - Shocker shocker = this._configuration.Shockers.First(s => s.ShockerIds.Contains(shockerId)); - switch (shockerAction.TriggerEvent) + switch (shockerAction.TriggerEvent) { - case CS2Event.OnKill: this._cs2GSI.OnKill += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnHeadshot: this._cs2GSI.OnHeadshot += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnDeath: this._cs2GSI.OnDeath += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnFlashed: this._cs2GSI.OnFlashed += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnBurning: this._cs2GSI.OnBurning += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnSmoked: this._cs2GSI.OnSmoked += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnRoundStart: this._cs2GSI.OnRoundStart += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnRoundOver: this._cs2GSI.OnRoundOver += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnRoundWin: this._cs2GSI.OnRoundWin += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnRoundLoss: this._cs2GSI.OnRoundLoss += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnDamageTaken: this._cs2GSI.OnDamageTaken += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnMatchStart: this._cs2GSI.OnMatchStart += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnMatchOver: this._cs2GSI.OnMatchOver += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnMoneyChange: this._cs2GSI.OnMoneyChange += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnHealthChange: this._cs2GSI.OnHealthChange += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnArmorChange: this._cs2GSI.OnArmorChange += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnHelmetChange: this._cs2GSI.OnHelmetChange += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnEquipmentValueChange: this._cs2GSI.OnEquipmentValueChange += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnTeamChange: this._cs2GSI.OnTeamChange += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnPlayerChange: this._cs2GSI.OnPlayerChange += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnHalfTime: this._cs2GSI.OnHalfTime += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnFreezeTime: this._cs2GSI.OnFreezeTime += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnBombPlanted: this._cs2GSI.OnBombPlanted += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnBombDefused: this._cs2GSI.OnBombDefused += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.OnBombExploded: this._cs2GSI.OnBombExploded += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.AnyEvent: this._cs2GSI.AnyEvent += args => EventHandler(args, shockerId, shocker, shockerAction); break; - case CS2Event.AnyMessage: this._cs2GSI.AnyMessage += args => EventHandler(args, shockerId, shocker, shockerAction); break; + case CS2Event.OnKill: this._cs2GSI.OnKill += args => EventHandler(args, shockerAction); break; + case CS2Event.OnHeadshot: this._cs2GSI.OnHeadshot += args => EventHandler(args, shockerAction); break; + case CS2Event.OnDeath: this._cs2GSI.OnDeath += args => EventHandler(args, shockerAction); break; + case CS2Event.OnFlashed: this._cs2GSI.OnFlashed += args => EventHandler(args, shockerAction); break; + case CS2Event.OnBurning: this._cs2GSI.OnBurning += args => EventHandler(args, shockerAction); break; + case CS2Event.OnSmoked: this._cs2GSI.OnSmoked += args => EventHandler(args, shockerAction); break; + case CS2Event.OnRoundStart: this._cs2GSI.OnRoundStart += args => EventHandler(args,shockerAction); break; + case CS2Event.OnRoundOver: this._cs2GSI.OnRoundOver += args => EventHandler(args, shockerAction); break; + case CS2Event.OnRoundWin: this._cs2GSI.OnRoundWin += args => EventHandler(args, shockerAction); break; + case CS2Event.OnRoundLoss: this._cs2GSI.OnRoundLoss += args => EventHandler(args, shockerAction); break; + case CS2Event.OnDamageTaken: this._cs2GSI.OnDamageTaken += args => EventHandler(args, shockerAction); break; + case CS2Event.OnMatchStart: this._cs2GSI.OnMatchStart += args => EventHandler(args, shockerAction); break; + case CS2Event.OnMatchOver: this._cs2GSI.OnMatchOver += args => EventHandler(args, shockerAction); break; + case CS2Event.OnMoneyChange: this._cs2GSI.OnMoneyChange += args => EventHandler(args, shockerAction); break; + case CS2Event.OnHealthChange: this._cs2GSI.OnHealthChange += args => EventHandler(args, shockerAction); break; + case CS2Event.OnArmorChange: this._cs2GSI.OnArmorChange += args => EventHandler(args, shockerAction); break; + case CS2Event.OnHelmetChange: this._cs2GSI.OnHelmetChange += args => EventHandler(args, shockerAction); break; + case CS2Event.OnEquipmentValueChange: this._cs2GSI.OnEquipmentValueChange += args => EventHandler(args, shockerAction); break; + case CS2Event.OnTeamChange: this._cs2GSI.OnTeamChange += args => EventHandler(args, shockerAction); break; + case CS2Event.OnPlayerChange: this._cs2GSI.OnPlayerChange += args => EventHandler(args, shockerAction); break; + case CS2Event.OnHalfTime: this._cs2GSI.OnHalfTime += args => EventHandler(args, shockerAction); break; + case CS2Event.OnFreezeTime: this._cs2GSI.OnFreezeTime += args => EventHandler(args, shockerAction); break; + case CS2Event.OnBombPlanted: this._cs2GSI.OnBombPlanted += args => EventHandler(args, shockerAction); break; + case CS2Event.OnBombDefused: this._cs2GSI.OnBombDefused += args => EventHandler(args, shockerAction); break; + case CS2Event.OnBombExploded: this._cs2GSI.OnBombExploded += args => EventHandler(args, shockerAction); break; + case CS2Event.AnyEvent: this._cs2GSI.AnyEvent += args => EventHandler(args, shockerAction); break; + case CS2Event.AnyMessage: this._cs2GSI.AnyMessage += args => EventHandler(args, shockerAction); break; default: this._logger?.Log(LogLevel.Debug, $"CS2Event {nameof(shockerAction.TriggerEvent)} unknown."); break; } - } } } - private void EventHandler(CS2EventArgs cs2EventArgs, string shockerId, Shocker shocker, ShockerAction shockerAction) + private void EventHandler(CS2EventArgs cs2EventArgs, ShockerAction shockerAction) { - this._logger?.Log(LogLevel.Information, $"Shocker: {shocker}\nID: {shockerId}\nAction: {shockerAction}\nEventArgs: {cs2EventArgs}"); - shocker.Control(shockerAction.Action, shockerId, - GetIntensity(shockerAction.ValueFromInput, shockerAction.TriggerEvent, cs2EventArgs, shockerId)); - } - - private int GetIntensity(bool valueFromInput, CS2Event cs2Event, CS2EventArgs eventArgs, string shockerId) - { - return valueFromInput - ? IntensityFromCS2Event(cs2Event, eventArgs, shockerId) - : this._configuration.Shockers.First(shocker => shocker.ShockerIds.Contains(shockerId)) - .IntensityRange.GetRandomRangeValue(); - } - - private int IntensityFromCS2Event(CS2Event cs2Event, CS2EventArgs eventArgs, string shockerId) - { - IntensityRange configuredRangeForShocker = this._configuration.Shockers - .First(shocker => shocker.ShockerIds.Contains(shockerId)) - .IntensityRange; - return cs2Event switch - { - CS2Event.OnDamageTaken => MapInt(eventArgs.ValueAsOrDefault(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max), - CS2Event.OnArmorChange => MapInt(eventArgs.ValueAsOrDefault(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max), - _ => configuredRangeForShocker.GetRandomRangeValue() - }; - } - - private int MapInt(int input, int fromLow, int fromHigh, int toLow, int toHigh) - { - int mappedValue = (input - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow; - return mappedValue; + this._logger?.Log(LogLevel.Information, $"Action {shockerAction}\nEventArgs: {cs2EventArgs}"); + shockerAction.Execute(cs2EventArgs); } } \ No newline at end of file diff --git a/OpenCS2hock/OpenCS2hock.csproj b/OpenCS2hock/OpenCS2hock.csproj index 3a47703..e395876 100644 --- a/OpenCS2hock/OpenCS2hock.csproj +++ b/OpenCS2hock/OpenCS2hock.csproj @@ -11,7 +11,8 @@ - + + diff --git a/OpenCS2hock/Program.cs b/OpenCS2hock/Program.cs index aa27900..fd47dcb 100644 --- a/OpenCS2hock/Program.cs +++ b/OpenCS2hock/Program.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.Logging; +using GlaxLogger; +using Microsoft.Extensions.Logging; namespace OpenCS2hock; diff --git a/OpenCS2hock/Setup.cs b/OpenCS2hock/Setup.cs index 57b2a35..7803f0c 100644 --- a/OpenCS2hock/Setup.cs +++ b/OpenCS2hock/Setup.cs @@ -1,8 +1,10 @@ using System.Text.RegularExpressions; +using CShocker.Devices.Abstract; +using CShocker.Devices.Additional; +using CShocker.Devices.APIs; using CShocker.Ranges; using CShocker.Shockers; using CShocker.Shockers.Abstract; -using CShocker.Shockers.APIS; using Microsoft.Extensions.Logging; using CS2Event = CS2GSI.CS2Event; @@ -16,36 +18,13 @@ public static class Setup Console.Clear(); Console.WriteLine("Running first-time setup."); Configuration c = new(); - - Console.WriteLine("First adding APIs:"); - Console.WriteLine("Press Enter."); - while (Console.ReadKey().Key != ConsoleKey.Enter) - {//NYAA - } - bool addShocker = true; - while (c.Shockers.Count < 1 || addShocker) - { - Console.Clear(); - AddShockerApi(ref c); - Console.WriteLine("Add another Shocker-API (Y/N):"); - addShocker = Console.ReadKey().Key == ConsoleKey.Y; - } + AddApisWorkflow(ref c); Console.Clear(); - Console.WriteLine("Now adding Actions:"); - Console.WriteLine("Press Enter."); - while (Console.ReadKey().Key != ConsoleKey.Enter) - {//NYAA - } - bool addAction = true; - while (c.ShockerActions.Count < 1 || addAction) - { - Console.Clear(); - AddAction(ref c); - Console.WriteLine("Add another Action (Y/N):"); - addAction = Console.ReadKey().Key == ConsoleKey.Y; - } + + AddActionsWorkflow(ref c); + return c; } @@ -55,11 +34,11 @@ public static class Setup while (pressedKey is not ConsoleKey.X && pressedKey is not ConsoleKey.Q) { Console.Clear(); - Console.WriteLine("Config Edit Mode."); - Console.WriteLine("What do you want to edit?"); + Console.WriteLine("Config Edit Mode.\n"); Console.WriteLine("1) LogLevel"); - Console.WriteLine("2) Shocker-APIs"); - Console.WriteLine("3) Event Actions"); + Console.WriteLine("2) Add API"); + Console.WriteLine("3) Refresh Shockers (OpenShock)"); + Console.WriteLine("3) Add Action"); Console.WriteLine("\nq) Quit Edit Mode"); pressedKey = Console.ReadKey().Key; switch (pressedKey) @@ -74,50 +53,73 @@ public static class Setup selected >= levels.Length) {//NYAA } + Console.WriteLine();//NewLine after Input c.LogLevel = Enum.Parse(levels[selected]); break; case ConsoleKey.D2: - bool addShocker = true; - while (c.Shockers.Count < 1 || addShocker) - { - Console.Clear(); - AddShockerApi(ref c); - Console.WriteLine("Add another Shocker-API (Y/N):"); - addShocker = Console.ReadKey().Key == ConsoleKey.Y; - } + AddApisWorkflow(ref c); break; case ConsoleKey.D3: - bool addAction = true; - while (c.ShockerActions.Count < 1 || addAction) + foreach (OpenShockApi api in c.Apis.Where(a => a is OpenShockApi)) { - Console.Clear(); - AddAction(ref c); - Console.WriteLine("Add another Action (Y/N):"); - addAction = Console.ReadKey().Key == ConsoleKey.Y; + Configuration configuration = c; + c.Shockers.AddRange(api.GetShockers().Where(s => !configuration.Shockers.Contains(s))); } + + break; + case ConsoleKey.D4: + AddActionsWorkflow(ref c); break; } } c.SaveConfiguration(); } + private static void AddApisWorkflow(ref Configuration c) + { + Console.WriteLine("Adding APIs."); + bool addApis = true; + while (c.Apis.Count < 1 || addApis) + { + Console.Clear(); + AddShockerApi(ref c); + Console.WriteLine("Add another Api (Y/N):"); + addApis = Console.ReadKey().Key == ConsoleKey.Y; + Console.WriteLine();//NewLine after Input + } + } + + private static void AddActionsWorkflow(ref Configuration c) + { + Console.WriteLine("Adding Actions."); + bool addAction = true; + while (c.ShockerActions.Count < 1 || addAction) + { + Console.Clear(); + AddAction(ref c); + Console.WriteLine("Add another Action (Y/N):"); + addAction = Console.ReadKey().Key == ConsoleKey.Y; + Console.WriteLine();//NewLine after Input + } + } + private static void AddShockerApi(ref Configuration c) { Console.WriteLine("Select API:"); Console.WriteLine("1) OpenShock (HTTP)"); Console.WriteLine("2) OpenShock (Serial)"); - Console.WriteLine("3) PiShock (HTTP)"); + Console.WriteLine("3) PiShock (HTTP) NotImplemented"); //TODO Console.WriteLine("4) PiShock (Serial) NotImplemented"); //TODO - string? selectedChar = Console.ReadLine(); - int selected; - while (!int.TryParse(selectedChar, out selected) || selected < 1 || selected > 3) - selectedChar = Console.ReadLine(); + char selectedChar = Console.ReadKey().KeyChar; + int selected = 0; + while (!int.TryParse(selectedChar.ToString(), out selected) || selected < 1 || selected > 3) + selectedChar = Console.ReadKey().KeyChar; + Console.WriteLine();//NewLine after Input string apiUri, apiKey; - Shocker newShocker; + Api? api = null; DurationRange durationRange; IntensityRange intensityRange; - List shockerIds = new(); switch (selected) { case 1: //OpenShock (HTTP) @@ -125,47 +127,33 @@ public static class Setup apiKey = QueryString("OpenShock API-Key:",""); intensityRange = GetIntensityRange(); durationRange = GetDurationRange(); - newShocker = new OpenShockHttp(shockerIds, intensityRange, durationRange, apiKey, apiUri); - newShocker.ShockerIds.AddRange(((OpenShockHttp)newShocker).GetShockers()); - break; + api = new OpenShockHttp(intensityRange, durationRange, apiKey, apiUri); + foreach(OpenShockShocker shocker in ((OpenShockHttp)api).GetShockers()) + c.Shockers.Add(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(); - SerialShocker.SerialPortInfo serialPort = SelectSerialPort(); - newShocker = new OpenShockSerial(new Dictionary(), intensityRange, - durationRange, serialPort); - foreach (KeyValuePair kv in ((OpenShockSerial)newShocker) - .GetShockers(apiUri, apiKey)) - { - newShocker.ShockerIds.Add(kv.Key); - ((OpenShockSerial)newShocker).Model.Add(kv.Key, kv.Value); - } - break; + SerialPortInfo serialPort = SelectSerialPort(); + api = new OpenShockSerial(intensityRange, durationRange, serialPort, apiKey, apiUri); + foreach (OpenShockShocker shocker in ((OpenShockSerial)api).GetShockers()) + c.Shockers.Add(shocker); + goto default; case 3: //PiShock (HTTP) - apiUri = QueryString("PiShock API-Endpoint (https://do.pishock.com/api/apioperate):", "https://do.pishock.com/api/apioperate"); - apiKey = QueryString("PiShock API-Key:",""); - string username = QueryString("Username:",""); - string shareCode = QueryString("Sharecode:",""); - Console.WriteLine("Shocker IDs associated with this API:"); - shockerIds = AddShockerIds(); - intensityRange = GetIntensityRange(); - durationRange = GetDurationRange(); - - newShocker = new PiShockHttp(shockerIds, intensityRange, durationRange, apiKey, username, shareCode, apiUri); - break; - // ReSharper disable thrice RedundantCaseLabel case 4: //PiShock (Serial) default: - throw new NotImplementedException(); + if (api is null) + throw new NotImplementedException(); + c.Apis.Add(api); + break; } - c.Shockers.Add(newShocker); } - private static SerialShocker.SerialPortInfo SelectSerialPort() + private static SerialPortInfo SelectSerialPort() { - List serialPorts = SerialShocker.GetSerialPorts(); + List serialPorts = SerialHelper.GetSerialPorts(); for(int i = 0; i < serialPorts.Count; i++) Console.WriteLine($"{i}) {serialPorts[i]}"); @@ -178,6 +166,7 @@ public static class Setup Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:"); selectedPortStr = Console.ReadLine(); } + Console.WriteLine();//NewLine after Input return serialPorts[selectedPort]; } @@ -185,21 +174,35 @@ public static class Setup private static void AddAction(ref Configuration c) { CS2Event triggerEvent = GetTrigger(); - Console.WriteLine("Shocker IDs to trigger when Event occurs:"); - List shockerIds = GetShockerIds(c.Shockers); + Shocker shocker = GetShocker(c.Shockers); ControlAction action = GetControlAction(); - bool useEventArgsValue = QueryBool("Try using EventArgs to control Intensity (within set limits)?", "false"); + bool useEventArgsValue = QueryBool("Try using EventArgs to control Intensity (within set limits)?", false); - c.ShockerActions.Add(new ShockerAction(triggerEvent, shockerIds, action, useEventArgsValue)); + c.ShockerActions.Add(new(triggerEvent, shocker, action, useEventArgsValue)); } - private static bool QueryBool(string queryString, string defaultResult) + private static Shocker GetShocker(List shockers) { - string value = QueryString(queryString, defaultResult); - bool ret; - while (bool.TryParse(value, out ret)) - value = QueryString(queryString, defaultResult); - return ret; + Console.WriteLine("Select Shocker:"); + 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) + Console.WriteLine("Select Shocker:"); + Console.WriteLine();//NewLine after Input + + Shocker shocker = shockers[selectedShockerIndex]; + + return shocker; + } + + private static bool QueryBool(string queryString, bool defaultResult) + { + Console.WriteLine(queryString); + char userInput = Console.ReadKey().KeyChar; + Console.WriteLine();//NewLine after Input + return bool.TryParse(userInput.ToString(), out bool ret) ? ret : defaultResult; } private static string QueryString(string queryString, string defaultResult) @@ -231,54 +234,6 @@ public static class Setup return new DurationRange(min, max); } - private static List AddShockerIds() - { - List ids = new(); - bool addAnother = true; - while (ids.Count < 1 || addAnother) - { - string id = QueryString("Shocker ID:", ""); - while (id.Length < 1) - id = QueryString("Shocker ID:", ""); - - ids.Add(id); - - Console.WriteLine("Add another ID? (Y/N):"); - addAnother = Console.ReadKey().Key == ConsoleKey.Y; - } - return ids; - } - - private static List GetShockerIds(List shockers) - { - List ids = new(); - bool addAnother = true; - while (ids.Count < 1 || addAnother) - { - Console.WriteLine("Select Shocker API:"); - for(int i = 0; i < shockers.Count; i++) - Console.WriteLine($"{i}) {shockers[i]}"); - - int selectedShocker; - while (!int.TryParse(Console.ReadLine(), out selectedShocker) || selectedShocker < 0 || selectedShocker >= shockers.Count) - Console.WriteLine("Select Shocker API:"); - - Console.WriteLine("Select Shocker:"); - for (int i = 0; i < shockers[selectedShocker].ShockerIds.Count; i++) - Console.WriteLine($"{i}) {shockers[selectedShocker].ShockerIds[i]}"); - - int selectedIndex; - while (!int.TryParse(Console.ReadLine(), out selectedIndex) || selectedIndex < 0 || selectedIndex >= shockers[selectedShocker].ShockerIds.Count) - Console.WriteLine("Select Shocker:"); - - ids.Add(shockers[selectedShocker].ShockerIds[selectedIndex]); - - Console.WriteLine("Add another ID? (Y/N):"); - addAnother = Console.ReadKey().Key == ConsoleKey.Y; - } - return ids; - } - private static CS2Event GetTrigger() { string[] names = Enum.GetNames(typeof(CS2Event)); diff --git a/OpenCS2hock/ShockerAction.cs b/OpenCS2hock/ShockerAction.cs index eb2f6f7..8e46327 100644 --- a/OpenCS2hock/ShockerAction.cs +++ b/OpenCS2hock/ShockerAction.cs @@ -1,5 +1,7 @@ using CS2GSI; -using CShocker.Shockers; +using CShocker.Devices.Additional; +using CShocker.Ranges; +using CShocker.Shockers.Abstract; namespace OpenCS2hock; @@ -7,22 +9,45 @@ public struct ShockerAction { public CS2Event TriggerEvent; // ReSharper disable thrice FieldCanBeMadeReadOnly.Global JsonDeserializer will throw a fit - public List ShockerIds; + public Shocker Shocker; public ControlAction Action; public bool ValueFromInput; - public ShockerAction(CS2Event trigger, List shockerIds, ControlAction action, bool valueFromInput = false) + public ShockerAction(CS2Event trigger, Shocker shocker, ControlAction action, bool valueFromInput = false) { this.TriggerEvent = trigger; - this.ShockerIds = shockerIds; + this.Shocker = shocker; this.Action = action; this.ValueFromInput = valueFromInput; } + public void Execute(CS2EventArgs cs2EventArgs) + { + int intensity = ValueFromInput ? IntensityFromCS2Event(cs2EventArgs) : Shocker.Api.IntensityRange.GetRandomRangeValue(); + Shocker.Control(Action, intensity); + } + + 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() + }; + } + + private int MapInt(int input, int fromLow, int fromHigh, int toLow, int toHigh) + { + int mappedValue = (input - fromLow) * (toHigh - toLow) / (fromHigh - fromLow) + toLow; + return mappedValue; + } + public override string ToString() { return $"Trigger Event: {Enum.GetName(typeof(CS2Event), this.TriggerEvent)}\n" + - $"ShockerIds: {string.Join(", ", ShockerIds)}\n" + + $"Shocker: {string.Join(", ", Shocker)}\n" + $"Action: {Enum.GetName(typeof(ControlAction), this.Action)}\n" + $"ValueFromInput: {ValueFromInput}"; }