CS2GSI/GameState/Player.cs

58 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 Player
{
2024-01-15 22:22:36 +01:00
public string SteamId, Name;
public PlayerActivity Activity;
public CS2Team? Team;
2024-01-15 20:04:37 +01:00
public int? ObserverSlot;
public PlayerState? State;
public PlayerMatchStats? MatchStats;
2024-01-15 20:39:38 +01:00
public override string ToString()
{
return $"{GetType()}\n" +
$"\t{Name} {SteamId} {Activity} {Team}\n" +
$"\t{State}\n" +
$"\t{MatchStats}\n";
}
2024-01-15 21:49:09 +01:00
internal static Player? ParseFromJObject(JObject jsonObject)
{
return new Player()
{
SteamId = jsonObject.SelectToken("player.steamid")!.Value<string>()!,
Name = jsonObject.SelectToken("player.name")!.Value<string>()!,
2024-01-15 22:22:36 +01:00
Team = CS2TeamFromString(jsonObject.SelectToken("player.team")!.Value<string>()!),
Activity = PlayerActivityFromString(jsonObject.SelectToken("player.activity")!.Value<string>()!),
2024-01-15 21:49:09 +01:00
State = PlayerState.ParseFromJObject(jsonObject),
MatchStats = PlayerMatchStats.ParseFromJObject(jsonObject)
};
}
2024-01-15 22:22:36 +01:00
public enum PlayerActivity {Playing, Menu, TextInput}
private static CS2Team CS2TeamFromString(string str)
{
2024-01-15 22:36:53 +01:00
return str.ToLower() switch
2024-01-15 22:22:36 +01:00
{
"t" => CS2Team.T,
"ct" => CS2Team.CT,
_ => throw new ArgumentOutOfRangeException()
};
}
private static PlayerActivity PlayerActivityFromString(string str)
{
return str switch
{
"playing" => PlayerActivity.Playing,
"menu" => PlayerActivity.Menu,
// ReSharper disable once StringLiteralTypo
"textinput" => PlayerActivity.TextInput,
_ => throw new ArgumentOutOfRangeException()
};
}
2024-01-15 20:04:37 +01:00
}