Ejemplo n.º 1
0
        private bool TryConnect()
        {
            Socket socket = connector.Connect();

            if (socket != null)
            {
                lastId++;
                Tcp    tcp    = new Tcp(socket);
                Player player = new Player(lastId, tcp, shipsInfo, projectilesController);
                player.CreateShip();
                CreateShipActionData[] data = new CreateShipActionData[bots.Count + players.Count + 1];
                data[0] = player.GetCreateShipActionData();
                structConverter.ConvertStructToBytes(data[0], DataType.CreateShipAction, out byte[] bytes);
                foreach (Player pl in players)
                {
                    pl.SendTcpData(bytes);
                }
                for (int i = 0; i < players.Count; i++)
                {
                    data[i + 1] = players[i].GetCreateShipActionData();
                }
                players.Add(player);
                for (int i = 0; i < bots.Count; i++)
                {
                    data[i + players.Count] = bots[i].GetCreateShipActionData();
                }
                structConverter.ConvertStructsToBytes(data, DataType.CreateShipAction, out bytes);
                player.SendTcpData(bytes);
                return(true);
            }
            return(false);
        }