3
3
using System . Diagnostics ;
4
4
using System . Drawing ;
5
5
using System . IO ;
6
- using System . Text . Json ;
6
+ using System . Net . Http ;
7
7
using System . Text . RegularExpressions ;
8
8
using System . Threading ;
9
9
using System . Threading . Tasks ;
10
10
using System . Windows . Forms ;
11
+ using Newtonsoft . Json ;
11
12
12
13
namespace TarkovPriceViewer
13
14
{
14
15
static class Program
15
16
{
17
+ public static bool UsingAPI = true ;
18
+
16
19
private static MainForm main = null ;
17
20
public static Dictionary < String , String > settings = new Dictionary < String , String > ( ) ;
18
21
public static readonly List < Item > itemlist = new List < Item > ( ) ;
@@ -29,21 +32,27 @@ static class Program
29
32
public static readonly String appname = "EscapeFromTarkov" ;
30
33
public static readonly String loading = "Loading..." ;
31
34
public static readonly String notfound = "Item Name Not Found." ;
32
- public static readonly String noflea = "Item not Found on Flea." ;
33
- public static readonly String notfinishloading = "Wait for Loading Data. Please Check Your Internet, and Check Tarkov Wiki Site." ;
35
+ public static readonly String waitingForTooltip = "Loading" ;
36
+ public static readonly String noflea = "Item not Found on the Flea Market." ;
37
+ public static readonly String notfinishloading = "Ballistics Data not finished loading. \n Please try again or check your Internet connection." ;
38
+ public static readonly String notfinishloadingAPI = "API not finished loading. \n Please try again or check your Internet connection." ;
34
39
public static readonly String presscomparekey = "Please Press Compare Key." ;
35
40
public static bool finishloadingballistics = false ;
41
+ public static bool finishloadingAPI = false ;
36
42
public static readonly String wiki = "https://escapefromtarkov.fandom.com/wiki/" ;
43
+ public static readonly String tarkov_dev = "https://tarkov.dev/" ;
37
44
public static readonly String tarkovmarket = "https://tarkov-market.com/item/" ;
38
45
public static readonly String official = "https://www.escapefromtarkov.com/" ;
39
- public static readonly String github = "https://github.com/hwangshkr /TarkovPriceViewer" ;
40
- public static readonly String checkupdate = "https://github.com/hwangshkr /TarkovPriceViewer/raw/main/README.md" ;
46
+ public static readonly String github = "https://github.com/Zotikus1001 /TarkovPriceViewer" ;
47
+ public static readonly String checkupdate = "https://github.com/Zotikus1001 /TarkovPriceViewer/raw/main/README.md" ;
41
48
public static readonly char rouble = '₽' ;
42
49
public static readonly char dollar = '$' ;
43
50
public static readonly char euro = '€' ;
44
51
public static readonly char [ ] splitcur = new char [ ] { rouble , dollar , euro } ;
45
52
public static readonly Regex inraid_filter = new Regex ( @"in raid" ) ;
46
53
public static readonly Regex money_filter = new Regex ( @"([\d,]+[₽\$€]|[₽\$€][\d,]+)" ) ;
54
+ public static DateTime APILastUpdated = DateTime . Now . AddHours ( - 5 ) ;
55
+ public static TarkovAPI . Data tarkovAPI ;
47
56
48
57
/// <summary>
49
58
/// 해당 애플리케이션의 주 진입점입니다.
@@ -65,14 +74,25 @@ static void Main()
65
74
}
66
75
catch ( Exception ex )
67
76
{
68
- Debug . WriteLine ( ex . Message ) ;
77
+ Debug . WriteLine ( "Error 15: " + ex . Message ) ;
69
78
}
70
79
}
71
80
ThreadPool . SetMinThreads ( 10 , 10 ) ;
72
81
ThreadPool . SetMaxThreads ( 20 , 20 ) ;
73
82
Task task = Task . Factory . StartNew ( ( ) => getBallistics ( ) ) ;
83
+
74
84
LoadSettings ( ) ;
75
- getItemList ( ) ;
85
+
86
+ if ( UsingAPI )
87
+ {
88
+ if ( File . Exists ( @"Resources\TarkovAPI.json" ) )
89
+ APILastUpdated = File . GetLastWriteTime ( @"Resources\TarkovAPI.json" ) ;
90
+
91
+ Task task2 = Task . Factory . StartNew ( ( ) => UpdateItemListAPI ( ) ) ;
92
+ }
93
+ else
94
+ getItemList ( ) ;
95
+
76
96
main = new MainForm ( ) ;
77
97
if ( Convert . ToBoolean ( settings [ "MinimizetoTrayWhenStartup" ] ) )
78
98
{
@@ -96,18 +116,113 @@ private static void getItemList()
96
116
{
97
117
String [ ] spl = textValue [ i ] . Split ( '\t ' ) ;
98
118
Item item = new Item ( ) ;
99
- item . name_display = spl [ 0 ] . Trim ( ) ;
100
- item . name_display2 = spl [ 2 ] . Trim ( ) ;
119
+ item . name_display = spl [ 0 ] . Trim ( ) ; //Column 1
120
+ item . name_display2 = spl [ 2 ] . Trim ( ) ; //Column 3
101
121
item . name_compare = item . name_display . ToLower ( ) . ToCharArray ( ) ;
102
122
item . name_compare2 = item . name_display2 . ToLower ( ) . ToCharArray ( ) ;
103
- item . market_address = spl [ 1 ] . Replace ( " " , "_" ) . Trim ( ) ;
104
- item . wiki_address = spl [ 0 ] . Replace ( " " , "_" ) . Trim ( ) ;
123
+ item . market_address = spl [ 1 ] . Replace ( " " , "_" ) . Trim ( ) ; //Column 2
124
+ item . wiki_address = spl [ 0 ] . Replace ( " " , "_" ) . Trim ( ) ; //Column 1
105
125
itemlist . Add ( item ) ;
106
126
}
107
127
}
108
128
Debug . WriteLine ( "itemlist Count : " + itemlist . Count ) ;
109
129
}
110
130
131
+ public static async void UpdateItemListAPI ( )
132
+ {
133
+ if ( UsingAPI )
134
+ {
135
+ //If Outdated by 15 minutes.
136
+ if ( ( DateTime . Now - APILastUpdated ) . TotalMinutes >= 15 )
137
+ {
138
+ try
139
+ {
140
+ Debug . WriteLine ( "\n --> Updating API..." ) ;
141
+
142
+ var data = new Dictionary < string , string > ( )
143
+ {
144
+ { "query" , "{\r \n items {\r \n name\r \n types\r \n lastLowPrice\r \n avg24hPrice\r \n updated\r \n fleaMarketFee\r \n link\r \n wikiLink\r \n width\r \n height\r \n properties {\r \n ... on ItemPropertiesAmmo {\r \n caliber\r \n damage\r \n projectileCount\r \n penetrationPower\r \n armorDamage\r \n fragmentationChance\r \n ammoType\r \n }\r \n ... on ItemPropertiesWeapon {\r \n caliber\r \n ergonomics\r \n defaultRecoilVertical\r \n defaultRecoilHorizontal\r \n defaultWidth\r \n defaultHeight\r \n defaultAmmo {\r \n name\r \n }\r \n }\r \n }\r \n sellFor {\r \n currency\r \n priceRUB\r \n vendor {\r \n name\r \n ... on TraderOffer {\r \n minTraderLevel\r \n }\r \n }\r \n }\r \n buyFor {\r \n currency\r \n priceRUB\r \n vendor {\r \n name\r \n ... on TraderOffer {\r \n minTraderLevel\r \n }\r \n }\r \n }\r \n usedInTasks {\r \n name\r \n trader {\r \n name\r \n }\r \n map {\r \n name\r \n }\r \n minPlayerLevel\r \n traderLevelRequirements {\r \n level\r \n }\r \n }\r \n }\r \n }" }
145
+ } ;
146
+
147
+ using ( var httpClient = new HttpClient ( ) )
148
+ {
149
+ //Http response message
150
+ var httpResponse = await httpClient . PostAsJsonAsync ( "https://api.tarkov.dev/graphql" , data ) ;
151
+ //Response content
152
+ string responseContent = await httpResponse . Content . ReadAsStringAsync ( ) ;
153
+
154
+ int index = responseContent . IndexOf ( "{\" data\" :" ) ;
155
+ if ( index != - 1 )
156
+ {
157
+ responseContent = responseContent . Remove ( index , 8 ) ;
158
+ responseContent = responseContent . Remove ( responseContent . Length - 1 , 1 ) ;
159
+ }
160
+
161
+ //Prettify JSON (Produces a larger file)
162
+ //responseContent = JToken.Parse(responseContent).ToString();
163
+
164
+ tarkovAPI = JsonConvert . DeserializeObject < TarkovAPI . Data > ( responseContent ) ;
165
+ APILastUpdated = DateTime . Now ;
166
+ finishloadingAPI = true ;
167
+ Debug . WriteLine ( "\n --> API Updated!" ) ;
168
+ File . WriteAllText ( @"Resources\TarkovAPI.json" , responseContent ) ;
169
+ }
170
+ }
171
+ catch ( Exception ex )
172
+ {
173
+ //MessageBox.Show("--> Error trying to update Tarkov API: " + ex.Message);
174
+ Thread . Sleep ( 500 ) ;
175
+ UpdateItemListAPI ( ) ;
176
+ }
177
+ }
178
+ else if ( tarkovAPI == null )
179
+ {
180
+ try
181
+ {
182
+ string responseContent = File . ReadAllText ( @"Resources\TarkovAPI.json" ) ;
183
+ tarkovAPI = JsonConvert . DeserializeObject < TarkovAPI . Data > ( responseContent ) ;
184
+ Debug . WriteLine ( "\n API Loaded from local File! \n " + LastUpdated ( APILastUpdated ) ) ;
185
+ finishloadingAPI = true ;
186
+ }
187
+ catch ( Exception ex )
188
+ {
189
+ //MessageBox.Show("\n--> Error trying to load Tarkov API from local file: " + ex.Message);
190
+ Thread . Sleep ( 500 ) ;
191
+ UpdateItemListAPI ( ) ;
192
+ }
193
+ }
194
+ else
195
+ Debug . WriteLine ( "--> No need to update API! \n --> " + LastUpdated ( APILastUpdated ) + "\n " ) ;
196
+ }
197
+ }
198
+
199
+ public static string LastUpdated ( DateTime time )
200
+ {
201
+ TimeSpan elapsed = DateTime . Now - time ;
202
+
203
+ if ( elapsed . TotalHours < 1 )
204
+ {
205
+ if ( elapsed . TotalMinutes < 1 )
206
+ return $ "Updated: { ( int ) elapsed . TotalMinutes } minute ago";
207
+ else
208
+ return $ "Updated: { ( int ) elapsed . TotalMinutes } minutes ago";
209
+ }
210
+ else if ( elapsed . TotalDays < 1 )
211
+ {
212
+ if ( elapsed . TotalHours < 2 )
213
+ return $ "Updated: { ( int ) elapsed . TotalHours } hour ago";
214
+ else
215
+ return $ "Updated: { ( int ) elapsed . TotalHours } hours ago";
216
+ }
217
+ else
218
+ {
219
+ if ( elapsed . TotalDays <= 1 )
220
+ return $ "Updated: { ( int ) elapsed . TotalDays } day ago";
221
+ else
222
+ return $ "Updated: { ( int ) elapsed . TotalDays } days ago";
223
+ }
224
+ }
225
+
111
226
public static void LoadSettings ( )
112
227
{
113
228
try
@@ -119,16 +234,16 @@ public static void LoadSettings()
119
234
String text = File . ReadAllText ( setting_path ) ;
120
235
try
121
236
{
122
- settings = JsonSerializer . Deserialize < Dictionary < String , String > > ( text ) ;
123
- } catch ( JsonException je )
237
+ settings = System . Text . Json . JsonSerializer . Deserialize < Dictionary < String , String > > ( text ) ;
238
+ } catch ( System . Text . Json . JsonException je )
124
239
{
125
- Debug . WriteLine ( je . Message ) ;
240
+ Debug . WriteLine ( "Error 11: " + je . Message ) ;
126
241
text = "{}" ;
127
- settings = JsonSerializer . Deserialize < Dictionary < String , String > > ( text ) ;
242
+ settings = System . Text . Json . JsonSerializer . Deserialize < Dictionary < String , String > > ( text ) ;
128
243
}
129
244
String st ;
130
245
settings . Remove ( "Version" ) ; //force
131
- settings . Add ( "Version" , "v1.18 " ) ; //force
246
+ settings . Add ( "Version" , "v1.22 " ) ; //force
132
247
if ( ! settings . TryGetValue ( "MinimizetoTrayWhenStartup" , out st ) )
133
248
{
134
249
settings . Add ( "MinimizetoTrayWhenStartup" , "false" ) ;
@@ -188,7 +303,7 @@ public static void LoadSettings()
188
303
}
189
304
catch ( Exception e )
190
305
{
191
- Debug . WriteLine ( e . Message ) ;
306
+ Debug . WriteLine ( "Error 12: " + e . Message ) ;
192
307
}
193
308
}
194
309
@@ -200,12 +315,12 @@ public static void SaveSettings()
200
315
{
201
316
File . Create ( setting_path ) . Dispose ( ) ;
202
317
}
203
- string jsonString = JsonSerializer . Serialize < Dictionary < String , String > > ( settings ) ;
318
+ string jsonString = System . Text . Json . JsonSerializer . Serialize < Dictionary < String , String > > ( settings ) ;
204
319
File . WriteAllText ( setting_path , jsonString . Replace ( "," , ",\n " ) ) ;
205
320
}
206
321
catch ( Exception e )
207
322
{
208
- Debug . WriteLine ( e . Message ) ;
323
+ Debug . WriteLine ( "Error 13: " + e . Message ) ;
209
324
}
210
325
}
211
326
@@ -220,9 +335,10 @@ private static void getBallistics()
220
335
HtmlAgilityPack . HtmlDocument doc = new HtmlAgilityPack . HtmlDocument ( ) ;
221
336
Debug . WriteLine ( Program . wiki + "Ballistics" ) ;
222
337
doc . LoadHtml ( wc . DownloadString ( Program . wiki + "Ballistics" ) ) ;
223
- HtmlAgilityPack . HtmlNode node_tm = doc . DocumentNode . SelectSingleNode ( "//table[@id='trkballtable']" ) ;
338
+ HtmlAgilityPack . HtmlNode node_tm = doc . DocumentNode . SelectSingleNode ( "//table[4]" ) ; //table[ @id='trkballtable']
224
339
HtmlAgilityPack . HtmlNodeCollection nodes = null ;
225
340
HtmlAgilityPack . HtmlNodeCollection sub_nodes = null ;
341
+
226
342
if ( node_tm != null )
227
343
{
228
344
node_tm = node_tm . SelectSingleNode ( ".//tbody" ) ;
@@ -299,7 +415,7 @@ private static void getBallistics()
299
415
}
300
416
catch ( Exception ex )
301
417
{
302
- Debug . WriteLine ( ex . Message ) ;
418
+ Debug . WriteLine ( "Error 14: " + ex . Message ) ;
303
419
}
304
420
damage += " = " + mul ;
305
421
}
@@ -334,11 +450,11 @@ private static void getBallistics()
334
450
}
335
451
catch ( Exception e )
336
452
{
337
- Debug . WriteLine ( "error with ballistics : " + e . Message ) ;
453
+ Debug . WriteLine ( "Error with Ballistics : " + e . Message ) ;
338
454
Thread . Sleep ( 3000 ) ;
339
455
}
340
456
}
341
- Debug . WriteLine ( "finish to get ballistics. " ) ;
457
+ Debug . WriteLine ( "Finished getting Ballistics! " ) ;
342
458
}
343
459
}
344
460
}
0 commit comments