-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathTPTracker.cpp
390 lines (310 loc) · 15.2 KB
/
TPTracker.cpp
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#include "TPTracker.h"
#include "GW2API.h"
#include "OverlayConfig.h"
#include "Language.h"
#include "Bedrock/UtilLib/PNGDecompressor.h"
#include "ThirdParty/BugSplat/inc/BugSplat.h"
using namespace jsonxx;
LIGHTWEIGHT_CRITICALSECTION itemCacheCritSec;
CDictionary<TS32, GW2ItemData> itemDataCache;
TBOOL HasGW2ItemData( TS32 itemID )
{
CLightweightCriticalSection cs( &itemCacheCritSec );
return itemDataCache.HasKey( itemID );
}
GW2ItemData GetGW2ItemData( TS32 itemID )
{
CLightweightCriticalSection cs( &itemCacheCritSec );
if ( itemDataCache.HasKey( itemID ) )
return itemDataCache[ itemID ];
return GW2ItemData();
}
void SetGW2ItemData( GW2ItemData& data )
{
CLightweightCriticalSection cs( &itemCacheCritSec );
itemDataCache[ data.itemID ] = data;
}
CString FetchHTTPS( LPCWSTR url, LPCWSTR path );
__inline CString ToGold( TS32 value )
{
TS32 copper = value % 100;
value /= 100;
TS32 silver = value % 100;
value /= 100;
CString result;
if ( value )
{
result += CString::Format( "%d", value ) + DICT( "gold" ) + " ";
result += CString::Format( "%.2d", silver ) + DICT( "silver" ) + " ";
result += CString::Format( "%.2d", copper ) + DICT( "copper" );
}
else
{
if ( silver )
{
result += CString::Format( "%d", silver ) + DICT( "silver" ) + " ";
result += CString::Format( "%.2d", copper ) + DICT( "copper" );
}
else
result = CString::Format( "%d", copper ) + DICT( "copper" );
}
return result;
}
void TPTracker::OnDraw( CWBDrawAPI* API )
{
CWBFont* f = GetFont( GetState() );
TS32 size = f->GetLineHeight();
TS32 onlyShowOutbid = Config::GetValue( "TPTrackerOnlyShowOutbid" );
TS32 nextSellOnly = Config::GetValue( "TPTrackerNextSellOnly" );
GW2::APIKeyManager::Status status = GW2::apiKeyManager.DisplayStatusText( API, f );
GW2::APIKey* key = GW2::apiKeyManager.GetIdentifiedAPIKey();
if ( key && key->valid && ( globalTimer.GetTime() - lastFetchTime > 150000 || !lastFetchTime ) && !beingFetched && !fetchThread.joinable() )
{
beingFetched = true;
fetchThread = std::thread( [this, key]()
{
SetPerThreadCRTExceptionBehavior();
CString qbuys = CString( "{\"buys\":" ) + key->QueryAPI( "v2/commerce/transactions/current/buys" );
CString qsells = CString( "{\"sells\":" ) + key->QueryAPI( "v2/commerce/transactions/current/sells" );
Object json;
Object json2;
json.parse( qbuys.GetPointer() );
json2.parse( qsells.GetPointer() );
CArray<TransactionItem> incoming;
CArray<TransactionItem> outgoing;
CArray<TS32> unknownItems;
CArray<TS32> priceCheckList;
if ( json.has<Array>( "buys" ) )
{
auto buyData = json.get<Array>( "buys" ).values();
for ( unsigned int x = 0; x < buyData.size(); x++ )
{
if ( !buyData[ x ]->is<Object>() )
continue;
Object& item = buyData[ x ]->get<Object>();
TransactionItem itemData;
if ( !TPTracker::ParseTransaction( item, itemData ) )
continue;
incoming += itemData;
if ( !itemDataCache.HasKey( itemData.itemID ) )
unknownItems += itemData.itemID;
priceCheckList.AddUnique( itemData.itemID );
}
}
if ( json2.has<Array>( "sells" ) )
{
auto buyData = json2.get<Array>( "sells" ).values();
for ( unsigned int x = 0; x < buyData.size(); x++ )
{
if ( !buyData[ x ]->is<Object>() )
continue;
Object& item = buyData[ x ]->get<Object>();
TransactionItem itemData;
if ( !TPTracker::ParseTransaction( item, itemData ) )
continue;
outgoing += itemData;
if ( !itemDataCache.HasKey( itemData.itemID ) )
unknownItems += itemData.itemID;
priceCheckList.AddUnique( itemData.itemID );
}
}
CString itemIds;
if ( unknownItems.NumItems() )
{
for ( int x = 0; x < unknownItems.NumItems(); x++ )
itemIds += CString::Format( "%d,", unknownItems[ x ] );
//https://api.guildwars2.com/v2/items?ids=28445,12452
CString items = CString( "{\"items\":" ) + key->QueryAPI( ( CString( "v2/items?ids=" ) + itemIds ).GetPointer() ) + "}";
Object itemjson;
itemjson.parse( items.GetPointer() );
if ( itemjson.has<Array>( "items" ) )
{
auto items = itemjson.get<Array>( "items" ).values();
for ( unsigned int x = 0; x < items.size(); x++ )
{
if ( !items[ x ]->is<Object>() )
continue;
Object& item = items[ x ]->get<Object>();
GW2ItemData itemData;
if ( !item.has<String>( "name" ) || !item.has<Number>( "id" ) )
continue;
itemData.name = CString( item.get<String>( "name" ).data() );
itemData.itemID = TS32( item.get<Number>( "id" ) );
if ( item.has<String>( "icon" ) )
{
CString iconFile = CString( item.get<String>( "icon" ).data() );
if ( iconFile.Find( "https://render.guildwars2.com/" ) == 0 )
{
WCHAR wpath[ 4096 ];
memset( wpath, 0, sizeof( wpath ) );
iconFile.Substring( 29 ).WriteAsWideChar( wpath, 4096 );
CString png = FetchHTTPS( L"render.guildwars2.com", wpath );
TU8* imageData = nullptr;
TS32 xres, yres;
if ( DecompressPNG( (TU8*)png.GetPointer(), png.Length(), imageData, xres, yres ) )
{
ARGBtoABGR( imageData, xres, yres );
CRect area = CRect( 0, 0, xres, yres );
itemData.icon = GetApplication()->GetAtlas()->AddImage( imageData, xres, yres, area );
}
}
}
SetGW2ItemData( itemData );
}
}
}
{
for ( int x = 0; x < priceCheckList.NumItems(); x++ )
itemIds += CString::Format( "%d,", priceCheckList[ x ] );
//https://api.guildwars2.com/v2/commerce/prices?ids=19684,19709
CString items = CString( "{\"items\":" ) + key->QueryAPI( ( CString( "v2/commerce/prices?ids=" ) + itemIds ).GetPointer() ) + "}";
Object itemjson;
itemjson.parse( items.GetPointer() );
if ( itemjson.has<Array>( "items" ) )
{
auto items = itemjson.get<Array>( "items" ).values();
for ( unsigned int x = 0; x < items.size(); x++ )
{
if ( !items[ x ]->is<Object>() )
continue;
Object& item = items[ x ]->get<Object>();
if ( !item.has<Number>( "id" ) || !item.has<Object>( "buys" ) || !item.has<Object>( "sells" ) )
continue;
TS32 id = TS32( item.get<Number>( "id" ) );
if ( !HasGW2ItemData( id ) )
continue;
Object buys = item.get<Object>( "buys" );
Object sells = item.get<Object>( "sells" );
if ( !buys.has<Number>( "unit_price" ) || !sells.has<Number>( "unit_price" ) )
continue;
GW2ItemData itemData = GetGW2ItemData( id );
itemData.buyPrice = TS32( buys.get<Number>( "unit_price" ) );
itemData.sellPrice = TS32( sells.get<Number>( "unit_price" ) );
SetGW2ItemData( itemData );
}
}
}
{
CLightweightCriticalSection cs( &itemCacheCritSec );
buys = incoming;
sells = outgoing;
}
beingFetched = false;
} );
}
if ( !beingFetched && fetchThread.joinable() )
{
lastFetchTime = globalTimer.GetTime();
fetchThread.join();
}
{
CLightweightCriticalSection cs( &itemCacheCritSec );
TS32 posy = 0;
TS32 lh = f->GetLineHeight();
if ( buys.NumItems() && Config::GetValue( "TPTrackerShowBuys" ) )
{
CArray<TS32> showedAlready;
TS32 textPosy = posy;
TS32 writtenCount = 0;
posy += lh + 2;
for ( int x = 0; x < buys.NumItems(); x++ )
{
if ( !HasGW2ItemData( buys[ x ].itemID ) )
continue;
auto& itemData = GetGW2ItemData( buys[ x ].itemID );
TBOOL outbid = buys[ x ].price < itemData.buyPrice;
if ( nextSellOnly && showedAlready.Find( itemData.itemID ) >= 0 )
continue;
if ( !onlyShowOutbid || outbid )
{
TS32 price = buys[ x ].price;
if ( nextSellOnly )
{
for ( int y = x; y < buys.NumItems(); y++ )
if ( buys[ y ].itemID == buys[ x ].itemID )
price = max( buys[ y ].price, buys[ x ].price );
}
if ( itemData.icon )
API->DrawAtlasElement( itemData.icon, CRect( lh, posy, lh * 2 + 5, posy + lh + 5 ), false, false, true, true, 0xffffffff );
CString text = itemData.name + " " + ToGold( price );
if ( buys[ x ].quantity > 1 )
text = CString::Format( "%d ", buys[ x ].quantity ) + text;
f->Write( API, text, CPoint( int( lh * 2.5 + 3 ), posy + 3 ), !outbid ? 0xffffffff : 0xffee6655 );
writtenCount++;
posy += lh + 6;
if ( nextSellOnly )
showedAlready.AddUnique( itemData.itemID );
}
}
posy += 2;
if ( writtenCount )
f->Write( API, DICT( onlyShowOutbid ? "outbidbuys" : "buylist" ), CPoint( 0, textPosy ), 0xffffffff );
else
posy -= lh + 4;
}
if ( sells.NumItems() && Config::GetValue( "TPTrackerShowSells" ) )
{
CArray<TS32> showedAlready;
TS32 textPosy = posy;
TS32 writtenCount = 0;
posy += lh + 2;
for ( int x = 0; x < sells.NumItems(); x++ )
{
if ( !HasGW2ItemData( sells[ x ].itemID ) )
continue;
auto& itemData = GetGW2ItemData( sells[ x ].itemID );
TBOOL outbid = sells[ x ].price > itemData.sellPrice;
if ( nextSellOnly && showedAlready.Find( itemData.itemID ) >= 0 )
continue;
if ( !onlyShowOutbid || outbid )
{
TS32 price = sells[ x ].price;
if ( nextSellOnly )
{
for ( int y = x; y < sells.NumItems(); y++ )
if ( sells[ y ].itemID == sells[ x ].itemID )
price = min( sells[ y ].price, sells[ x ].price );
}
if ( itemData.icon )
API->DrawAtlasElement( itemData.icon, CRect( lh, posy, lh * 2 + 5, posy + lh + 5 ), false, false, true, true, 0xffffffff );
CString text = itemData.name + " " + ToGold( price );
if ( sells[ x ].quantity > 1 )
text = CString::Format( "%d ", sells[ x ].quantity ) + text;
f->Write( API, text, CPoint( int( lh * 2.5 + 3 ), posy + 3 ), !outbid ? 0xffffffff : 0xffee6655 );
writtenCount++;
posy += lh + 6;
if ( nextSellOnly )
showedAlready.AddUnique( itemData.itemID );
}
}
if ( writtenCount )
f->Write( API, DICT( onlyShowOutbid ? "outbidsells" : "selllist" ), CPoint( 0, textPosy ), 0xffffffff );
}
}
DrawBorder( API );
}
TBOOL TPTracker::ParseTransaction( Object& object, TransactionItem& output )
{
if ( !object.has<Number>( "id" ) || !object.has<Number>( "item_id" ) || !object.has<Number>( "price" ) || !object.has<Number>( "quantity" ) )
return false;
output.transactionID = TS32( object.get<Number>( "id" ) );
output.itemID = TS32( object.get<Number>( "item_id" ) );
output.price = TS32( object.get<Number>( "price" ) );
output.quantity = TS32( object.get<Number>( "quantity" ) );
return true;
}
TPTracker::TPTracker( CWBItem* Parent, CRect Position ) : CWBItem( Parent, Position )
{}
TPTracker::~TPTracker()
{
if ( fetchThread.joinable() )
fetchThread.join();
}
CWBItem* TPTracker::Factory( CWBItem* Root, CXMLNode& node, CRect& Pos )
{
return new TPTracker( Root, Pos );
}
TBOOL TPTracker::IsMouseTransparent( CPoint& ClientSpacePoint, WBMESSAGE MessageType )
{
return true;
}