OpenShock, Range for Duration and Intensity (ConfiuguredInteger), CS2 Message Handler
This commit is contained in:
parent
685a3f4f41
commit
6eb1c2c25a
69
OpenCS2hock/CS2MessageHandler.cs
Normal file
69
OpenCS2hock/CS2MessageHandler.cs
Normal 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}
|
||||||
|
}
|
17
OpenCS2hock/ConfiguredInteger.cs
Normal file
17
OpenCS2hock/ConfiguredInteger.cs
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -14,6 +14,16 @@ public static class Installer
|
|||||||
return JsonConvert.DeserializeObject<Settings>(File.ReadAllText(settingsFilePath));
|
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()
|
public static void InstallGsi()
|
||||||
{
|
{
|
||||||
string installLocation = Path.Combine(GetInstallDirectory(), "game\\csgo\\cfg\\gamestate_integration_opencs2hock.cfg");
|
string installLocation = Path.Combine(GetInstallDirectory(), "game\\csgo\\cfg\\gamestate_integration_opencs2hock.cfg");
|
||||||
|
@ -3,13 +3,18 @@
|
|||||||
public class OpenCS2hock
|
public class OpenCS2hock
|
||||||
{
|
{
|
||||||
private GSIServer GSIServer { get; init; }
|
private GSIServer GSIServer { get; init; }
|
||||||
private List<Shocker> _shockers = new();
|
private readonly CS2MessageHandler _cs2MessageHandler;
|
||||||
|
private readonly List<Shocker> _shockers;
|
||||||
private readonly Settings _settings;
|
private readonly Settings _settings;
|
||||||
|
|
||||||
public OpenCS2hock(string? settingsPath = null)
|
public OpenCS2hock(string? settingsPath = null)
|
||||||
{
|
{
|
||||||
_settings = Installer.GetSettings(settingsPath);
|
_settings = Installer.GetSettings(settingsPath);
|
||||||
|
this._shockers = Installer.GetShockers(_settings);
|
||||||
Installer.InstallGsi();
|
Installer.InstallGsi();
|
||||||
|
|
||||||
|
this._cs2MessageHandler = new CS2MessageHandler();
|
||||||
|
|
||||||
this.GSIServer = new GSIServer(3000);
|
this.GSIServer = new GSIServer(3000);
|
||||||
this.GSIServer.OnMessage += OnGSIMessage;
|
this.GSIServer.OnMessage += OnGSIMessage;
|
||||||
|
|
||||||
@ -21,10 +26,42 @@ public class OpenCS2hock
|
|||||||
runningThread.Start();
|
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)
|
private void OnGSIMessage(string content)
|
||||||
{
|
{
|
||||||
string fileName = Path.Combine(Environment.CurrentDirectory, $"{DateTime.Now.ToLongTimeString().Replace(':','.')}.json");
|
string fileName = Path.Combine(Environment.CurrentDirectory, $"{DateTime.Now.ToLongTimeString().Replace(':','.')}.json");
|
||||||
File.WriteAllText(fileName, content);
|
File.WriteAllText(fileName, content);
|
||||||
Console.WriteLine(fileName);
|
Console.WriteLine(fileName);
|
||||||
|
_cs2MessageHandler.HandleCS2Message(content);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -23,4 +23,8 @@
|
|||||||
</Compile>
|
</Compile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
51
OpenCS2hock/OpenShock.cs
Normal file
51
OpenCS2hock/OpenShock.cs
Normal 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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -9,9 +9,6 @@ public struct Settings
|
|||||||
Shockers = Array.Empty<string>()
|
Shockers = Array.Empty<string>()
|
||||||
};
|
};
|
||||||
|
|
||||||
public short FixedIntensity = 30;
|
|
||||||
public short FixedDuration = 1000;
|
|
||||||
|
|
||||||
public Range IntensityRange = new ()
|
public Range IntensityRange = new ()
|
||||||
{
|
{
|
||||||
Min = 0,
|
Min = 0,
|
||||||
@ -24,14 +21,14 @@ public struct Settings
|
|||||||
Max = 2000
|
Max = 2000
|
||||||
};
|
};
|
||||||
|
|
||||||
public Actions Actions = new()
|
public Dictionary<string, string> Actions = new()
|
||||||
{
|
{
|
||||||
OnKill = "Nothing",
|
{"OnKill", "Nothing"},
|
||||||
OnDeath = "Shock",
|
{"OnDeath", "Shock"},
|
||||||
OnRoundStart = "Vibrate",
|
{"OnRoundStart", "Vibrate"},
|
||||||
OnRoundEnd = "Nothing",
|
{"OnRoundEnd", "Nothing"},
|
||||||
OnRoundWin = "Beep",
|
{"OnRoundWin", "Beep"},
|
||||||
OnRoundLoss = "Nothing"
|
{"OnRoundLoss", "Nothing"}
|
||||||
};
|
};
|
||||||
|
|
||||||
public Settings()
|
public Settings()
|
||||||
@ -39,7 +36,7 @@ public struct Settings
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal Shocker.ControlAction StringToAction(string str)
|
public static Shocker.ControlAction StringToAction(string str)
|
||||||
{
|
{
|
||||||
return str.ToLower() switch
|
return str.ToLower() switch
|
||||||
{
|
{
|
||||||
@ -61,8 +58,3 @@ public struct Range
|
|||||||
{
|
{
|
||||||
public short Min, Max;
|
public short Min, Max;
|
||||||
}
|
}
|
||||||
|
|
||||||
public struct Actions
|
|
||||||
{
|
|
||||||
public string OnKill, OnDeath, OnRoundStart, OnRoundEnd, OnRoundWin, OnRoundLoss;
|
|
||||||
}
|
|
@ -3,19 +3,21 @@
|
|||||||
public abstract class Shocker
|
public abstract class Shocker
|
||||||
{
|
{
|
||||||
protected readonly HttpClient HttpClient;
|
protected readonly HttpClient HttpClient;
|
||||||
protected readonly string ApiKey;
|
protected readonly string ApiKey,Endpoint;
|
||||||
protected readonly string Endpoint;
|
protected readonly string[] ShockerIds;
|
||||||
protected string[] ShockerIds;
|
protected readonly ConfiguredInteger Intensity, Duration;
|
||||||
|
|
||||||
public enum ControlAction { Beep, Vibrate, Shock, Nothing }
|
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.Endpoint = endpoint;
|
||||||
this.ApiKey = apiKey;
|
this.ApiKey = apiKey;
|
||||||
this.HttpClient = new HttpClient();
|
this.HttpClient = new HttpClient();
|
||||||
this.ShockerIds = shockerIds;
|
this.ShockerIds = shockerIds;
|
||||||
|
this.Intensity = intensity;
|
||||||
|
this.Duration = duration;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user