Clean up abandoned games on disconnect

When a client disconnects without ever having joined a game, it leaves
behind an empty game on the server. Remove these when a player
disconnects.

Co-Authored-By: js6pak <me@6pak.dev>
This commit is contained in:
miniduikbootandjs6pak committed 2024-12-22 17:02:56 +01:00
1 parent 792c9626e4
commit a7c37b6bcf
2 files changed
+10

No files matched your search

+1
View File
@@ -373,6 +373,7 @@ namespace Impostor.Server.Net
_logger.LogInformation("Client {0} disconnecting, reason: {1}", Id, reason);
_clientManager.Remove(this);
await _gameManager.OnClientDisconnectAsync(this);
}
private bool IsPacketAllowed(IMessageReader message, bool hostOnly)
@@ -142,5 +142,14 @@ namespace Impostor.Server.Net.Manager
return (true, game);
}
internal async ValueTask OnClientDisconnectAsync(IClient client)
{
if (_gamesCreatedBy.TryRemove(client, out var game) && game is { PlayerCount: 0, GameState: not GameStates.Destroyed })
{
_logger.LogWarning("Client {Name}({ClientId}) left empty game open when disconnecting", client.Name, client.Id);
await RemoveAsync(game.Code);
}
}
}
}