CS2GSI/GameState/CS2GameState.cs

60 lines
1.6 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 CS2GameState
{
2024-01-15 20:49:09 +01:00
public string ProviderSteamId;
2024-01-15 20:04:37 +01:00
public int Timestamp;
public Map? Map;
public Player? Player;
2024-01-15 22:08:37 +01:00
public Round? Round;
2024-01-15 20:39:38 +01:00
public override string ToString()
{
return $"{GetType()}\n" +
2024-01-15 20:49:09 +01:00
$"\tTime: {Timestamp}\tSteamId: {ProviderSteamId}\n" +
2024-01-15 20:39:38 +01:00
$"\t{Map}\n" +
2024-01-15 22:08:37 +01:00
$"\t{Round}\n" +
2024-01-15 20:39:38 +01:00
$"\t{Player}\n";
}
2024-01-15 21:49:09 +01:00
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),
2024-01-15 22:08:37 +01:00
Player = GameState.Player.ParseFromJObject(jsonObject),
Round = GameState.Round.ParseFromJObject(jsonObject)
2024-01-15 21:49:09 +01:00
};
}
2024-01-15 21:13:26 +01:00
internal CS2GameState? UpdateGameStateForLocal(CS2GameState? previousLocalState)
{
if (previousLocalState is null)
return this.Player?.SteamId == ProviderSteamId ? this : null;
if (this.Player?.SteamId != ProviderSteamId)
return this.WithPlayer(previousLocalState.Value.Player);
return this;
}
private CS2GameState WithPlayer(Player? player)
{
this.Player = player;
return this;
}
private CS2GameState WithMap(Map? map)
{
this.Map = map;
return this;
}
private CS2GameState WithTimestamp(int timestamp)
{
this.Timestamp = timestamp;
return this;
}
2024-01-15 20:04:37 +01:00
}