OpenShock, Range for Duration and Intensity (ConfiuguredInteger), CS2 Message Handler

This commit is contained in:
glax 2024-01-14 00:30:25 +01:00
parent 685a3f4f41
commit 6eb1c2c25a
8 changed files with 205 additions and 23 deletions

View File

@ -0,0 +1,69 @@
using Newtonsoft.Json.Linq;
namespace OpenCS2hock;
public class CS2MessageHandler
{
public delegate void CS2EventHandler();
public event CS2EventHandler? OnKill;
public event CS2EventHandler? OnDeath;
public event CS2EventHandler? OnRoundStart;
public event CS2EventHandler? OnRoundEnd;
public event CS2EventHandler? OnRoundWin;
public event CS2EventHandler? OnRoundLoss;
public void HandleCS2Message(string message)
{
JObject messageJson = JObject.Parse(message);
JToken? previously = messageJson.GetValue("previously");
RoundState currentRoundState = ParseRoundStateFromString(messageJson["round"]?.Value<string>("phase"));
RoundState previousRoundState = ParseRoundStateFromString(previously?["round"]?.Value<string>("phase"));
if(previousRoundState == RoundState.FreezeTime && currentRoundState == RoundState.Live)
OnRoundStart?.Invoke();
if(previousRoundState == RoundState.Live && currentRoundState == RoundState.FreezeTime)
OnRoundEnd?.Invoke();
Team playerTeam = ParseTeamFromString(messageJson["player"]?.Value<string>("team"));
Team winnerTeam = ParseTeamFromString(messageJson["round"]?.Value<string>("win_team"));
if(winnerTeam != Team.None && playerTeam == winnerTeam)
OnRoundWin?.Invoke();
else if(winnerTeam != Team.None && playerTeam != winnerTeam)
OnRoundLoss?.Invoke();
int? previousDeaths = previously?["player"]?["match_stats"]?.Value<int>("deaths");
int? currentDeaths = messageJson["player"]?["match_stats"]?.Value<int>("deaths");
if(currentDeaths > previousDeaths)
OnDeath?.Invoke();
int? previousKills = previously?["player"]?["match_stats"]?.Value<int>("kills");
int? currentKills = messageJson["player"]?["match_stats"]?.Value<int>("kills");
if(currentKills > previousKills)
OnKill?.Invoke();
}
private RoundState ParseRoundStateFromString(string? str)
{
return str switch
{
"live" => RoundState.Live,
"freezetime" => RoundState.FreezeTime,
_ => RoundState.Unknown
};
}
private Team ParseTeamFromString(string? str)
{
return str switch
{
"T" => Team.T,
"CT" => Team.CT,
_ => Team.None
};
}
private enum RoundState {FreezeTime, Live, Unknown}
private enum Team {T, CT, None}
}

View File

@ -0,0 +1,17 @@
namespace OpenCS2hock;
public class ConfiguredInteger
{
private readonly int _min, _max;
public ConfiguredInteger(int min = 0, int max = 50)
{
this._min = min;
this._max = max;
}
public int GetValue()
{
return Random.Shared.Next(_min, _max);
}
}

View File

@ -13,6 +13,16 @@ public static class Installer
return JsonConvert.DeserializeObject<Settings>(File.ReadAllText(settingsFilePath));
}
public static List<Shocker> GetShockers(Settings settings)
{
List<Shocker> shockers = new();
shockers.Add(new OpenShock(settings.OpenShockSettings.Endpoint, settings.OpenShockSettings.ApiKey,
settings.OpenShockSettings.Shockers,
new ConfiguredInteger(settings.IntensityRange.Min, settings.IntensityRange.Max),
new ConfiguredInteger(settings.DurationRange.Min, settings.DurationRange.Max)));
return shockers;
}
public static void InstallGsi()
{

View File

@ -3,13 +3,18 @@
public class OpenCS2hock
{
private GSIServer GSIServer { get; init; }
private List<Shocker> _shockers = new();
private readonly CS2MessageHandler _cs2MessageHandler;
private readonly List<Shocker> _shockers;
private readonly Settings _settings;
public OpenCS2hock(string? settingsPath = null)
{
_settings = Installer.GetSettings(settingsPath);
this._shockers = Installer.GetShockers(_settings);
Installer.InstallGsi();
this._cs2MessageHandler = new CS2MessageHandler();
this.GSIServer = new GSIServer(3000);
this.GSIServer.OnMessage += OnGSIMessage;
@ -21,10 +26,42 @@ public class OpenCS2hock
runningThread.Start();
}
private void SetupEventHandlers()
{
foreach (Shocker shocker in this._shockers)
{
foreach (KeyValuePair<string, string> kv in _settings.Actions)
{
switch (kv.Key)
{
case "OnKill":
this._cs2MessageHandler.OnKill += () => shocker.Control(Settings.StringToAction(kv.Value));
break;
case "OnDeath":
this._cs2MessageHandler.OnDeath += () => shocker.Control(Settings.StringToAction(kv.Value));
break;
case "OnRoundStart":
this._cs2MessageHandler.OnRoundStart += () => shocker.Control(Settings.StringToAction(kv.Value));
break;
case "OnRoundEnd":
this._cs2MessageHandler.OnRoundEnd += () => shocker.Control(Settings.StringToAction(kv.Value));
break;
case "OnRoundLoss":
this._cs2MessageHandler.OnRoundLoss += () => shocker.Control(Settings.StringToAction(kv.Value));
break;
case "OnRoundWin":
this._cs2MessageHandler.OnRoundWin += () => shocker.Control(Settings.StringToAction(kv.Value));
break;
}
}
}
}
private void OnGSIMessage(string content)
{
string fileName = Path.Combine(Environment.CurrentDirectory, $"{DateTime.Now.ToLongTimeString().Replace(':','.')}.json");
File.WriteAllText(fileName, content);
Console.WriteLine(fileName);
_cs2MessageHandler.HandleCS2Message(content);
}
}

View File

@ -23,4 +23,8 @@
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

51
OpenCS2hock/OpenShock.cs Normal file
View File

@ -0,0 +1,51 @@
using System.Net.Http.Headers;
namespace OpenCS2hock;
public class OpenShock : Shocker
{
public override void Control(ControlAction action, string? shockerId = null)
{
if(shockerId is null)
foreach(string shocker in ShockerIds)
SendRequestMessage(action, shocker);
else
SendRequestMessage(action, shockerId);
}
private void SendRequestMessage(ControlAction action, string shockerId)
{
HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}/1/shockers/control")
{
Headers =
{
UserAgent = { new ProductInfoHeaderValue("OpenCS2hock", "1") },
Accept = { new MediaTypeWithQualityHeaderValue("application/json") },
Authorization = new AuthenticationHeaderValue("Basic", ApiKey)
},
Content = new StringContent(@"[ { "+
$"\"id\": \"{shockerId}\"," +
$"\"type\": {ControlActionToByte(action)},"+
$"\"intensity\": {Intensity.GetValue()},"+
$"\"duration\": {Duration.GetValue()}"+
"}]")
};
this.HttpClient.Send(request);
}
private byte ControlActionToByte(ControlAction action)
{
return action switch
{
ControlAction.Beep => 3,
ControlAction.Vibrate => 2,
ControlAction.Shock => 1,
_ => 0
};
}
public OpenShock(string endpoint, string apiKey, string[] shockerIds, ConfiguredInteger intensity, ConfiguredInteger duration) : base(endpoint, apiKey, shockerIds, intensity, duration)
{
}
}

View File

@ -9,9 +9,6 @@ public struct Settings
Shockers = Array.Empty<string>()
};
public short FixedIntensity = 30;
public short FixedDuration = 1000;
public Range IntensityRange = new ()
{
Min = 0,
@ -24,22 +21,22 @@ public struct Settings
Max = 2000
};
public Actions Actions = new()
public Dictionary<string, string> Actions = new()
{
OnKill = "Nothing",
OnDeath = "Shock",
OnRoundStart = "Vibrate",
OnRoundEnd = "Nothing",
OnRoundWin = "Beep",
OnRoundLoss = "Nothing"
{"OnKill", "Nothing"},
{"OnDeath", "Shock"},
{"OnRoundStart", "Vibrate"},
{"OnRoundEnd", "Nothing"},
{"OnRoundWin", "Beep"},
{"OnRoundLoss", "Nothing"}
};
public Settings()
{
}
internal Shocker.ControlAction StringToAction(string str)
public static Shocker.ControlAction StringToAction(string str)
{
return str.ToLower() switch
{
@ -60,9 +57,4 @@ public struct OpenShockSettings
public struct Range
{
public short Min, Max;
}
public struct Actions
{
public string OnKill, OnDeath, OnRoundStart, OnRoundEnd, OnRoundWin, OnRoundLoss;
}

View File

@ -3,19 +3,21 @@
public abstract class Shocker
{
protected readonly HttpClient HttpClient;
protected readonly string ApiKey;
protected readonly string Endpoint;
protected string[] ShockerIds;
protected readonly string ApiKey,Endpoint;
protected readonly string[] ShockerIds;
protected readonly ConfiguredInteger Intensity, Duration;
public enum ControlAction { Beep, Vibrate, Shock, Nothing }
public abstract void Control(ControlAction action, byte intensity, short duration, string? shockerId = null);
public abstract void Control(ControlAction action, string? shockerId = null);
protected Shocker(string endpoint, string apiKey, string[] shockerIds)
protected Shocker(string endpoint, string apiKey, string[] shockerIds, ConfiguredInteger intensity, ConfiguredInteger duration)
{
this.Endpoint = endpoint;
this.ApiKey = apiKey;
this.HttpClient = new HttpClient();
this.ShockerIds = shockerIds;
this.Intensity = intensity;
this.Duration = duration;
}
}