Compare commits

..

No commits in common. "master" and "3.3" have entirely different histories.
master ... 3.3

5 changed files with 105 additions and 114 deletions

View File

@ -15,7 +15,7 @@ public struct Configuration
public List<Api> Apis { get; init; } public List<Api> Apis { get; init; }
public Dictionary<int, Shocker> Shockers { get; init; } public List<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(_configuration.Shockers, cs2EventArgs); shockerAction.Execute(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.3.1" /> <PackageReference Include="CShocker" Version="2.2.0" />
<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,6 +1,4 @@
using System.Runtime.InteropServices; using System.Text.RegularExpressions;
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;
@ -46,15 +44,15 @@ public static class Setup
switch (pressedKey) switch (pressedKey)
{ {
case ConsoleKey.D1: case ConsoleKey.D1:
string[] levels = Enum.GetNames<LogLevel>();
int selected;
do
{
Console.Clear();
Console.WriteLine("New LogLevel:"); Console.WriteLine("New LogLevel:");
for (int i = 0; i < levels.Length; i++) string[] levels = Enum.GetNames<LogLevel>();
for(int i = 0; i < levels.Length; i++)
Console.WriteLine($"{i}) {levels[i]}"); Console.WriteLine($"{i}) {levels[i]}");
} while (!int.TryParse(Console.ReadKey().KeyChar.ToString(), out selected) || selected < 0 || selected >= levels.Length); int selected;
while (!int.TryParse(Console.ReadKey().KeyChar.ToString(), out selected) || selected < 0 ||
selected >= levels.Length)
{//NYAA
}
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;
@ -62,12 +60,10 @@ 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;
foreach(OpenShockShocker s in api.GetShockers().Where(s => !configuration.Shockers.ContainsValue(s))) c.Shockers.AddRange(api.GetShockers().Where(s => !configuration.Shockers.Contains(s)));
c.Shockers.Add(c.Shockers.Any() ? c.Shockers.Keys.Max() + 1 : 0, s);
} }
break; break;
@ -111,42 +107,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 Windows Only)"); Console.WriteLine("2) OpenShock (Serial)");
Console.WriteLine("3) PiShock (HTTP) NotImplemented"); //TODO Console.WriteLine("3) PiShock (HTTP) NotImplemented"); //TODO
Console.WriteLine("4) PiShock (Serial Windows Only) NotImplemented"); //TODO Console.WriteLine("4) PiShock (Serial) NotImplemented"); //TODO
char selectedChar = Console.ReadKey().KeyChar; char selectedChar = Console.ReadKey().KeyChar;
int selected; int selected = 0;
while (!int.TryParse(selectedChar.ToString(), out selected) || selected < 1 || selected > 4) while (!int.TryParse(selectedChar.ToString(), out selected) || selected < 1 || selected > 3)
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:","");
api = new OpenShockHttp(apiKey, apiUri); intensityRange = GetIntensityRange();
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(c.Shockers.Any() ? c.Shockers.Keys.Max() + 1 : 0, shocker); c.Shockers.Add(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(serialPort, apiKey, apiUri); api = new OpenShockSerial(intensityRange, durationRange, serialPort, apiKey, apiUri);
foreach (OpenShockShocker shocker in ((OpenShockSerial)api).GetShockers()) foreach (OpenShockShocker shocker in ((OpenShockSerial)api).GetShockers())
c.Shockers.Add(c.Shockers.Any() ? c.Shockers.Keys.Max() + 1 : 0, shocker); c.Shockers.Add(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();
@ -155,19 +151,21 @@ public static class Setup
} }
} }
[SupportedOSPlatform("windows")]
private static SerialPortInfo SelectSerialPort() private static SerialPortInfo SelectSerialPort()
{ {
List<SerialPortInfo> serialPorts = SerialHelper.GetSerialPorts(); List<SerialPortInfo> serialPorts = SerialHelper.GetSerialPorts();
int selectedPort;
do
{
Console.Clear();
for(int i = 0; i < serialPorts.Count; i++) for(int i = 0; i < serialPorts.Count; i++)
Console.WriteLine($"{i}) {serialPorts[i]}"); Console.WriteLine($"{i}) {serialPorts[i]}");
Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:"); Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:");
} while (!int.TryParse(Console.ReadLine(), out selectedPort) || selectedPort < 0 || selectedPort >= serialPorts.Count); string? selectedPortStr = Console.ReadLine();
int selectedPort = -1;
while (!int.TryParse(selectedPortStr, out selectedPort) || selectedPort < 0 || selectedPort > serialPorts.Count - 1)
{
Console.WriteLine($"Select Serial Port [0-{serialPorts.Count-1}]:");
selectedPortStr = Console.ReadLine();
}
Console.WriteLine();//NewLine after Input Console.WriteLine();//NewLine after Input
return serialPorts[selectedPort]; return serialPorts[selectedPort];
@ -176,74 +174,27 @@ public static class Setup
private static void AddAction(ref Configuration c) private static void AddAction(ref Configuration c)
{ {
CS2Event triggerEvent = GetTrigger(); CS2Event triggerEvent = GetTrigger();
int shockerId = GetShockerId(c.Shockers); Shocker shocker = GetShocker(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, shockerId, action, useEventArgsValue, intensityRange, durationRange)); c.ShockerActions.Add(new(triggerEvent, shocker, action, useEventArgsValue));
} }
private static int GetShockerId(Dictionary<int, Shocker> shockersDict) private static Shocker GetShocker(List<Shocker> shockers)
{ {
List<Shocker> shockers = shockersDict.Values.ToList();
int selectedShockerIndex;
do
{
Console.Clear();
Console.WriteLine("Select Shocker:"); Console.WriteLine("Select Shocker:");
for (int i = 0; i < shockers.Count; i++) for (int i = 0; i < shockers.Count; i++)
Console.WriteLine($"{i}) {shockers[i]}"); Console.WriteLine($"{i}) {shockers[i]}");
} while (!int.TryParse(Console.ReadLine(), out selectedShockerIndex) || selectedShockerIndex < 0 || selectedShockerIndex > shockersDict.Count);
int selectedShockerIndex;
while (!int.TryParse(Console.ReadLine(), out selectedShockerIndex) || selectedShockerIndex < 0 || selectedShockerIndex > shockers.Count)
Console.WriteLine("Select Shocker:");
Console.WriteLine();//NewLine after Input Console.WriteLine();//NewLine after Input
Shocker shocker = shockers[selectedShockerIndex]; Shocker shocker = shockers[selectedShockerIndex];
return shockersDict.First(s => s.Value == shocker).Key; return shocker;
}
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)
@ -260,4 +211,54 @@ 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,42 +9,32 @@ 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 int ShockerId; public Shocker Shocker;
public ControlAction Action; public ControlAction Action;
public bool ValueFromInput; public bool ValueFromInput;
public IntegerRange IntensityRange, DurationRange;
public ShockerAction(CS2Event trigger, int shockerId, ControlAction action, bool valueFromInput, IntegerRange intensityRange, IntegerRange durationRange) public ShockerAction(CS2Event trigger, Shocker shocker, ControlAction action, bool valueFromInput = false)
{ {
this.TriggerEvent = trigger; this.TriggerEvent = trigger;
this.ShockerId = shockerId; this.Shocker = shocker;
this.Action = action; this.Action = action;
this.ValueFromInput = valueFromInput; this.ValueFromInput = valueFromInput;
this.IntensityRange = intensityRange;
this.DurationRange = durationRange;
} }
public void Execute(Dictionary<int, Shocker> shockers, CS2EventArgs cs2EventArgs) public void Execute(CS2EventArgs cs2EventArgs)
{ {
if (!shockers.ContainsKey(ShockerId)) int intensity = ValueFromInput ? IntensityFromCS2Event(cs2EventArgs) : Shocker.Api.IntensityRange.GetRandomRangeValue();
return; Shocker.Control(Action, intensity);
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, IntensityRange.Min, IntensityRange.Max), CS2Event.OnDamageTaken => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max),
CS2Event.OnArmorChange => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 0, 100, IntensityRange.Min, IntensityRange.Max), CS2Event.OnArmorChange => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max),
_ => RandomValueFromRange(IntensityRange) _ => configuredRangeForShocker.GetRandomRangeValue()
}; };
} }
@ -57,7 +47,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" +
$"ShockerId: {ShockerId}\n" + $"Shocker: {string.Join(", ", Shocker)}\n" +
$"Action: {Enum.GetName(typeof(ControlAction), this.Action)}\n" + $"Action: {Enum.GetName(typeof(ControlAction), this.Action)}\n" +
$"ValueFromInput: {ValueFromInput}"; $"ValueFromInput: {ValueFromInput}";
} }