From a0642e6a30ba73861a3cc42e84a9acb78c917a89 Mon Sep 17 00:00:00 2001 From: "Robin C. Ladiges" Date: Mon, 7 Nov 2022 20:49:34 +0100 Subject: [PATCH] fix: only send shines to connected clients and save only after sending otherwise it will save that the client got it to the bag and then fail sending it, therefore forever preventing the client to get the shine. --- Server/Program.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Server/Program.cs b/Server/Program.cs index bbd2d7c..37bce16 100644 --- a/Server/Program.cs +++ b/Server/Program.cs @@ -87,10 +87,11 @@ async Task ClientSyncShineBag(Client client) { if ((bool?) client.Metadata["speedrun"] ?? false) return; ConcurrentBag clientBag = (ConcurrentBag) (client.Metadata["shineSync"] ??= new ConcurrentBag()); foreach (int shine in shineBag.Except(clientBag).ToArray()) { - clientBag.Add(shine); + if (!client.Connected) return; await client.Send(new ShinePacket { ShineId = shine }); + clientBag.Add(shine); } } catch { // errors that can happen when sending will crash the server :) @@ -100,7 +101,7 @@ async Task ClientSyncShineBag(Client client) { async void SyncShineBag() { try { await PersistShines(); - await Parallel.ForEachAsync(server.Clients.ToArray(), async (client, _) => await ClientSyncShineBag(client)); + await Parallel.ForEachAsync(server.ClientsConnected.ToArray(), async (client, _) => await ClientSyncShineBag(client)); } catch { // errors that can happen shines change will crash the server :) }