|
1 | | -using HtmlAgilityPack; |
| 1 | +using epvpapi.TBM; |
| 2 | +using HtmlAgilityPack; |
2 | 3 | using System; |
3 | 4 | using System.Collections.Generic; |
4 | 5 | using System.Linq; |
@@ -181,6 +182,95 @@ public List<PrivateMessage> GetPrivateMessages(uint firstPage, uint pageCount, P |
181 | 182 | return fetchedMessages; |
182 | 183 | } |
183 | 184 |
|
| 185 | + |
| 186 | + /// <summary> |
| 187 | + /// Retrieves all <c>Treasure</c>s that have been bought and/or sold using the logged-in user account |
| 188 | + /// </summary> |
| 189 | + /// <param name="queryStatus"> |
| 190 | + /// Type of <c>Treasure</c> to query. Either <c>Treasure.Query.SoldListed</c> |
| 191 | + /// for querying treasures that have been sold/listed or <c>Treasure.Query.Bought</c> |
| 192 | + /// for treasure that have been bought </param> |
| 193 | + /// <param name="pageCount"> Amount of pages to retrieve, one page may contain up to 15 treasures </param> |
| 194 | + /// <param name="startIndex"> Index indcating the page to start from </param> |
| 195 | + /// <returns> List of all <c>Treasure</c>s that could be retrieved </returns> |
| 196 | + public List<Treasure> GetTreasures(Treasure.Query queryStatus = Treasure.Query.SoldListed, uint pageCount = 1, uint startIndex = 1) |
| 197 | + { |
| 198 | + Session.ThrowIfInvalid(); |
| 199 | + |
| 200 | + var listedTreasures = new List<Treasure>(); |
| 201 | + for (var i = startIndex; i < (startIndex + pageCount); ++i) |
| 202 | + { |
| 203 | + var res = Session.Get("http://www.elitepvpers.com/theblackmarket/treasures/" + |
| 204 | + ((queryStatus == Treasure.Query.Bought) ? "bought" : "soldunsold") |
| 205 | + + "/" + i); |
| 206 | + var htmlDocument = new HtmlDocument(); |
| 207 | + htmlDocument.LoadHtml(res.ToString()); |
| 208 | + |
| 209 | + var rootFormNode = htmlDocument.GetElementbyId("contentbg"); |
| 210 | + if (rootFormNode == null) continue; |
| 211 | + |
| 212 | + var tableNode = rootFormNode.SelectSingleNode("table[1]/tr[1]/td[1]/table[1]/tr[2]/td[1]/div[1]/div[3]/table[1]"); |
| 213 | + if (tableNode == null) continue; |
| 214 | + |
| 215 | + // skip the first <tr> element since that is the table header |
| 216 | + foreach (var treasureListingNode in tableNode.GetElementsByTagName("tr").Skip(1)) |
| 217 | + { |
| 218 | + var idNode = treasureListingNode.SelectSingleNode("td[1]"); |
| 219 | + var titleNode = treasureListingNode.SelectSingleNode("td[2]"); |
| 220 | + var costNode = treasureListingNode.SelectSingleNode("td[3]"); |
| 221 | + var opponentNode = treasureListingNode.SelectSingleNode("td[4]"); |
| 222 | + var listedTreasure = new Treasure |
| 223 | + { |
| 224 | + // first column is the id with a trailing # |
| 225 | + ID = (idNode != null) ? Convert.ToUInt32(idNode.InnerText.TrimStart('#')) : 0, |
| 226 | + |
| 227 | + // second column is the treasure title |
| 228 | + Title = (titleNode != null) ? titleNode.InnerText : "", |
| 229 | + }; |
| 230 | + |
| 231 | + // since this function is only available for logged-in users, the seller (or buyer, depends on the request) is automatically the logged-in user |
| 232 | + if (queryStatus == Treasure.Query.Bought) |
| 233 | + listedTreasure.Buyer = User; |
| 234 | + else |
| 235 | + listedTreasure.Seller = User; |
| 236 | + |
| 237 | + // third column is the cost |
| 238 | + var match = new Regex(@"([0-9]+) eg").Match(costNode.InnerText); |
| 239 | + if (match.Groups.Count > 1) |
| 240 | + listedTreasure.Cost = Convert.ToUInt32(match.Groups[1].Value); |
| 241 | + |
| 242 | + // the last column is the treasure buyer or seller |
| 243 | + if (opponentNode != null) |
| 244 | + { |
| 245 | + opponentNode = opponentNode.SelectSingleNode("a[1]"); |
| 246 | + if (opponentNode != null) |
| 247 | + { |
| 248 | + var opponent = opponentNode.Attributes.Contains("href") |
| 249 | + ? new User(opponentNode.InnerText, |
| 250 | + epvpapi.User.FromURL(opponentNode.Attributes["href"].Value)) |
| 251 | + : new User(); |
| 252 | + |
| 253 | + if (queryStatus == Treasure.Query.Bought) |
| 254 | + { |
| 255 | + listedTreasure.Seller = opponent; |
| 256 | + listedTreasure.Available = false; |
| 257 | + } |
| 258 | + else |
| 259 | + { |
| 260 | + listedTreasure.Buyer = opponent; |
| 261 | + listedTreasure.Available = false; |
| 262 | + } |
| 263 | + } |
| 264 | + } |
| 265 | + |
| 266 | + listedTreasures.Add(listedTreasure); |
| 267 | + } |
| 268 | + } |
| 269 | + |
| 270 | + return listedTreasures; |
| 271 | + } |
| 272 | + |
| 273 | + |
184 | 274 | /// <summary> |
185 | 275 | /// Removes/disables the current Avatar of the <c>User</c> |
186 | 276 | /// </summary> |
|
0 commit comments