From 4cfe1dcc4de30f6d24078f94b960cbd7f57e1136 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 15 Jan 2024 22:22:36 +0100 Subject: [PATCH] Add custom enums for GameStates --- CS2GSI.sln.DotSettings | 1 + GameState/CS2Team.cs | 6 +++++ GameState/{Team.cs => GameStateTeam.cs} | 16 ++++++------ GameState/Map.cs | 30 ++++++++++++++++------ GameState/Player.cs | 33 ++++++++++++++++++++++--- GameState/Round.cs | 20 +++++++++++++-- 6 files changed, 86 insertions(+), 20 deletions(-) create mode 100644 GameState/CS2Team.cs rename GameState/{Team.cs => GameStateTeam.cs} (57%) diff --git a/CS2GSI.sln.DotSettings b/CS2GSI.sln.DotSettings index c13b46b..d625e59 100644 --- a/CS2GSI.sln.DotSettings +++ b/CS2GSI.sln.DotSettings @@ -1,6 +1,7 @@  CS True + True True True True diff --git a/GameState/CS2Team.cs b/GameState/CS2Team.cs new file mode 100644 index 0000000..1ef6447 --- /dev/null +++ b/GameState/CS2Team.cs @@ -0,0 +1,6 @@ +namespace CS2GSI.GameState; + +public enum CS2Team +{ + T, CT +} diff --git a/GameState/Team.cs b/GameState/GameStateTeam.cs similarity index 57% rename from GameState/Team.cs rename to GameState/GameStateTeam.cs index 5eba456..86df151 100644 --- a/GameState/Team.cs +++ b/GameState/GameStateTeam.cs @@ -2,8 +2,9 @@ namespace CS2GSI.GameState; -public struct Team +public struct GameStateTeam { + public CS2Team Team; public int Score, ConsecutiveRoundLosses, TimeoutsRemaining, MatchesWonThisSeries; public override string ToString() @@ -15,14 +16,15 @@ public struct Team $"\tMatchesWonThisSeries: {MatchesWonThisSeries}\n"; } - internal static Team ParseFromJObject(JObject jsonObject, string team) + internal static GameStateTeam ParseFromJObject(JObject jsonObject, CS2Team team) { - return new Team() + return new GameStateTeam() { - Score = jsonObject.SelectToken($"map.team_{team}.score")!.Value(), - ConsecutiveRoundLosses = jsonObject.SelectToken($"map.team_{team}.consecutive_round_losses")!.Value(), - TimeoutsRemaining = jsonObject.SelectToken($"map.team_{team}.timeouts_remaining")!.Value(), - MatchesWonThisSeries = jsonObject.SelectToken($"map.team_{team}.matches_won_this_series")!.Value(), + Team = team, + Score = jsonObject.SelectToken($"map.team_{team.ToString().ToLower()}.score")!.Value(), + ConsecutiveRoundLosses = jsonObject.SelectToken($"map.team_{team.ToString().ToLower()}.consecutive_round_losses")!.Value(), + TimeoutsRemaining = jsonObject.SelectToken($"map.team_{team.ToString().ToLower()}.timeouts_remaining")!.Value(), + MatchesWonThisSeries = jsonObject.SelectToken($"map.team_{team.ToString().ToLower()}.matches_won_this_series")!.Value(), }; } } \ No newline at end of file diff --git a/GameState/Map.cs b/GameState/Map.cs index 2d5785a..51ddcd2 100644 --- a/GameState/Map.cs +++ b/GameState/Map.cs @@ -4,17 +4,18 @@ namespace CS2GSI.GameState; public struct Map { - public string Mode, Name, Phase; + public string Mode, Name; + public MapPhase Phase; public int Round, NumMatchesToWinSeries; - public Team TeamCT, TeamT; + public GameStateTeam GameStateTeamCT, GameStateTeamT; public override string ToString() { return $"{GetType()}\n" + $"\t{Mode} {Name} {Round} Matches to Win Series: {NumMatchesToWinSeries}\n" + $"\t{Phase}\n" + - $"\t{TeamCT}\n" + - $"\t{TeamT}\n"; + $"\t{GameStateTeamCT}\n" + + $"\t{GameStateTeamT}\n"; } internal static Map? ParseFromJObject(JObject jsonObject) @@ -24,12 +25,27 @@ public struct Map { Mode = jsonObject.SelectToken("map.mode")!.Value()!, Name = jsonObject.SelectToken("map.name")!.Value()!, - Phase = jsonObject.SelectToken("map.phase")!.Value()!, + Phase = MapPhaseFromString(jsonObject.SelectToken("map.phase")!.Value()!), Round = jsonObject.SelectToken("map.round")!.Value(), NumMatchesToWinSeries = jsonObject.SelectToken("map.num_matches_to_win_series")!.Value(), - TeamCT = Team.ParseFromJObject(jsonObject, "ct"), - TeamT = Team.ParseFromJObject(jsonObject, "t") + GameStateTeamCT = GameStateTeam.ParseFromJObject(jsonObject, CS2Team.CT), + GameStateTeamT = GameStateTeam.ParseFromJObject(jsonObject, CS2Team.T) } : null; } + + public enum MapPhase {Warmup, Live, Intermission, GameOver} + + private static MapPhase MapPhaseFromString(string str) + { + return str switch + { + "warmup" => MapPhase.Warmup, + "live" => MapPhase.Live, + "intermission" => MapPhase.Intermission, + // ReSharper disable once StringLiteralTypo + "gameover" => MapPhase.GameOver, + _ => throw new ArgumentOutOfRangeException() + }; + } } \ No newline at end of file diff --git a/GameState/Player.cs b/GameState/Player.cs index e47f35c..d2f6272 100644 --- a/GameState/Player.cs +++ b/GameState/Player.cs @@ -4,8 +4,9 @@ namespace CS2GSI.GameState; public struct Player { - public string SteamId, Name, Activity; - public string? Team; + public string SteamId, Name; + public PlayerActivity Activity; + public CS2Team? Team; public int? ObserverSlot; public PlayerState? State; public PlayerMatchStats? MatchStats; @@ -24,10 +25,34 @@ public struct Player { SteamId = jsonObject.SelectToken("player.steamid")!.Value()!, Name = jsonObject.SelectToken("player.name")!.Value()!, - Team = jsonObject.SelectToken("player.team")!.Value()!, - Activity = jsonObject.SelectToken("player.activity")!.Value()!, + Team = CS2TeamFromString(jsonObject.SelectToken("player.team")!.Value()!), + Activity = PlayerActivityFromString(jsonObject.SelectToken("player.activity")!.Value()!), State = PlayerState.ParseFromJObject(jsonObject), MatchStats = PlayerMatchStats.ParseFromJObject(jsonObject) }; } + + public enum PlayerActivity {Playing, Menu, TextInput} + + private static CS2Team CS2TeamFromString(string str) + { + return str switch + { + "t" => CS2Team.T, + "ct" => CS2Team.CT, + _ => throw new ArgumentOutOfRangeException() + }; + } + + private static PlayerActivity PlayerActivityFromString(string str) + { + return str switch + { + "playing" => PlayerActivity.Playing, + "menu" => PlayerActivity.Menu, + // ReSharper disable once StringLiteralTypo + "textinput" => PlayerActivity.TextInput, + _ => throw new ArgumentOutOfRangeException() + }; + } } \ No newline at end of file diff --git a/GameState/Round.cs b/GameState/Round.cs index 4ff8ee1..8d56972 100644 --- a/GameState/Round.cs +++ b/GameState/Round.cs @@ -4,7 +4,8 @@ namespace CS2GSI.GameState; public struct Round { - public string Phase, WinnerTeam, BombStatus; + public RoundPhase Phase; + public string WinnerTeam, BombStatus; public override string ToString() { @@ -16,9 +17,24 @@ public struct Round { return new Round() { - Phase = jsonObject.SelectToken("round.phase")!.Value()!, + Phase = RoundPhaseFromString(jsonObject.SelectToken("round.phase")!.Value()!), WinnerTeam = jsonObject.SelectToken("round.win_team")!.Value()!, BombStatus = jsonObject.SelectToken("round.bomb")!.Value()! }; } + + public enum RoundPhase + { + Over, Freezetime, Live + } + private static RoundPhase RoundPhaseFromString(string str) + { + return str switch + { + "over" => RoundPhase.Over, + "live" => RoundPhase.Live, + "freezetime" => RoundPhase.Freezetime, + _ => throw new ArgumentOutOfRangeException() + }; + } } \ No newline at end of file