-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
627 lines (564 loc) · 15.3 KB
/
index.js
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
const fetch = require('node-fetch');
/**
* Trucky API client class
* @class
*/
class TruckyAPIClient
{
/**
* Creates an instance of TruckyAPIClient.
*
* @memberOf TruckyAPIClient
*/
constructor()
{
/** @access private */
this.config = {
serviceUrl: 'https://api.truckyapp.com'
}
}
/**
*
*
* @access private
* @param {string} url
* @ignore
* @async
* @returns {Promise}
*
* @memberOf TruckyAPIClient
*/
async executeRequest(url, method = "GET", payload)
{
try
{
var promise = new Promise((resolve, reject) => {
fetch(this.config.serviceUrl + url, {method: method}).then((response) => {
return response.json()
}).then((json) => {
resolve(json);
})
});
return promise;
} catch (error) {
console.debug('TruckyServices API request: ' + error.message);
return null;
}
}
/**
* Resolve Steam Vanity Url returning Steam player data if found
*
* @param {string} username Steam user vanity url
* @async
* @returns {Promise<TruckyAPIResponse>}
*
* @memberOf TruckyAPIClient
*/
async resolveVanityUrl(username)
{
var response = await this.executeRequest('/v2/steam/resolveVanityUrl?username=' + username);
return response;
}
/**
* Returns Steam Player data if found
*
* @param {string} steamid Steam User ID
* @async
* @returns {Promise<TruckyAPIResponse>}
*
* @memberOf TruckyServices
*/
async getPlayerSummaries(steamid)
{
var response = await this.executeRequest('/v2/steam/getPlayerSummaries?steamid=' + steamid);
return response;
}
/**
* Get ETS2Map Points of interest
*
* @returns {Promise<TruckyAPIResponse>}
* @async
* @memberof TruckyAPIClient
*/
async pois()
{
var response = await this.executeRequest('/v2/map/pois');
return response;
}
/**
* Check TruckersMP player online status
*
* @param {number} playerID TruckersMP User ID
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async isOnline(playerID)
{
var response = await this.executeRequest('/v2/map/online?playerID=' + playerID);
return response;
}
/**
* Check TruckersMP player online status - With improved informations, mapped map server and calculated location
*
* @param {number} playerID TruckersMP User ID
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async isOnlineImproved(playerID)
{
var response = await this.executeRequest('/v3/map/online?playerID=' + playerID);
return response;
}
/**
* Get Steam Friends
*
* @param {string} steamID Steam User ID
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async getFriends(steamID)
{
var response = await this.executeRequest('/v2/steam/getFriendsData?steamid=' + steamID);
return response;
}
/**
* Get TruckersMP news feed from Steam RSS
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async news()
{
var response = await this.executeRequest('/v2/rss/truckersMP');
return response;
}
/**
* Get ETS2 News feed from Steam RSS
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async ets2News()
{
var response = await this.executeRequest('/v2/rss/ets2');
return response;
}
/**
* Get ATS News feed from Steam RSS
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async atsNews()
{
var response = await this.executeRequest('/v2/rss/ats');
return response;
}
/**
* Get TruckersMP current update info and current plugin version
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async update_info()
{
var response = await this.executeRequest('/v2/truckersmp/update_info');
return response;
}
/**
* Get events and meetup from ETS2.com and truckers.events
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async events()
{
var response = await this.executeRequest('/v2/events/upcoming');
return response;
}
async eventDetail(id)
{
var response = await this.executeRequest('/v2/events/detail?eventID=' + id);
return response;
}
/**
* Get TruckersMP servers
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async servers()
{
var response = await this.executeRequest('/v2/truckersmp/servers');
return response;
}
/**
* Get TruckersMP game version info
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async game_version()
{
var response = await this.executeRequest('/v2/truckersmp/version');
return response;
}
/**
* Get TruckersMP game time
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async game_time()
{
var response = await this.executeRequest('/v2/truckersmp/time');
return response;
}
/**
* Get TruckersMP rules
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async rules()
{
var response = await this.executeRequest('/v2/truckersmp/rules');
return response;
}
/**
* Get TruckersMP player data
*
* @param {number} id TruckersMP User ID
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async player(id)
{
var response = await this.executeRequest('/v2/truckersmp/player?playerID=' + id);
return response;
}
/**
* Get TruckersMP player bans data
*
* @param {number} id TruckersMP User ID
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async bans(id)
{
var response = await this.executeRequest('/v2/truckersmp/bans?playerID=' + id);
return response;
}
/**
* Search player by Steam ID, Steam Username or TruckersMP ID based on searchType parameter
*
* @param {string} searchTerm Search Term, can be TruckersMP User ID, Steam User ID or Steam Vanity url
* @param {string} searchType Can be "steamid", "truckersmpid", "steamusername"
* @async
* @returns {Promise<TruckyAPIResponse>}
*
* @memberOf TruckyAPIClient
*/
async searchPlayer(searchTerm, searchType)
{
var playerInfo = {
found: false,
steamProfileInfo: null,
truckersMPProfileInfo: null,
bans: []
};
//console.warn(searchType);
switch (searchType) {
case 'steamusername':
var steamResponse = await this.resolveVanityUrl(searchTerm);
if (steamResponse.found) {
playerInfo.steamProfileInfo = steamResponse.playerInfo;
var apiResponse = await this.player(steamResponse.steamID);
if (!apiResponse.error) {
playerInfo.found = true;
playerInfo.truckersMPProfileInfo = apiResponse.response;
playerInfo.bans = await this.bans(playerInfo.truckersMPProfileInfo.id);
// playerInfo.onlineStatus = await
// truckyApi.isOnline(playerInfo.truckersMPProfileInfo.id);
} else {
playerInfo.found = false;
}
}
break;
case 'steamid':
case 'truckersmpid':
var apiResponse = await this.player(searchTerm);
if (!apiResponse.error) {
playerInfo.truckersMPProfileInfo = apiResponse.response;
var steamProfileInfo = await this.getPlayerSummaries(playerInfo.truckersMPProfileInfo.steamID64);
playerInfo.steamProfileInfo = steamProfileInfo.playerInfo;
playerInfo.bans = await this.bans(playerInfo.truckersMPProfileInfo.id);
// playerInfo.onlineStatus = await
// truckyApi.isOnline(playerInfo.truckersMPProfileInfo.id);
playerInfo.found = true;
}
break;
}
return playerInfo;
}
/**
* Get TruckersMP traffic data from traffic.krashnz.com
*
* @param {string} server TruckersMP Server short name
* @param {string} game Can be "ETS2" or "ATS"
* @async
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
*/
async traffic(server, game)
{
var response = await this.executeRequest('/v2/traffic?server=' + server + '&game=' + game);
return response;
}
async topTraffic(server, game)
{
var response = await this.executeRequest('/v2/traffic/top?server=' + server + '&game=' + game);
return response;
}
/**
* Get TruckersMP get traffic servers data from traffic.krashnz.com
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async traffic_servers()
{
var response = await this.executeRequest('/v2/traffic/servers?addAts=true');
return response;
}
/**
* Get World Of Trucks Random Gallery
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async wot_gallery_random()
{
var response = await this.executeRequest('/v2/wot/gallery/random');
return response;
}
/**
* Get World Of Trucks Editor's pick
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async wot_gallery_editorspick()
{
var response = await this.executeRequest('/v2/wot/gallery/editorspick');
return response;
}
/**
* Get World Of Trucks Best rated
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async wot_gallery_bestrated()
{
var response = await this.executeRequest('/v2/wot/gallery/bestrated');
return response;
}
/**
* Get World Of Trucks most viewed
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async wot_gallery_mostviewed()
{
var response = await this.executeRequest('/v2/wot/gallery/mostviewed');
return response;
}
/**
* Get current Twitch streams for ETS2
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async streams_twitch_ets2()
{
var response = await this.executeRequest('/v2/streams/twitch/ets2');
return response;
}
/**
* Get current Twitch streams for ATS
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async streams_twitch_ats()
{
var response = await this.executeRequest('/v2/streams/twitch/ats');
return response;
}
/**
* Get TruckersFM current listeners
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async truckersfm_listeners()
{
var response = await this.executeRequest('/v2/truckersfm/listeners');
return response;
}
/**
* Get TruckersFM current show
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async truckersfm_shows()
{
var response = await this.executeRequest('/v2/truckersfm/shows');
return response;
}
/**
* Get TruckersFM last played song
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async truckersfm_lastPlayed()
{
var response = await this.executeRequest('/v2/truckersfm/lastPlayed');
return response;
}
/**
* Get players online state by TruckersMP ids array
* Search TruckersMP player by username
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async onlineList(ids)
{
var response = await this.executeRequest('/v2/map/onlineList?ids=' + ids.join(','));
return response;
}
/**
* Search TruckersMP Player by username
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async searchPlayerByTruckersMPUsername(query)
{
var response = await this.executeRequest('/v2/truckersmp/searchPlayer?query=' + query);
return response;
}
/**
* Get TruckersMP player complete information with online state and Steam profile data
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async getPlayerInfoComplete(query)
{
var response = await this.executeRequest('/v2/trucky/player?query=' + query);
return response;
}
/**
* Get list of ETS2 cities, with coordinates and country
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async ets2_cities()
{
var response = await this.executeRequest('/v2/map/cities/ets2');
return response;
}
/**
* Get list of ATS cities, with coordinates and country
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async ats_cities()
{
var response = await this.executeRequest('/v2/map/cities/ats');
return response;
}
/**
* Get list of all cities, with coordinates, country and game
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async cities_all()
{
var response = await this.executeRequest('/v2/map/cities/all');
return response;
}
/**
* Get Map servers list, mapped on API servers
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async map_servers()
{
var response = await this.executeRequest('/v2/map/servers');
return response;
}
/**
* Search player online on map
*
* @returns {Promise<TruckyAPIResponse>}
* @memberof TruckyAPIClient
* @async
*/
async searchPlayerOnMap(query)
{
var response = await this.executeRequest('/v2/map/searchPlayer?query=' + query);
return response;
}
}
/**
* Trucky API client response prototype
* @property {object} response
* @typedef {object} TruckyAPIResponse
* @class
*/
var TruckyAPIResponse = {
response: {}
}
module.exports = TruckyAPIClient;