Compare commits
6 Commits
3dabf95bb0
...
6eb1c2c25a
Author | SHA1 | Date | |
---|---|---|---|
6eb1c2c25a | |||
685a3f4f41 | |||
4b90db389d | |||
ae278b402e | |||
cfd5d2e2c3 | |||
1673310db6 |
@ -2,5 +2,6 @@
|
|||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CS/@EntryIndexedValue">CS</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CS/@EntryIndexedValue">CS</s:String>
|
||||||
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GSI/@EntryIndexedValue">GSI</s:String>
|
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=GSI/@EntryIndexedValue">GSI</s:String>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=appmanifest/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=appmanifest/@EntryIndexedValue">True</s:Boolean>
|
||||||
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=freezetime/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=libraryfolders/@EntryIndexedValue">True</s:Boolean>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=libraryfolders/@EntryIndexedValue">True</s:Boolean>
|
||||||
<s:Boolean x:Key="/Default/UserDictionary/Words/=steamapps/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
<s:Boolean x:Key="/Default/UserDictionary/Words/=steamapps/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
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);
|
||||||
|
}
|
||||||
|
}
|
@ -20,6 +20,8 @@ public class GSIServer
|
|||||||
|
|
||||||
Thread connectionListener = new (HandleConnection);
|
Thread connectionListener = new (HandleConnection);
|
||||||
connectionListener.Start();
|
connectionListener.Start();
|
||||||
|
|
||||||
|
IsRunning = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
private async void HandleConnection()
|
private async void HandleConnection()
|
||||||
|
@ -1,9 +1,29 @@
|
|||||||
using Microsoft.Win32;
|
using Microsoft.Win32;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace OpenCS2hock;
|
namespace OpenCS2hock;
|
||||||
|
|
||||||
public static class Installer
|
public static class Installer
|
||||||
{
|
{
|
||||||
|
public static Settings GetSettings(string? path = null)
|
||||||
|
{
|
||||||
|
string settingsFilePath = path ?? "config.json";
|
||||||
|
if (!File.Exists(settingsFilePath))
|
||||||
|
File.WriteAllText(settingsFilePath, JsonConvert.SerializeObject(new Settings(), Formatting.Indented));
|
||||||
|
|
||||||
|
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,11 +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;
|
||||||
|
|
||||||
public OpenCS2hock()
|
public OpenCS2hock(string? settingsPath = null)
|
||||||
{
|
{
|
||||||
|
_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;
|
||||||
|
|
||||||
@ -19,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)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,6 @@ public class Program
|
|||||||
{
|
{
|
||||||
public static void Main(string[] args)
|
public static void Main(string[] args)
|
||||||
{
|
{
|
||||||
new OpenCS2hock();
|
OpenCS2hock openCS2Hock = new OpenCS2hock();
|
||||||
}
|
}
|
||||||
}
|
}
|
60
OpenCS2hock/Settings.cs
Normal file
60
OpenCS2hock/Settings.cs
Normal file
@ -0,0 +1,60 @@
|
|||||||
|
namespace OpenCS2hock;
|
||||||
|
|
||||||
|
public struct Settings
|
||||||
|
{
|
||||||
|
public OpenShockSettings OpenShockSettings = new()
|
||||||
|
{
|
||||||
|
Endpoint = "https://api.shocklink.net",
|
||||||
|
ApiKey = "",
|
||||||
|
Shockers = Array.Empty<string>()
|
||||||
|
};
|
||||||
|
|
||||||
|
public Range IntensityRange = new ()
|
||||||
|
{
|
||||||
|
Min = 0,
|
||||||
|
Max = 100
|
||||||
|
};
|
||||||
|
|
||||||
|
public Range DurationRange = new()
|
||||||
|
{
|
||||||
|
Min = 1000,
|
||||||
|
Max = 2000
|
||||||
|
};
|
||||||
|
|
||||||
|
public Dictionary<string, string> Actions = new()
|
||||||
|
{
|
||||||
|
{"OnKill", "Nothing"},
|
||||||
|
{"OnDeath", "Shock"},
|
||||||
|
{"OnRoundStart", "Vibrate"},
|
||||||
|
{"OnRoundEnd", "Nothing"},
|
||||||
|
{"OnRoundWin", "Beep"},
|
||||||
|
{"OnRoundLoss", "Nothing"}
|
||||||
|
};
|
||||||
|
|
||||||
|
public Settings()
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Shocker.ControlAction StringToAction(string str)
|
||||||
|
{
|
||||||
|
return str.ToLower() switch
|
||||||
|
{
|
||||||
|
"shock" => Shocker.ControlAction.Shock,
|
||||||
|
"vibrate" => Shocker.ControlAction.Vibrate,
|
||||||
|
"beep" => Shocker.ControlAction.Beep,
|
||||||
|
_ => Shocker.ControlAction.Nothing
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct OpenShockSettings
|
||||||
|
{
|
||||||
|
public string Endpoint, ApiKey;
|
||||||
|
public string[] Shockers;
|
||||||
|
}
|
||||||
|
|
||||||
|
public struct Range
|
||||||
|
{
|
||||||
|
public short Min, Max;
|
||||||
|
}
|
@ -2,13 +2,22 @@
|
|||||||
|
|
||||||
public abstract class Shocker
|
public abstract class Shocker
|
||||||
{
|
{
|
||||||
public string ApiKey, Endpoint;
|
protected readonly HttpClient HttpClient;
|
||||||
public enum ControlAction { Beep, Vibrate, Shock }
|
protected readonly string ApiKey,Endpoint;
|
||||||
public abstract void Control(ControlAction action, byte intensity, short duration);
|
protected readonly string[] ShockerIds;
|
||||||
|
protected readonly ConfiguredInteger Intensity, Duration;
|
||||||
|
|
||||||
public Shocker(string endpoint, string apiKey)
|
public enum ControlAction { Beep, Vibrate, Shock, Nothing }
|
||||||
|
|
||||||
|
public abstract void Control(ControlAction action, string? shockerId = null);
|
||||||
|
|
||||||
|
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.ShockerIds = shockerIds;
|
||||||
|
this.Intensity = intensity;
|
||||||
|
this.Duration = duration;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user