GameState Structs and Parser

This commit is contained in:
glax 2024-01-15 20:04:37 +01:00
parent aa4dcc05f5
commit ecd5be84d3
8 changed files with 136 additions and 0 deletions

View File

@ -21,4 +21,8 @@
</Compile>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
</Project>

87
CS2GSIJsonParser.cs Normal file
View File

@ -0,0 +1,87 @@
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()
{
Timestamp = jsonObject.SelectToken("provider.timestamp")!.Value<int>(),
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<string>()!,
Name = jsonObject.SelectToken("map.name")!.Value<string>()!,
Phase = jsonObject.SelectToken("map.phase")!.Value<string>()!,
Round = jsonObject.SelectToken("map.round")!.Value<int>(),
NumMatchesToWinSeries = jsonObject.SelectToken("map.num_matches_to_win_series")!.Value<int>(),
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<int>(),
ConsecutiveRoundLosses = jsonObject.SelectToken($"map.team_{team}.consecutive_round_losses")!.Value<int>(),
TimeoutsRemaining = jsonObject.SelectToken($"map.team_{team}.timeouts_remaining")!.Value<int>(),
MatchesWonThisSeries = jsonObject.SelectToken($"map.team_{team}.matches_won_this_series")!.Value<int>(),
};
}
private static Player? ParsePlayerFromJObject(JObject jsonObject)
{
return new Player()
{
SteamId = jsonObject.SelectToken("player.steamid")!.Value<string>()!,
Name = jsonObject.SelectToken("player.name")!.Value<string>()!,
Team = jsonObject.SelectToken("player.team")!.Value<string>()!,
Activity = jsonObject.SelectToken("player.activity")!.Value<string>()!,
State = ParsePlayerStateFromJObject(jsonObject),
MatchStats = ParsePlayerMatchStatsFromJObject(jsonObject)
};
}
private static PlayerState ParsePlayerStateFromJObject(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>(),
};
}
private static PlayerMatchStats ParsePlayerMatchStatsFromJObject(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>(),
};
}
}

View File

@ -0,0 +1,8 @@
namespace CS2GSI.GameState;
public struct CS2GameState
{
public int Timestamp;
public Map? Map;
public Player? Player;
}

8
GameState/Map.cs Normal file
View File

@ -0,0 +1,8 @@
namespace CS2GSI.GameState;
public struct Map
{
public string Mode, Name, Phase;
public int Round, NumMatchesToWinSeries;
public Team TeamCT, TeamT;
}

10
GameState/Player.cs Normal file
View File

@ -0,0 +1,10 @@
namespace CS2GSI.GameState;
public struct Player
{
public string SteamId, Name, Activity;
public string? Team;
public int? ObserverSlot;
public PlayerState? State;
public PlayerMatchStats? MatchStats;
}

View File

@ -0,0 +1,6 @@
namespace CS2GSI.GameState;
public struct PlayerMatchStats
{
public int Kills, Assists, Deaths, MVPs, Score;
}

7
GameState/PlayerState.cs Normal file
View File

@ -0,0 +1,7 @@
namespace CS2GSI.GameState;
public struct PlayerState
{
public int Health, Armor, Flashed, Smoked, Burning, Money, RoundKills, RoundHs, EquipmentValue;
public bool Helmet;
}

6
GameState/Team.cs Normal file
View File

@ -0,0 +1,6 @@
namespace CS2GSI.GameState;
public struct Team
{
public int Score, ConsecutiveRoundLosses, TimeoutsRemaining, MatchesWonThisSeries;
}