private async Task findConnection()
        {
            while (_startQueue.hasHextUser())
            {
                //teory failed
                if (_startQueue.CurrLevel + _targetQueue.CurrLevel >= 6)
                {
                    break;
                }

                if (_startQueue.CurrLevel > _currLevel && _currLevel != 0)
                {
                    swapQueuesIfNeeded();
                    _currLevel++;
                    continue;
                }

                //get next user
                VKUser user = _startQueue.getNextUser();
                _currLevel = user.Level;

                System.Console.WriteLine("currLevel = {0}, currQ={1}, currUser={2}", _currLevel, _startQueue.Root.ID, user.ID);

                await prepareUser(user, _startQueue);

                VKUser res = proceedUser(user);
                if (res != null)
                {
                    makeConnection(res);
                    break;
                }
            }
        }
 public async Task fetchFriendsTest2()
 {
     VKUser user = new VKUser(198489790);
     FriendsFetcher fetcher = new FriendsFetcher(user);
     List<VKUser> result = await fetcher.fetchFriends();
     Assert.AreNotEqual(0, result.Count, "Method fetchFriends() don`t find friends correctly");
 }
Exemplo n.º 3
0
 public VKFriendsQueue(VKUser root)
 {
     _root         = root;
     _set          = new HashSet <VKUser>(new VKUserComparer());
     _proceedQueue = new Queue <VKUser>();
     _proceedQueue.Enqueue(_root);
 }
Exemplo n.º 4
0
 public VKUser(int id, VKUser father)
 {
     _id     = id;
     _father = father;
     _level  = _father.Level + 1;
     _childs = new List <VKUser>();
 }
        /// <summary>
        /// Restores path from user to father and adds to result path.
        /// </summary>
        /// <param name="user"></param>
        private void makeConnection(VKUser user)
        {
            makeDefaultQueues();

            VKUser startSide  = _startQueue.getUser(user.ID);
            VKUser targetSide = _targetQueue.getUser(user.ID);

            List <VKUser> tempList = new List <VKUser>();
            VKUser        currUser = startSide;

            while (currUser != null)
            {
                tempList.Add(currUser);
                currUser = currUser.Father;
            }
            tempList.Reverse();
            _path.AddRange(tempList);

            tempList.Clear();
            currUser = targetSide.Father;
            while (currUser != null)
            {
                tempList.Add(currUser);
                currUser = currUser.Father;
            }
            _path.AddRange(tempList);
        }
 public VKUser(int id, VKUser father)
 {
     _id = id;
     _father = father;
     _level = _father.Level + 1;
     _childs = new List<VKUser>();
 }
 public void addChildTest1()
 {
     int expected = 1;
     VKUser user = new VKUser(29411485);
     VKUser child = new VKUser(141371820);
     user.addChild(child);
     Assert.AreEqual(expected, user.Childs.Count, "Method addChild() doesn`t add child to user correctly");
 }
 public void addUserTest2()
 {
     VKUser root = new VKUser(29411485);
     VKFriendsQueue queue = new VKFriendsQueue(root);
     VKUser user = new VKUser(141371820);
     queue.addUser(user);
     Assert.AreEqual(root, queue.Root, "Method addUser() doesn`t add user to queue correctly");
 }
 public void addChildTest3()
 {
     VKUser user = new VKUser(29411485);
     VKUser child = new VKUser(141371820);
     VKUser expected = user;
     user.addChild(child);
     VKUser result = child.Father;
     Assert.AreEqual(expected, result, "Method addChild() doesn`t add child to user correctly");
 }
 public void containsUserFalseTest2()
 {
     bool expected = false;
     VKUser root = new VKUser(29411485);
     VKFriendsQueue queue = new VKFriendsQueue(root);
     VKUser user = new VKUser(141371820);
     queue.addUser(user);
     bool result = queue.containsUser(null);
     Assert.AreEqual(expected, result, "Method containsUser() doesn`t find user in queue correctly");
 }
 public async Task fetchFriendsTest1()
 {
     VKUser user = new VKUser(325145123);
     FriendsFetcher fetcher = new FriendsFetcher(user);
     List<VKUser> friends = new List<VKUser>();
     friends.Add(new VKUser(27735856));
     friends.Add(new VKUser(59054252));
     List<VKUser> result = await fetcher.fetchFriends();
     Assert.AreEqual(friends[0].ID, result[0].ID, "Method fetchFriends() don`t find friends correctly");
     Assert.AreEqual(friends[1].ID, result[1].ID, "Method fetchFriends() don`t find friends correctly");
 }
        public VKConnectionFinder(VKUser start, VKUser target)
        {
            _startUser  = start;
            _targetUser = target;

            _startQueue  = new VKFriendsQueue(start);
            _targetQueue = new VKFriendsQueue(target);

            _path = new List <VKUser>();

            _currLevel = 0;
        }
Exemplo n.º 13
0
        //test
        public bool addUser(VKUser user)
        {
            if (_set.Contains(user))
            {
                return(false);
            }

            _set.Add(user);
            _proceedQueue.Enqueue(user);

            return(true);
        }
        /// <summary>
        /// Checks if friends of user has common friends with target user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        private VKUser proceedUser(VKUser user)
        {
            foreach (VKUser fr in user.Childs)
            {
                if (_targetQueue.containsUser(fr))
                {
                    return(fr);
                }
            }

            return(null);
        }
        public VKConnectionFinder(VKUser start, VKUser target)
        {
            _startUser = start;
            _targetUser = target;

            _startQueue = new VKFriendsQueue(start);
            _targetQueue = new VKFriendsQueue(target);

            _path = new List<VKUser>();

            _currLevel = 0;
        }
        /// <summary>
        /// Get friends and adds to queue.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="q"></param>
        /// <returns></returns>
        private async Task prepareUser(VKUser user, VKFriendsQueue q)
        {
            if (user.Childs.Count == 0)
            {
                q.addUser(user);

                FriendsFetcher fetcher = new FriendsFetcher(user);
                List <VKUser>  friends = await fetcher.fetchFriends();

                _status = fetcher.Status;

                foreach (VKUser usr in friends)
                {
                    if (q.addUser(usr))
                    {
                        user.addChild(usr);
                    }
                }
            }
        }
 public void hasNextUserTrueTest()
 {
     bool expected = true;
     VKUser root = new VKUser(29411485);
     VKFriendsQueue queue = new VKFriendsQueue(root);
     bool result = queue.hasHextUser();
     Assert.AreEqual(expected, result, "Method hasNextUser() doesn`t find next user in queue correctly");
 }
 public void getUserTest2()
 {
     VKUser expected = null;
     VKUser root = new VKUser(29411485);
     VKFriendsQueue queue = new VKFriendsQueue(root);
     VKUser user = new VKUser(141371820);
     queue.addUser(user);
     VKUser result = queue.getUser(1);
     Assert.AreEqual(expected, result, "Method getUser() doesn`t get user in queue correctly");
 }
 public void getNextUserTest3()
 {
     VKUser expected = null;
     VKUser root = new VKUser(29411485);
     VKFriendsQueue queue = new VKFriendsQueue(root);
     queue.getNextUser();
     VKUser result = queue.getNextUser();
     Assert.AreEqual(expected, result, "Method getNextUser() doesn`t extract user from queue correctly");
 }
 public void getNextUserTest1()
 {
     int expected = 0;
     VKUser root = new VKUser(29411485);
     VKFriendsQueue queue = new VKFriendsQueue(root);
     queue.getNextUser();
     Assert.AreEqual(expected, queue.UserCount, "Method getNextUser() doesn`t extract user from queue correctly");
 }
        /// <summary>
        /// Restores path from user to father and adds to result path.
        /// </summary>
        /// <param name="user"></param>
        private void makeConnection(VKUser user)
        {
            makeDefaultQueues();

            VKUser startSide = _startQueue.getUser(user.ID);
            VKUser targetSide = _targetQueue.getUser(user.ID);

            List<VKUser> tempList = new List<VKUser>();
            VKUser currUser = startSide;
            while (currUser != null)
            {
                tempList.Add(currUser);
                currUser = currUser.Father;
            }
            tempList.Reverse();
            _path.AddRange(tempList);

            tempList.Clear();
            currUser = targetSide.Father;
            while (currUser != null)
            {
                tempList.Add(currUser);
                currUser = currUser.Father;
            }
            _path.AddRange(tempList);

        }
Exemplo n.º 22
0
 //test
 public void addChild(VKUser child)
 {
     _childs.Add(child);
     child._father = this;
 }
 public VKUserViewData(VKUser user)
     : base(user.ID, user.Father)
 {
     _nick = "";
     _photoUrl = "";
 }
        /// <summary>
        /// Checks if friends of user has common friends with target user
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        private VKUser proceedUser(VKUser user)
        {
            foreach (VKUser fr in user.Childs)
            {
                if (_targetQueue.containsUser(fr))
                {
                    return fr;
                }
            }

            return null;
        }
 public void constructorTest()
 {
     VKUser root = new VKUser(29411485);
     VKFriendsQueue queue = new VKFriendsQueue(root);
     Assert.AreEqual(1, queue.UserCount, "Constructor doesn`t initialize VKFriendsQueue object correctly");
 }
Exemplo n.º 26
0
 //test
 public void addChild(VKUser child)
 {
     _childs.Add(child);
     child._father = this;
 }
 public FriendsFetcher(VKUser user)
 {
     _user    = user;
     _friends = new List <VKUser>();
 }
Exemplo n.º 28
0
 //test
 public bool containsUser(VKUser user)
 {
     return(_set.Contains(user));
 }
Exemplo n.º 29
0
 public VKUserViewData(VKUser user)
     : base(user.ID, user.Father)
 {
     _nick     = "";
     _photoUrl = "";
 }
        /// <summary>
        /// Get friends and adds to queue.
        /// </summary>
        /// <param name="user"></param>
        /// <param name="q"></param>
        /// <returns></returns>
        private async Task prepareUser(VKUser user, VKFriendsQueue q)
        {
            if (user.Childs.Count == 0)
            {
                q.addUser(user);

                FriendsFetcher fetcher = new FriendsFetcher(user);
                List<VKUser> friends = await fetcher.fetchFriends();
                _status = fetcher.Status;

                foreach (VKUser usr in friends)
                {
                    if (q.addUser(usr))
                    {
                        user.addChild(usr);
                    }
                }

                
            }
        }
 public void childTest()
 {
     int expected = 0;
     VKUser user = new VKUser(29411485);
     Assert.AreEqual(expected, user.Childs.Count, "Property Child doesn`t extract childs from user correctly");
 }
 public FriendsFetcher(VKUser user)
 {
     _user = user;
     _friends = new List<VKUser>();
 }