From e83fbd7f66326e042f03bf7534ea25bdd79783ed Mon Sep 17 00:00:00 2001 From: glax Date: Mon, 15 Jan 2024 21:49:09 +0100 Subject: [PATCH] Move parsing of Gamestate to structs --- CS2GSIJsonParser.cs | 88 ----------------------------------- GameState/CS2GameState.cs | 15 +++++- GameState/Map.cs | 20 +++++++- GameState/Player.cs | 17 ++++++- GameState/PlayerMatchStats.cs | 16 ++++++- GameState/PlayerState.cs | 21 ++++++++- GameState/Team.cs | 15 +++++- 7 files changed, 98 insertions(+), 94 deletions(-) delete mode 100644 CS2GSIJsonParser.cs diff --git a/CS2GSIJsonParser.cs b/CS2GSIJsonParser.cs deleted file mode 100644 index 56565dd..0000000 --- a/CS2GSIJsonParser.cs +++ /dev/null @@ -1,88 +0,0 @@ -using CS2GSI.GameState; -using Newtonsoft.Json.Linq; - -namespace CS2GSI; - -internal static class CS2GSIJsonParser -{ - internal static CS2GameState ParseGameStateFromJson(string jsonString) - { - JObject jsonObject = JObject.Parse(jsonString); - return new CS2GameState() - { - ProviderSteamId = jsonObject.SelectToken("provider.steamid")!.Value()!, - Timestamp = jsonObject.SelectToken("provider.timestamp")!.Value(), - Map = ParseMapFromJObject(jsonObject), - Player = ParsePlayerFromJObject(jsonObject) - }; - } - - private static Map? ParseMapFromJObject(JObject jsonObject) - { - return jsonObject.SelectToken("map") is { } mapToken - ? new Map() - { - Mode = jsonObject.SelectToken("map.mode")!.Value()!, - Name = jsonObject.SelectToken("map.name")!.Value()!, - Phase = jsonObject.SelectToken("map.phase")!.Value()!, - Round = jsonObject.SelectToken("map.round")!.Value(), - NumMatchesToWinSeries = jsonObject.SelectToken("map.num_matches_to_win_series")!.Value(), - TeamCT = ParseTeamFromJObject(jsonObject, "ct"), - TeamT = ParseTeamFromJObject(jsonObject, "t") - } - : null; - } - - private static Team ParseTeamFromJObject(JObject jsonObject, string team) - { - return new Team() - { - 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(), - }; - } - - private static Player? ParsePlayerFromJObject(JObject jsonObject) - { - return new 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()!, - State = ParsePlayerStateFromJObject(jsonObject), - MatchStats = ParsePlayerMatchStatsFromJObject(jsonObject) - }; - } - - private static PlayerState ParsePlayerStateFromJObject(JObject jsonObject) - { - return new PlayerState() - { - Health = jsonObject.SelectToken($"player.state.health")!.Value(), - Armor = jsonObject.SelectToken($"player.state.armor")!.Value(), - Helmet = jsonObject.SelectToken($"player.state.helmet")!.Value(), - Flashed = jsonObject.SelectToken($"player.state.flashed")!.Value(), - Smoked = jsonObject.SelectToken($"player.state.smoked")!.Value(), - Burning = jsonObject.SelectToken($"player.state.burning")!.Value(), - Money = jsonObject.SelectToken($"player.state.money")!.Value(), - RoundKills = jsonObject.SelectToken($"player.state.round_kills")!.Value(), - RoundHs = jsonObject.SelectToken($"player.state.round_killhs")!.Value(), - EquipmentValue = jsonObject.SelectToken($"player.state.equip_value")!.Value(), - }; - } - - private static PlayerMatchStats ParsePlayerMatchStatsFromJObject(JObject jsonObject) - { - return new PlayerMatchStats() - { - Kills = jsonObject.SelectToken($"player.match_stats.kills")!.Value(), - Assists = jsonObject.SelectToken($"player.match_stats.assists")!.Value(), - Deaths = jsonObject.SelectToken($"player.match_stats.deaths")!.Value(), - MVPs = jsonObject.SelectToken($"player.match_stats.mvps")!.Value(), - Score = jsonObject.SelectToken($"player.match_stats.score")!.Value(), - }; - } -} \ No newline at end of file diff --git a/GameState/CS2GameState.cs b/GameState/CS2GameState.cs index 9d200c8..3ed34a9 100644 --- a/GameState/CS2GameState.cs +++ b/GameState/CS2GameState.cs @@ -1,4 +1,6 @@ -namespace CS2GSI.GameState; +using Newtonsoft.Json.Linq; + +namespace CS2GSI.GameState; public struct CS2GameState { @@ -14,6 +16,17 @@ public struct CS2GameState $"\t{Map}\n" + $"\t{Player}\n"; } + + internal static CS2GameState ParseFromJObject(JObject jsonObject) + { + return new CS2GameState() + { + ProviderSteamId = jsonObject.SelectToken("provider.steamid")!.Value()!, + Timestamp = jsonObject.SelectToken("provider.timestamp")!.Value(), + Map = GameState.Map.ParseFromJObject(jsonObject), + Player = GameState.Player.ParseFromJObject(jsonObject) + }; + } internal CS2GameState? UpdateGameStateForLocal(CS2GameState? previousLocalState) { diff --git a/GameState/Map.cs b/GameState/Map.cs index 0feff99..2d5785a 100644 --- a/GameState/Map.cs +++ b/GameState/Map.cs @@ -1,4 +1,6 @@ -namespace CS2GSI.GameState; +using Newtonsoft.Json.Linq; + +namespace CS2GSI.GameState; public struct Map { @@ -14,4 +16,20 @@ public struct Map $"\t{TeamCT}\n" + $"\t{TeamT}\n"; } + + internal static Map? ParseFromJObject(JObject jsonObject) + { + return jsonObject.SelectToken("map") is { } mapToken + ? new Map() + { + Mode = jsonObject.SelectToken("map.mode")!.Value()!, + Name = jsonObject.SelectToken("map.name")!.Value()!, + Phase = 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") + } + : null; + } } \ No newline at end of file diff --git a/GameState/Player.cs b/GameState/Player.cs index 14e6ffd..e47f35c 100644 --- a/GameState/Player.cs +++ b/GameState/Player.cs @@ -1,4 +1,6 @@ -namespace CS2GSI.GameState; +using Newtonsoft.Json.Linq; + +namespace CS2GSI.GameState; public struct Player { @@ -15,4 +17,17 @@ public struct Player $"\t{State}\n" + $"\t{MatchStats}\n"; } + + internal static Player? ParseFromJObject(JObject jsonObject) + { + return new 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()!, + State = PlayerState.ParseFromJObject(jsonObject), + MatchStats = PlayerMatchStats.ParseFromJObject(jsonObject) + }; + } } \ No newline at end of file diff --git a/GameState/PlayerMatchStats.cs b/GameState/PlayerMatchStats.cs index a08c149..a370457 100644 --- a/GameState/PlayerMatchStats.cs +++ b/GameState/PlayerMatchStats.cs @@ -1,9 +1,23 @@ -namespace CS2GSI.GameState; +using Newtonsoft.Json.Linq; + +namespace CS2GSI.GameState; public struct PlayerMatchStats { public int Kills, Assists, Deaths, MVPs, Score; + internal static PlayerMatchStats ParseFromJObject(JObject jsonObject) + { + return new PlayerMatchStats() + { + Kills = jsonObject.SelectToken($"player.match_stats.kills")!.Value(), + Assists = jsonObject.SelectToken($"player.match_stats.assists")!.Value(), + Deaths = jsonObject.SelectToken($"player.match_stats.deaths")!.Value(), + MVPs = jsonObject.SelectToken($"player.match_stats.mvps")!.Value(), + Score = jsonObject.SelectToken($"player.match_stats.score")!.Value(), + }; + } + public override string ToString() { return $"{GetType()}\n" + diff --git a/GameState/PlayerState.cs b/GameState/PlayerState.cs index 7f27a84..a37d65f 100644 --- a/GameState/PlayerState.cs +++ b/GameState/PlayerState.cs @@ -1,10 +1,29 @@ -namespace CS2GSI.GameState; +using Newtonsoft.Json.Linq; + +namespace CS2GSI.GameState; public struct PlayerState { public int Health, Armor, Flashed, Smoked, Burning, Money, RoundKills, RoundHs, EquipmentValue; public bool Helmet; + internal static PlayerState ParseFromJObject(JObject jsonObject) + { + return new PlayerState() + { + Health = jsonObject.SelectToken($"player.state.health")!.Value(), + Armor = jsonObject.SelectToken($"player.state.armor")!.Value(), + Helmet = jsonObject.SelectToken($"player.state.helmet")!.Value(), + Flashed = jsonObject.SelectToken($"player.state.flashed")!.Value(), + Smoked = jsonObject.SelectToken($"player.state.smoked")!.Value(), + Burning = jsonObject.SelectToken($"player.state.burning")!.Value(), + Money = jsonObject.SelectToken($"player.state.money")!.Value(), + RoundKills = jsonObject.SelectToken($"player.state.round_kills")!.Value(), + RoundHs = jsonObject.SelectToken($"player.state.round_killhs")!.Value(), + EquipmentValue = jsonObject.SelectToken($"player.state.equip_value")!.Value(), + }; + } + public override string ToString() { return $"{GetType()}\n" + diff --git a/GameState/Team.cs b/GameState/Team.cs index fcc6169..5eba456 100644 --- a/GameState/Team.cs +++ b/GameState/Team.cs @@ -1,4 +1,6 @@ -namespace CS2GSI.GameState; +using Newtonsoft.Json.Linq; + +namespace CS2GSI.GameState; public struct Team { @@ -12,4 +14,15 @@ public struct Team $"\tTimeoutsRemaining: {TimeoutsRemaining}\n" + $"\tMatchesWonThisSeries: {MatchesWonThisSeries}\n"; } + + internal static Team ParseFromJObject(JObject jsonObject, string team) + { + return new Team() + { + 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(), + }; + } } \ No newline at end of file