public void Run(ManagerLogger managerLogger, Server server, NetIncomingMessage inc, PlayerAndConnection playerAndConnection, GameRoom gameRoom)
 {
     managerLogger.AddLogMessage("server", "New connection...");
     var data = inc.ReadByte();
     if (data == (byte)PacketType.Login)
     {
         managerLogger.AddLogMessage("server", "..connection accpeted.");
         playerAndConnection = CreatePlayer(inc, gameRoom.Players, gameRoom.ManagerCamera);
         inc.SenderConnection.Approve();
         var outmsg = server.NetServer.CreateMessage();
         outmsg.Write((byte)PacketType.Login);
         outmsg.Write(true);
         outmsg.Write(gameRoom.Players.Count);
         for (int n = 0; n < gameRoom.Players.Count; n++)
         {
             var p = gameRoom.Players[n];
             outmsg.Write(p.Player.Username);
             outmsg.WriteAllProperties(p.Player.Position);
         }
         server.NetServer.SendMessage(outmsg, inc.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0);
         var command = new PlayerPositionCommand();
         command.Run(managerLogger, server,inc,playerAndConnection,gameRoom);
         server.SendNewPlayerEvent(playerAndConnection.Player.Username, gameRoom.GameRoomId);
     }
     else
     {
         inc.SenderConnection.Deny("Didn't send correct information.");
     }
 }
Esempio n. 2
0
 public Server(ManagerLogger managerLogger)
 {
     _managerLogger = managerLogger;
     _gameRooms = new List<GameRoom>();
     _config = new NetPeerConfiguration("networkGame") { Port = 14241 };
     _config.EnableMessageType(NetIncomingMessageType.ConnectionApproval);
     NetServer = new NetServer(_config);
 }
Esempio n. 3
0
 public MainForm()
 {
     _managerLogger = new ManagerLogger();
     _managerLogger.NewLogMessageEvent += NewLogMessageEvent;
     _server = new Server(_managerLogger);
     _server.NewPlayerEvent += NewPlayerEvent;
     InitializeComponent();
 }
 public void Run(ManagerLogger managerLogger, Server server, NetIncomingMessage inc, PlayerAndConnection playerAndConnection, GameRoom gameRoom)
 {
     managerLogger.AddLogMessage("server",string.Format("Kicking {0}",playerAndConnection.Player.Username));
     var outmessage = server.NetServer.CreateMessage();
     outmessage.Write((byte)PacketType.Kick);
     outmessage.Write(playerAndConnection.Player.Username);
     server.NetServer.SendToAll(outmessage, NetDeliveryMethod.ReliableOrdered);
     //Kick player
     playerAndConnection.Connection.Disconnect("Bye bye, you're kicked.");
 }
        public void Run(ManagerLogger managerLogger, Server server, NetIncomingMessage inc, PlayerAndConnection playerAndConnection, GameRoom gameRoom)
        {
            managerLogger.AddLogMessage("server", "Received new input");
            var key = (Keys)inc.ReadByte();
            var name = inc.ReadString();
            playerAndConnection = gameRoom.Players.FirstOrDefault(p => p.Player.Username == name);
            if (playerAndConnection == null)
            {
                managerLogger.AddLogMessage("server", string.Format("Could not find player with name {0}", name));
                return;
            }

            int x = 0;
            int y = 0;

            switch (key)
            {
                case Keys.Down:
                    y++;
                    break;

                case Keys.Up:
                    y--;
                    break;

                case Keys.Left:
                    x--;
                    break;

                case Keys.Right:
                    x++;
                    break;
            }

            var player = playerAndConnection.Player;
            var position = playerAndConnection.Player.Position;
            if (!ManagerCollision.CheckCollision(new Rectangle(position.XPosition + x, position.YPosition + y, 100, 50),
                player.Username, gameRoom.Players.Select(p => p.Player).ToList()))
            {
                position.XPosition += x;
                position.YPosition += y;

                position.Visible = gameRoom.ManagerCamera.InScreenCheck(new Vector2(position.XPosition, position.YPosition));
                if (position.Visible)
                {
                    var screenPosition =
                        gameRoom.ManagerCamera.WorldToScreenPosition(new Vector2(position.XPosition, position.YPosition));
                    position.ScreenXPosition = (int) screenPosition.X;
                    position.ScreenYPosition = (int) screenPosition.Y;
                }

                var command = new PlayerPositionCommand();
                command.Run(managerLogger, server, inc, playerAndConnection, gameRoom);
            }
        }
 public GameRoom(string gameRoomId, Server server,  ManagerLogger logger)
 {
     GameRoomId = gameRoomId;
     _server = server; 
     Players = new List<PlayerAndConnection>();
     _cancellationTokenSource = new CancellationTokenSource();
     _task = new Task(Update,_cancellationTokenSource.Token);
     _task.Start();
     _logger = logger; 
     ManagerCamera = new ManagerCamera();
 }
 public void Run(ManagerLogger managerLogger, Server server, NetIncomingMessage inc, PlayerAndConnection playerAndConnection, GameRoom gameRoom)
 {
     if (playerAndConnection != null)
     {
         managerLogger.AddLogMessage("server", "Sending out new player position to all in group " + gameRoom.GameRoomId);
         var outmessage = server.NetServer.CreateMessage();
         outmessage.Write((byte) PacketType.PlayerPosition);
         outmessage.WriteAllProperties(playerAndConnection.Player);
         server.NetServer.SendMessage(outmessage, gameRoom.Players.Select(p => p.Connection).ToList(),
             NetDeliveryMethod.ReliableOrdered, 0);
     }
 }
 public void Run(ManagerLogger managerLogger, Server server, NetIncomingMessage inc, PlayerAndConnection playerAndConnection, GameRoom gameRoom)
 {
     managerLogger.AddLogMessage("server", "Sending full player list");
     var outmessage = server.NetServer.CreateMessage();
     outmessage.Write((byte)PacketType.AllPlayers);
     outmessage.Write(CameraUpdate);
     outmessage.Write(gameRoom.Players.Count);
     foreach (var p in gameRoom.Players)
     {
         outmessage.WriteAllProperties(p.Player);
     }
     server.NetServer.SendMessage(outmessage, gameRoom.Players.Select(p => p.Connection).ToList(), NetDeliveryMethod.ReliableOrdered, 0);
 }