Fix null-references

This commit is contained in:
glax 2024-01-15 23:32:57 +01:00
parent b5b65dbe45
commit 1d5df8a095
8 changed files with 91 additions and 84 deletions

View File

@ -40,7 +40,7 @@ public class CS2GSI
this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState);
this.logger?.Log(LogLevel.Debug, $"Updated Local State:\n{_lastLocalGameState.ToString()}");
if (_lastLocalGameState is not null)
if (_lastLocalGameState is not null && _allGameStates.Count > 0)
{
List<ValueTuple<CS2Event, CS2EventArgs>> generatedEvents = CS2EventGenerator.GenerateEvents(_lastLocalGameState.Value, newState, _allGameStates.Last());
this.logger?.Log(LogLevel.Information, $"Generated {generatedEvents.Count} events.");

View File

@ -12,11 +12,11 @@ public struct CS2GameState
public override string ToString()
{
return $"{GetType()}\n" +
$"\tTime: {Timestamp}\tSteamId: {ProviderSteamId}\n" +
$"\t{Map}\n" +
$"\t{Round}\n" +
$"\t{Player}\n";
return $"{GetType().Name}\n" +
$"..Time: {Timestamp}\tProviderSteamId: {ProviderSteamId}\n" +
$"..{Map.ToString()?.Replace("\n", "\n...")}\n" +
$"..{Round.ToString()?.Replace("\n", "\n...")}\n" +
$"..{Player.ToString()?.Replace("\n", "\n...")}\n";
}
internal static CS2GameState ParseFromJObject(JObject jsonObject)

View File

@ -9,11 +9,11 @@ public struct GameStateTeam
public override string ToString()
{
return $"{GetType()}\n" +
$"\tScore: {Score}\n" +
$"\tConsecutiveRoundLosses: {ConsecutiveRoundLosses}\n" +
$"\tTimeoutsRemaining: {TimeoutsRemaining}\n" +
$"\tMatchesWonThisSeries: {MatchesWonThisSeries}\n";
return $"{GetType().Name}\n" +
$"..Team {Team}\tScore: {Score}\n" +
$"..ConsecutiveRoundLosses: {ConsecutiveRoundLosses}\n" +
$"..TimeoutsRemaining: {TimeoutsRemaining}\n" +
$"..MatchesWonThisSeries: {MatchesWonThisSeries}\n";
}
internal static GameStateTeam ParseFromJObject(JObject jsonObject, CS2Team team)

View File

@ -4,18 +4,20 @@ namespace CS2GSI.GameState;
public struct Map
{
public string Mode, Name;
public string Mode, MapName;
public MapPhase Phase;
public int Round, NumMatchesToWinSeries;
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{GameStateTeamCT}\n" +
$"\t{GameStateTeamT}\n";
return $"{GetType().Name}\n" +
$"..Mode: {Mode} Map: {MapName}\n" +
$"..Round: {Round}\n" +
$"..Matches to Win Series: {NumMatchesToWinSeries}\n" +
$"..Phase: {Phase}\n" +
$"..{GameStateTeamCT.ToString().Replace("\n", "\n...")}\n" +
$"..{GameStateTeamT.ToString().Replace("\n", "\n...")}\n";
}
internal static Map? ParseFromJObject(JObject jsonObject)
@ -24,7 +26,7 @@ public struct Map
? new Map()
{
Mode = jsonObject.SelectToken("map.mode")!.Value<string>()!,
Name = jsonObject.SelectToken("map.name")!.Value<string>()!,
MapName = jsonObject.SelectToken("map.name")!.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>(),

View File

@ -13,34 +13,36 @@ public struct Player
public override string ToString()
{
return $"{GetType()}\n" +
$"\t{Name} {SteamId} {Activity} {Team}\n" +
$"\t{State}\n" +
$"\t{MatchStats}\n";
return $"{GetType().Name}\n" +
$"..Name: {Name} SteamId: {SteamId}\n" +
$"..Activity: {Activity}\n" +
$"..Team: {Team}\n" +
$"..{State.ToString()?.Replace("\n", "\n...")}\n" +
$"..{MatchStats.ToString()?.Replace("\n", "\n...")}\n";
}
internal static Player? ParseFromJObject(JObject jsonObject)
{
return new Player()
return jsonObject.SelectToken("player") is not null ? new Player()
{
SteamId = jsonObject.SelectToken("player.steamid")!.Value<string>()!,
Name = jsonObject.SelectToken("player.name")!.Value<string>()!,
Team = CS2TeamFromString(jsonObject.SelectToken("player.team")!.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)
};
} : null;
}
public enum PlayerActivity {Playing, Menu, TextInput}
private static CS2Team CS2TeamFromString(string str)
private static CS2Team? CS2TeamFromString(string? str)
{
return str.ToLower() switch
return str?.ToLower() switch
{
"t" => CS2Team.T,
"ct" => CS2Team.CT,
_ => throw new ArgumentOutOfRangeException()
_ => null
};
}

View File

@ -6,23 +6,23 @@ public struct PlayerMatchStats
{
public int Kills, Assists, Deaths, MVPs, Score;
internal static PlayerMatchStats ParseFromJObject(JObject jsonObject)
{
return new PlayerMatchStats()
{
Kills = jsonObject.SelectToken($"player.match_stats.kills")!.Value<int>(),
Assists = jsonObject.SelectToken($"player.match_stats.assists")!.Value<int>(),
Deaths = jsonObject.SelectToken($"player.match_stats.deaths")!.Value<int>(),
MVPs = jsonObject.SelectToken($"player.match_stats.mvps")!.Value<int>(),
Score = jsonObject.SelectToken($"player.match_stats.score")!.Value<int>(),
};
}
public override string ToString()
{
return $"{GetType()}\n" +
$"\tKAD: {Kills} {Assists} {Deaths}\n" +
$"\tMVPs: {MVPs}\n" +
$"\tScore: {Score}\n";
return $"{GetType().Name}\n" +
$"..KAD: {Kills} {Assists} {Deaths}\n" +
$"..MVPs: {MVPs}\n" +
$"..Score: {Score}\n";
}
internal static PlayerMatchStats? ParseFromJObject(JObject jsonObject)
{
return jsonObject.SelectToken("player.match_stats") is not null ? new PlayerMatchStats()
{
Kills = jsonObject.SelectToken("player.match_stats.kills")!.Value<int>(),
Assists = jsonObject.SelectToken("player.match_stats.assists")!.Value<int>(),
Deaths = jsonObject.SelectToken("player.match_stats.deaths")!.Value<int>(),
MVPs = jsonObject.SelectToken("player.match_stats.mvps")!.Value<int>(),
Score = jsonObject.SelectToken("player.match_stats.score")!.Value<int>(),
} : null;
}
}

View File

@ -7,34 +7,34 @@ public struct PlayerState
public int Health, Armor, Flashed, Smoked, Burning, Money, RoundKills, RoundHs, EquipmentValue;
public bool Helmet;
internal static PlayerState ParseFromJObject(JObject jsonObject)
{
return new PlayerState()
{
Health = jsonObject.SelectToken($"player.state.health")!.Value<int>(),
Armor = jsonObject.SelectToken($"player.state.armor")!.Value<int>(),
Helmet = jsonObject.SelectToken($"player.state.helmet")!.Value<bool>(),
Flashed = jsonObject.SelectToken($"player.state.flashed")!.Value<int>(),
Smoked = jsonObject.SelectToken($"player.state.smoked")!.Value<int>(),
Burning = jsonObject.SelectToken($"player.state.burning")!.Value<int>(),
Money = jsonObject.SelectToken($"player.state.money")!.Value<int>(),
RoundKills = jsonObject.SelectToken($"player.state.round_kills")!.Value<int>(),
RoundHs = jsonObject.SelectToken($"player.state.round_killhs")!.Value<int>(),
EquipmentValue = jsonObject.SelectToken($"player.state.equip_value")!.Value<int>(),
};
}
public override string ToString()
{
return $"{GetType()}\n" +
$"\tHealth: {Health}\n" +
$"\tArmor: {Armor}\n" +
$"\tFlashed: {Flashed}\n" +
$"\tSmoked: {Smoked}\n" +
$"\tBurning: {Burning}\n" +
$"\tMoney: {Money}\n" +
$"\tRoundKills: {RoundKills}\n" +
$"\tRoundHs: {RoundHs}\n" +
$"\tEquipmentValue: {EquipmentValue}\n";
return $"{GetType().Name}\n" +
$"..Health: {Health}\n" +
$"..Armor: {Armor}\n" +
$"..Flashed: {Flashed}\n" +
$"..Smoked: {Smoked}\n" +
$"..Burning: {Burning}\n" +
$"..Money: {Money}\n" +
$"..RoundKills: {RoundKills}\n" +
$"..RoundHs: {RoundHs}\n" +
$"..EquipmentValue: {EquipmentValue}\n";
}
internal static PlayerState? ParseFromJObject(JObject jsonObject)
{
return jsonObject.SelectToken("player.state") is not null ? new PlayerState()
{
Health = jsonObject.SelectToken("player.state.health")!.Value<int>(),
Armor = jsonObject.SelectToken("player.state.armor")!.Value<int>(),
Helmet = jsonObject.SelectToken("player.state.helmet")!.Value<bool>(),
Flashed = jsonObject.SelectToken("player.state.flashed")!.Value<int>(),
Smoked = jsonObject.SelectToken("player.state.smoked")!.Value<int>(),
Burning = jsonObject.SelectToken("player.state.burning")!.Value<int>(),
Money = jsonObject.SelectToken("player.state.money")!.Value<int>(),
RoundKills = jsonObject.SelectToken("player.state.round_kills")!.Value<int>(),
RoundHs = jsonObject.SelectToken("player.state.round_killhs")!.Value<int>(),
EquipmentValue = jsonObject.SelectToken("player.state.equip_value")!.Value<int>(),
} : null;
}
}

View File

@ -5,29 +5,32 @@ namespace CS2GSI.GameState;
public struct Round
{
public RoundPhase Phase;
public BombStatus Bomb;
public CS2Team WinnerTeam;
public BombStatus? Bomb;
public CS2Team? WinnerTeam;
public override string ToString()
{
return $"{GetType()}\n" +
$"\t{Phase} {WinnerTeam} {Bomb}\n";
return $"{GetType().Name}\n" +
$"..Phase: {Phase}\n" +
$"..Winner: {WinnerTeam}\n" +
$"..Bomb: {Bomb}\n";
}
internal static Round? ParseFromJObject(JObject jsonObject)
{
return new Round()
return jsonObject.SelectToken("round") is not null ? new Round()
{
Phase = RoundPhaseFromString(jsonObject.SelectToken("round.phase")!.Value<string>()!),
WinnerTeam = CS2TeamFromString(jsonObject.SelectToken("round.win_team")!.Value<string>()!),
Bomb = BombStatusFromString(jsonObject.SelectToken("round.bomb")!.Value<string>()!)
};
WinnerTeam = CS2TeamFromString(jsonObject.SelectToken("round.win_team")?.Value<string>()),
Bomb = BombStatusFromString(jsonObject.SelectToken("round.bomb")?.Value<string>())
} : null;
}
public enum RoundPhase
{
Over, Freezetime, Live
}
private static RoundPhase RoundPhaseFromString(string str)
{
return str switch
@ -44,24 +47,24 @@ public struct Round
Planted, Exploded, Defused
}
private static BombStatus BombStatusFromString(string str)
private static BombStatus? BombStatusFromString(string? str)
{
return str switch
{
"planted" => BombStatus.Planted,
"exploded" => BombStatus.Exploded,
"defused" => BombStatus.Defused,
_ => throw new ArgumentOutOfRangeException()
_ => null
};
}
private static CS2Team CS2TeamFromString(string str)
private static CS2Team? CS2TeamFromString(string? str)
{
return str.ToLower() switch
return str?.ToLower() switch
{
"t" => CS2Team.T,
"ct" => CS2Team.CT,
_ => throw new ArgumentOutOfRangeException()
_ => null
};
}
}