-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathFleaPriceCache.cs
73 lines (59 loc) · 1.8 KB
/
FleaPriceCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;
using static LootValue.Globals;
namespace LootValue
{
internal static class FleaPriceCache
{
static Dictionary<string, CachePrice> cache = new Dictionary<string, CachePrice>();
public static async Task<double?> FetchPrice(string templateId)
{
bool fleaAvailable = Session.RagFair.Available || LootValueMod.ShowFleaPriceBeforeAccess.Value;
if (!fleaAvailable || !LootValueMod.EnableFleaQuickSell.Value)
return null;
if (cache.ContainsKey(templateId))
{
double secondsSinceLastUpdate = (DateTime.Now - cache[templateId].lastUpdate).TotalSeconds;
if (secondsSinceLastUpdate > 300)
return await QueryAndTryUpsertPrice(templateId, true);
else
return cache[templateId].price;
}
else
return await QueryAndTryUpsertPrice(templateId, false);
}
private static async Task<string> QueryPrice(string templateId)
{
return await CustomRequestHandler.PostJsonAsync("/LootValue/GetItemLowestFleaPrice", JsonConvert.SerializeObject(new FleaPriceRequest(templateId)));
}
private static async Task<double?> QueryAndTryUpsertPrice(string templateId, bool update)
{
string response = await QueryPrice(templateId);
bool hasPlayerFleaPrice = !(string.IsNullOrEmpty(response) || response == "null");
if (hasPlayerFleaPrice)
{
double price = double.Parse(response);
if (price < 0)
{
cache.Remove(templateId);
return null;
}
cache[templateId] = new CachePrice(price);
return price;
}
return null;
}
}
internal struct CachePrice
{
public double price { get; private set; }
public DateTime lastUpdate { get; private set; }
public CachePrice(double price)
{
this.price = price;
lastUpdate = DateTime.Now;
}
}
}