示例#1
0
        private void TestGetTreasures(Treasure.Query targetStatus)
        {
            try
            {
                var treasures = TestEnvironment.Session.Profile.GetTreasures(TestEnvironment.Session, targetStatus, 1, 1);
                if (treasures.Count == 0)
                {
                    Assert.Fail("No treasures were retrieved");
                }

                if (treasures.Count > 15)
                {
                    Assert.Fail("Too much treasures were retrieved");
                }

                foreach (var treasure in treasures)
                {
                    Assert.AreNotEqual(0, treasure.ID, "The ID of a treasure was not set");
                    Assert.AreNotEqual(0, treasure.Title.Length, "The title of a treasure was not set");
                    Assert.AreNotEqual(0, treasure.Cost, "The cost of a treasure was not set");

                    // The bought request needs to have the seller property set while the selled/listed query does not
                    if (targetStatus == Treasure.Query.Bought)
                    {
                        Assert.AreNotEqual(0, treasure.Seller.ID);
                        Assert.AreNotEqual(0, treasure.Seller.Name.Length);
                    }
                }
            }
            catch (RequestFailedException exc)
            {
                AssertExtender.Exception("A HTTP request failed", exc);
            }
            catch (InvalidSessionException exc)
            {
                AssertExtender.Exception("Session is invalid", exc);
            }
        }
示例#2
0
        /// <summary>
        /// Retrieves all <c>Treasure</c>s that have been bought and/or sold using the logged-in user account
        /// </summary>
        /// <param name="session">Session used for sending the request</param>
        /// <param name="queryStatus">
        /// Type of <c>Treasure</c> to query. Either <c>Treasure.Query.SoldListed</c>
        /// for querying treasures that have been sold/listed or <c>Treasure.Query.Bought</c>
        /// for treasures that were bought </param>
        /// <param name="pageCount"> Amount of pages to retrieve, one page may contain up to 15 treasures </param>
        /// <param name="startIndex"> Index of the first page to request (1 for the first page, 2 for the second, ...) </param>
        /// <returns> List of all <c>Treasure</c>s that could be retrieved </returns>
        public List <Treasure> GetTreasures(AuthenticatedSession <TUser> session, Treasure.Query queryStatus = Treasure.Query.SoldListed, uint pageCount = 1, uint startIndex = 1)
        {
            session.ThrowIfInvalid();

            var listedTreasures = new List <Treasure>();

            for (var i = startIndex; i < (startIndex + pageCount); ++i)
            {
                var res = session.Get("http://www.elitepvpers.com/theblackmarket/treasures/" +
                                      ((queryStatus == Treasure.Query.Bought) ? "bought" : "soldunsold")
                                      + "/" + i);

                var htmlDocument = new HtmlDocument();
                htmlDocument.LoadHtml(res);

                var rootFormNode = htmlDocument.GetElementbyId("contentbg");
                if (rootFormNode == null)
                {
                    continue;
                }

                var tableNode = rootFormNode.SelectSingleNode("table[1]/tr[1]/td[1]/table[1]/tr[2]/td[1]/div[1]/div[3]/table[1]");
                if (tableNode == null)
                {
                    continue;
                }

                // skip the first <tr> element since that is the table header
                foreach (var treasureListingNode in tableNode.ChildNodes.GetElementsByTagName("tr").Skip(1))
                {
                    var idNode         = treasureListingNode.SelectSingleNode("td[1]");
                    var titleNode      = treasureListingNode.SelectSingleNode("td[2]");
                    var costNode       = treasureListingNode.SelectSingleNode("td[3]");
                    var opponentNode   = treasureListingNode.SelectSingleNode("td[4]");
                    var listedTreasure = new Treasure
                    {
                        // first column is the id with a trailing #
                        ID = (idNode != null) ? idNode.InnerText.TrimStart('#').To <int>() : 0,

                        // second column is the treasure title
                        Title = (titleNode != null) ? titleNode.InnerText : "",
                    };

                    // since this function is only available for logged-in users, the seller (or buyer, depends on the request) is automatically the logged-in user
                    if (queryStatus == Treasure.Query.Bought)
                    {
                        listedTreasure.Buyer = User;
                    }
                    else
                    {
                        listedTreasure.Seller = User;
                    }

                    // third column is the cost
                    var match = new Regex(@"([0-9]+) eg").Match(costNode.InnerText);
                    if (match.Groups.Count > 1)
                    {
                        listedTreasure.Cost = Convert.ToUInt32(match.Groups[1].Value);
                    }

                    // the last column is the treasure buyer or seller
                    if (opponentNode != null)
                    {
                        opponentNode = opponentNode.SelectSingleNode("a[1]");
                        if (opponentNode != null)
                        {
                            var opponent = opponentNode.Attributes.Contains("href")
                                ? new User(opponentNode.InnerText,
                                           epvpapi.User.FromUrl(opponentNode.Attributes["href"].Value))
                                : new User();

                            if (queryStatus == Treasure.Query.Bought)
                            {
                                listedTreasure.Seller    = opponent;
                                listedTreasure.Available = false;
                            }
                            else
                            {
                                listedTreasure.Buyer     = opponent;
                                listedTreasure.Available = false;
                            }
                        }
                    }

                    listedTreasures.Add(listedTreasure);
                }
            }

            return(listedTreasures);
        }