Compare commits

...

6 Commits
3.3 ... master

Author SHA1 Message Date
c6bd342e87 Merge remote-tracking branch 'origin/master' 2024-02-12 01:37:32 +01:00
82e14c8b9e Use do-while instead of convoluted setups. 2024-02-12 01:37:28 +01:00
3fe43b6e63 Use do-while instead of convoluted setups. 2024-02-12 01:23:44 +01:00
4c7a3c9069 Serial only on Windows check 2024-02-12 01:23:07 +01:00
fcb5bf0a68 Cleanup 2024-02-12 01:22:46 +01:00
3659e357f2 Dependency Update
Less convoluted json.
2024-02-11 23:38:04 +01:00
5 changed files with 114 additions and 105 deletions

View File

@ -15,7 +15,7 @@ public struct Configuration
public List<Api> Apis { get; init; } public List<Api> Apis { get; init; }
public List<Shocker> Shockers { get; init; } public Dictionary<int, Shocker> Shockers { get; init; }
public Configuration() public Configuration()
{ {

View File

@ -71,6 +71,6 @@ public class OpenCS2hock
private void EventHandler(CS2EventArgs cs2EventArgs, ShockerAction shockerAction) private void EventHandler(CS2EventArgs cs2EventArgs, ShockerAction shockerAction)
{ {
this._logger?.Log(LogLevel.Information, $"Action {shockerAction}\nEventArgs: {cs2EventArgs}"); this._logger?.Log(LogLevel.Information, $"Action {shockerAction}\nEventArgs: {cs2EventArgs}");
shockerAction.Execute(cs2EventArgs); shockerAction.Execute(_configuration.Shockers, cs2EventArgs);
} }
} }

View File

@ -11,7 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="CS2GSI" Version="1.0.7" /> <PackageReference Include="CS2GSI" Version="1.0.7" />
<PackageReference Include="CShocker" Version="2.2.0" /> <PackageReference Include="CShocker" Version="2.3.1" />
<PackageReference Include="GlaxLogger" Version="1.0.6" /> <PackageReference Include="GlaxLogger" Version="1.0.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>

View File

@ -1,4 +1,6 @@
using System.Text.RegularExpressions; using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Text.RegularExpressions;
using CShocker.Devices.Abstract; using CShocker.Devices.Abstract;
using CShocker.Devices.Additional; using CShocker.Devices.Additional;
using CShocker.Devices.APIs; using CShocker.Devices.APIs;
@ -44,15 +46,15 @@ public static class Setup
switch (pressedKey) switch (pressedKey)
{ {
case ConsoleKey.D1: case ConsoleKey.D1:
Console.WriteLine("New LogLevel:");
string[] levels = Enum.GetNames<LogLevel>(); string[] levels = Enum.GetNames<LogLevel>();
for(int i = 0; i < levels.Length; i++)
Console.WriteLine($"{i}) {levels[i]}");
int selected; int selected;
while (!int.TryParse(Console.ReadKey().KeyChar.ToString(), out selected) || selected < 0 || do
selected >= levels.Length) {
{//NYAA Console.Clear();
} Console.WriteLine("New LogLevel:");
for (int i = 0; i < levels.Length; i++)
Console.WriteLine($"{i}) {levels[i]}");
} while (!int.TryParse(Console.ReadKey().KeyChar.ToString(), out selected) || selected < 0 || selected >= levels.Length);
Console.WriteLine();//NewLine after Input Console.WriteLine();//NewLine after Input
c.LogLevel = Enum.Parse<LogLevel>(levels[selected]); c.LogLevel = Enum.Parse<LogLevel>(levels[selected]);
break; break;
@ -60,10 +62,12 @@ public static class Setup
AddApisWorkflow(ref c); AddApisWorkflow(ref c);
break; break;
case ConsoleKey.D3: case ConsoleKey.D3:
// ReSharper disable once PossibleInvalidCastExceptionInForeachLoop Only returning OpenShockApi Objects
foreach (OpenShockApi api in c.Apis.Where(a => a is OpenShockApi)) foreach (OpenShockApi api in c.Apis.Where(a => a is OpenShockApi))
{ {
Configuration configuration = c; 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; break;
@ -107,42 +111,42 @@ public static class Setup
{ {
Console.WriteLine("Select API:"); Console.WriteLine("Select API:");
Console.WriteLine("1) OpenShock (HTTP)"); Console.WriteLine("1) OpenShock (HTTP)");
Console.WriteLine("2) OpenShock (Serial)"); Console.WriteLine("2) OpenShock (Serial Windows Only)");
Console.WriteLine("3) PiShock (HTTP) NotImplemented"); //TODO Console.WriteLine("3) PiShock (HTTP) NotImplemented"); //TODO
Console.WriteLine("4) PiShock (Serial) NotImplemented"); //TODO Console.WriteLine("4) PiShock (Serial Windows Only) NotImplemented"); //TODO
char selectedChar = Console.ReadKey().KeyChar; char selectedChar = Console.ReadKey().KeyChar;
int selected = 0; int selected;
while (!int.TryParse(selectedChar.ToString(), out selected) || selected < 1 || selected > 3) while (!int.TryParse(selectedChar.ToString(), out selected) || selected < 1 || selected > 4)
selectedChar = Console.ReadKey().KeyChar; selectedChar = Console.ReadKey().KeyChar;
Console.WriteLine();//NewLine after Input Console.WriteLine();//NewLine after Input
string apiUri, apiKey; string apiUri, apiKey;
Api? api = null; Api? api = null;
DurationRange durationRange;
IntensityRange intensityRange;
switch (selected) switch (selected)
{ {
case 1: //OpenShock (HTTP) case 1: //OpenShock (HTTP)
apiUri = QueryString("OpenShock API-Endpoint (https://api.shocklink.net):", "https://api.shocklink.net"); apiUri = QueryString("OpenShock API-Endpoint (https://api.shocklink.net):", "https://api.shocklink.net");
apiKey = QueryString("OpenShock API-Key:",""); apiKey = QueryString("OpenShock API-Key:","");
intensityRange = GetIntensityRange(); api = new OpenShockHttp(apiKey, apiUri);
durationRange = GetDurationRange();
api = new OpenShockHttp(intensityRange, durationRange, apiKey, apiUri);
foreach(OpenShockShocker shocker in ((OpenShockHttp)api).GetShockers()) 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; goto default;
case 2: //OpenShock (Serial) case 2: //OpenShock (Serial)
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
throw new PlatformNotSupportedException("Serial is only supported on Windows.");
apiUri = QueryString("OpenShock API-Endpoint (https://api.shocklink.net):", "https://api.shocklink.net"); apiUri = QueryString("OpenShock API-Endpoint (https://api.shocklink.net):", "https://api.shocklink.net");
apiKey = QueryString("OpenShock API-Key:",""); apiKey = QueryString("OpenShock API-Key:","");
intensityRange = GetIntensityRange();
durationRange = GetDurationRange();
SerialPortInfo serialPort = SelectSerialPort(); SerialPortInfo serialPort = SelectSerialPort();
api = new OpenShockSerial(intensityRange, durationRange, serialPort, apiKey, apiUri); api = new OpenShockSerial(serialPort, apiKey, apiUri);
foreach (OpenShockShocker shocker in ((OpenShockSerial)api).GetShockers()) 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; goto default;
case 3: //PiShock (HTTP) case 3: //PiShock (HTTP)
goto default;
case 4: //PiShock (Serial) case 4: //PiShock (Serial)
if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
throw new PlatformNotSupportedException("Serial is only supported on Windows.");
goto default;
default: default:
if (api is null) if (api is null)
throw new NotImplementedException(); throw new NotImplementedException();
@ -151,21 +155,19 @@ public static class Setup
} }
} }
[SupportedOSPlatform("windows")]
private static SerialPortInfo SelectSerialPort() private static SerialPortInfo SelectSerialPort()
{ {
List<SerialPortInfo> serialPorts = SerialHelper.GetSerialPorts(); List<SerialPortInfo> serialPorts = SerialHelper.GetSerialPorts();
for(int i = 0; i < serialPorts.Count; i++)
Console.WriteLine($"{i}) {serialPorts[i]}");
Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:"); int selectedPort;
string? selectedPortStr = Console.ReadLine(); do
int selectedPort = -1;
while (!int.TryParse(selectedPortStr, out selectedPort) || selectedPort < 0 || selectedPort > serialPorts.Count - 1)
{ {
Console.Clear();
for(int i = 0; i < serialPorts.Count; i++)
Console.WriteLine($"{i}) {serialPorts[i]}");
Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:"); Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:");
selectedPortStr = Console.ReadLine(); } while (!int.TryParse(Console.ReadLine(), out selectedPort) || selectedPort < 0 || selectedPort >= serialPorts.Count);
}
Console.WriteLine();//NewLine after Input Console.WriteLine();//NewLine after Input
return serialPorts[selectedPort]; return serialPorts[selectedPort];
@ -174,27 +176,74 @@ public static class Setup
private static void AddAction(ref Configuration c) private static void AddAction(ref Configuration c)
{ {
CS2Event triggerEvent = GetTrigger(); CS2Event triggerEvent = GetTrigger();
Shocker shocker = GetShocker(c.Shockers); int shockerId = GetShockerId(c.Shockers);
ControlAction action = GetControlAction(); 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);
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<Shocker> shockers) private static int GetShockerId(Dictionary<int, Shocker> shockersDict)
{ {
Console.WriteLine("Select Shocker:"); List<Shocker> shockers = shockersDict.Values.ToList();
for (int i = 0; i < shockers.Count; i++)
Console.WriteLine($"{i}) {shockers[i]}");
int selectedShockerIndex; int selectedShockerIndex;
while (!int.TryParse(Console.ReadLine(), out selectedShockerIndex) || selectedShockerIndex < 0 || selectedShockerIndex > shockers.Count) do
{
Console.Clear();
Console.WriteLine("Select Shocker:"); Console.WriteLine("Select Shocker:");
for (int i = 0; i < shockers.Count; i++)
Console.WriteLine($"{i}) {shockers[i]}");
} while (!int.TryParse(Console.ReadLine(), out selectedShockerIndex) || selectedShockerIndex < 0 || selectedShockerIndex > shockersDict.Count);
Console.WriteLine();//NewLine after Input Console.WriteLine();//NewLine after Input
Shocker shocker = shockers[selectedShockerIndex]; Shocker shocker = shockers[selectedShockerIndex];
return shocker; return shockersDict.First(s => s.Value == shocker).Key;
}
private static IntegerRange GetIntegerRange(int min, int max, string? unit = null)
{
Regex rangeRex = new (@"([0-9]{1,5})\-([0-9]{1,5})");
string intensityRangeStr;
do
{
intensityRangeStr = QueryString($"Range ({min}-{max}) {(unit is null ? "" : $"in {unit}")}:", "0-100");
} while (!rangeRex.IsMatch(intensityRangeStr));
return new IntegerRange(short.Parse(rangeRex.Match(intensityRangeStr).Groups[1].Value), short.Parse(rangeRex.Match(intensityRangeStr).Groups[2].Value));
}
private static CS2Event GetTrigger()
{
string[] eventNames = Enum.GetNames(typeof(CS2Event));
int selectedIndex;
do
{
Console.Clear();
Console.WriteLine("Select CS2 Trigger-Event:");
for (int i = 0; i < eventNames.Length; i++)
Console.WriteLine($"{i}) {eventNames[i]}");
} while (!int.TryParse(Console.ReadLine(), out selectedIndex) || selectedIndex < 0 || selectedIndex >= eventNames.Length);
return Enum.Parse<CS2Event>(eventNames[selectedIndex]);
}
private static ControlAction GetControlAction()
{
string[] actionNames = Enum.GetNames(typeof(ControlAction));
int selectedIndex;
do
{
Console.Clear();
Console.WriteLine("Select Action:");
for (int i = 0; i < actionNames.Length; i++)
Console.WriteLine($"{i}) {actionNames[i]}");
} while (!int.TryParse(Console.ReadLine(), out selectedIndex) || selectedIndex < 0 || selectedIndex >= actionNames.Length);
return Enum.Parse<ControlAction>(actionNames[selectedIndex]);
} }
private static bool QueryBool(string queryString, bool defaultResult) private static bool QueryBool(string queryString, bool defaultResult)
@ -211,54 +260,4 @@ public static class Setup
string? userInput = Console.ReadLine(); string? userInput = Console.ReadLine();
return userInput?.Length > 0 ? userInput : defaultResult; return userInput?.Length > 0 ? userInput : defaultResult;
} }
private static IntensityRange GetIntensityRange()
{
Regex intensityRangeRex = new (@"([0-9]{1,3})\-(1?[0-9]{1,2})");
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);
}
private static CS2Event GetTrigger()
{
string[] names = Enum.GetNames(typeof(CS2Event));
Console.WriteLine("Select CS2 Trigger-Event:");
for (int i = 0; i < names.Length; i++)
Console.WriteLine($"{i}) {names[i]}");
int selectedIndex;
while (!int.TryParse(Console.ReadLine(), out selectedIndex))
Console.WriteLine("Select CS2 Trigger-Event:");
return Enum.Parse<CS2Event>(names[selectedIndex]);
}
private static ControlAction GetControlAction()
{
string[] names = Enum.GetNames(typeof(ControlAction));
Console.WriteLine("Select Action:");
for (int i = 0; i < names.Length; i++)
Console.WriteLine($"{i}) {names[i]}");
int selectedIndex;
while (!int.TryParse(Console.ReadLine(), out selectedIndex))
Console.WriteLine("Select Action:");
return Enum.Parse<ControlAction>(names[selectedIndex]);
}
} }

View File

@ -9,32 +9,42 @@ public struct ShockerAction
{ {
public CS2Event TriggerEvent; public CS2Event TriggerEvent;
// ReSharper disable thrice FieldCanBeMadeReadOnly.Global JsonDeserializer will throw a fit // ReSharper disable thrice FieldCanBeMadeReadOnly.Global JsonDeserializer will throw a fit
public Shocker Shocker; public int ShockerId;
public ControlAction Action; public ControlAction Action;
public bool ValueFromInput; 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.TriggerEvent = trigger;
this.Shocker = shocker; this.ShockerId = shockerId;
this.Action = action; this.Action = action;
this.ValueFromInput = valueFromInput; this.ValueFromInput = valueFromInput;
this.IntensityRange = intensityRange;
this.DurationRange = durationRange;
} }
public void Execute(CS2EventArgs cs2EventArgs) public void Execute(Dictionary<int, Shocker> shockers, CS2EventArgs cs2EventArgs)
{ {
int intensity = ValueFromInput ? IntensityFromCS2Event(cs2EventArgs) : Shocker.Api.IntensityRange.GetRandomRangeValue(); if (!shockers.ContainsKey(ShockerId))
Shocker.Control(Action, intensity); 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) private int IntensityFromCS2Event(CS2EventArgs cs2EventArgs)
{ {
IntensityRange configuredRangeForShocker = Shocker.Api.IntensityRange;
return TriggerEvent switch return TriggerEvent switch
{ {
CS2Event.OnDamageTaken => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max), CS2Event.OnDamageTaken => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 0, 100, IntensityRange.Min, IntensityRange.Max),
CS2Event.OnArmorChange => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max), CS2Event.OnArmorChange => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 0, 100, IntensityRange.Min, IntensityRange.Max),
_ => configuredRangeForShocker.GetRandomRangeValue() _ => RandomValueFromRange(IntensityRange)
}; };
} }
@ -47,7 +57,7 @@ public struct ShockerAction
public override string ToString() public override string ToString()
{ {
return $"Trigger Event: {Enum.GetName(typeof(CS2Event), this.TriggerEvent)}\n" + 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" + $"Action: {Enum.GetName(typeof(ControlAction), this.Action)}\n" +
$"ValueFromInput: {ValueFromInput}"; $"ValueFromInput: {ValueFromInput}";
} }