From: miniduikboot Date: Tue, 17 Dec 2024 21:08:23 +0000 (+0100) Subject: Clean up abandoned games on disconnect X-Git-Tag: v1.10.2~2 X-Git-Url: https://git.deb.at/?a=commitdiff_plain;h=a7c37b6bcf560afd146ef3e36a8344ba7a9f6063;p=rhonda%2Fimpostor.git 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 --- diff --git a/src/Impostor.Server/Net/Client.cs b/src/Impostor.Server/Net/Client.cs index 25d1054..20cd05e 100644 --- a/src/Impostor.Server/Net/Client.cs +++ b/src/Impostor.Server/Net/Client.cs @@ -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) diff --git a/src/Impostor.Server/Net/Manager/GameManager.cs b/src/Impostor.Server/Net/Manager/GameManager.cs index 7de311a..d8d5e97 100644 --- a/src/Impostor.Server/Net/Manager/GameManager.cs +++ b/src/Impostor.Server/Net/Manager/GameManager.cs @@ -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); + } + } } }