]> git.deb.at Git - rhonda/impostor.git/commitdiff
Clean up abandoned games on disconnect
authorminiduikboot <mini@duikbo.at>
Tue, 17 Dec 2024 21:08:23 +0000 (22:08 +0100)
committerminiduikboot <mini@duikbo.at>
Sun, 22 Dec 2024 16:02:56 +0000 (17:02 +0100)
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>
src/Impostor.Server/Net/Client.cs
src/Impostor.Server/Net/Manager/GameManager.cs

index 25d105428ac875b7b46507178c476f957fd75bf9..20cd05ef03582b7a224b7efca6045d3ef56d579b 100644 (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)
index 7de311a919db66bb5c7020e0f85345d6de97c0c0..d8d5e97d3719fd009c597d90f44b039ef949d095 100644 (file)
@@ -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);
+            }
+        }
     }
 }