Skip to content

Commit ee0c8a4

Browse files
author
Mostey
committed
Merge pull request #15 from Mostey/tbm_treasures
Retrieving Treasures bound to the user account
2 parents 942d67c + 530655d commit ee0c8a4

File tree

2 files changed

+109
-6
lines changed

2 files changed

+109
-6
lines changed

src/epvpapi/Connection/ProfileSession.cs

Lines changed: 91 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using HtmlAgilityPack;
1+
using epvpapi.TBM;
2+
using HtmlAgilityPack;
23
using System;
34
using System.Collections.Generic;
45
using System.Linq;
@@ -181,6 +182,95 @@ public List<PrivateMessage> GetPrivateMessages(uint firstPage, uint pageCount, P
181182
return fetchedMessages;
182183
}
183184

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+
184274
/// <summary>
185275
/// Removes/disables the current Avatar of the <c>User</c>
186276
/// </summary>

src/epvpapi/TBM/Treasure.cs

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,19 @@ namespace epvpapi.TBM
1515
/// </summary>
1616
public class Treasure : UniqueObject, IUniqueWebObject, IDefaultUpdatable, IDeletable
1717
{
18+
public enum Query
19+
{
20+
/// <summary>
21+
/// If the <c>Treasure</c> was bought, basically the same as <c>SoldListed</c>
22+
/// </summary>
23+
Bought,
24+
25+
/// <summary>
26+
/// If the <c>Treasure</c> was sold and/or listed
27+
/// </summary>
28+
SoldListed
29+
}
30+
1831
/// <summary>
1932
/// Title of the treasure, visible for everyone
2033
/// </summary>
@@ -61,17 +74,16 @@ public class Treasure : UniqueObject, IUniqueWebObject, IDefaultUpdatable, IDele
6174
public DateTime PurchaseDate { get; set; }
6275

6376
/// <summary>
64-
/// If the <c>Treasure</c> was already sold to someone, this value is set to true
77+
/// If the <c>Treasure</c> was already sold to someone, this value is set to fakse
6578
/// </summary>
66-
public bool Sold { get; set; }
67-
79+
public bool Available { get; set; }
6880

6981
public Treasure(string title, string content, uint cost):
7082
this(0, title, content, cost)
7183
{ }
7284

7385

74-
public Treasure(uint id, string title = null, string content = null, uint cost = 0):
86+
public Treasure(uint id = 0, string title = null, string content = null, uint cost = 0):
7587
base(id)
7688
{
7789
Title = title;
@@ -81,6 +93,7 @@ public Treasure(uint id, string title = null, string content = null, uint cost =
8193
Buyer = new User();
8294
CreationDate = new DateTime();
8395
PurchaseDate = new DateTime();
96+
Available = true;
8497
}
8598

8699
/// <summary>
@@ -185,7 +198,7 @@ public void Update(Session session)
185198
}
186199
else if (key == "Purchase date:")
187200
{
188-
Sold = true;
201+
Available = false;
189202
var countdownNode = valueNode.SelectSingleNode("div[1]");
190203
PurchaseDate = (countdownNode != null) ? countdownNode.InnerText.ToElitepvpersDateTime() : new DateTime();
191204
}

0 commit comments

Comments
 (0)