Esempio n. 1
0
        public void ReadMessages()
        {
            NetIncomingMessage message;

            while (true)
            {
                while ((message = server.ReadMessage()) != null)
                {
                    switch (message.MessageType)
                    {
                    case NetIncomingMessageType.Data:
                    {
                        var  data      = (CommandType)message.ReadByte();
                        var  commandId = message.ReadString();
                        byte playerID  = message.ReadByte();

                        ICommand command = null;

                        Player[] cl = new Player[_clients.Count];
                        for (int i = 0; i < _clients.Count; i++)
                        {
                            cl[i] = _clients[i];
                        }

                        switch (data)
                        {
                        case CommandType.Movement:
                            string direction = message.ReadString();
                            var    xmovement = direction.Contains("L") ? -5 : 0;
                            xmovement += direction.Contains("R") ? 5 : 0;
                            var ymovement = direction.Contains("U") ? -5 : 0;
                            ymovement += direction.Contains("D") ? 5 : 0;
                            command    = new MovementCommand(GetServerPlayer(message), xmovement, ymovement, direction, commandId);

                            break;

                        case CommandType.Interact:
                            break;
                        }

                        command.Run();
                        SendCommandToAll(command, playerID);


                        break;
                    }

                    case NetIncomingMessageType.DebugMessage:
                        break;

                    case NetIncomingMessageType.StatusChanged:
                        if (message.SenderConnection.Status == NetConnectionStatus.Connected)
                        {
                            var newPlayer = new ServerPlayer(message.SenderConnection, 100, 100, 0);
                            newPlayer.SetID(currentID);
                            currentID++;

                            var command        = new ConnectCommand <ServerPlayer>(_clients, newPlayer, ICommand.GenerateRandID());
                            var welcomeCommand = new WelcomeCommand <ServerPlayer>(newPlayer, _clients.ToArray(), null, newPlayer.ID, 100, 100, ICommand.GenerateRandID());

                            var welcomeMessage = CreateMessage(welcomeCommand);

                            server.SendMessage(welcomeMessage, message.SenderConnection, NetDeliveryMethod.ReliableOrdered);

                            SendCommandToAll(command, newPlayer.ID);
                            command.Run();
                        }
                        if (message.SenderConnection.Status == NetConnectionStatus.Disconnected)
                        {
                            var leavingPlayer = GetServerPlayer(message);
                            var command       = new DisconnectCommand <ServerPlayer>(_clients, leavingPlayer, ICommand.GenerateRandID());

                            SendCommandToAll(command, leavingPlayer.ID);
                            command.Run();
                        }
                        break;

                    default:
                        break;
                    }
                    server.Recycle(message);
                }
            }
        }