Move parsing of Gamestate to structs

This commit is contained in:
glax 2024-01-15 21:49:09 +01:00
parent 8d865d49fd
commit e83fbd7f66
7 changed files with 98 additions and 94 deletions

View File

@ -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<string>()!,
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

@ -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<string>()!,
Timestamp = jsonObject.SelectToken("provider.timestamp")!.Value<int>(),
Map = GameState.Map.ParseFromJObject(jsonObject),
Player = GameState.Player.ParseFromJObject(jsonObject)
};
}
internal CS2GameState? UpdateGameStateForLocal(CS2GameState? previousLocalState)
{

View File

@ -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<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 = Team.ParseFromJObject(jsonObject, "ct"),
TeamT = Team.ParseFromJObject(jsonObject, "t")
}
: null;
}
}

View File

@ -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<string>()!,
Name = jsonObject.SelectToken("player.name")!.Value<string>()!,
Team = jsonObject.SelectToken("player.team")!.Value<string>()!,
Activity = jsonObject.SelectToken("player.activity")!.Value<string>()!,
State = PlayerState.ParseFromJObject(jsonObject),
MatchStats = PlayerMatchStats.ParseFromJObject(jsonObject)
};
}
}

View File

@ -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<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>(),
};
}
public override string ToString()
{
return $"{GetType()}\n" +

View File

@ -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<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>(),
};
}
public override string ToString()
{
return $"{GetType()}\n" +

View File

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