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(), }; } }