private void HandlePacket(PlayerInfo player, PacketData packet)
        {
            if (packet.Packet is PacketPing)
            {
                foreach (var user in connectedUsers)
                {
                    if (user.Key.Address == player.Address && user.Key.Port == player.Port)
                    {
                        user.Key.Restart = true;
                    }
                }
                player.Stopwatch.Reset();
            }

            if (packet.Packet is PacketDisconnect)
            {
                RemovePlayer(player);
                foreach (var user in connectedUsers)
                {
                    outboundPacketQueue.Enqueue(new PacketDisconnect(player.Object.ID, new IPEndPoint(user.Key.Address, user.Key.Port)));
                }
                if (player != null)
                    Line("Disconnected with user " + player.Username);
            }

            if (packet.Packet is PacketMove)
            {
                PacketMove move = (PacketMove)packet.Packet;
                foreach (var item in connectedUsers)
                {
                    if (item.Key.Address == player.Address && item.Key.Port == player.Port && item.Key.Object.ID == move.ID && item.Key.Object.Health > 0)
                        item.Key.Object.Area = new RectangleFloat(move.NewPosition.X, move.NewPosition.Y, move.NewPosition.Width, move.NewPosition.Height);

                    if (item.Key.Object.Health > 0)
                        outboundPacketQueue.Enqueue(new PacketMove(move.ID, move.NewPosition, new IPEndPoint(item.Key.Address, item.Key.Port)));
                }
            }

            if (packet.Packet is PacketShoot)
            {
                PacketShoot shoot = (PacketShoot)packet.Packet;
                shoot.Object.ID = ++bulletIdCount;
                bulletList.Add(shoot.Object);

                foreach (var item in connectedUsers)
                {
                    outboundPacketQueue.Enqueue(new PacketShoot(shoot.Object, new IPEndPoint(item.Key.Address, item.Key.Port)));
                }

                shoot.Object.Shoot();
            }
        }
 private void AddPlayer(PlayerInfo player, PacketData loginPacket)
 {
     var packets = new ConcurrentQueue<PacketData>();
     connectedUsers.AddOrUpdate(player, packets, (oldKey, oldValue) => packets);
 }
        private void Listen(IPEndPoint client)
        {
            string receivedMessage;
            byte[] receivedBytes;

            Line("Waiting for connection...");

            Stopwatch sw = new Stopwatch();
            sw.Start();

            while (true)
            {
                try
                {
                    receivedBytes = server.Receive(ref client);
                    receivedMessage = Encoding.ASCII.GetString(receivedBytes);

                    PacketData packet = Packet.Parse(receivedMessage);

                    if (packet != null && !(packet.Packet is PacketLogin))
                    {
                        foreach (var player in connectedUsers)
                        {
                            if (player.Key.Address.Equals(client.Address) && player.Key.Port.Equals(client.Port))
                            {
                                player.Value.Enqueue(packet);
                            }
                        }
                    }

                    if (packet.Packet is PacketLogin)
                    {
                        foreach (var team in teamList)
                        {
                            foreach (var teamPlayer in team.Players)
                            {
                                if (!connectedUsers.Keys.Contains(teamPlayer))
                                {
                                    team.Players.Remove(teamPlayer);
                                    break;
                                }
                            }
                        }

                        idCount++;
                        Player playerObj = new Player(idCount, 0, 0, 80, 80);
                        playerObj.Team = teamList[GetLowestTeamIndex()];
                        playerObj.Name = packet.Parameters[0].ToString();

                        if (playerObj.Team.Identifier == 0)
                        {
                            playerObj.Area.X = World.RedSpawn.X;
                            playerObj.Area.Y = World.RedSpawn.Y;
                        }
                        if (playerObj.Team.Identifier == 1)
                        {
                            playerObj.Area.X = World.BlueSpawn.X;
                            playerObj.Area.Y = World.BlueSpawn.Y;
                        }
                        else if (playerObj.Team.Identifier == 2)
                        {
                            playerObj.Area.X = World.GreenSpawn.X;
                            playerObj.Area.Y = World.GreenSpawn.Y;
                        }
                        else if (playerObj.Team.Identifier == 3)
                        {
                            playerObj.Area.X = World.YellowSpawn.X;
                            playerObj.Area.Y = World.YellowSpawn.Y;
                        }

                        PlayerInfo player = new PlayerInfo(client.Address, client.Port, packet.Parameters[0].ToString(), playerObj);
                        teamList[GetLowestTeamIndex()].Players.Add(player);

                        List<Player> mObjects = new List<Player>();
                        foreach (var item in connectedUsers)
                        {
                            mObjects.Add(item.Key.Object);
                            outboundPacketQueue.Enqueue(new PacketAddPlayer(player.Object, new IPEndPoint(item.Key.Address, item.Key.Port)));
                            outboundPacketQueue.Enqueue(new PacketAddPlayer(item.Key.Object, client));
                        }

                        AddPlayer(player, packet);
                        Line("Connected with player " + player.Username);

                        outboundPacketQueue.Enqueue(new PacketID(idCount, playerObj.Team, new IPEndPoint(player.Address, player.Port)));
                    }
                }
                catch (SocketException ex)
                {
                    Console.WriteLine(ex);
                }
            }
        }
 private void RemovePlayer(PlayerInfo player)
 {
     try
     {
         ConcurrentQueue<PacketData> ignoreForGarbage;
         connectedUsers.TryRemove(player, out ignoreForGarbage);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Data);
     }
 }
 public void AddPlayer(PlayerInfo player)
 {
     _playerList.Add(player);
 }