Compare commits
10 Commits
6eb1c2c25a
...
671fdc5314
Author | SHA1 | Date | |
---|---|---|---|
671fdc5314 | |||
66f234e19a | |||
0303efac16 | |||
47b721d419 | |||
d102c970ec | |||
850d9c842b | |||
ceb7fb087c | |||
bc43aba60e | |||
c418bb0460 | |||
bd41858a17 |
@ -16,29 +16,27 @@ public class CS2MessageHandler
|
|||||||
{
|
{
|
||||||
JObject messageJson = JObject.Parse(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)
|
if(previousRoundState == RoundState.FreezeTime && 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();
|
||||||
|
|
||||||
Team playerTeam = ParseTeamFromString(messageJson["player"]?.Value<string>("team"));
|
Team playerTeam = ParseTeamFromString(messageJson.SelectToken("player.team", false)?.Value<string>());
|
||||||
Team winnerTeam = ParseTeamFromString(messageJson["round"]?.Value<string>("win_team"));
|
Team winnerTeam = ParseTeamFromString(messageJson.SelectToken("round.win_team", false)?.Value<string>());
|
||||||
if(winnerTeam != Team.None && playerTeam == winnerTeam)
|
if(winnerTeam != Team.None && playerTeam == winnerTeam)
|
||||||
OnRoundWin?.Invoke();
|
OnRoundWin?.Invoke();
|
||||||
else if(winnerTeam != Team.None && playerTeam != winnerTeam)
|
else if(winnerTeam != Team.None && playerTeam != winnerTeam)
|
||||||
OnRoundLoss?.Invoke();
|
OnRoundLoss?.Invoke();
|
||||||
|
|
||||||
int? previousDeaths = previously?["player"]?["match_stats"]?.Value<int>("deaths");
|
int? previousDeaths = messageJson.SelectToken("previously.player.match_stats.deaths", false)?.Value<int>();
|
||||||
int? currentDeaths = messageJson["player"]?["match_stats"]?.Value<int>("deaths");
|
int? currentDeaths = messageJson.SelectToken("player.match_stats.deaths", false)?.Value<int>();
|
||||||
if(currentDeaths > previousDeaths)
|
if(currentDeaths > previousDeaths)
|
||||||
OnDeath?.Invoke();
|
OnDeath?.Invoke();
|
||||||
|
|
||||||
int? previousKills = previously?["player"]?["match_stats"]?.Value<int>("kills");
|
int? previousKills = messageJson.SelectToken("previously.player.match_stats.kills", false)?.Value<int>();
|
||||||
int? currentKills = messageJson["player"]?["match_stats"]?.Value<int>("kills");
|
int? currentKills = messageJson.SelectToken("player.match_stats.kills", false)?.Value<int>();
|
||||||
if(currentKills > previousKills)
|
if(currentKills > previousKills)
|
||||||
OnKill?.Invoke();
|
OnKill?.Invoke();
|
||||||
}
|
}
|
||||||
|
@ -33,12 +33,11 @@ public class GSIServer
|
|||||||
|
|
||||||
Console.WriteLine($"[{request.HttpMethod}] {request.Url} - {request.UserAgent}");
|
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()));
|
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();
|
string content = await reader.ReadToEndAsync();
|
||||||
Console.WriteLine(content);
|
|
||||||
OnMessage?.Invoke(content);
|
OnMessage?.Invoke(content);
|
||||||
}
|
}
|
||||||
HttpListener.Close();
|
HttpListener.Close();
|
||||||
|
@ -11,9 +11,11 @@ public class OpenCS2hock
|
|||||||
{
|
{
|
||||||
_settings = Installer.GetSettings(settingsPath);
|
_settings = Installer.GetSettings(settingsPath);
|
||||||
this._shockers = Installer.GetShockers(_settings);
|
this._shockers = Installer.GetShockers(_settings);
|
||||||
|
Console.WriteLine(_settings);
|
||||||
Installer.InstallGsi();
|
Installer.InstallGsi();
|
||||||
|
|
||||||
this._cs2MessageHandler = new CS2MessageHandler();
|
this._cs2MessageHandler = new CS2MessageHandler();
|
||||||
|
this.SetupEventHandlers();
|
||||||
|
|
||||||
this.GSIServer = new GSIServer(3000);
|
this.GSIServer = new GSIServer(3000);
|
||||||
this.GSIServer.OnMessage += OnGSIMessage;
|
this.GSIServer.OnMessage += OnGSIMessage;
|
||||||
@ -59,9 +61,9 @@ public class OpenCS2hock
|
|||||||
|
|
||||||
private void OnGSIMessage(string content)
|
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);
|
File.WriteAllText(fileName, content);
|
||||||
Console.WriteLine(fileName);
|
|
||||||
_cs2MessageHandler.HandleCS2Message(content);
|
_cs2MessageHandler.HandleCS2Message(content);
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,36 +1,29 @@
|
|||||||
using System.Net.Http.Headers;
|
using System.Net.Http.Headers;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
namespace OpenCS2hock;
|
namespace OpenCS2hock;
|
||||||
|
|
||||||
public class OpenShock : Shocker
|
public class OpenShock : Shocker
|
||||||
{
|
{
|
||||||
public override void Control(ControlAction action, string? shockerId = null)
|
protected override void ControlInternal(ControlAction action, string shockerId, int intensity, int duration)
|
||||||
{
|
|
||||||
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")
|
HttpRequestMessage request = new (HttpMethod.Post, $"{Endpoint}/1/shockers/control")
|
||||||
{
|
{
|
||||||
Headers =
|
Headers =
|
||||||
{
|
{
|
||||||
UserAgent = { new ProductInfoHeaderValue("OpenCS2hock", "1") },
|
UserAgent = { new ProductInfoHeaderValue("OpenCS2hock", "1") },
|
||||||
Accept = { new MediaTypeWithQualityHeaderValue("application/json") },
|
Accept = { new MediaTypeWithQualityHeaderValue("application/json") }
|
||||||
Authorization = new AuthenticationHeaderValue("Basic", ApiKey)
|
|
||||||
},
|
},
|
||||||
Content = new StringContent(@"[ { "+
|
Content = new StringContent(@"[ { "+
|
||||||
$"\"id\": \"{shockerId}\"," +
|
$"\"id\": \"{shockerId}\"," +
|
||||||
$"\"type\": {ControlActionToByte(action)},"+
|
$"\"type\": {ControlActionToByte(action)},"+
|
||||||
$"\"intensity\": {Intensity.GetValue()},"+
|
$"\"intensity\": {intensity},"+
|
||||||
$"\"duration\": {Duration.GetValue()}"+
|
$"\"duration\": {duration}"+
|
||||||
"}]")
|
"}]", Encoding.UTF8, new MediaTypeHeaderValue("application/json"))
|
||||||
};
|
};
|
||||||
this.HttpClient.Send(request);
|
request.Headers.Add("OpenShockToken", ApiKey);
|
||||||
|
HttpResponseMessage response = this.HttpClient.Send(request);
|
||||||
|
Console.WriteLine(response.StatusCode);
|
||||||
}
|
}
|
||||||
|
|
||||||
private byte ControlActionToByte(ControlAction action)
|
private byte ControlActionToByte(ControlAction action)
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
namespace OpenCS2hock;
|
using Newtonsoft.Json;
|
||||||
|
|
||||||
|
namespace OpenCS2hock;
|
||||||
|
|
||||||
public struct Settings
|
public struct Settings
|
||||||
{
|
{
|
||||||
@ -12,13 +14,13 @@ public struct Settings
|
|||||||
public Range IntensityRange = new ()
|
public Range IntensityRange = new ()
|
||||||
{
|
{
|
||||||
Min = 0,
|
Min = 0,
|
||||||
Max = 100
|
Max = 50
|
||||||
};
|
};
|
||||||
|
|
||||||
public Range DurationRange = new()
|
public Range DurationRange = new()
|
||||||
{
|
{
|
||||||
Min = 1000,
|
Min = 1000,
|
||||||
Max = 2000
|
Max = 1000
|
||||||
};
|
};
|
||||||
|
|
||||||
public Dictionary<string, string> Actions = new()
|
public Dictionary<string, string> Actions = new()
|
||||||
@ -36,6 +38,11 @@ public struct Settings
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public override string ToString()
|
||||||
|
{
|
||||||
|
return JsonConvert.SerializeObject(this, Formatting.Indented);
|
||||||
|
}
|
||||||
|
|
||||||
public static Shocker.ControlAction StringToAction(string str)
|
public static Shocker.ControlAction StringToAction(string str)
|
||||||
{
|
{
|
||||||
return str.ToLower() switch
|
return str.ToLower() switch
|
||||||
|
@ -4,20 +4,32 @@ public abstract class Shocker
|
|||||||
{
|
{
|
||||||
protected readonly HttpClient HttpClient;
|
protected readonly HttpClient HttpClient;
|
||||||
protected readonly string ApiKey,Endpoint;
|
protected readonly string ApiKey,Endpoint;
|
||||||
protected readonly string[] ShockerIds;
|
private readonly string[] _shockerIds;
|
||||||
protected readonly ConfiguredInteger Intensity, Duration;
|
private readonly ConfiguredInteger _intensity, _duration;
|
||||||
|
|
||||||
public enum ControlAction { Beep, Vibrate, Shock, Nothing }
|
public enum ControlAction { Beep, Vibrate, Shock, Nothing }
|
||||||
|
|
||||||
public abstract void Control(ControlAction action, string? shockerId = null);
|
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);
|
||||||
|
|
||||||
protected Shocker(string endpoint, string apiKey, string[] shockerIds, ConfiguredInteger intensity, ConfiguredInteger duration)
|
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.HttpClient = new HttpClient();
|
||||||
this.ShockerIds = shockerIds;
|
this._shockerIds = shockerIds;
|
||||||
this.Intensity = intensity;
|
this._intensity = intensity;
|
||||||
this.Duration = duration;
|
this._duration = duration;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user