Compare commits

..

No commits in common. "671fdc53141cbe16a72a41a643605f7ed38fd28d" and "6eb1c2c25a82ea9388e9c44c0bf0d5ca184bafe7" have entirely different histories.

6 changed files with 41 additions and 52 deletions

View File

@ -15,28 +15,30 @@ public class CS2MessageHandler
public void HandleCS2Message(string message)
{
JObject messageJson = JObject.Parse(message);
JToken? previously = messageJson.GetValue("previously");
RoundState currentRoundState = ParseRoundStateFromString(messageJson.SelectToken("round.phase", false)?.Value<string>());
RoundState previousRoundState = ParseRoundStateFromString(messageJson.SelectToken("previously.round.phase", false)?.Value<string>());
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.SelectToken("player.team", false)?.Value<string>());
Team winnerTeam = ParseTeamFromString(messageJson.SelectToken("round.win_team", false)?.Value<string>());
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 = messageJson.SelectToken("previously.player.match_stats.deaths", false)?.Value<int>();
int? currentDeaths = messageJson.SelectToken("player.match_stats.deaths", false)?.Value<int>();
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 = messageJson.SelectToken("previously.player.match_stats.kills", false)?.Value<int>();
int? currentKills = messageJson.SelectToken("player.match_stats.kills", false)?.Value<int>();
int? previousKills = previously?["player"]?["match_stats"]?.Value<int>("kills");
int? currentKills = messageJson["player"]?["match_stats"]?.Value<int>("kills");
if(currentKills > previousKills)
OnKill?.Invoke();
}

View File

@ -33,11 +33,12 @@ public class GSIServer
Console.WriteLine($"[{request.HttpMethod}] {request.Url} - {request.UserAgent}");
HttpResponseMessage responseMessage = new (HttpStatusCode.Accepted);
HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.Accepted);
context.Response.OutputStream.Write(Encoding.UTF8.GetBytes(responseMessage.ToString()));
StreamReader reader = new (request.InputStream, request.ContentEncoding);
StreamReader reader = new StreamReader(request.InputStream, request.ContentEncoding);
string content = await reader.ReadToEndAsync();
Console.WriteLine(content);
OnMessage?.Invoke(content);
}
HttpListener.Close();

View File

@ -11,11 +11,9 @@ public class OpenCS2hock
{
_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;
@ -61,9 +59,9 @@ public class OpenCS2hock
private void OnGSIMessage(string content)
{
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, $"{DateTime.Now.ToLongTimeString().Replace(':','.')}.json");
File.WriteAllText(fileName, content);
Console.WriteLine(fileName);
_cs2MessageHandler.HandleCS2Message(content);
}
}

View File

@ -1,29 +1,36 @@
using System.Net.Http.Headers;
using System.Text;
namespace OpenCS2hock;
public class OpenShock : Shocker
{
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
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") }
Accept = { new MediaTypeWithQualityHeaderValue("application/json") },
Authorization = new AuthenticationHeaderValue("Basic", ApiKey)
},
Content = new StringContent(@"[ { "+
$"\"id\": \"{shockerId}\"," +
$"\"type\": {ControlActionToByte(action)},"+
$"\"intensity\": {intensity},"+
$"\"duration\": {duration}"+
"}]", Encoding.UTF8, new MediaTypeHeaderValue("application/json"))
$"\"intensity\": {Intensity.GetValue()},"+
$"\"duration\": {Duration.GetValue()}"+
"}]")
};
request.Headers.Add("OpenShockToken", ApiKey);
HttpResponseMessage response = this.HttpClient.Send(request);
Console.WriteLine(response.StatusCode);
this.HttpClient.Send(request);
}
private byte ControlActionToByte(ControlAction action)

View File

@ -1,6 +1,4 @@
using Newtonsoft.Json;
namespace OpenCS2hock;
namespace OpenCS2hock;
public struct Settings
{
@ -14,13 +12,13 @@ public struct Settings
public Range IntensityRange = new ()
{
Min = 0,
Max = 50
Max = 100
};
public Range DurationRange = new()
{
Min = 1000,
Max = 1000
Max = 2000
};
public Dictionary<string, string> Actions = new()
@ -37,12 +35,7 @@ public struct Settings
{
}
public override string ToString()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}
public static Shocker.ControlAction StringToAction(string str)
{
return str.ToLower() switch

View File

@ -4,32 +4,20 @@ public abstract class Shocker
{
protected readonly HttpClient HttpClient;
protected readonly string ApiKey,Endpoint;
private readonly string[] _shockerIds;
private readonly ConfiguredInteger _intensity, _duration;
protected readonly string[] ShockerIds;
protected readonly ConfiguredInteger Intensity, Duration;
public enum ControlAction { Beep, Vibrate, Shock, Nothing }
public void Control(ControlAction action, string? shockerId = null)
{
int intensity = _intensity.GetValue();
int duration = _duration.GetValue();
Console.WriteLine($"{action} {intensity} {duration}");
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);
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.ApiKey = apiKey;
this.HttpClient = new HttpClient();
this._shockerIds = shockerIds;
this._intensity = intensity;
this._duration = duration;
this.ShockerIds = shockerIds;
this.Intensity = intensity;
this.Duration = duration;
}
}