Skip to content

Commit

Permalink
feat(game-service): add max client number
Browse files Browse the repository at this point in the history
  • Loading branch information
codingben committed Apr 13, 2023
1 parent e553f14 commit d84e0b9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
Expand Up @@ -6,9 +6,12 @@ namespace Game.Application.Components
public class GameClientCollection : ComponentBase, IGameClientCollection
{
private readonly Dictionary<int, IGameClient> collection;
private readonly int maxNumberOfClients;

public GameClientCollection()
public GameClientCollection(int maxNumberOfClients)
{
this.maxNumberOfClients = maxNumberOfClients;

collection = new Dictionary<int, IGameClient>();
}

Expand All @@ -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 =
Expand All @@ -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)
Expand Down
Expand Up @@ -2,7 +2,7 @@ namespace Game.Application.Components
{
public interface IGameClientCollection
{
void Add(IGameClient gameClient);
bool Add(IGameClient gameClient);

void Remove(int id);

Expand Down

0 comments on commit d84e0b9

Please sign in to comment.