From a7c37b6bcf560afd146ef3e36a8344ba7a9f6063 Mon Sep 17 00:00:00 2001 From: miniduikboot Date: Tue, 17 Dec 2024 22:08:23 +0100 Subject: [PATCH] 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 --- src/Impostor.Server/Net/Client.cs | 1 + src/Impostor.Server/Net/Manager/GameManager.cs | 9 +++++++++ 2 files changed, 10 insertions(+) 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); + } + } } } -- 2.39.5