CS2GSI/GameState/Round.cs

67 lines
1.7 KiB
C#
Raw Normal View History

2024-01-15 22:08:37 +01:00
using Newtonsoft.Json.Linq;
namespace CS2GSI.GameState;
public struct Round
{
2024-01-15 22:22:36 +01:00
public RoundPhase Phase;
2024-01-15 22:36:53 +01:00
public BombStatus Bomb;
public CS2Team WinnerTeam;
2024-01-15 22:08:37 +01:00
public override string ToString()
{
return $"{GetType()}\n" +
2024-01-15 22:36:53 +01:00
$"\t{Phase} {WinnerTeam} {Bomb}\n";
2024-01-15 22:08:37 +01:00
}
internal static Round? ParseFromJObject(JObject jsonObject)
{
return new Round()
{
2024-01-15 22:22:36 +01:00
Phase = RoundPhaseFromString(jsonObject.SelectToken("round.phase")!.Value<string>()!),
2024-01-15 22:36:53 +01:00
WinnerTeam = CS2TeamFromString(jsonObject.SelectToken("round.win_team")!.Value<string>()!),
Bomb = BombStatusFromString(jsonObject.SelectToken("round.bomb")!.Value<string>()!)
2024-01-15 22:08:37 +01:00
};
}
2024-01-15 22:22:36 +01:00
public enum RoundPhase
{
Over, Freezetime, Live
}
private static RoundPhase RoundPhaseFromString(string str)
{
return str switch
{
"over" => RoundPhase.Over,
"live" => RoundPhase.Live,
"freezetime" => RoundPhase.Freezetime,
_ => throw new ArgumentOutOfRangeException()
};
}
2024-01-15 22:36:53 +01:00
public enum BombStatus
{
Planted, Exploded, Defused
}
private static BombStatus BombStatusFromString(string str)
{
return str switch
{
"planted" => BombStatus.Planted,
"exploded" => BombStatus.Exploded,
"defused" => BombStatus.Defused,
_ => throw new ArgumentOutOfRangeException()
};
}
private static CS2Team CS2TeamFromString(string str)
{
return str.ToLower() switch
{
"t" => CS2Team.T,
"ct" => CS2Team.CT,
_ => throw new ArgumentOutOfRangeException()
};
}
2024-01-15 22:08:37 +01:00
}