Compare commits

..

No commits in common. "1c161cde5567afa7fa3e69e11df2696d7214c71d" and "b5b65dbe456ed153c2fe46dd6a58d187f039187c" have entirely different histories.

12 changed files with 105 additions and 133 deletions

View File

@ -2,20 +2,5 @@
public class CS2EventArgs : EventArgs public class CS2EventArgs : EventArgs
{ {
public object? Value;
public CS2EventArgs(object? value = null)
{
this.Value = value;
}
public T? ValueAsOrDefault<T>()
{
return Value is T val ? val : default;
}
public override string ToString()
{
return Value?.ToString() ?? "NoArgs";
}
} }

View File

@ -12,12 +12,10 @@ internal static class CS2EventGenerator
events.AddRange(GenerateMapEvents(previousPlayerState, newGameState, lastGameState)); events.AddRange(GenerateMapEvents(previousPlayerState, newGameState, lastGameState));
if(lastGameState.Player?.SteamId != newGameState.Player?.SteamId) if(lastGameState.Player?.SteamId != newGameState.Player?.SteamId)
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnPlayerChange, new CS2EventArgs(newGameState.Player?.SteamId))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnPlayerChange, new CS2EventArgs()));
if(events.Count > 0) if(events.Count > 0)
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyEvent, new CS2EventArgs(events.Count))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyEvent, new CS2EventArgs()));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyMessage, new CS2EventArgs()));
return events; return events;
} }
@ -65,13 +63,13 @@ internal static class CS2EventGenerator
return events; return events;
if(newGameState.Player?.MatchStats?.Kills > previousPlayerState.Player?.MatchStats?.Kills && newGameState.Player is { MatchStats: not null}) if(newGameState.Player?.MatchStats?.Kills > previousPlayerState.Player?.MatchStats?.Kills && newGameState.Player is { MatchStats: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnKill, new CS2EventArgs(newGameState.Player?.MatchStats?.Kills))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnKill, new CS2EventArgs()));
if(newGameState.Player?.State?.RoundHs > previousPlayerState.Player?.State?.RoundHs && newGameState.Player is { State: not null}) if(newGameState.Player?.State?.RoundHs > previousPlayerState.Player?.State?.RoundHs && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHeadshot, new CS2EventArgs(newGameState.Player?.State?.RoundHs))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHeadshot, new CS2EventArgs()));
if(newGameState.Player?.MatchStats?.Deaths > previousPlayerState.Player?.MatchStats?.Deaths && newGameState.Player is { MatchStats: not null}) if(newGameState.Player?.MatchStats?.Deaths > previousPlayerState.Player?.MatchStats?.Deaths && newGameState.Player is { MatchStats: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDeath, new CS2EventArgs(newGameState.Player?.MatchStats?.Deaths))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDeath, new CS2EventArgs()));
if(newGameState.Player?.State?.Flashed > 0 && newGameState.Player is { State: not null}) if(newGameState.Player?.State?.Flashed > 0 && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnFlashed, new CS2EventArgs())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnFlashed, new CS2EventArgs()));
@ -83,25 +81,25 @@ internal static class CS2EventGenerator
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnSmoked, new CS2EventArgs())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnSmoked, new CS2EventArgs()));
if(newGameState.Player?.State?.Health < previousPlayerState.Player?.State?.Health && newGameState.Player is { State: not null}) if(newGameState.Player?.State?.Health < previousPlayerState.Player?.State?.Health && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDamageTaken, new CS2EventArgs(newGameState.Player?.State?.Health))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDamageTaken, new CS2EventArgs()));
if(newGameState.Player?.State?.Health != previousPlayerState.Player?.State?.Health && newGameState.Player is { State: not null}) if(newGameState.Player?.State?.Health != previousPlayerState.Player?.State?.Health && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHealthChange, new CS2EventArgs(newGameState.Player?.State?.Health))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHealthChange, new CS2EventArgs()));
if(newGameState.Player?.State?.Money != previousPlayerState.Player?.State?.Money && newGameState.Player is { State: not null}) if(newGameState.Player?.State?.Money != previousPlayerState.Player?.State?.Money && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnMoneyChange, new CS2EventArgs(newGameState.Player?.State?.Money))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnMoneyChange, new CS2EventArgs()));
if(newGameState.Player?.State?.Armor != previousPlayerState.Player?.State?.Armor && newGameState.Player is { State: not null}) if(newGameState.Player?.State?.Armor != previousPlayerState.Player?.State?.Armor && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnArmorChange, new CS2EventArgs(newGameState.Player?.State?.Armor))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnArmorChange, new CS2EventArgs()));
if(newGameState.Player?.State?.Helmet != previousPlayerState.Player?.State?.Helmet && newGameState.Player is { State: not null}) if(newGameState.Player?.State?.Helmet != previousPlayerState.Player?.State?.Helmet && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHelmetChange, new CS2EventArgs(newGameState.Player?.State?.Helmet))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHelmetChange, new CS2EventArgs()));
if(newGameState.Player?.State?.EquipmentValue != previousPlayerState.Player?.State?.EquipmentValue && newGameState.Player is { State: not null}) if(newGameState.Player?.State?.EquipmentValue != previousPlayerState.Player?.State?.EquipmentValue && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnEquipmentValueChange, new CS2EventArgs(newGameState.Player?.State?.EquipmentValue))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnEquipmentValueChange, new CS2EventArgs()));
if(newGameState.Player?.Team != previousPlayerState.Player?.Team && newGameState.Player is not null) if(newGameState.Player?.Team != previousPlayerState.Player?.Team && newGameState.Player is not null)
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnTeamChange, new CS2EventArgs(newGameState.Player?.Team))); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnTeamChange, new CS2EventArgs()));
return events; return events;

View File

@ -37,15 +37,17 @@ public class CS2GSI
JObject jsonObject = JObject.Parse(messageJson); JObject jsonObject = JObject.Parse(messageJson);
CS2GameState newState = CS2GameState.ParseFromJObject(jsonObject); CS2GameState newState = CS2GameState.ParseFromJObject(jsonObject);
this.logger?.Log(LogLevel.Debug, $"Received State:\n{newState.ToString()}"); this.logger?.Log(LogLevel.Debug, $"Received State:\n{newState.ToString()}");
if (_lastLocalGameState is not null && _allGameStates.Count > 0)
{
List<ValueTuple<CS2Event, CS2EventArgs>> generatedEvents = CS2EventGenerator.GenerateEvents(_lastLocalGameState.Value, newState, _allGameStates.Last());
this.logger?.Log(LogLevel.Information, $"Generated {generatedEvents.Count} events:\n\t{string.Join("\n\t", generatedEvents)}");
InvokeEvents(generatedEvents);
}
this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState); this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState);
this.logger?.Log(LogLevel.Debug, $"Updated Local State:\n{_lastLocalGameState.ToString()}"); this.logger?.Log(LogLevel.Debug, $"Updated Local State:\n{_lastLocalGameState.ToString()}");
if (_lastLocalGameState is not null)
{
List<ValueTuple<CS2Event, CS2EventArgs>> generatedEvents = CS2EventGenerator.GenerateEvents(_lastLocalGameState.Value, newState, _allGameStates.Last());
this.logger?.Log(LogLevel.Information, $"Generated {generatedEvents.Count} events.");
if(generatedEvents.Count > 0)
this.logger?.Log(LogLevel.Debug, $"Events:\n\t{string.Join("\n\t", generatedEvents)}");
InvokeEvents(generatedEvents);
}
_allGameStates.Add(newState); _allGameStates.Add(newState);
} }
@ -132,9 +134,6 @@ public class CS2GSI
case CS2Event.AnyEvent: case CS2Event.AnyEvent:
AnyEvent?.Invoke(cs2Event.Item2); AnyEvent?.Invoke(cs2Event.Item2);
break; break;
case CS2Event.AnyMessage:
AnyMessage?.Invoke(cs2Event.Item2);
break;
default: default:
this.logger?.Log(LogLevel.Error, $"Unknown Event {cs2Event}"); this.logger?.Log(LogLevel.Error, $"Unknown Event {cs2Event}");
return; return;
@ -165,8 +164,7 @@ public class CS2GSI
OnBombPlanted, OnBombPlanted,
OnBombDefused, OnBombDefused,
OnBombExploded, OnBombExploded,
AnyEvent, AnyEvent
AnyMessage
} }
public delegate void CS2EventHandler(CS2EventArgs eventArgs); public delegate void CS2EventHandler(CS2EventArgs eventArgs);
@ -194,7 +192,6 @@ public class CS2GSI
OnBombPlanted, OnBombPlanted,
OnBombDefused, OnBombDefused,
OnBombExploded, OnBombExploded,
AnyEvent, AnyEvent;
AnyMessage;
} }

View File

@ -44,8 +44,8 @@ internal class GSIServer
StreamReader reader = new (request.InputStream, request.ContentEncoding); StreamReader reader = new (request.InputStream, request.ContentEncoding);
string content = await reader.ReadToEndAsync(); string content = await reader.ReadToEndAsync();
this.logger?.Log(LogLevel.Debug, content);
OnMessage?.Invoke(content); OnMessage?.Invoke(content);
this.logger?.Log(LogLevel.Debug, content);
} }
HttpListener.Close(); HttpListener.Close();
IsRunning = false; IsRunning = false;

View File

@ -12,11 +12,11 @@ public struct CS2GameState
public override string ToString() public override string ToString()
{ {
return $"{GetType().Name}\n" + return $"{GetType()}\n" +
$"..Time: {Timestamp}\tProviderSteamId: {ProviderSteamId}\n" + $"\tTime: {Timestamp}\tSteamId: {ProviderSteamId}\n" +
$"..{Map.ToString()?.Replace("\n", "\n...")}\n" + $"\t{Map}\n" +
$"..{Round.ToString()?.Replace("\n", "\n...")}\n" + $"\t{Round}\n" +
$"..{Player.ToString()?.Replace("\n", "\n...")}\n"; $"\t{Player}\n";
} }
internal static CS2GameState ParseFromJObject(JObject jsonObject) internal static CS2GameState ParseFromJObject(JObject jsonObject)

View File

@ -9,11 +9,11 @@ public struct GameStateTeam
public override string ToString() public override string ToString()
{ {
return $"{GetType().Name}\n" + return $"{GetType()}\n" +
$"..Team {Team}\tScore: {Score}\n" + $"\tScore: {Score}\n" +
$"..ConsecutiveRoundLosses: {ConsecutiveRoundLosses}\n" + $"\tConsecutiveRoundLosses: {ConsecutiveRoundLosses}\n" +
$"..TimeoutsRemaining: {TimeoutsRemaining}\n" + $"\tTimeoutsRemaining: {TimeoutsRemaining}\n" +
$"..MatchesWonThisSeries: {MatchesWonThisSeries}\n"; $"\tMatchesWonThisSeries: {MatchesWonThisSeries}\n";
} }
internal static GameStateTeam ParseFromJObject(JObject jsonObject, CS2Team team) internal static GameStateTeam ParseFromJObject(JObject jsonObject, CS2Team team)

View File

@ -4,20 +4,18 @@ namespace CS2GSI.GameState;
public struct Map public struct Map
{ {
public string Mode, MapName; public string Mode, Name;
public MapPhase Phase; public MapPhase Phase;
public int Round, NumMatchesToWinSeries; public int Round, NumMatchesToWinSeries;
public GameStateTeam GameStateTeamCT, GameStateTeamT; public GameStateTeam GameStateTeamCT, GameStateTeamT;
public override string ToString() public override string ToString()
{ {
return $"{GetType().Name}\n" + return $"{GetType()}\n" +
$"..Mode: {Mode} Map: {MapName}\n" + $"\t{Mode} {Name} {Round} Matches to Win Series: {NumMatchesToWinSeries}\n" +
$"..Round: {Round}\n" + $"\t{Phase}\n" +
$"..Matches to Win Series: {NumMatchesToWinSeries}\n" + $"\t{GameStateTeamCT}\n" +
$"..Phase: {Phase}\n" + $"\t{GameStateTeamT}\n";
$"..{GameStateTeamCT.ToString().Replace("\n", "\n...")}\n" +
$"..{GameStateTeamT.ToString().Replace("\n", "\n...")}\n";
} }
internal static Map? ParseFromJObject(JObject jsonObject) internal static Map? ParseFromJObject(JObject jsonObject)
@ -26,7 +24,7 @@ public struct Map
? new Map() ? new Map()
{ {
Mode = jsonObject.SelectToken("map.mode")!.Value<string>()!, Mode = jsonObject.SelectToken("map.mode")!.Value<string>()!,
MapName = jsonObject.SelectToken("map.name")!.Value<string>()!, Name = jsonObject.SelectToken("map.name")!.Value<string>()!,
Phase = MapPhaseFromString(jsonObject.SelectToken("map.phase")!.Value<string>()!), Phase = MapPhaseFromString(jsonObject.SelectToken("map.phase")!.Value<string>()!),
Round = jsonObject.SelectToken("map.round")!.Value<int>(), Round = jsonObject.SelectToken("map.round")!.Value<int>(),
NumMatchesToWinSeries = jsonObject.SelectToken("map.num_matches_to_win_series")!.Value<int>(), NumMatchesToWinSeries = jsonObject.SelectToken("map.num_matches_to_win_series")!.Value<int>(),

View File

@ -13,36 +13,34 @@ public struct Player
public override string ToString() public override string ToString()
{ {
return $"{GetType().Name}\n" + return $"{GetType()}\n" +
$"..Name: {Name} SteamId: {SteamId}\n" + $"\t{Name} {SteamId} {Activity} {Team}\n" +
$"..Activity: {Activity}\n" + $"\t{State}\n" +
$"..Team: {Team}\n" + $"\t{MatchStats}\n";
$"..{State.ToString()?.Replace("\n", "\n...")}\n" +
$"..{MatchStats.ToString()?.Replace("\n", "\n...")}\n";
} }
internal static Player? ParseFromJObject(JObject jsonObject) internal static Player? ParseFromJObject(JObject jsonObject)
{ {
return jsonObject.SelectToken("player") is not null ? new Player() return new Player()
{ {
SteamId = jsonObject.SelectToken("player.steamid")!.Value<string>()!, SteamId = jsonObject.SelectToken("player.steamid")!.Value<string>()!,
Name = jsonObject.SelectToken("player.name")!.Value<string>()!, Name = jsonObject.SelectToken("player.name")!.Value<string>()!,
Team = CS2TeamFromString(jsonObject.SelectToken("player.team")?.Value<string>()), Team = CS2TeamFromString(jsonObject.SelectToken("player.team")!.Value<string>()!),
Activity = PlayerActivityFromString(jsonObject.SelectToken("player.activity")!.Value<string>()!), Activity = PlayerActivityFromString(jsonObject.SelectToken("player.activity")!.Value<string>()!),
State = PlayerState.ParseFromJObject(jsonObject), State = PlayerState.ParseFromJObject(jsonObject),
MatchStats = PlayerMatchStats.ParseFromJObject(jsonObject) MatchStats = PlayerMatchStats.ParseFromJObject(jsonObject)
} : null; };
} }
public enum PlayerActivity {Playing, Menu, TextInput} public enum PlayerActivity {Playing, Menu, TextInput}
private static CS2Team? CS2TeamFromString(string? str) private static CS2Team CS2TeamFromString(string str)
{ {
return str?.ToLower() switch return str.ToLower() switch
{ {
"t" => CS2Team.T, "t" => CS2Team.T,
"ct" => CS2Team.CT, "ct" => CS2Team.CT,
_ => null _ => throw new ArgumentOutOfRangeException()
}; };
} }

View File

@ -6,23 +6,23 @@ public struct PlayerMatchStats
{ {
public int Kills, Assists, Deaths, MVPs, Score; public int Kills, Assists, Deaths, MVPs, Score;
public override string ToString() internal static PlayerMatchStats ParseFromJObject(JObject jsonObject)
{ {
return $"{GetType().Name}\n" + return new PlayerMatchStats()
$"..KAD: {Kills} {Assists} {Deaths}\n" + {
$"..MVPs: {MVPs}\n" + Kills = jsonObject.SelectToken($"player.match_stats.kills")!.Value<int>(),
$"..Score: {Score}\n"; Assists = jsonObject.SelectToken($"player.match_stats.assists")!.Value<int>(),
Deaths = jsonObject.SelectToken($"player.match_stats.deaths")!.Value<int>(),
MVPs = jsonObject.SelectToken($"player.match_stats.mvps")!.Value<int>(),
Score = jsonObject.SelectToken($"player.match_stats.score")!.Value<int>(),
};
} }
internal static PlayerMatchStats? ParseFromJObject(JObject jsonObject) public override string ToString()
{ {
return jsonObject.SelectToken("player.match_stats") is not null ? new PlayerMatchStats() return $"{GetType()}\n" +
{ $"\tKAD: {Kills} {Assists} {Deaths}\n" +
Kills = jsonObject.SelectToken("player.match_stats.kills")!.Value<int>(), $"\tMVPs: {MVPs}\n" +
Assists = jsonObject.SelectToken("player.match_stats.assists")!.Value<int>(), $"\tScore: {Score}\n";
Deaths = jsonObject.SelectToken("player.match_stats.deaths")!.Value<int>(),
MVPs = jsonObject.SelectToken("player.match_stats.mvps")!.Value<int>(),
Score = jsonObject.SelectToken("player.match_stats.score")!.Value<int>(),
} : null;
} }
} }

View File

@ -7,34 +7,34 @@ public struct PlayerState
public int Health, Armor, Flashed, Smoked, Burning, Money, RoundKills, RoundHs, EquipmentValue; public int Health, Armor, Flashed, Smoked, Burning, Money, RoundKills, RoundHs, EquipmentValue;
public bool Helmet; public bool Helmet;
public override string ToString() internal static PlayerState ParseFromJObject(JObject jsonObject)
{ {
return $"{GetType().Name}\n" + return new PlayerState()
$"..Health: {Health}\n" + {
$"..Armor: {Armor}\n" + Health = jsonObject.SelectToken($"player.state.health")!.Value<int>(),
$"..Flashed: {Flashed}\n" + Armor = jsonObject.SelectToken($"player.state.armor")!.Value<int>(),
$"..Smoked: {Smoked}\n" + Helmet = jsonObject.SelectToken($"player.state.helmet")!.Value<bool>(),
$"..Burning: {Burning}\n" + Flashed = jsonObject.SelectToken($"player.state.flashed")!.Value<int>(),
$"..Money: {Money}\n" + Smoked = jsonObject.SelectToken($"player.state.smoked")!.Value<int>(),
$"..RoundKills: {RoundKills}\n" + Burning = jsonObject.SelectToken($"player.state.burning")!.Value<int>(),
$"..RoundHs: {RoundHs}\n" + Money = jsonObject.SelectToken($"player.state.money")!.Value<int>(),
$"..EquipmentValue: {EquipmentValue}\n"; RoundKills = jsonObject.SelectToken($"player.state.round_kills")!.Value<int>(),
RoundHs = jsonObject.SelectToken($"player.state.round_killhs")!.Value<int>(),
EquipmentValue = jsonObject.SelectToken($"player.state.equip_value")!.Value<int>(),
};
} }
internal static PlayerState? ParseFromJObject(JObject jsonObject) public override string ToString()
{ {
return jsonObject.SelectToken("player.state") is not null ? new PlayerState() return $"{GetType()}\n" +
{ $"\tHealth: {Health}\n" +
Health = jsonObject.SelectToken("player.state.health")!.Value<int>(), $"\tArmor: {Armor}\n" +
Armor = jsonObject.SelectToken("player.state.armor")!.Value<int>(), $"\tFlashed: {Flashed}\n" +
Helmet = jsonObject.SelectToken("player.state.helmet")!.Value<bool>(), $"\tSmoked: {Smoked}\n" +
Flashed = jsonObject.SelectToken("player.state.flashed")!.Value<int>(), $"\tBurning: {Burning}\n" +
Smoked = jsonObject.SelectToken("player.state.smoked")!.Value<int>(), $"\tMoney: {Money}\n" +
Burning = jsonObject.SelectToken("player.state.burning")!.Value<int>(), $"\tRoundKills: {RoundKills}\n" +
Money = jsonObject.SelectToken("player.state.money")!.Value<int>(), $"\tRoundHs: {RoundHs}\n" +
RoundKills = jsonObject.SelectToken("player.state.round_kills")!.Value<int>(), $"\tEquipmentValue: {EquipmentValue}\n";
RoundHs = jsonObject.SelectToken("player.state.round_killhs")!.Value<int>(),
EquipmentValue = jsonObject.SelectToken("player.state.equip_value")!.Value<int>(),
} : null;
} }
} }

View File

@ -5,32 +5,29 @@ namespace CS2GSI.GameState;
public struct Round public struct Round
{ {
public RoundPhase Phase; public RoundPhase Phase;
public BombStatus? Bomb; public BombStatus Bomb;
public CS2Team? WinnerTeam; public CS2Team WinnerTeam;
public override string ToString() public override string ToString()
{ {
return $"{GetType().Name}\n" + return $"{GetType()}\n" +
$"..Phase: {Phase}\n" + $"\t{Phase} {WinnerTeam} {Bomb}\n";
$"..Winner: {WinnerTeam}\n" +
$"..Bomb: {Bomb}\n";
} }
internal static Round? ParseFromJObject(JObject jsonObject) internal static Round? ParseFromJObject(JObject jsonObject)
{ {
return jsonObject.SelectToken("round") is not null ? new Round() return new Round()
{ {
Phase = RoundPhaseFromString(jsonObject.SelectToken("round.phase")!.Value<string>()!), Phase = RoundPhaseFromString(jsonObject.SelectToken("round.phase")!.Value<string>()!),
WinnerTeam = CS2TeamFromString(jsonObject.SelectToken("round.win_team")?.Value<string>()), WinnerTeam = CS2TeamFromString(jsonObject.SelectToken("round.win_team")!.Value<string>()!),
Bomb = BombStatusFromString(jsonObject.SelectToken("round.bomb")?.Value<string>()) Bomb = BombStatusFromString(jsonObject.SelectToken("round.bomb")!.Value<string>()!)
} : null; };
} }
public enum RoundPhase public enum RoundPhase
{ {
Over, Freezetime, Live Over, Freezetime, Live
} }
private static RoundPhase RoundPhaseFromString(string str) private static RoundPhase RoundPhaseFromString(string str)
{ {
return str switch return str switch
@ -47,24 +44,24 @@ public struct Round
Planted, Exploded, Defused Planted, Exploded, Defused
} }
private static BombStatus? BombStatusFromString(string? str) private static BombStatus BombStatusFromString(string str)
{ {
return str switch return str switch
{ {
"planted" => BombStatus.Planted, "planted" => BombStatus.Planted,
"exploded" => BombStatus.Exploded, "exploded" => BombStatus.Exploded,
"defused" => BombStatus.Defused, "defused" => BombStatus.Defused,
_ => null _ => throw new ArgumentOutOfRangeException()
}; };
} }
private static CS2Team? CS2TeamFromString(string? str) private static CS2Team CS2TeamFromString(string str)
{ {
return str?.ToLower() switch return str.ToLower() switch
{ {
"t" => CS2Team.T, "t" => CS2Team.T,
"ct" => CS2Team.CT, "ct" => CS2Team.CT,
_ => null _ => throw new ArgumentOutOfRangeException()
}; };
} }
} }

View File

@ -1,12 +1,11 @@
// See https://aka.ms/new-console-template for more information // See https://aka.ms/new-console-template for more information
using CS2GSI.TestApp; using CS2GSI.TestApp;
using Microsoft.Extensions.Logging;
public class TestApp public class TestApp
{ {
public static void Main(string[] args) public static void Main(string[] args)
{ {
new CS2GSI.CS2GSI(new Logger(LogLevel.Information)); new CS2GSI.CS2GSI(new Logger());
} }
} }