Keep running thread

This commit is contained in:
glax 2024-01-15 22:38:20 +01:00
parent 01b5a4945f
commit 8b374e73a9

View File

@ -26,26 +26,32 @@ public class CS2GSI
return;
}
this._gsiServer = new GSIServer(3000);
this._gsiServer.OnMessage += messageJson =>
{
JObject jsonObject = JObject.Parse(messageJson);
CS2GameState newState = CS2GameState.ParseFromJObject(jsonObject);
this.logger?.Log(LogLevel.Debug, $"Received State:\n{newState.ToString()}");
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);
};
this._gsiServer.OnMessage += GsiServerOnOnMessage;
while(this._gsiServer.IsRunning)
Thread.Sleep(10);
}
private void GsiServerOnOnMessage(string messageJson)
{
JObject jsonObject = JObject.Parse(messageJson);
CS2GameState newState = CS2GameState.ParseFromJObject(jsonObject);
this.logger?.Log(LogLevel.Debug, $"Received State:\n{newState.ToString()}");
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);
}
private void InvokeEvents(List<ValueTuple<CS2Event, CS2EventArgs>> cs2Events)
{
foreach(ValueTuple<CS2Event, CS2EventArgs> cs2Event in cs2Events)