Add custom enums for GameStates

This commit is contained in:
glax 2024-01-15 22:22:36 +01:00
parent 63c33e7227
commit 4cfe1dcc4d
6 changed files with 86 additions and 20 deletions

View File

@ -1,6 +1,7 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeStyle/Naming/CSharpNaming/Abbreviations/=CS/@EntryIndexedValue">CS</s:String>
<s:Boolean x:Key="/Default/UserDictionary/Words/=appmanifest/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Freezetime/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=gamestate/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=installdir/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=killhs/@EntryIndexedValue">True</s:Boolean>

6
GameState/CS2Team.cs Normal file
View File

@ -0,0 +1,6 @@
namespace CS2GSI.GameState;
public enum CS2Team
{
T, CT
}

View File

@ -2,8 +2,9 @@
namespace CS2GSI.GameState;
public struct Team
public struct GameStateTeam
{
public CS2Team Team;
public int Score, ConsecutiveRoundLosses, TimeoutsRemaining, MatchesWonThisSeries;
public override string ToString()
@ -15,14 +16,15 @@ public struct Team
$"\tMatchesWonThisSeries: {MatchesWonThisSeries}\n";
}
internal static Team ParseFromJObject(JObject jsonObject, string team)
internal static GameStateTeam ParseFromJObject(JObject jsonObject, CS2Team team)
{
return new Team()
return new GameStateTeam()
{
Score = jsonObject.SelectToken($"map.team_{team}.score")!.Value<int>(),
ConsecutiveRoundLosses = jsonObject.SelectToken($"map.team_{team}.consecutive_round_losses")!.Value<int>(),
TimeoutsRemaining = jsonObject.SelectToken($"map.team_{team}.timeouts_remaining")!.Value<int>(),
MatchesWonThisSeries = jsonObject.SelectToken($"map.team_{team}.matches_won_this_series")!.Value<int>(),
Team = team,
Score = jsonObject.SelectToken($"map.team_{team.ToString().ToLower()}.score")!.Value<int>(),
ConsecutiveRoundLosses = jsonObject.SelectToken($"map.team_{team.ToString().ToLower()}.consecutive_round_losses")!.Value<int>(),
TimeoutsRemaining = jsonObject.SelectToken($"map.team_{team.ToString().ToLower()}.timeouts_remaining")!.Value<int>(),
MatchesWonThisSeries = jsonObject.SelectToken($"map.team_{team.ToString().ToLower()}.matches_won_this_series")!.Value<int>(),
};
}
}

View File

@ -4,17 +4,18 @@ namespace CS2GSI.GameState;
public struct Map
{
public string Mode, Name, Phase;
public string Mode, Name;
public MapPhase Phase;
public int Round, NumMatchesToWinSeries;
public Team TeamCT, TeamT;
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{TeamCT}\n" +
$"\t{TeamT}\n";
$"\t{GameStateTeamCT}\n" +
$"\t{GameStateTeamT}\n";
}
internal static Map? ParseFromJObject(JObject jsonObject)
@ -24,12 +25,27 @@ public struct Map
{
Mode = jsonObject.SelectToken("map.mode")!.Value<string>()!,
Name = jsonObject.SelectToken("map.name")!.Value<string>()!,
Phase = jsonObject.SelectToken("map.phase")!.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>(),
TeamCT = Team.ParseFromJObject(jsonObject, "ct"),
TeamT = Team.ParseFromJObject(jsonObject, "t")
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()
};
}
}

View File

@ -4,8 +4,9 @@ namespace CS2GSI.GameState;
public struct Player
{
public string SteamId, Name, Activity;
public string? Team;
public string SteamId, Name;
public PlayerActivity Activity;
public CS2Team? Team;
public int? ObserverSlot;
public PlayerState? State;
public PlayerMatchStats? MatchStats;
@ -24,10 +25,34 @@ public struct Player
{
SteamId = jsonObject.SelectToken("player.steamid")!.Value<string>()!,
Name = jsonObject.SelectToken("player.name")!.Value<string>()!,
Team = jsonObject.SelectToken("player.team")!.Value<string>()!,
Activity = jsonObject.SelectToken("player.activity")!.Value<string>()!,
Team = CS2TeamFromString(jsonObject.SelectToken("player.team")!.Value<string>()!),
Activity = PlayerActivityFromString(jsonObject.SelectToken("player.activity")!.Value<string>()!),
State = PlayerState.ParseFromJObject(jsonObject),
MatchStats = PlayerMatchStats.ParseFromJObject(jsonObject)
};
}
public enum PlayerActivity {Playing, Menu, TextInput}
private static CS2Team CS2TeamFromString(string str)
{
return str switch
{
"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()
};
}
}

View File

@ -4,7 +4,8 @@ namespace CS2GSI.GameState;
public struct Round
{
public string Phase, WinnerTeam, BombStatus;
public RoundPhase Phase;
public string WinnerTeam, BombStatus;
public override string ToString()
{
@ -16,9 +17,24 @@ public struct Round
{
return new Round()
{
Phase = jsonObject.SelectToken("round.phase")!.Value<string>()!,
Phase = RoundPhaseFromString(jsonObject.SelectToken("round.phase")!.Value<string>()!),
WinnerTeam = jsonObject.SelectToken("round.win_team")!.Value<string>()!,
BombStatus = jsonObject.SelectToken("round.bomb")!.Value<string>()!
};
}
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()
};
}
}