Example #1
0
        public void Given_InGameAlreadyMaxPlayers_When_AddNextClient_Then_ReturnStatusNotAllowedForMorePlayers()
        {
            for (int i = 0; i < PoloGameEngine.MaxOnlineClients; i++)
            {
                _gameEngine.AddPlayer($"Player{i}");
            }

            _gameEngine.AddPlayer("NextPlayer").Should().Be(AddPlayerOperationResult.CannotAddMorePlayers);
        }
Example #2
0
        private async Task StartListener(TcpListener server)
        {
            try
            {
                var gameEnded = false;
                while (!gameEnded)
                {
                    if (!_game.IsGameReadyToStart)
                    {
                        _logger.LogInformation("Waiting for a connection...");
                        TcpClient client = await server.AcceptTcpClientAsync().ConfigureAwait(false);

                        var clientId = Guid.NewGuid().ToString();
                        _logger.LogInformation($"Client: {clientId} connected!");
                        _clients.TryAdd(client, clientId);
                        _game.AddPlayer(clientId);
                        HandleDevice(client, clientId);
                    }
                    else
                    {
                        var tasksHandleGameForPlayers = new List <Task <bool> >();
                        foreach (var client in _clients)
                        {
                            tasksHandleGameForPlayers.Add(HandleGameForPlayer(client.Key, client.Value));
                        }
                        var results = await Task.WhenAll(tasksHandleGameForPlayers);

                        if (results.All(x => x))
                        {
                            foreach (var client in _clients)
                            {
                                await NotifyPlayers(client.Key, client.Value).ConfigureAwait(false);
                            }
                            gameEnded = true;
                        }
                    }
                }
                _logger.LogInformation("Game ended!");
            }
            catch (SocketException e)
            {
                _logger.LogError("SocketException: {0}", e);
                server.Stop();
            }
        }