CS2GSI/GameState/Map.cs

51 lines
1.8 KiB
C#

using Newtonsoft.Json.Linq;
namespace CS2GSI.GameState;
public struct Map
{
public string Mode, Name;
public MapPhase Phase;
public int Round, NumMatchesToWinSeries;
public GameStateTeam GameStateTeamCT, GameStateTeamT;
public override string ToString()
{
return $"{GetType()}\n" +
$"\t{Mode} {Name} {Round} Matches to Win Series: {NumMatchesToWinSeries}\n" +
$"\t{Phase}\n" +
$"\t{GameStateTeamCT}\n" +
$"\t{GameStateTeamT}\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 = MapPhaseFromString(jsonObject.SelectToken("map.phase")!.Value<string>()!),
Round = jsonObject.SelectToken("map.round")!.Value<int>(),
NumMatchesToWinSeries = jsonObject.SelectToken("map.num_matches_to_win_series")!.Value<int>(),
GameStateTeamCT = GameStateTeam.ParseFromJObject(jsonObject, CS2Team.CT),
GameStateTeamT = GameStateTeam.ParseFromJObject(jsonObject, CS2Team.T)
}
: null;
}
public enum MapPhase {Warmup, Live, Intermission, GameOver}
private static MapPhase MapPhaseFromString(string str)
{
return str switch
{
"warmup" => MapPhase.Warmup,
"live" => MapPhase.Live,
"intermission" => MapPhase.Intermission,
// ReSharper disable once StringLiteralTypo
"gameover" => MapPhase.GameOver,
_ => throw new ArgumentOutOfRangeException()
};
}
}