Compare commits
5 Commits
45eea0c7c5
...
8cca25266a
Author | SHA1 | Date | |
---|---|---|---|
8cca25266a | |||
54c82c93e2 | |||
8526c6b00b | |||
f77f5bc3b4 | |||
5824e24748 |
@ -2,26 +2,34 @@
|
|||||||
|
|
||||||
namespace OpenCS2hock;
|
namespace OpenCS2hock;
|
||||||
|
|
||||||
public class CS2MessageHandler
|
internal class CS2MessageHandler
|
||||||
{
|
{
|
||||||
public delegate void CS2EventHandler();
|
internal delegate void CS2EventHandler();
|
||||||
public event CS2EventHandler? OnKill;
|
internal event CS2EventHandler? OnKill;
|
||||||
public event CS2EventHandler? OnDeath;
|
internal event CS2EventHandler? OnDeath;
|
||||||
public event CS2EventHandler? OnRoundStart;
|
internal event CS2EventHandler? OnRoundStart;
|
||||||
public event CS2EventHandler? OnRoundEnd;
|
internal event CS2EventHandler? OnRoundEnd;
|
||||||
public event CS2EventHandler? OnRoundWin;
|
internal event CS2EventHandler? OnRoundWin;
|
||||||
public event CS2EventHandler? OnRoundLoss;
|
internal event CS2EventHandler? OnRoundLoss;
|
||||||
|
|
||||||
public void HandleCS2Message(string message)
|
internal void HandleCS2Message(string message, string mySteamId)
|
||||||
{
|
{
|
||||||
JObject messageJson = JObject.Parse(message);
|
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 currentRoundState = ParseRoundStateFromString(messageJson.SelectToken("round.phase", false)?.Value<string>());
|
||||||
RoundState previousRoundState = ParseRoundStateFromString(messageJson.SelectToken("previously.round.phase", false)?.Value<string>());
|
RoundState previousRoundState = ParseRoundStateFromString(messageJson.SelectToken("previously.round.phase", false)?.Value<string>());
|
||||||
if(previousRoundState == RoundState.FreezeTime && currentRoundState == RoundState.Live)
|
if(previousRoundState == RoundState.Over && currentRoundState == RoundState.Live)
|
||||||
OnRoundStart?.Invoke();
|
OnRoundStart?.Invoke();
|
||||||
if(previousRoundState == RoundState.Live && currentRoundState == RoundState.FreezeTime)
|
if(previousRoundState == RoundState.Live && currentRoundState == RoundState.FreezeTime)
|
||||||
OnRoundEnd?.Invoke();
|
OnRoundEnd?.Invoke();
|
||||||
|
if(previousRoundState == RoundState.Live && currentRoundState == RoundState.Over)
|
||||||
|
OnRoundEnd?.Invoke();
|
||||||
|
|
||||||
Team playerTeam = ParseTeamFromString(messageJson.SelectToken("player.team", false)?.Value<string>());
|
Team playerTeam = ParseTeamFromString(messageJson.SelectToken("player.team", false)?.Value<string>());
|
||||||
Team winnerTeam = ParseTeamFromString(messageJson.SelectToken("round.win_team", false)?.Value<string>());
|
Team winnerTeam = ParseTeamFromString(messageJson.SelectToken("round.win_team", false)?.Value<string>());
|
||||||
@ -47,6 +55,7 @@ public class CS2MessageHandler
|
|||||||
{
|
{
|
||||||
"live" => RoundState.Live,
|
"live" => RoundState.Live,
|
||||||
"freezetime" => RoundState.FreezeTime,
|
"freezetime" => RoundState.FreezeTime,
|
||||||
|
"over" => RoundState.Over,
|
||||||
_ => RoundState.Unknown
|
_ => RoundState.Unknown
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@ -61,7 +70,7 @@ public class CS2MessageHandler
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
private enum RoundState {FreezeTime, Live, Unknown}
|
private enum RoundState {FreezeTime, Live, Over, Unknown}
|
||||||
|
|
||||||
private enum Team {T, CT, None}
|
private enum Team {T, CT, None}
|
||||||
}
|
}
|
@ -1,16 +1,16 @@
|
|||||||
namespace OpenCS2hock;
|
namespace OpenCS2hock;
|
||||||
|
|
||||||
public class ConfiguredInteger
|
internal class ConfiguredInteger
|
||||||
{
|
{
|
||||||
private readonly int _min, _max;
|
private readonly int _min, _max;
|
||||||
|
|
||||||
public ConfiguredInteger(int min = 0, int max = 50)
|
internal ConfiguredInteger(int min = 0, int max = 50)
|
||||||
{
|
{
|
||||||
this._min = min;
|
this._min = min;
|
||||||
this._max = max;
|
this._max = max;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int GetValue()
|
internal int GetValue()
|
||||||
{
|
{
|
||||||
return Random.Shared.Next(_min, _max);
|
return Random.Shared.Next(_min, _max);
|
||||||
}
|
}
|
||||||
|
@ -3,16 +3,16 @@ using System.Text;
|
|||||||
|
|
||||||
namespace OpenCS2hock;
|
namespace OpenCS2hock;
|
||||||
|
|
||||||
public class GSIServer
|
internal class GSIServer
|
||||||
{
|
{
|
||||||
private HttpListener HttpListener { get; init; }
|
private HttpListener HttpListener { get; init; }
|
||||||
public delegate void OnMessageEventHandler(string content);
|
internal delegate void OnMessageEventHandler(string content);
|
||||||
public event OnMessageEventHandler? OnMessage;
|
internal event OnMessageEventHandler? OnMessage;
|
||||||
|
|
||||||
private bool _keepRunning = true;
|
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 = new HttpListener();
|
||||||
HttpListener.Prefixes.Add($"http://127.0.0.1:{port}/");
|
HttpListener.Prefixes.Add($"http://127.0.0.1:{port}/");
|
||||||
|
@ -5,7 +5,7 @@ namespace OpenCS2hock;
|
|||||||
|
|
||||||
public static class Installer
|
public static class Installer
|
||||||
{
|
{
|
||||||
public static Settings GetSettings(string? path = null)
|
internal static Settings GetSettings(string? path = null)
|
||||||
{
|
{
|
||||||
string settingsFilePath = path ?? "config.json";
|
string settingsFilePath = path ?? "config.json";
|
||||||
if (!File.Exists(settingsFilePath))
|
if (!File.Exists(settingsFilePath))
|
||||||
@ -14,7 +14,7 @@ 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)
|
internal static List<Shocker> GetShockers(Settings settings)
|
||||||
{
|
{
|
||||||
List<Shocker> shockers = new();
|
List<Shocker> shockers = new();
|
||||||
shockers.Add(new OpenShock(settings.OpenShockSettings.Endpoint, settings.OpenShockSettings.ApiKey,
|
shockers.Add(new OpenShock(settings.OpenShockSettings.Endpoint, settings.OpenShockSettings.ApiKey,
|
||||||
@ -24,13 +24,13 @@ public static class Installer
|
|||||||
return shockers;
|
return shockers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void InstallGsi()
|
internal 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");
|
||||||
File.WriteAllText(installLocation, Resources.GSI_CFG_Content);
|
File.WriteAllText(installLocation, Resources.GSI_CFG_Content);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static string GetInstallDirectory(int appId = 730)
|
private static string GetInstallDirectory(int appId = 730)
|
||||||
{
|
{
|
||||||
string steamInstallation =
|
string steamInstallation =
|
||||||
#pragma warning disable CA1416 //Registry only available on Windows
|
#pragma warning disable CA1416 //Registry only available on Windows
|
||||||
|
@ -64,6 +64,6 @@ public class OpenCS2hock
|
|||||||
Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, "CS2Events"));
|
Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, "CS2Events"));
|
||||||
string fileName = Path.Combine(Environment.CurrentDirectory, "CS2Events" ,$"{DateTime.Now.ToLongTimeString().Replace(':','.')}.json");
|
string fileName = Path.Combine(Environment.CurrentDirectory, "CS2Events" ,$"{DateTime.Now.ToLongTimeString().Replace(':','.')}.json");
|
||||||
File.WriteAllText(fileName, content);
|
File.WriteAllText(fileName, content);
|
||||||
_cs2MessageHandler.HandleCS2Message(content);
|
_cs2MessageHandler.HandleCS2Message(content, _settings.SteamId);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,7 +3,7 @@ using System.Text;
|
|||||||
|
|
||||||
namespace OpenCS2hock;
|
namespace OpenCS2hock;
|
||||||
|
|
||||||
public class OpenShock : Shocker
|
internal class OpenShock : Shocker
|
||||||
{
|
{
|
||||||
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
|
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
|
||||||
{
|
{
|
||||||
@ -23,7 +23,7 @@ public class OpenShock : Shocker
|
|||||||
};
|
};
|
||||||
request.Headers.Add("OpenShockToken", ApiKey);
|
request.Headers.Add("OpenShockToken", ApiKey);
|
||||||
HttpResponseMessage response = this.HttpClient.Send(request);
|
HttpResponseMessage response = this.HttpClient.Send(request);
|
||||||
Console.WriteLine(response.StatusCode);
|
Console.WriteLine($"{request.RequestUri} response: {response.StatusCode}");
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte ControlActionToByte(ControlAction action)
|
private byte ControlActionToByte(ControlAction action)
|
||||||
@ -37,7 +37,7 @@ public class OpenShock : Shocker
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public OpenShock(string endpoint, string apiKey, string[] shockerIds, ConfiguredInteger intensity, ConfiguredInteger duration) : base(endpoint, apiKey, shockerIds, intensity, duration)
|
internal OpenShock(string endpoint, string apiKey, string[] shockerIds, ConfiguredInteger intensity, ConfiguredInteger duration) : base(endpoint, apiKey, shockerIds, intensity, duration)
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ namespace OpenCS2hock;
|
|||||||
|
|
||||||
public struct Settings
|
public struct Settings
|
||||||
{
|
{
|
||||||
|
public string SteamId = "";
|
||||||
public OpenShockSettings OpenShockSettings = new()
|
public OpenShockSettings OpenShockSettings = new()
|
||||||
{
|
{
|
||||||
Endpoint = "https://api.shocklink.net",
|
Endpoint = "https://api.shocklink.net",
|
||||||
@ -13,8 +14,8 @@ public struct Settings
|
|||||||
|
|
||||||
public Range IntensityRange = new ()
|
public Range IntensityRange = new ()
|
||||||
{
|
{
|
||||||
Min = 0,
|
Min = 20,
|
||||||
Max = 50
|
Max = 60
|
||||||
};
|
};
|
||||||
|
|
||||||
public Range DurationRange = new()
|
public Range DurationRange = new()
|
||||||
@ -43,7 +44,7 @@ public struct Settings
|
|||||||
return JsonConvert.SerializeObject(this, Formatting.Indented);
|
return JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Shocker.ControlAction StringToAction(string str)
|
internal static Shocker.ControlAction StringToAction(string str)
|
||||||
{
|
{
|
||||||
return str.ToLower() switch
|
return str.ToLower() switch
|
||||||
{
|
{
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
namespace OpenCS2hock;
|
namespace OpenCS2hock;
|
||||||
|
|
||||||
public abstract class Shocker
|
internal abstract class Shocker
|
||||||
{
|
{
|
||||||
protected readonly HttpClient HttpClient;
|
protected readonly HttpClient HttpClient;
|
||||||
protected readonly string ApiKey,Endpoint;
|
protected readonly string ApiKey,Endpoint;
|
||||||
private readonly string[] _shockerIds;
|
private readonly string[] _shockerIds;
|
||||||
private readonly ConfiguredInteger _intensity, _duration;
|
private readonly ConfiguredInteger _intensity, _duration;
|
||||||
|
|
||||||
public enum ControlAction { Beep, Vibrate, Shock, Nothing }
|
internal enum ControlAction { Beep, Vibrate, Shock, Nothing }
|
||||||
|
|
||||||
public void Control(ControlAction action, string? shockerId = null)
|
internal void Control(ControlAction action, string? shockerId = null)
|
||||||
{
|
{
|
||||||
int intensity = _intensity.GetValue();
|
int intensity = _intensity.GetValue();
|
||||||
int duration = _duration.GetValue();
|
int duration = _duration.GetValue();
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
"OpenCS2hock"
|
"OpenCS2hock"
|
||||||
{
|
{
|
||||||
"uri" "http://127.0.0.1:3000"
|
"uri" "http://127.0.0.1:3000"
|
||||||
"timeout" "5.0"
|
"timeout" "2.0"
|
||||||
"buffer" "0.1"
|
"buffer" "0.0"
|
||||||
"throttle" "0.5"
|
"throttle" "0.1"
|
||||||
"heartbeat" "60.0"
|
"heartbeat" "60.0"
|
||||||
"output"
|
"output"
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user