Compare commits

..

No commits in common. "1c161cde5567afa7fa3e69e11df2696d7214c71d" and "b5b65dbe456ed153c2fe46dd6a58d187f039187c" have entirely different histories.

12 changed files with 105 additions and 133 deletions

View File

@ -2,20 +2,5 @@
public class CS2EventArgs : EventArgs
{
public object? Value;
public CS2EventArgs(object? value = null)
{
this.Value = value;
}
public T? ValueAsOrDefault<T>()
{
return Value is T val ? val : default;
}
public override string ToString()
{
return Value?.ToString() ?? "NoArgs";
}
}

View File

@ -12,12 +12,10 @@ internal static class CS2EventGenerator
events.AddRange(GenerateMapEvents(previousPlayerState, newGameState, lastGameState));
if(lastGameState.Player?.SteamId != newGameState.Player?.SteamId)
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnPlayerChange, new CS2EventArgs(newGameState.Player?.SteamId)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnPlayerChange, new CS2EventArgs()));
if(events.Count > 0)
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyEvent, new CS2EventArgs(events.Count)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyMessage, new CS2EventArgs()));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.AnyEvent, new CS2EventArgs()));
return events;
}
@ -65,13 +63,13 @@ internal static class CS2EventGenerator
return events;
if(newGameState.Player?.MatchStats?.Kills > previousPlayerState.Player?.MatchStats?.Kills && newGameState.Player is { MatchStats: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnKill, new CS2EventArgs(newGameState.Player?.MatchStats?.Kills)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnKill, new CS2EventArgs()));
if(newGameState.Player?.State?.RoundHs > previousPlayerState.Player?.State?.RoundHs && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHeadshot, new CS2EventArgs(newGameState.Player?.State?.RoundHs)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHeadshot, new CS2EventArgs()));
if(newGameState.Player?.MatchStats?.Deaths > previousPlayerState.Player?.MatchStats?.Deaths && newGameState.Player is { MatchStats: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDeath, new CS2EventArgs(newGameState.Player?.MatchStats?.Deaths)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDeath, new CS2EventArgs()));
if(newGameState.Player?.State?.Flashed > 0 && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnFlashed, new CS2EventArgs()));
@ -83,25 +81,25 @@ internal static class CS2EventGenerator
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnSmoked, new CS2EventArgs()));
if(newGameState.Player?.State?.Health < previousPlayerState.Player?.State?.Health && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDamageTaken, new CS2EventArgs(newGameState.Player?.State?.Health)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnDamageTaken, new CS2EventArgs()));
if(newGameState.Player?.State?.Health != previousPlayerState.Player?.State?.Health && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHealthChange, new CS2EventArgs(newGameState.Player?.State?.Health)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHealthChange, new CS2EventArgs()));
if(newGameState.Player?.State?.Money != previousPlayerState.Player?.State?.Money && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnMoneyChange, new CS2EventArgs(newGameState.Player?.State?.Money)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnMoneyChange, new CS2EventArgs()));
if(newGameState.Player?.State?.Armor != previousPlayerState.Player?.State?.Armor && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnArmorChange, new CS2EventArgs(newGameState.Player?.State?.Armor)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnArmorChange, new CS2EventArgs()));
if(newGameState.Player?.State?.Helmet != previousPlayerState.Player?.State?.Helmet && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHelmetChange, new CS2EventArgs(newGameState.Player?.State?.Helmet)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnHelmetChange, new CS2EventArgs()));
if(newGameState.Player?.State?.EquipmentValue != previousPlayerState.Player?.State?.EquipmentValue && newGameState.Player is { State: not null})
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnEquipmentValueChange, new CS2EventArgs(newGameState.Player?.State?.EquipmentValue)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnEquipmentValueChange, new CS2EventArgs()));
if(newGameState.Player?.Team != previousPlayerState.Player?.Team && newGameState.Player is not null)
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnTeamChange, new CS2EventArgs(newGameState.Player?.Team)));
events.Add(new ValueTuple<CS2Event, CS2EventArgs>(CS2Event.OnTeamChange, new CS2EventArgs()));
return events;

View File

@ -37,15 +37,17 @@ public class CS2GSI
JObject jsonObject = JObject.Parse(messageJson);
CS2GameState newState = CS2GameState.ParseFromJObject(jsonObject);
this.logger?.Log(LogLevel.Debug, $"Received State:\n{newState.ToString()}");
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:\n\t{string.Join("\n\t", generatedEvents)}");
InvokeEvents(generatedEvents);
}
this._lastLocalGameState = newState.UpdateGameStateForLocal(_lastLocalGameState);
this.logger?.Log(LogLevel.Debug, $"Updated Local State:\n{_lastLocalGameState.ToString()}");
if (_lastLocalGameState is not null)
{
List<ValueTuple<CS2Event, CS2EventArgs>> generatedEvents = CS2EventGenerator.GenerateEvents(_lastLocalGameState.Value, newState, _allGameStates.Last());
this.logger?.Log(LogLevel.Information, $"Generated {generatedEvents.Count} events.");
if(generatedEvents.Count > 0)
this.logger?.Log(LogLevel.Debug, $"Events:\n\t{string.Join("\n\t", generatedEvents)}");
InvokeEvents(generatedEvents);
}
_allGameStates.Add(newState);
}
@ -132,9 +134,6 @@ public class CS2GSI
case CS2Event.AnyEvent:
AnyEvent?.Invoke(cs2Event.Item2);
break;
case CS2Event.AnyMessage:
AnyMessage?.Invoke(cs2Event.Item2);
break;
default:
this.logger?.Log(LogLevel.Error, $"Unknown Event {cs2Event}");
return;
@ -165,8 +164,7 @@ public class CS2GSI
OnBombPlanted,
OnBombDefused,
OnBombExploded,
AnyEvent,
AnyMessage
AnyEvent
}
public delegate void CS2EventHandler(CS2EventArgs eventArgs);
@ -194,7 +192,6 @@ public class CS2GSI
OnBombPlanted,
OnBombDefused,
OnBombExploded,
AnyEvent,
AnyMessage;
AnyEvent;
}

View File

@ -44,8 +44,8 @@ internal class GSIServer
StreamReader reader = new (request.InputStream, request.ContentEncoding);
string content = await reader.ReadToEndAsync();
this.logger?.Log(LogLevel.Debug, content);
OnMessage?.Invoke(content);
this.logger?.Log(LogLevel.Debug, content);
}
HttpListener.Close();
IsRunning = false;

View File

@ -12,11 +12,11 @@ public struct CS2GameState
public override string ToString()
{
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";
return $"{GetType()}\n" +
$"\tTime: {Timestamp}\tSteamId: {ProviderSteamId}\n" +
$"\t{Map}\n" +
$"\t{Round}\n" +
$"\t{Player}\n";
}
internal static CS2GameState ParseFromJObject(JObject jsonObject)

View File

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

View File

@ -4,20 +4,18 @@ namespace CS2GSI.GameState;
public struct Map
{
public string Mode, MapName;
public string Mode, Name;
public MapPhase Phase;
public int Round, NumMatchesToWinSeries;
public GameStateTeam GameStateTeamCT, GameStateTeamT;
public override string ToString()
{
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";
return $"{GetType()}\n" +
$"\t{Mode} {Name} {Round} Matches to Win Series: {NumMatchesToWinSeries}\n" +
$"\t{Phase}\n" +
$"\t{GameStateTeamCT}\n" +
$"\t{GameStateTeamT}\n";
}
internal static Map? ParseFromJObject(JObject jsonObject)
@ -26,7 +24,7 @@ public struct Map
? new Map()
{
Mode = jsonObject.SelectToken("map.mode")!.Value<string>()!,
MapName = jsonObject.SelectToken("map.name")!.Value<string>()!,
Name = 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,36 +13,34 @@ public struct Player
public override string ToString()
{
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";
return $"{GetType()}\n" +
$"\t{Name} {SteamId} {Activity} {Team}\n" +
$"\t{State}\n" +
$"\t{MatchStats}\n";
}
internal static Player? ParseFromJObject(JObject jsonObject)
{
return jsonObject.SelectToken("player") is not null ? new Player()
return 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,
_ => null
_ => throw new ArgumentOutOfRangeException()
};
}

View File

@ -6,23 +6,23 @@ public struct PlayerMatchStats
{
public int Kills, Assists, Deaths, MVPs, Score;
public override string ToString()
internal static PlayerMatchStats ParseFromJObject(JObject jsonObject)
{
return $"{GetType().Name}\n" +
$"..KAD: {Kills} {Assists} {Deaths}\n" +
$"..MVPs: {MVPs}\n" +
$"..Score: {Score}\n";
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>(),
};
}
internal static PlayerMatchStats? ParseFromJObject(JObject jsonObject)
public override string ToString()
{
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;
return $"{GetType()}\n" +
$"\tKAD: {Kills} {Assists} {Deaths}\n" +
$"\tMVPs: {MVPs}\n" +
$"\tScore: {Score}\n";
}
}

View File

@ -7,34 +7,34 @@ public struct PlayerState
public int Health, Armor, Flashed, Smoked, Burning, Money, RoundKills, RoundHs, EquipmentValue;
public bool Helmet;
public override string ToString()
internal static PlayerState ParseFromJObject(JObject jsonObject)
{
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";
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>(),
};
}
internal static PlayerState? ParseFromJObject(JObject jsonObject)
public override string ToString()
{
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;
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";
}
}

View File

@ -5,32 +5,29 @@ 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().Name}\n" +
$"..Phase: {Phase}\n" +
$"..Winner: {WinnerTeam}\n" +
$"..Bomb: {Bomb}\n";
return $"{GetType()}\n" +
$"\t{Phase} {WinnerTeam} {Bomb}\n";
}
internal static Round? ParseFromJObject(JObject jsonObject)
{
return jsonObject.SelectToken("round") is not null ? new Round()
return 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>())
} : null;
WinnerTeam = CS2TeamFromString(jsonObject.SelectToken("round.win_team")!.Value<string>()!),
Bomb = BombStatusFromString(jsonObject.SelectToken("round.bomb")!.Value<string>()!)
};
}
public enum RoundPhase
{
Over, Freezetime, Live
}
private static RoundPhase RoundPhaseFromString(string str)
{
return str switch
@ -47,24 +44,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,
_ => null
_ => throw new ArgumentOutOfRangeException()
};
}
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,
_ => null
_ => throw new ArgumentOutOfRangeException()
};
}
}

View File

@ -1,12 +1,11 @@
// See https://aka.ms/new-console-template for more information
using CS2GSI.TestApp;
using Microsoft.Extensions.Logging;
public class TestApp
{
public static void Main(string[] args)
{
new CS2GSI.CS2GSI(new Logger(LogLevel.Information));
new CS2GSI.CS2GSI(new Logger());
}
}