public void AddPlayer(Player player) {
     lock (_players) {
         _players.Add(player);
         if (_players.Count == 1) {
             player.IsHost = true;
             player.Ready = false;
         }
     }
 }
 public void RemovePlayer(Player player) {
     lock (_players) {
         _players.Remove(player);
         if (player.IsHost) {
             _players[0].IsHost = true;
         }
         foreach (Player pl in _players)
             if (_gameStarted) {
                 pl.TcpClient.Send($"[Notify:PlayerLeft:{player.CornerId}|{player.Name}]");
                 lock (_midGameDisconnects)
                     _midGameDisconnects.Add(player.CornerId);
             }
             else {
                 pl.TcpClient.Send($"[Lobby:SetPlayers:{PlayerList()}]");
             }
         if(_gameStarted)
             if (_currentPlayer == player)
                 EndTurn(player.Guid);
     }
 }
        public void StartGame(string guid) {
            if (_gameStarted)
                return;
            lock (_players) {
                if (_players.Any(x => !x.Ready && !x.IsHost))
                    return;
                if (!GetPlayer(guid).IsHost)
                    return;

                _gameStarted = true;
                _currentPlayer = _players[0];
                string playersData = string.Empty;
                foreach (Player pl in _players)
                    playersData += "(" + pl.Name + "|" + pl.CornerId + ")";

                foreach (Player player in _players) {
                    player.TcpClient.Send($"[Lobby:StartGame:{playersData}]");
                }
            }
        }
        public void EndTurn(string guid) {
            lock (_currentPlayer) {
                if (_currentPlayer.Guid != guid) {
                    Player player = GetPlayer(guid);
                    player.TcpClient.Send("[Error:Can't end turn if it's not your turn...]"); //TODO: Resources
                }
                else {
                    lock (_players) {

                        if (_players.All(x => !x.IsAlive)) {
                            GameWon(_players[0].Guid);
                            return;
                        }
                        do {
                            int currentIndex = _players.IndexOf(_currentPlayer) + 1;
                            if (currentIndex > _players.Count - 1)
                                currentIndex = 0;

                            _currentPlayer = _players[currentIndex];

                        } while (!_currentPlayer.IsAlive);

                        foreach (Player player in _players)
                            player.TcpClient.Send($"[Notify:TurnEnd:{_currentPlayer.CornerId}|{_currentPlayer.Name}]");
                    }
                    Console.WriteLine("Turn ended");
                }
            }

        }
 public void SetName(TcpClient sender, string name) {
     int corner= GetRandomCorner();
     if (corner < 0) {
         sender.Send("[Error:ERR4:The lobby you tried to join is full.]");
         sender.Dispose();
         return;
     }
     if (_gameStarted) {
         sender.Send("[Error:ERR5:The game you tried to join is already in progress.]");
         sender.Dispose();
         return;
     }
     Player player = new Player(Guid.NewGuid(), sender) {
         Name = name,
         CornerId = corner
     };
     AddPlayer(player);
     player.TcpClient.Send($"[Response:Authenticated:{player.Guid}|{player.CornerId}|{player.Name}|{_lobbyId}]" +
                           $"[Response:SetPlayers:{PlayerList()}]");
 }