Compare commits

...

6 Commits

Author SHA1 Message Date
1c161cde55 Add EventArgs 2024-01-15 23:52:30 +01:00
bc711d65e6 Formatted Logoutput 2024-01-15 23:43:34 +01:00
65b9b195ca Fix wrong logic on updating last local gamestate 2024-01-15 23:43:27 +01:00
b084b0e13f Add "AnyMessage" Event 2024-01-15 23:43:05 +01:00
4b66a7147a GSIServer log messagecontent before invoking event 2024-01-15 23:33:14 +01:00
1d5df8a095 Fix null-references 2024-01-15 23:32:57 +01:00
12 changed files with 134 additions and 106 deletions

View File

@ -2,5 +2,20 @@
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,10 +12,12 @@ 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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnPlayerChange, new CS2EventArgs(newGameState.Player?.SteamId)));
if(events.Count > 0) if(events.Count > 0)
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyEvent, new CS2EventArgs())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyEvent, new CS2EventArgs(events.Count)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyMessage, new CS2EventArgs()));
return events; return events;
} }
@ -63,13 +65,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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnKill, new CS2EventArgs(newGameState.Player?.MatchStats?.Kills)));
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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHeadshot, new CS2EventArgs(newGameState.Player?.State?.RoundHs)));
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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDeath, new CS2EventArgs(newGameState.Player?.MatchStats?.Deaths)));
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()));
@ -81,25 +83,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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDamageTaken, new CS2EventArgs(newGameState.Player?.State?.Health)));
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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHealthChange, new CS2EventArgs(newGameState.Player?.State?.Health)));
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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnMoneyChange, new CS2EventArgs(newGameState.Player?.State?.Money)));
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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnArmorChange, new CS2EventArgs(newGameState.Player?.State?.Armor)));
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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHelmetChange, new CS2EventArgs(newGameState.Player?.State?.Helmet)));
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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnEquipmentValueChange, new CS2EventArgs(newGameState.Player?.State?.EquipmentValue)));
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())); events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnTeamChange, new CS2EventArgs(newGameState.Player?.Team)));
return events; return events;

View File

@ -37,17 +37,15 @@ 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()}");
this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState);
this.logger?.Log(LogLevel.Debug, $"Updated Local State:\n{_lastLocalGameState.ToString()}");
if (_lastLocalGameState is not null) if (_lastLocalGameState is not null && _allGameStates.Count > 0)
{ {
List<ValueTuple<CS2Event, CS2EventArgs>> generatedEvents = CS2EventGenerator.GenerateEvents(_lastLocalGameState.Value, newState, _allGameStates.Last()); List<ValueTuple<CS2Event, CS2EventArgs>> generatedEvents = CS2EventGenerator.GenerateEvents(_lastLocalGameState.Value, newState, _allGameStates.Last());
this.logger?.Log(LogLevel.Information, $"Generated {generatedEvents.Count} events."); this.logger?.Log(LogLevel.Information, $"Generated {generatedEvents.Count} events:\n\t{string.Join("\n\t", generatedEvents)}");
if(generatedEvents.Count > 0)
this.logger?.Log(LogLevel.Debug, $"Events:\n\t{string.Join("\n\t", generatedEvents)}");
InvokeEvents(generatedEvents); InvokeEvents(generatedEvents);
} }
this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState);
this.logger?.Log(LogLevel.Debug, $"Updated Local State:\n{_lastLocalGameState.ToString()}");
_allGameStates.Add(newState); _allGameStates.Add(newState);
} }
@ -134,6 +132,9 @@ 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;
@ -164,7 +165,8 @@ public class CS2GSI
OnBombPlanted, OnBombPlanted,
OnBombDefused, OnBombDefused,
OnBombExploded, OnBombExploded,
AnyEvent AnyEvent,
AnyMessage
} }
public delegate void CS2EventHandler(CS2EventArgs eventArgs); public delegate void CS2EventHandler(CS2EventArgs eventArgs);
@ -192,6 +194,7 @@ 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();
OnMessage?.Invoke(content);
this.logger?.Log(LogLevel.Debug, content); this.logger?.Log(LogLevel.Debug, content);
OnMessage?.Invoke(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()}\n" + return $"{GetType().Name}\n" +
$"\tTime: {Timestamp}\tSteamId: {ProviderSteamId}\n" + $"..Time: {Timestamp}\tProviderSteamId: {ProviderSteamId}\n" +
$"\t{Map}\n" + $"..{Map.ToString()?.Replace("\n", "\n...")}\n" +
$"\t{Round}\n" + $"..{Round.ToString()?.Replace("\n", "\n...")}\n" +
$"\t{Player}\n"; $"..{Player.ToString()?.Replace("\n", "\n...")}\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()}\n" + return $"{GetType().Name}\n" +
$"\tScore: {Score}\n" + $"..Team {Team}\tScore: {Score}\n" +
$"\tConsecutiveRoundLosses: {ConsecutiveRoundLosses}\n" + $"..ConsecutiveRoundLosses: {ConsecutiveRoundLosses}\n" +
$"\tTimeoutsRemaining: {TimeoutsRemaining}\n" + $"..TimeoutsRemaining: {TimeoutsRemaining}\n" +
$"\tMatchesWonThisSeries: {MatchesWonThisSeries}\n"; $"..MatchesWonThisSeries: {MatchesWonThisSeries}\n";
} }
internal static GameStateTeam ParseFromJObject(JObject jsonObject, CS2Team team) internal static GameStateTeam ParseFromJObject(JObject jsonObject, CS2Team team)

View File

@ -4,18 +4,20 @@ namespace CS2GSI.GameState;
public struct Map public struct Map
{ {
public string Mode, Name; public string Mode, MapName;
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()}\n" + return $"{GetType().Name}\n" +
$"\t{Mode} {Name} {Round} Matches to Win Series: {NumMatchesToWinSeries}\n" + $"..Mode: {Mode} Map: {MapName}\n" +
$"\t{Phase}\n" + $"..Round: {Round}\n" +
$"\t{GameStateTeamCT}\n" + $"..Matches to Win Series: {NumMatchesToWinSeries}\n" +
$"\t{GameStateTeamT}\n"; $"..Phase: {Phase}\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)
@ -24,7 +26,7 @@ public struct Map
? new Map() ? new Map()
{ {
Mode = jsonObject.SelectToken("map.mode")!.Value<string>()!, Mode = jsonObject.SelectToken("map.mode")!.Value<string>()!,
Name = jsonObject.SelectToken("map.name")!.Value<string>()!, MapName = 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,34 +13,36 @@ public struct Player
public override string ToString() public override string ToString()
{ {
return $"{GetType()}\n" + return $"{GetType().Name}\n" +
$"\t{Name} {SteamId} {Activity} {Team}\n" + $"..Name: {Name} SteamId: {SteamId}\n" +
$"\t{State}\n" + $"..Activity: {Activity}\n" +
$"\t{MatchStats}\n"; $"..Team: {Team}\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 new Player() return jsonObject.SelectToken("player") is not null ? 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,
_ => throw new ArgumentOutOfRangeException() _ => null
}; };
} }

View File

@ -6,23 +6,23 @@ public struct PlayerMatchStats
{ {
public int Kills, Assists, Deaths, MVPs, Score; public int Kills, Assists, Deaths, MVPs, Score;
internal static PlayerMatchStats ParseFromJObject(JObject jsonObject)
{
return new PlayerMatchStats()
{
Kills = jsonObject.SelectToken($"player.match_stats.kills")!.Value<int>(),
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>(),
};
}
public override string ToString() public override string ToString()
{ {
return $"{GetType()}\n" + return $"{GetType().Name}\n" +
$"\tKAD: {Kills} {Assists} {Deaths}\n" + $"..KAD: {Kills} {Assists} {Deaths}\n" +
$"\tMVPs: {MVPs}\n" + $"..MVPs: {MVPs}\n" +
$"\tScore: {Score}\n"; $"..Score: {Score}\n";
}
internal static PlayerMatchStats? ParseFromJObject(JObject jsonObject)
{
return jsonObject.SelectToken("player.match_stats") is not null ? new PlayerMatchStats()
{
Kills = jsonObject.SelectToken("player.match_stats.kills")!.Value<int>(),
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>(),
} : 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;
internal static PlayerState ParseFromJObject(JObject jsonObject)
{
return new PlayerState()
{
Health = jsonObject.SelectToken($"player.state.health")!.Value<int>(),
Armor = jsonObject.SelectToken($"player.state.armor")!.Value<int>(),
Helmet = jsonObject.SelectToken($"player.state.helmet")!.Value<bool>(),
Flashed = jsonObject.SelectToken($"player.state.flashed")!.Value<int>(),
Smoked = jsonObject.SelectToken($"player.state.smoked")!.Value<int>(),
Burning = jsonObject.SelectToken($"player.state.burning")!.Value<int>(),
Money = jsonObject.SelectToken($"player.state.money")!.Value<int>(),
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>(),
};
}
public override string ToString() public override string ToString()
{ {
return $"{GetType()}\n" + return $"{GetType().Name}\n" +
$"\tHealth: {Health}\n" + $"..Health: {Health}\n" +
$"\tArmor: {Armor}\n" + $"..Armor: {Armor}\n" +
$"\tFlashed: {Flashed}\n" + $"..Flashed: {Flashed}\n" +
$"\tSmoked: {Smoked}\n" + $"..Smoked: {Smoked}\n" +
$"\tBurning: {Burning}\n" + $"..Burning: {Burning}\n" +
$"\tMoney: {Money}\n" + $"..Money: {Money}\n" +
$"\tRoundKills: {RoundKills}\n" + $"..RoundKills: {RoundKills}\n" +
$"\tRoundHs: {RoundHs}\n" + $"..RoundHs: {RoundHs}\n" +
$"\tEquipmentValue: {EquipmentValue}\n"; $"..EquipmentValue: {EquipmentValue}\n";
}
internal static PlayerState? ParseFromJObject(JObject jsonObject)
{
return jsonObject.SelectToken("player.state") is not null ? new PlayerState()
{
Health = jsonObject.SelectToken("player.state.health")!.Value<int>(),
Armor = jsonObject.SelectToken("player.state.armor")!.Value<int>(),
Helmet = jsonObject.SelectToken("player.state.helmet")!.Value<bool>(),
Flashed = jsonObject.SelectToken("player.state.flashed")!.Value<int>(),
Smoked = jsonObject.SelectToken("player.state.smoked")!.Value<int>(),
Burning = jsonObject.SelectToken("player.state.burning")!.Value<int>(),
Money = jsonObject.SelectToken("player.state.money")!.Value<int>(),
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>(),
} : null;
} }
} }

View File

@ -5,29 +5,32 @@ 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()}\n" + return $"{GetType().Name}\n" +
$"\t{Phase} {WinnerTeam} {Bomb}\n"; $"..Phase: {Phase}\n" +
$"..Winner: {WinnerTeam}\n" +
$"..Bomb: {Bomb}\n";
} }
internal static Round? ParseFromJObject(JObject jsonObject) internal static Round? ParseFromJObject(JObject jsonObject)
{ {
return new Round() return jsonObject.SelectToken("round") is not null ? 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
@ -44,24 +47,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,
_ => throw new ArgumentOutOfRangeException() _ => null
}; };
} }
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,
_ => throw new ArgumentOutOfRangeException() _ => null
}; };
} }
} }

View File

@ -1,11 +1,12 @@
// 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()); new CS2GSI.CS2GSI(new Logger(LogLevel.Information));
} }
} }