From d84e0b9d6895036f19e7416d06d94b101f20b06c Mon Sep 17 00:00:00 2001 From: Ben Oukhanov Date: Thu, 13 Apr 2023 23:59:21 +0300 Subject: [PATCH] feat(game-service): add max client number --- .../Components/Client/GameClientCollection.cs | 14 ++++++++++++-- .../Components/Client/IGameClientCollection.cs | 2 +- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/game-service/Game.Application/Components/Client/GameClientCollection.cs b/src/game-service/Game.Application/Components/Client/GameClientCollection.cs index dd5be6450..ef59bcf8b 100644 --- a/src/game-service/Game.Application/Components/Client/GameClientCollection.cs +++ b/src/game-service/Game.Application/Components/Client/GameClientCollection.cs @@ -6,9 +6,12 @@ namespace Game.Application.Components public class GameClientCollection : ComponentBase, IGameClientCollection { private readonly Dictionary collection; + private readonly int maxNumberOfClients; - public GameClientCollection() + public GameClientCollection(int maxNumberOfClients) { + this.maxNumberOfClients = maxNumberOfClients; + collection = new Dictionary(); } @@ -24,8 +27,14 @@ protected override void OnRemoved() collection.Clear(); } - public void Add(IGameClient gameClient) + public bool Add(IGameClient gameClient) { + if (Count() >= maxNumberOfClients) + { + GameLog.Debug($"Server reached maximum number of clients."); + return false; + } + var id = gameClient.Id; var webSocketConnectionProvider = @@ -37,6 +46,7 @@ public void Add(IGameClient gameClient) collection.Add(id, gameClient); GameLog.Debug($"Client #{id} connected."); + return true; } public void Remove(int id) diff --git a/src/game-service/Game.Application/Components/Client/IGameClientCollection.cs b/src/game-service/Game.Application/Components/Client/IGameClientCollection.cs index be5ae7bfc..e5fa7934d 100644 --- a/src/game-service/Game.Application/Components/Client/IGameClientCollection.cs +++ b/src/game-service/Game.Application/Components/Client/IGameClientCollection.cs @@ -2,7 +2,7 @@ namespace Game.Application.Components { public interface IGameClientCollection { - void Add(IGameClient gameClient); + bool Add(IGameClient gameClient); void Remove(int id);