From 9489c45e9f71fea14184aac0538b863ab72fc405 Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 15 Jan 2024 22:36:53 +0100 Subject: [PATCH] Add custom enums for GameStates --- GameState/Player.cs | 2 +- GameState/Round.cs | 35 +++++++++++++++++++++++++++++++---- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/GameState/Player.cs b/GameState/Player.cs index d2f6272..9da3d41 100644 --- a/GameState/Player.cs +++ b/GameState/Player.cs @@ -36,7 +36,7 @@ public struct Player private static CS2Team CS2TeamFromString(string str) { - return str switch + return str.ToLower() switch { "t" => CS2Team.T, "ct" => CS2Team.CT, diff --git a/GameState/Round.cs b/GameState/Round.cs index 8d56972..0ab4868 100644 --- a/GameState/Round.cs +++ b/GameState/Round.cs @@ -5,12 +5,13 @@ namespace CS2GSI.GameState; public struct Round { public RoundPhase Phase; - public string WinnerTeam, BombStatus; + public BombStatus Bomb; + public CS2Team WinnerTeam; public override string ToString() { return $"{GetType()}\n" + - $"\t{Phase} {WinnerTeam} {BombStatus}\n"; + $"\t{Phase} {WinnerTeam} {Bomb}\n"; } internal static Round? ParseFromJObject(JObject jsonObject) @@ -18,8 +19,8 @@ public struct Round return new Round() { Phase = RoundPhaseFromString(jsonObject.SelectToken("round.phase")!.Value()!), - WinnerTeam = jsonObject.SelectToken("round.win_team")!.Value()!, - BombStatus = jsonObject.SelectToken("round.bomb")!.Value()! + WinnerTeam = CS2TeamFromString(jsonObject.SelectToken("round.win_team")!.Value()!), + Bomb = BombStatusFromString(jsonObject.SelectToken("round.bomb")!.Value()!) }; } @@ -37,4 +38,30 @@ public struct Round _ => throw new ArgumentOutOfRangeException() }; } + + public enum BombStatus + { + Planted, Exploded, Defused + } + + private static BombStatus BombStatusFromString(string str) + { + return str switch + { + "planted" => BombStatus.Planted, + "exploded" => BombStatus.Exploded, + "defused" => BombStatus.Defused, + _ => throw new ArgumentOutOfRangeException() + }; + } + + private static CS2Team CS2TeamFromString(string str) + { + return str.ToLower() switch + { + "t" => CS2Team.T, + "ct" => CS2Team.CT, + _ => throw new ArgumentOutOfRangeException() + }; + } } \ No newline at end of file