Compare commits
22 Commits
3dabf95bb0
...
1.0
Author | SHA1 | Date | |
---|---|---|---|
8cca25266a | |||
54c82c93e2 | |||
8526c6b00b | |||
f77f5bc3b4 | |||
5824e24748 | |||
45eea0c7c5 | |||
671fdc5314 | |||
66f234e19a | |||
0303efac16 | |||
47b721d419 | |||
d102c970ec | |||
850d9c842b | |||
ceb7fb087c | |||
bc43aba60e | |||
c418bb0460 | |||
bd41858a17 | |||
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/=GSI/@EntryIndexedValue">GSI</s:String>
|
||||
<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/=steamapps/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
|
76
OpenCS2hock/CS2MessageHandler.cs
Normal file
76
OpenCS2hock/CS2MessageHandler.cs
Normal file
@ -0,0 +1,76 @@
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace OpenCS2hock;
|
||||
|
||||
internal class CS2MessageHandler
|
||||
{
|
||||
internal delegate void CS2EventHandler();
|
||||
internal event CS2EventHandler? OnKill;
|
||||
internal event CS2EventHandler? OnDeath;
|
||||
internal event CS2EventHandler? OnRoundStart;
|
||||
internal event CS2EventHandler? OnRoundEnd;
|
||||
internal event CS2EventHandler? OnRoundWin;
|
||||
internal event CS2EventHandler? OnRoundLoss;
|
||||
|
||||
internal void HandleCS2Message(string message, string mySteamId)
|
||||
{
|
||||
JObject messageJson = JObject.Parse(message);
|
||||
string? steamId = messageJson.SelectToken("player.steamid", false)?.Value<string>();
|
||||
if (steamId is null || steamId != mySteamId)
|
||||
{
|
||||
Console.WriteLine("Not my steamid");
|
||||
return;
|
||||
}
|
||||
|
||||
RoundState currentRoundState = ParseRoundStateFromString(messageJson.SelectToken("round.phase", false)?.Value<string>());
|
||||
RoundState previousRoundState = ParseRoundStateFromString(messageJson.SelectToken("previously.round.phase", false)?.Value<string>());
|
||||
if(previousRoundState == RoundState.Over && currentRoundState == RoundState.Live)
|
||||
OnRoundStart?.Invoke();
|
||||
if(previousRoundState == RoundState.Live && currentRoundState == RoundState.FreezeTime)
|
||||
OnRoundEnd?.Invoke();
|
||||
if(previousRoundState == RoundState.Live && currentRoundState == RoundState.Over)
|
||||
OnRoundEnd?.Invoke();
|
||||
|
||||
Team playerTeam = ParseTeamFromString(messageJson.SelectToken("player.team", false)?.Value<string>());
|
||||
Team winnerTeam = ParseTeamFromString(messageJson.SelectToken("round.win_team", false)?.Value<string>());
|
||||
if(winnerTeam != Team.None && playerTeam == winnerTeam)
|
||||
OnRoundWin?.Invoke();
|
||||
else if(winnerTeam != Team.None && playerTeam != winnerTeam)
|
||||
OnRoundLoss?.Invoke();
|
||||
|
||||
int? previousDeaths = messageJson.SelectToken("previously.player.match_stats.deaths", false)?.Value<int>();
|
||||
int? currentDeaths = messageJson.SelectToken("player.match_stats.deaths", false)?.Value<int>();
|
||||
if(currentDeaths > previousDeaths)
|
||||
OnDeath?.Invoke();
|
||||
|
||||
int? previousKills = messageJson.SelectToken("previously.player.match_stats.kills", false)?.Value<int>();
|
||||
int? currentKills = messageJson.SelectToken("player.match_stats.kills", false)?.Value<int>();
|
||||
if(currentKills > previousKills)
|
||||
OnKill?.Invoke();
|
||||
}
|
||||
|
||||
private RoundState ParseRoundStateFromString(string? str)
|
||||
{
|
||||
return str switch
|
||||
{
|
||||
"live" => RoundState.Live,
|
||||
"freezetime" => RoundState.FreezeTime,
|
||||
"over" => RoundState.Over,
|
||||
_ => RoundState.Unknown
|
||||
};
|
||||
}
|
||||
|
||||
private Team ParseTeamFromString(string? str)
|
||||
{
|
||||
return str switch
|
||||
{
|
||||
"T" => Team.T,
|
||||
"CT" => Team.CT,
|
||||
_ => Team.None
|
||||
};
|
||||
}
|
||||
|
||||
private enum RoundState {FreezeTime, Live, Over, 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;
|
||||
|
||||
internal class ConfiguredInteger
|
||||
{
|
||||
private readonly int _min, _max;
|
||||
|
||||
internal ConfiguredInteger(int min = 0, int max = 50)
|
||||
{
|
||||
this._min = min;
|
||||
this._max = max;
|
||||
}
|
||||
|
||||
internal int GetValue()
|
||||
{
|
||||
return Random.Shared.Next(_min, _max);
|
||||
}
|
||||
}
|
@ -3,16 +3,16 @@ using System.Text;
|
||||
|
||||
namespace OpenCS2hock;
|
||||
|
||||
public class GSIServer
|
||||
internal class GSIServer
|
||||
{
|
||||
private HttpListener HttpListener { get; init; }
|
||||
public delegate void OnMessageEventHandler(string content);
|
||||
public event OnMessageEventHandler? OnMessage;
|
||||
internal delegate void OnMessageEventHandler(string content);
|
||||
internal event OnMessageEventHandler? OnMessage;
|
||||
|
||||
private bool _keepRunning = true;
|
||||
public bool IsRunning { get; private set; }
|
||||
internal bool IsRunning { get; private set; }
|
||||
|
||||
public GSIServer(int port)
|
||||
internal GSIServer(int port)
|
||||
{
|
||||
HttpListener = new HttpListener();
|
||||
HttpListener.Prefixes.Add($"http://127.0.0.1:{port}/");
|
||||
@ -20,6 +20,8 @@ public class GSIServer
|
||||
|
||||
Thread connectionListener = new (HandleConnection);
|
||||
connectionListener.Start();
|
||||
|
||||
IsRunning = true;
|
||||
}
|
||||
|
||||
private async void HandleConnection()
|
||||
@ -31,12 +33,11 @@ public class GSIServer
|
||||
|
||||
Console.WriteLine($"[{request.HttpMethod}] {request.Url} - {request.UserAgent}");
|
||||
|
||||
HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.Accepted);
|
||||
HttpResponseMessage responseMessage = new (HttpStatusCode.Accepted);
|
||||
context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(responseMessage.ToString()));
|
||||
|
||||
StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding);
|
||||
StreamReader reader = new (request.InputStream, request.ContentEncoding);
|
||||
string content = await reader.ReadToEndAsync();
|
||||
Console.WriteLine(content);
|
||||
OnMessage?.Invoke(content);
|
||||
}
|
||||
HttpListener.Close();
|
||||
|
@ -1,16 +1,36 @@
|
||||
using Microsoft.Win32;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace OpenCS2hock;
|
||||
|
||||
public static class Installer
|
||||
{
|
||||
public static void InstallGsi()
|
||||
internal 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));
|
||||
}
|
||||
|
||||
internal 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;
|
||||
}
|
||||
|
||||
internal static void InstallGsi()
|
||||
{
|
||||
string installLocation = Path.Combine(GetInstallDirectory(), "game\\csgo\\cfg\\gamestate_integration_opencs2hock.cfg");
|
||||
File.WriteAllText(installLocation, Resources.GSI_CFG_Content);
|
||||
}
|
||||
|
||||
public static string GetInstallDirectory(int appId = 730)
|
||||
private static string GetInstallDirectory(int appId = 730)
|
||||
{
|
||||
string steamInstallation =
|
||||
#pragma warning disable CA1416 //Registry only available on Windows
|
||||
|
@ -3,11 +3,20 @@
|
||||
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()
|
||||
public OpenCS2hock(string? settingsPath = null)
|
||||
{
|
||||
_settings = Installer.GetSettings(settingsPath);
|
||||
this._shockers = Installer.GetShockers(_settings);
|
||||
Console.WriteLine(_settings);
|
||||
Installer.InstallGsi();
|
||||
|
||||
this._cs2MessageHandler = new CS2MessageHandler();
|
||||
this.SetupEventHandlers();
|
||||
|
||||
this.GSIServer = new GSIServer(3000);
|
||||
this.GSIServer.OnMessage += OnGSIMessage;
|
||||
|
||||
@ -19,10 +28,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");
|
||||
Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, "CS2Events"));
|
||||
string fileName = Path.Combine(Environment.CurrentDirectory, "CS2Events" ,$"{DateTime.Now.ToLongTimeString().Replace(':','.')}.json");
|
||||
File.WriteAllText(fileName, content);
|
||||
Console.WriteLine(fileName);
|
||||
_cs2MessageHandler.HandleCS2Message(content, _settings.SteamId);
|
||||
}
|
||||
}
|
@ -23,4 +23,8 @@
|
||||
</Compile>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
44
OpenCS2hock/OpenShock.cs
Normal file
44
OpenCS2hock/OpenShock.cs
Normal file
@ -0,0 +1,44 @@
|
||||
using System.Net.Http.Headers;
|
||||
using System.Text;
|
||||
|
||||
namespace OpenCS2hock;
|
||||
|
||||
internal class OpenShock : Shocker
|
||||
{
|
||||
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
|
||||
{
|
||||
HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}/1/shockers/control")
|
||||
{
|
||||
Headers =
|
||||
{
|
||||
UserAgent = { new ProductInfoHeaderValue("OpenCS2hock", "1") },
|
||||
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
|
||||
},
|
||||
Content = new StringContent(@"[ { "+
|
||||
$"\"id\": \"{shockerId}\"," +
|
||||
$"\"type\": {ControlActionToByte(action)},"+
|
||||
$"\"intensity\": {intensity},"+
|
||||
$"\"duration\": {duration}"+
|
||||
"}]", Encoding.UTF8, new MediaTypeHeaderValue("application/json"))
|
||||
};
|
||||
request.Headers.Add("OpenShockToken", ApiKey);
|
||||
HttpResponseMessage response = this.HttpClient.Send(request);
|
||||
Console.WriteLine($"{request.RequestUri} response: {response.StatusCode}");
|
||||
}
|
||||
|
||||
private byte ControlActionToByte(ControlAction action)
|
||||
{
|
||||
return action switch
|
||||
{
|
||||
ControlAction.Beep => 3,
|
||||
ControlAction.Vibrate => 2,
|
||||
ControlAction.Shock => 1,
|
||||
_ => 0
|
||||
};
|
||||
}
|
||||
|
||||
internal 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)
|
||||
{
|
||||
new OpenCS2hock();
|
||||
OpenCS2hock openCS2Hock = new OpenCS2hock();
|
||||
}
|
||||
}
|
68
OpenCS2hock/Settings.cs
Normal file
68
OpenCS2hock/Settings.cs
Normal file
@ -0,0 +1,68 @@
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace OpenCS2hock;
|
||||
|
||||
public struct Settings
|
||||
{
|
||||
public string SteamId = "";
|
||||
public OpenShockSettings OpenShockSettings = new()
|
||||
{
|
||||
Endpoint = "https://api.shocklink.net",
|
||||
ApiKey = "",
|
||||
Shockers = Array.Empty<string>()
|
||||
};
|
||||
|
||||
public Range IntensityRange = new ()
|
||||
{
|
||||
Min = 20,
|
||||
Max = 60
|
||||
};
|
||||
|
||||
public Range DurationRange = new()
|
||||
{
|
||||
Min = 1000,
|
||||
Max = 1000
|
||||
};
|
||||
|
||||
public Dictionary<string, string> Actions = new()
|
||||
{
|
||||
{"OnKill", "Nothing"},
|
||||
{"OnDeath", "Shock"},
|
||||
{"OnRoundStart", "Vibrate"},
|
||||
{"OnRoundEnd", "Nothing"},
|
||||
{"OnRoundWin", "Beep"},
|
||||
{"OnRoundLoss", "Nothing"}
|
||||
};
|
||||
|
||||
public Settings()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||
}
|
||||
|
||||
internal 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;
|
||||
}
|
@ -1,14 +1,37 @@
|
||||
namespace OpenCS2hock;
|
||||
|
||||
public abstract class Shocker
|
||||
internal abstract class Shocker
|
||||
{
|
||||
public string ApiKey, Endpoint;
|
||||
public enum ControlAction { Beep, Vibrate, Shock }
|
||||
public abstract void Control(ControlAction action, byte intensity, short duration);
|
||||
protected readonly HttpClient HttpClient;
|
||||
protected readonly string ApiKey,Endpoint;
|
||||
private readonly string[] _shockerIds;
|
||||
private readonly ConfiguredInteger _intensity, _duration;
|
||||
|
||||
public Shocker(string endpoint, string apiKey)
|
||||
internal enum ControlAction { Beep, Vibrate, Shock, Nothing }
|
||||
|
||||
internal void Control(ControlAction action, string? shockerId = null)
|
||||
{
|
||||
int intensity = _intensity.GetValue();
|
||||
int duration = _duration.GetValue();
|
||||
Console.WriteLine($"{action} {intensity} {duration}");
|
||||
if (action is ControlAction.Nothing)
|
||||
return;
|
||||
if(shockerId is null)
|
||||
foreach (string shocker in _shockerIds)
|
||||
ControlInternal(action, shocker, intensity, duration);
|
||||
else
|
||||
ControlInternal(action, shockerId, intensity, duration);
|
||||
}
|
||||
|
||||
protected abstract void ControlInternal(ControlAction action, string shockerId, int intensity, int duration);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
@ -1,9 +1,9 @@
|
||||
"OpenCS2hock"
|
||||
{
|
||||
"uri" "http://127.0.0.1:3000"
|
||||
"timeout" "5.0"
|
||||
"buffer" "0.1"
|
||||
"throttle" "0.5"
|
||||
"timeout" "2.0"
|
||||
"buffer" "0.0"
|
||||
"throttle" "0.1"
|
||||
"heartbeat" "60.0"
|
||||
"output"
|
||||
{
|
||||
|
Reference in New Issue
Block a user