Fix null-references
This commit is contained in:
parent
b5b65dbe45
commit
1d5df8a095
@ -40,7 +40,7 @@ public class CS2GSI
|
|||||||
this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState);
|
this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState);
|
||||||
this.logger?.Log(LogLevel.Debug, $"Updated Local State:\n{_lastLocalGameState.ToString()}");
|
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());
|
List<ValueTuple<CS2Event, CS2EventArgs>> generatedEvents = CS2EventGenerator.GenerateEvents(_lastLocalGameState.Value, newState, _allGameStates.Last());
|
||||||
this.logger?.Log(LogLevel.Information, $"Generated {generatedEvents.Count} events.");
|
this.logger?.Log(LogLevel.Information, $"Generated {generatedEvents.Count} events.");
|
||||||
|
@ -12,11 +12,11 @@ public struct CS2GameState
|
|||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"{GetType()}\n" +
|
return $"{GetType().Name}\n" +
|
||||||
$"\tTime: {Timestamp}\tSteamId: {ProviderSteamId}\n" +
|
$"..Time: {Timestamp}\tProviderSteamId: {ProviderSteamId}\n" +
|
||||||
$"\t{Map}\n" +
|
$"..{Map.ToString()?.Replace("\n", "\n...")}\n" +
|
||||||
$"\t{Round}\n" +
|
$"..{Round.ToString()?.Replace("\n", "\n...")}\n" +
|
||||||
$"\t{Player}\n";
|
$"..{Player.ToString()?.Replace("\n", "\n...")}\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static CS2GameState ParseFromJObject(JObject jsonObject)
|
internal static CS2GameState ParseFromJObject(JObject jsonObject)
|
||||||
|
@ -9,11 +9,11 @@ public struct GameStateTeam
|
|||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"{GetType()}\n" +
|
return $"{GetType().Name}\n" +
|
||||||
$"\tScore: {Score}\n" +
|
$"..Team {Team}\tScore: {Score}\n" +
|
||||||
$"\tConsecutiveRoundLosses: {ConsecutiveRoundLosses}\n" +
|
$"..ConsecutiveRoundLosses: {ConsecutiveRoundLosses}\n" +
|
||||||
$"\tTimeoutsRemaining: {TimeoutsRemaining}\n" +
|
$"..TimeoutsRemaining: {TimeoutsRemaining}\n" +
|
||||||
$"\tMatchesWonThisSeries: {MatchesWonThisSeries}\n";
|
$"..MatchesWonThisSeries: {MatchesWonThisSeries}\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static GameStateTeam ParseFromJObject(JObject jsonObject, CS2Team team)
|
internal static GameStateTeam ParseFromJObject(JObject jsonObject, CS2Team team)
|
||||||
|
@ -4,18 +4,20 @@ namespace CS2GSI.GameState;
|
|||||||
|
|
||||||
public struct Map
|
public struct Map
|
||||||
{
|
{
|
||||||
public string Mode, Name;
|
public string Mode, MapName;
|
||||||
public MapPhase Phase;
|
public MapPhase Phase;
|
||||||
public int Round, NumMatchesToWinSeries;
|
public int Round, NumMatchesToWinSeries;
|
||||||
public GameStateTeam GameStateTeamCT, GameStateTeamT;
|
public GameStateTeam GameStateTeamCT, GameStateTeamT;
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"{GetType()}\n" +
|
return $"{GetType().Name}\n" +
|
||||||
$"\t{Mode} {Name} {Round} Matches to Win Series: {NumMatchesToWinSeries}\n" +
|
$"..Mode: {Mode} Map: {MapName}\n" +
|
||||||
$"\t{Phase}\n" +
|
$"..Round: {Round}\n" +
|
||||||
$"\t{GameStateTeamCT}\n" +
|
$"..Matches to Win Series: {NumMatchesToWinSeries}\n" +
|
||||||
$"\t{GameStateTeamT}\n";
|
$"..Phase: {Phase}\n" +
|
||||||
|
$"..{GameStateTeamCT.ToString().Replace("\n", "\n...")}\n" +
|
||||||
|
$"..{GameStateTeamT.ToString().Replace("\n", "\n...")}\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static Map? ParseFromJObject(JObject jsonObject)
|
internal static Map? ParseFromJObject(JObject jsonObject)
|
||||||
@ -24,7 +26,7 @@ public struct Map
|
|||||||
? new Map()
|
? new Map()
|
||||||
{
|
{
|
||||||
Mode = jsonObject.SelectToken("map.mode")!.Value<string>()!,
|
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>()!),
|
Phase = MapPhaseFromString(jsonObject.SelectToken("map.phase")!.Value<string>()!),
|
||||||
Round = jsonObject.SelectToken("map.round")!.Value<int>(),
|
Round = jsonObject.SelectToken("map.round")!.Value<int>(),
|
||||||
NumMatchesToWinSeries = jsonObject.SelectToken("map.num_matches_to_win_series")!.Value<int>(),
|
NumMatchesToWinSeries = jsonObject.SelectToken("map.num_matches_to_win_series")!.Value<int>(),
|
||||||
|
@ -13,34 +13,36 @@ public struct Player
|
|||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"{GetType()}\n" +
|
return $"{GetType().Name}\n" +
|
||||||
$"\t{Name} {SteamId} {Activity} {Team}\n" +
|
$"..Name: {Name} SteamId: {SteamId}\n" +
|
||||||
$"\t{State}\n" +
|
$"..Activity: {Activity}\n" +
|
||||||
$"\t{MatchStats}\n";
|
$"..Team: {Team}\n" +
|
||||||
|
$"..{State.ToString()?.Replace("\n", "\n...")}\n" +
|
||||||
|
$"..{MatchStats.ToString()?.Replace("\n", "\n...")}\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static Player? ParseFromJObject(JObject jsonObject)
|
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>()!,
|
SteamId = jsonObject.SelectToken("player.steamid")!.Value<string>()!,
|
||||||
Name = jsonObject.SelectToken("player.name")!.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>()!),
|
Activity = PlayerActivityFromString(jsonObject.SelectToken("player.activity")!.Value<string>()!),
|
||||||
State = PlayerState.ParseFromJObject(jsonObject),
|
State = PlayerState.ParseFromJObject(jsonObject),
|
||||||
MatchStats = PlayerMatchStats.ParseFromJObject(jsonObject)
|
MatchStats = PlayerMatchStats.ParseFromJObject(jsonObject)
|
||||||
};
|
} : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum PlayerActivity {Playing, Menu, TextInput}
|
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,
|
"t" => CS2Team.T,
|
||||||
"ct" => CS2Team.CT,
|
"ct" => CS2Team.CT,
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => null
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,23 +6,23 @@ public struct PlayerMatchStats
|
|||||||
{
|
{
|
||||||
public int Kills, Assists, Deaths, MVPs, Score;
|
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()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"{GetType()}\n" +
|
return $"{GetType().Name}\n" +
|
||||||
$"\tKAD: {Kills} {Assists} {Deaths}\n" +
|
$"..KAD: {Kills} {Assists} {Deaths}\n" +
|
||||||
$"\tMVPs: {MVPs}\n" +
|
$"..MVPs: {MVPs}\n" +
|
||||||
$"\tScore: {Score}\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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -7,34 +7,34 @@ public struct PlayerState
|
|||||||
public int Health, Armor, Flashed, Smoked, Burning, Money, RoundKills, RoundHs, EquipmentValue;
|
public int Health, Armor, Flashed, Smoked, Burning, Money, RoundKills, RoundHs, EquipmentValue;
|
||||||
public bool Helmet;
|
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()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"{GetType()}\n" +
|
return $"{GetType().Name}\n" +
|
||||||
$"\tHealth: {Health}\n" +
|
$"..Health: {Health}\n" +
|
||||||
$"\tArmor: {Armor}\n" +
|
$"..Armor: {Armor}\n" +
|
||||||
$"\tFlashed: {Flashed}\n" +
|
$"..Flashed: {Flashed}\n" +
|
||||||
$"\tSmoked: {Smoked}\n" +
|
$"..Smoked: {Smoked}\n" +
|
||||||
$"\tBurning: {Burning}\n" +
|
$"..Burning: {Burning}\n" +
|
||||||
$"\tMoney: {Money}\n" +
|
$"..Money: {Money}\n" +
|
||||||
$"\tRoundKills: {RoundKills}\n" +
|
$"..RoundKills: {RoundKills}\n" +
|
||||||
$"\tRoundHs: {RoundHs}\n" +
|
$"..RoundHs: {RoundHs}\n" +
|
||||||
$"\tEquipmentValue: {EquipmentValue}\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;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -5,29 +5,32 @@ namespace CS2GSI.GameState;
|
|||||||
public struct Round
|
public struct Round
|
||||||
{
|
{
|
||||||
public RoundPhase Phase;
|
public RoundPhase Phase;
|
||||||
public BombStatus Bomb;
|
public BombStatus? Bomb;
|
||||||
public CS2Team WinnerTeam;
|
public CS2Team? WinnerTeam;
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"{GetType()}\n" +
|
return $"{GetType().Name}\n" +
|
||||||
$"\t{Phase} {WinnerTeam} {Bomb}\n";
|
$"..Phase: {Phase}\n" +
|
||||||
|
$"..Winner: {WinnerTeam}\n" +
|
||||||
|
$"..Bomb: {Bomb}\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static Round? ParseFromJObject(JObject jsonObject)
|
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>()!),
|
Phase = RoundPhaseFromString(jsonObject.SelectToken("round.phase")!.Value<string>()!),
|
||||||
WinnerTeam = CS2TeamFromString(jsonObject.SelectToken("round.win_team")!.Value<string>()!),
|
WinnerTeam = CS2TeamFromString(jsonObject.SelectToken("round.win_team")?.Value<string>()),
|
||||||
Bomb = BombStatusFromString(jsonObject.SelectToken("round.bomb")!.Value<string>()!)
|
Bomb = BombStatusFromString(jsonObject.SelectToken("round.bomb")?.Value<string>())
|
||||||
};
|
} : null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public enum RoundPhase
|
public enum RoundPhase
|
||||||
{
|
{
|
||||||
Over, Freezetime, Live
|
Over, Freezetime, Live
|
||||||
}
|
}
|
||||||
|
|
||||||
private static RoundPhase RoundPhaseFromString(string str)
|
private static RoundPhase RoundPhaseFromString(string str)
|
||||||
{
|
{
|
||||||
return str switch
|
return str switch
|
||||||
@ -44,24 +47,24 @@ public struct Round
|
|||||||
Planted, Exploded, Defused
|
Planted, Exploded, Defused
|
||||||
}
|
}
|
||||||
|
|
||||||
private static BombStatus BombStatusFromString(string str)
|
private static BombStatus? BombStatusFromString(string? str)
|
||||||
{
|
{
|
||||||
return str switch
|
return str switch
|
||||||
{
|
{
|
||||||
"planted" => BombStatus.Planted,
|
"planted" => BombStatus.Planted,
|
||||||
"exploded" => BombStatus.Exploded,
|
"exploded" => BombStatus.Exploded,
|
||||||
"defused" => BombStatus.Defused,
|
"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,
|
"t" => CS2Team.T,
|
||||||
"ct" => CS2Team.CT,
|
"ct" => CS2Team.CT,
|
||||||
_ => throw new ArgumentOutOfRangeException()
|
_ => null
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user