CS2GSI/GameState/Map.cs

51 lines
1.8 KiB
C#
Raw Normal View History

2024-01-15 21:49:09 +01:00
using Newtonsoft.Json.Linq;
namespace CS2GSI.GameState;
2024-01-15 20:04:37 +01:00
public struct Map
{
2024-01-15 22:22:36 +01:00
public string Mode, Name;
public MapPhase Phase;
2024-01-15 20:04:37 +01:00
public int Round, NumMatchesToWinSeries;
2024-01-15 22:22:36 +01:00
public GameStateTeam GameStateTeamCT, GameStateTeamT;
2024-01-15 20:39:38 +01:00
public override string ToString()
{
return $"{GetType()}\n" +
$"\t{Mode} {Name} {Round} Matches to Win Series: {NumMatchesToWinSeries}\n" +
$"\t{Phase}\n" +
2024-01-15 22:22:36 +01:00
$"\t{GameStateTeamCT}\n" +
$"\t{GameStateTeamT}\n";
2024-01-15 20:39:38 +01:00
}
2024-01-15 21:49:09 +01:00
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>()!,
2024-01-15 22:22:36 +01:00
Phase = MapPhaseFromString(jsonObject.SelectToken("map.phase")!.Value<string>()!),
2024-01-15 21:49:09 +01:00
Round = jsonObject.SelectToken("map.round")!.Value<int>(),
NumMatchesToWinSeries = jsonObject.SelectToken("map.num_matches_to_win_series")!.Value<int>(),
2024-01-15 22:22:36 +01:00
GameStateTeamCT = GameStateTeam.ParseFromJObject(jsonObject, CS2Team.CT),
GameStateTeamT = GameStateTeam.ParseFromJObject(jsonObject, CS2Team.T)
2024-01-15 21:49:09 +01:00
}
: null;
}
2024-01-15 22:22:36 +01:00
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()
};
}
2024-01-15 20:04:37 +01:00
}