Dependency Update

Less convoluted json.
This commit is contained in:
glax 2024-02-11 23:38:04 +01:00
parent eb732aad13
commit 3659e357f2
5 changed files with 43 additions and 48 deletions

View File

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

View File

@ -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);
}
}

View File

@ -11,7 +11,7 @@
<ItemGroup>
<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="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

View File

@ -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<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;
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()

View File

@ -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<int, Shocker> 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<int>(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max),
CS2Event.OnArmorChange => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 0, 100, configuredRangeForShocker.Min, configuredRangeForShocker.Max),
_ => configuredRangeForShocker.GetRandomRangeValue()
CS2Event.OnDamageTaken => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 0, 100, IntensityRange.Min, IntensityRange.Max),
CS2Event.OnArmorChange => MapInt(cs2EventArgs.ValueAsOrDefault<int>(), 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}";
}