Compare commits

..

No commits in common. "8cca25266a413975d2314ebf72802422aeeeaa45" and "45eea0c7c50d4d4da4edfabb20dcd47573ed7263" have entirely different histories.

9 changed files with 37 additions and 47 deletions

View File

@ -2,34 +2,26 @@
namespace OpenCS2hock;
internal class CS2MessageHandler
public 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;
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;
internal void HandleCS2Message(string message, string mySteamId)
public void HandleCS2Message(string 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 previousRoundState = ParseRoundStateFromString(messageJson.SelectToken("previously.round.phase", false)?.Value<string>());
if(previousRoundState == RoundState.Over && currentRoundState == RoundState.Live)
if(previousRoundState == RoundState.FreezeTime && 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>());
@ -55,7 +47,6 @@ internal class CS2MessageHandler
{
"live" => RoundState.Live,
"freezetime" => RoundState.FreezeTime,
"over" => RoundState.Over,
_ => RoundState.Unknown
};
}
@ -70,7 +61,7 @@ internal class CS2MessageHandler
};
}
private enum RoundState {FreezeTime, Live, Over, Unknown}
private enum RoundState {FreezeTime, Live, Unknown}
private enum Team {T, CT, None}
}

View File

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

View File

@ -3,16 +3,16 @@ using System.Text;
namespace OpenCS2hock;
internal class GSIServer
public class GSIServer
{
private HttpListener HttpListener { get; init; }
internal delegate void OnMessageEventHandler(string content);
internal event OnMessageEventHandler? OnMessage;
public delegate void OnMessageEventHandler(string content);
public event OnMessageEventHandler? OnMessage;
private bool _keepRunning = true;
internal bool IsRunning { get; private set; }
public bool IsRunning { get; private set; }
internal GSIServer(int port)
public GSIServer(int port)
{
HttpListener = new HttpListener();
HttpListener.Prefixes.Add($"http://127.0.0.1:{port}/");

View File

@ -5,7 +5,7 @@ namespace OpenCS2hock;
public static class Installer
{
internal static Settings GetSettings(string? path = null)
public static Settings GetSettings(string? path = null)
{
string settingsFilePath = path ?? "config.json";
if (!File.Exists(settingsFilePath))
@ -14,7 +14,7 @@ public static class Installer
return JsonConvert.DeserializeObject<Settings>(File.ReadAllText(settingsFilePath));
}
internal static List<Shocker> GetShockers(Settings settings)
public static List<Shocker> GetShockers(Settings settings)
{
List<Shocker> shockers = new();
shockers.Add(new OpenShock(settings.OpenShockSettings.Endpoint, settings.OpenShockSettings.ApiKey,
@ -24,13 +24,13 @@ public static class Installer
return shockers;
}
internal static void InstallGsi()
public static void InstallGsi()
{
string installLocation = Path.Combine(GetInstallDirectory(), "game\\csgo\\cfg\\gamestate_integration_opencs2hock.cfg");
File.WriteAllText(installLocation, Resources.GSI_CFG_Content);
}
private static string GetInstallDirectory(int appId = 730)
public static string GetInstallDirectory(int appId = 730)
{
string steamInstallation =
#pragma warning disable CA1416 //Registry only available on Windows

View File

@ -64,6 +64,6 @@ public class OpenCS2hock
Directory.CreateDirectory(Path.Combine(Environment.CurrentDirectory, "CS2Events"));
string fileName = Path.Combine(Environment.CurrentDirectory, "CS2Events" ,$"{DateTime.Now.ToLongTimeString().Replace(':','.')}.json");
File.WriteAllText(fileName, content);
_cs2MessageHandler.HandleCS2Message(content, _settings.SteamId);
_cs2MessageHandler.HandleCS2Message(content);
}
}

View File

@ -3,7 +3,7 @@ using System.Text;
namespace OpenCS2hock;
internal class OpenShock : Shocker
public class OpenShock : Shocker
{
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
{
@ -23,7 +23,7 @@ internal class OpenShock : Shocker
};
request.Headers.Add("OpenShockToken", ApiKey);
HttpResponseMessage response = this.HttpClient.Send(request);
Console.WriteLine($"{request.RequestUri} response: {response.StatusCode}");
Console.WriteLine(response.StatusCode);
}
private byte ControlActionToByte(ControlAction action)
@ -37,7 +37,7 @@ internal class OpenShock : Shocker
};
}
internal OpenShock(string endpoint, string apiKey, string[] shockerIds, ConfiguredInteger intensity, ConfiguredInteger duration) : base(endpoint, apiKey, shockerIds, intensity, duration)
public OpenShock(string endpoint, string apiKey, string[] shockerIds, ConfiguredInteger intensity, ConfiguredInteger duration) : base(endpoint, apiKey, shockerIds, intensity, duration)
{
}

View File

@ -4,7 +4,6 @@ namespace OpenCS2hock;
public struct Settings
{
public string SteamId = "";
public OpenShockSettings OpenShockSettings = new()
{
Endpoint = "https://api.shocklink.net",
@ -14,8 +13,8 @@ public struct Settings
public Range IntensityRange = new ()
{
Min = 20,
Max = 60
Min = 0,
Max = 50
};
public Range DurationRange = new()
@ -44,7 +43,7 @@ public struct Settings
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
internal static Shocker.ControlAction StringToAction(string str)
public static Shocker.ControlAction StringToAction(string str)
{
return str.ToLower() switch
{

View File

@ -1,15 +1,15 @@
namespace OpenCS2hock;
internal abstract class Shocker
public abstract class Shocker
{
protected readonly HttpClient HttpClient;
protected readonly string ApiKey,Endpoint;
private readonly string[] _shockerIds;
private readonly ConfiguredInteger _intensity, _duration;
internal enum ControlAction { Beep, Vibrate, Shock, Nothing }
public enum ControlAction { Beep, Vibrate, Shock, Nothing }
internal void Control(ControlAction action, string? shockerId = null)
public void Control(ControlAction action, string? shockerId = null)
{
int intensity = _intensity.GetValue();
int duration = _duration.GetValue();

View File

@ -1,9 +1,9 @@
"OpenCS2hock"
{
"uri" "http://127.0.0.1:3000"
"timeout" "2.0"
"buffer" "0.0"
"throttle" "0.1"
"timeout" "5.0"
"buffer" "0.1"
"throttle" "0.5"
"heartbeat" "60.0"
"output"
{