private static Vector2 ServerGetPosition()
        {
            int x, y;

            NetHelper.SendMessageTo(TcpClient,
                                    NetHelper.BuildRequestString((int)Headers.DataType.RequestData, (int)Headers.RequestDataType.GetPlayerPosition, Id));

            string receivedMessage;

            while (true)
            {
                receivedMessage = NetHelper.ReceiveMessageFrom(TcpClient);

                if (receivedMessage != string.Empty)
                {
                    break;
                }
            }

            string[] message = receivedMessage.Split(Headers.SplitChar);

            x = Int32.Parse(message[0]);
            y = Int32.Parse(message[1]);

            return(new Vector2(x, y));
        }
        public static List <Drawable> ServerGetTiles()
        {
            var dList = new List <Drawable>();

            // Send message
            NetHelper.SendMessageTo(TcpClient,
                                    NetHelper.BuildRequestString((int)Headers.DataType.RequestData,
                                                                 (int)Headers.RequestDataType.GetWorldTiles));

            // Receive message
            string message;

            while (true)
            {
                message = NetHelper.ReceiveMessageFrom(TcpClient);

                if (message != string.Empty)
                {
                    break;
                }
            }

            // Parse message into drawable's and add to list. Server sends drawable's in the format of: x:y:charInteger:colorInteger;
            string[] drawables = message.Split(Headers.SplitChar);
            foreach (string drawable in drawables)
            {
                if (!drawable.Contains(':'))
                {
                    continue;
                }

                string[] info = drawable.Split(':');

                int x, y, charInteger, colorInteger;
                x            = int.Parse(info[0]);
                y            = int.Parse(info[1]);
                charInteger  = int.Parse(info[2]);
                colorInteger = int.Parse(info[3]);

                char         ch    = (char)charInteger;
                ConsoleColor color = (ConsoleColor)colorInteger;

                dList.Add(new Drawable(new Vector2(x, y), ch, color));
            }

            return(dList);
        }
        private static bool ServerSetPlayerName(string name)
        {
            NetHelper.SendMessageTo(TcpClient, NetHelper.BuildRequestString((int)Headers.DataType.SetPlayerName, Id) + name);

            string message;

            while (true)
            {
                message = NetHelper.ReceiveMessageFrom(TcpClient);

                if (message != string.Empty)
                {
                    break;
                }
            }

            return(message == "1");
        }
        private static bool ServerSetColor(ConsoleColor color)
        {
            int colorInt = (int)color;

            NetHelper.SendMessageTo(TcpClient, NetHelper.BuildRequestString((int)Headers.DataType.SetPlayerColor, Id, colorInt));

            string message;

            while (true)
            {
                message = NetHelper.ReceiveMessageFrom(TcpClient);

                if (message != string.Empty)
                {
                    break;
                }
            }

            return(message == "1");
        }
        private static bool ServerSetDrawChar(char drawChar)
        {
            int charInt = drawChar;

            NetHelper.SendMessageTo(TcpClient, NetHelper.BuildRequestString((int)Headers.DataType.SetPlayerChar, Id, charInt));

            string message;

            while (true)
            {
                message = NetHelper.ReceiveMessageFrom(TcpClient);

                if (message != string.Empty)
                {
                    break;
                }
            }

            return(message == "1");
        }
        private static bool ServerSetPosition(Vector2 position)
        {
            int x = position.X;
            int y = position.Y;

            NetHelper.SendMessageTo(TcpClient, NetHelper.BuildRequestString((int)Headers.DataType.SetPlayerPosition, Id, x, y));

            string message;

            while (true)
            {
                message = NetHelper.ReceiveMessageFrom(TcpClient);

                if (message != string.Empty)
                {
                    break;
                }
            }

            return(message == "1");
        }
        public static List <Player> ServerGetPlayers()
        {
            var pList = new List <Player>();

            NetHelper.SendMessageTo(TcpClient,
                                    NetHelper.BuildRequestString((int)Headers.DataType.RequestData,
                                                                 (int)Headers.RequestDataType.GetPlayersInfo));

            string message;

            while (true)
            {
                message = NetHelper.ReceiveMessageFrom(TcpClient);

                if (message != String.Empty)
                {
                    break;
                }
            }

            string[] players = message.Split(Headers.SplitChar);

            foreach (string player in players)
            {
                if (player == String.Empty)
                {
                    continue;
                }

                string[] info = player.Split(':');

                var p = new Player(new Vector2(Int32.Parse(info[1]), Int32.Parse(info[2])), (char)Int32.Parse(info[3]));
                p.Drawable.Color = (ConsoleColor)int.Parse(info[4]);
                p.Name           = info[5];

                pList.Add(p);
            }

            return(pList);
        }
예제 #8
0
        public static void Update()
        {
            World.Update();

            int clientCount = ConnectedClients.Count(c => c != null);

            //Console.WriteLine("Server client count: " + clientCount);

            foreach (Client c in ConnectedClients)
            {
                if (c == null)
                {
                    continue;
                }

                TcpClient tcpClient = c.TcpClient;

                if (!tcpClient.GetStream().DataAvailable)
                {
                    continue;
                }

                string message = NetHelper.ReceiveMessageFrom(tcpClient);

                string[] messages = message.Split(Headers.SplitChar);
                int      dataType = int.Parse(messages[0]);

                switch (dataType)
                {
                case (int)Headers.DataType.RequestData:
                {
                    int requestType = int.Parse(messages[1]);

                    switch (requestType)
                    {
                    case (int)Headers.RequestDataType.GetPlayerPosition:
                    {
                        int    id         = int.Parse(messages[2]);
                        string posMessage = NetHelper.BuildRequestString(ConnectedClients[id].Player.Position.X, ConnectedClients[id].Player.Position.Y);

                        NetHelper.SendMessageTo(tcpClient, posMessage);
                        break;
                    }

                    case (int)Headers.RequestDataType.GetPlayersInfo:                                             // Send players position and draw character in the form of: id:x:y:drawChar:drawColor:name.
                    {
                        string msg = string.Empty;
                        foreach (Client _c in ConnectedClients)
                        {
                            if (_c == null)
                            {
                                continue;
                            }

                            msg += _c.Id + ":" + _c.Player.Position.X + ":" + _c.Player.Position.Y + ":" + (int)_c.Player.DrawChar + ":" + (int)_c.Player.Drawable.Color + ":" + _c.Player.Name + Headers.SplitChar;
                        }

                        NetHelper.SendMessageTo(tcpClient, msg);
                        break;
                    }

                    case (int)Headers.RequestDataType.GetFreeSlot:
                    {
                        bool sendFull = true;
                        for (int i = 0; i < ConnectedClients.Length; i++)
                        {
                            Client _c = ConnectedClients[i];
                            if (_c == null)
                            {
                                // Send i to tcpClient.
                                NetHelper.SendMessageTo(tcpClient,
                                                        (int)Headers.DataType.RequestJoin + Headers.SplitChar.ToString() + (int)MathHelper.Clamp(i - 1, 0, int.MaxValue));
                                sendFull = false;
                                break;
                            }
                        }

                        if (sendFull)
                        {
                            NetHelper.SendMessageTo(tcpClient, (int)Headers.DataType.RequestJoin + ";-1");
                        }

                        break;
                    }

                    case (int)Headers.RequestDataType.GetWorldTiles:
                    {
                        string msg = "";
                        foreach (Drawable d in World.CharTiles)
                        {
                            msg += d.Position.X + ":" + d.Position.Y + ":" + (int)d.DrawChar + ":" + (int)d.Color + Headers.SplitChar;
                        }

                        NetHelper.SendMessageTo(tcpClient, msg);

                        break;
                    }
                    }

                    break;
                }

                case (int)Headers.DataType.SetPlayerPosition:
                {
                    int id      = int.Parse(messages[1]);
                    int x       = int.Parse(messages[2]);
                    int y       = int.Parse(messages[3]);
                    int success = 1;

                    // Collision check with console buffer size.
                    if (x >= 0 && y >= 0 && x < Console.BufferWidth && y < Console.BufferHeight)
                    {
                        foreach (Client client in ConnectedClients)
                        {
                            if (client == null)
                            {
                                continue;
                            }

                            // Collision check with other players.
                            if (client.Player.Position == new Vector2(x, y))
                            {
                                success = 0;
                            }
                        }

                        // Collision check with world.
                        if (!(x >= World.Width || y >= World.Height))
                        {
                            if (World.CharTiles[x + y * World.Width].DrawChar != ' ')
                            {
                                success = 0;
                            }
                        }
                        else
                        {
                            success = 0;
                        }
                    }
                    else
                    {
                        success = 0;
                    }

                    if (success == 1)
                    {
                        ConnectedClients[id].Player.Position = new Vector2(x, y);
                        NetHelper.SendMessageTo(tcpClient, "1");
                    }
                    else
                    {
                        NetHelper.SendMessageTo(tcpClient, "0");
                    }

                    break;
                }

                case (int)Headers.DataType.SetPlayerChar:
                {
                    int  id      = int.Parse(messages[1]);
                    char charSet = (char)int.Parse(messages[2]);

                    if (charSet != Headers.SplitChar)
                    {
                        ConnectedClients[id].Player.DrawChar = charSet;

                        NetHelper.SendMessageTo(tcpClient, "1");
                    }
                    else
                    {
                        NetHelper.SendMessageTo(tcpClient, "0");
                    }

                    break;
                }

                case (int)Headers.DataType.SetPlayerColor:
                {
                    int          id    = int.Parse(messages[1]);
                    ConsoleColor color = (ConsoleColor)int.Parse(messages[2]);

                    ConnectedClients[id].Player.Drawable.Color = color;

                    NetHelper.SendMessageTo(tcpClient, "1");

                    break;
                }

                case (int)Headers.DataType.SetPlayerName:
                {
                    int    id   = int.Parse(messages[1]);
                    string name = messages[2];

                    ConnectedClients[id].Player.Name = name;

                    NetHelper.SendMessageTo(tcpClient, "1");

                    Console.WriteLine("Set name to: " + name);

                    break;
                }

                case (int)Headers.DataType.SendChatMessage:
                {
                    int          id    = int.Parse(messages[1]);
                    string       msg   = messages[2];
                    ConsoleColor color = (ConsoleColor)int.Parse(messages[3]);

                    if (msg.Contains(Headers.SplitChar))
                    {
                        continue;
                    }

                    foreach (Client _c in ConnectedClients)
                    {
                        if (_c == null)
                        {
                            continue;
                        }

                        NetHelper.SendMessageTo(_c.TcpClient,
                                                NetHelper.BuildRequestString((int)Headers.DataType.SendChatMessage, _c.Id) + msg +
                                                Headers.SplitChar + (int)color);
                    }

                    break;
                }

                default:
                {
                    Console.WriteLine("Unknown data type recieved: " + dataType + ".");
                    break;
                }
                }
            }
        }
        public static void Initialize(string ip, int port)
        {
            TcpClient = new TcpClient(ip, port);
            Player    = new Player(new Vector2(0));
            Id        = -1;

            NetHelper.SendMessageTo(TcpClient, "0");

            while (true)
            {
                string status;
                status = NetHelper.ReceiveMessageFrom(TcpClient);

                if (status == "OK")
                {
                    break;
                }
            }

            NetHelper.SendMessageTo(TcpClient,
                                    NetHelper.BuildRequestString((int)Headers.DataType.RequestData,
                                                                 (int)Headers.RequestDataType.GetFreeSlot));

            while (!Stream.DataAvailable)
            {
                Console.WriteLine("Waiting for ID from server...");

                Thread.Sleep(1000 / 2);
            }

            // Possibly have id.
            string[] message = NetHelper.ReceiveMessageFrom(TcpClient).Split(Headers.SplitChar);

            switch (Int32.Parse(message[0]))
            {
            case (int)Headers.DataType.RequestJoin:
            {
                Id        = Int32.Parse(message[1]);
                Player.Id = Id;
                Console.WriteLine("Player id set to: " + Id);

                if (Id == -1)
                {
                    Console.WriteLine("Server is full.");
                    Environment.Exit(0);
                }
                break;
            }

            default:
            {
                Console.WriteLine("Received message was invalid.");
                break;
            }
            }

            ServerSetDrawChar(DrawChar);
            ServerSetColor(DrawColor);
            ServerSetPlayerName(PlayerName);

            World.CharTiles = ServerGetTiles();

            Console.ReadKey(true);
        }