forked from kelnage/sg-play-bro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDo You Even Play, Bro.user.js
836 lines (800 loc) · 40.4 KB
/
Do You Even Play, Bro.user.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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
// ==UserScript==
// @name Do You Even Play, Bro?
// @namespace https://www.steamgifts.com/user/kelnage
// @version 1.6.4
// @description Display playing stats for SteamGifts users
// @author kelnage
// @match https://www.steamgifts.com/user/*/giveaways/won*
// @grant GM_xmlhttpRequest
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_deleteValue
// @connect self
// @connect api.steampowered.com
// @connect store.steampowered.com
// @connect howlongtobeat.com
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.0/jquery.min.js
// @require https://cdnjs.cloudflare.com/ajax/libs/jquery-sparklines/2.1.2/jquery.sparkline.js
// @updateURL https://raw.githubusercontent.com/kelnage/sg-play-bro/master/Do%20You%20Even%20Play%2C%20Bro.meta.js
// @downloadURL https://raw.githubusercontent.com/kelnage/sg-play-bro/master/Do%20You%20Even%20Play%2C%20Bro.user.js
// ==/UserScript==
var CURRENT_VERSION = [1,6,4];
var username = $(".featured__heading__medium").text();
var userID64 = $('[data-tooltip="Visit Steam Profile"]').attr("href").match(/https?:\/\/steamcommunity.com\/profiles\/([0-9]*)/)[1];
var WINS_URL = "https://www.steamgifts.com/user/" + username + "/giveaways/won/search";
var PLAYTIME_URL = "https://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/"; // takes a steamid and API key
var ACHIEVEMENTS_URL = "https://api.steampowered.com/ISteamUserStats/GetPlayerAchievements/v0001/"; // takes a steamid, appid and API key
var HLTB_URL = "https://howlongtobeat.com/search_main.php"; // takes a (POST) queryString and (GET) page number
var STEAM_API_KEY = GM_getValue("DYEPB_API_KEY");
var API_KEY_REGEXP = /[0-9A-Z]{32}/;
var WAIT_MILLIS = 500;
var PLAYTIME_CACHE_KEY = "DYEPB_PLAYTIME_CACHE_" + encodeURIComponent(username),
ACHIEVEMENT_CACHE_KEY = "DYEPB_ACHIEVEMENT_CACHE_" + encodeURIComponent(username),
WINS_CACHE_KEY = "DYEPB_WINS_CACHE_" + encodeURIComponent(username),
LAST_CACHE_KEY = "DYEPB_LAST_CACHED_" + encodeURIComponent(username),
USER_CACHE_VERSION_KEY = "DYEPB_USER_CACHE_VERSION_" + encodeURIComponent(username),
SUB_APPID_CACHE_KEY = "DYEPB_SUB_APPID_CACHE",
SUB_APPID_CACHE_VERSION_KEY = "DYEPB_SUB_APPID_CACHE_VERSION",
CHART_TEXT_PREFERENCE = "DYEPB_CHART_TEXT",
EXPECTED_PLAYTIME_CACHE_KEY = "DYEPB_HLTB_CACHE",
DISABLE_HLTB_KEY = "DYEPB_DISABLE_HLTB";
var $percentage = $('<div class="featured__table__row__right"></div>'),
$average_total_playtime = $('<div class="featured__table__row__right"></div>'),
$playtime_any_counts = $('<div class="featured__table__row__right" style="text-align: right"></div>'),
$playtime_5_10_counts = $('<div class="featured__table__row__right" style="text-align: right"></div>'),
$playtime_expectation_below = $('<span></span>'),
$playtime_expectation_between = $('<span></span>'),
$playtime_expectation_above = $('<span></span>'),
$playtime_expectation = $('<div class="featured__table__row__right" style="text-align: right"></div>'),
$achievement_any_counts = $('<div class="featured__table__row__right" style="text-align: right"></div>'),
$achievement_counts_chart = $('<div class="featured__table__row__right" style="text-align: right"></div>'),
$achievement_25_100_counts = $('<div class="featured__table__row__right" style="text-align: right"></div>'),
$last_updated = $('<span title="" style="color: rgba(255,255,255,0.4)"></span>'),
$disable_hltb = $('<input type="checkbox" id="disable_hltb" name="disable_hltb" value="disable_hltb" style="width: auto; margin: 0px 0.5em">'),
$hltb_left_row = $('<div class="featured__table__row"></div>'),
$progress_text = $('<span style="margin-left: 0.3em"></span>'),
$rm_key_link = $('<a style="margin-left: 0.5em;color: rgba(255,255,255,0.6)" href="#">Delete cached data</a>'),
$toolbar = $('<div id="sg_dyepb_toolbar" style="color: rgba(255,255,255,0.4)" class="nav__left-container"></div>'),
$fetch_button = $('<a class="nav__button" href="#">' + (GM_getValue(LAST_CACHE_KEY) ? 'Update Playing Info' : 'Fetch Playing Info' ) + '</a>'),
$key_button = $('<a class="nav__button" href="#">Provide API Key</a>'),
$button_container = $('<div class="nav__button-container"></div>'),
$hltb_status_container = $('<div id="dyepb_hltb_status"></div>'),
$progress_container = $('<div id="dyepb_progress" style="margin: 0.5em 0"><img src="https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/0.16.1/images/loader-large.gif" height="10px" width="10px" /></div>'),
$chart_text_switch = $('<a href="#" style="font-size: smaller">chart</a>');
var playtimeCache = {},
achievementCache = {},
winsCache = {},
subAppIdsCache = {},
expectedPlaytimeCache = {},
activeRequests = 0,
errorCount = 0,
run_status = "STOPPED"; // can be STOPPED, PLAYTIME, WON_GAMES, ACHIEVEMENTS
if(JSON.parse(GM_getValue(USER_CACHE_VERSION_KEY, "[0,0,0]")) > [1,3,2]) { // Ignore caches from versions older than 1.3.3
if(GM_getValue(PLAYTIME_CACHE_KEY)) {
playtimeCache = JSON.parse(GM_getValue(PLAYTIME_CACHE_KEY));
}
if(GM_getValue(ACHIEVEMENT_CACHE_KEY)) {
achievementCache = JSON.parse(GM_getValue(ACHIEVEMENT_CACHE_KEY));
}
if(GM_getValue(WINS_CACHE_KEY)) {
var tempWinsCache = JSON.parse(GM_getValue(WINS_CACHE_KEY));
if(Array.isArray(tempWinsCache)) { // convert old array into an object
for(var i = 0; i < tempWinsCache.length; i++) {
winsCache['a'+tempWinsCache[i].appid] = tempWinsCache[i].appid;
}
} else {
winsCache = tempWinsCache;
}
}
if(GM_getValue(EXPECTED_PLAYTIME_CACHE_KEY)) {
expectedPlaytimeCache = JSON.parse(GM_getValue(EXPECTED_PLAYTIME_CACHE_KEY));
}
}
if(GM_getValue(SUB_APPID_CACHE_KEY)) {
if(JSON.parse(GM_getValue(SUB_APPID_CACHE_VERSION_KEY, "[0,0,0]")) > [1,3,2]) { // Ignore caches from versions older than 1.3.3
subAppIdsCache = JSON.parse(GM_getValue(SUB_APPID_CACHE_KEY));
}
}
var errorFn = function(response) {
activeRequests -= 1;
errorCount += 1;
console.log("Error details: ", response);
};
var maxIndex = function(arr, val) {
var i = arr.length - 1;
while(i >= 0) {
if(arr[i] == val) { return i; }
i--;
}
return 0;
};
var summaryStats = function(arr) {
var total = arr.reduce(function(x, y) { return x + y; }, 0),
min = Math.min(...arr),
max = Math.max(...arr);
return {"min": min, "median": (min + max) / 2, "mean": total / arr.length, "max": max, "total": total} ;
};
var formatPercentage = function(x, per, precision) {
if(isNaN(x / per)) {
return "N/A";
}
return Number(x / per * 100).toPrecision(precision) + "%";
};
var formatMinutes = function(mins) {
if(isNaN(mins)) {
return "N/A";
}
if(mins < 60) {
return mins.toPrecision(2) + " minutes";
} else {
var hours = mins / 60;
if(hours < 99.5) {
return hours.toPrecision(2) + " hours";
} else if(hours < 999.5) {
return hours.toPrecision(3) + " hours";
} else if(hours < 9999.5) {
return hours.toPrecision(4) + " hours";
} else {
return hours.toPrecision(5) + " hours";
}
}
};
var formatMinutesRange = function(min, max) {
var min_time = formatMinutes(min),
max_time = formatMinutes(max);
if(min_time == max_time) {
return min_time;
} else {
if(min_time.substr(-5) == max_time.substr(-5)) {
min_time = min_time.replace(/ .*$/, "");
}
return min_time + "-" + max_time;
}
};
var parseHLTBPlaytime = function(time) {
if(time === "--") {
return null;
}
time = time.replace(/½/, ".5");
if(time.match(/[Mm]in/)) {
return parseFloat(time);
}
if(time.match(/[Hh]our/)) {
return parseFloat(time) * 60;
}
return null;
};
var enhanceRow = function($heading, minutesPlayed, achievementCounts, minExpectedPlaytime, maxExpectedPlaytime, appid, hltb_id, hltb_game) {
var $playtimeSpan = $heading.find(".dyegb_playtime"), $achievementSpan = $heading.find(".dyegb_achievement"), $expectedPlaytimeSpan = $heading.find(".dyegb_exp_playtime");
if(minutesPlayed) {
if($playtimeSpan.length > 0) {
$playtimeSpan.text(formatMinutes(minutesPlayed));
} else {
$playtimeSpan = $('<span class="dyegb_playtime giveaway__heading__thin">' + formatMinutes(minutesPlayed) + '</span>');
$heading.append($playtimeSpan);
}
}
if(hltb_id) {
if($expectedPlaytimeSpan.length > 0) {
if(minExpectedPlaytime) {
$expectedPlaytimeSpan.find(".dyegb_exp_playtime_value").text(formatMinutesRange(minExpectedPlaytime, maxExpectedPlaytime));
} else {
$expectedPlaytimeSpan.remove();
}
} else if(minExpectedPlaytime) {
if($playtimeSpan.length > 0) {
$expectedPlaytimeSpan = $('<span class="dyegb_exp_playtime giveaway__heading__thin" title="HLTB stats for ' +
hltb_game + '"><a target="_blank" href="https://howlongtobeat.com/game.php?id=' +
hltb_id + '">(<span class="dyegb_exp_playtime_value">' +
formatMinutesRange(minExpectedPlaytime, maxExpectedPlaytime) + '</span>)</a></span>');
$playtimeSpan.append($expectedPlaytimeSpan);
} else {
$expectedPlaytimeSpan = $('<span class="dyegb_exp_playtime giveaway__heading__thin" title="HLTB stats for ' +
hltb_game + '">HLTB: <a target="_blank" href="https://howlongtobeat.com/game.php?id=' +
hltb_id + '"><span class="dyegb_exp_playtime_value">' +
formatMinutesRange(minExpectedPlaytime, maxExpectedPlaytime) + '</span></a></span>');
$heading.append($expectedPlaytimeSpan);
}
}
}
if(GM_getValue(DISABLE_HLTB_KEY, false)) {
$expectedPlaytimeSpan.css("display", "none");
} else {
$expectedPlaytimeSpan.css("display", "inline");
}
if(achievementCounts && achievementCounts.total > 0) {
if($achievementSpan.length === 0) {
$achievementSpan = $('<a href="https://steamcommunity.com/profiles/'+userID64+'/stats/'+appid+'/?tab=achievements" target="_new" class="dyegb_achievement giveaway__heading__thin">' +
formatPercentage(achievementCounts.achieved, achievementCounts.total, 3) + '</a>');
$heading.append($achievementSpan);
}
if(achievementCounts.achieved === 0) {
$achievementSpan.text("0%");
} else {
$achievementSpan.attr('style', "font-weight: bold");
$achievementSpan.text(formatPercentage(achievementCounts.achieved, achievementCounts.total, 3));
$achievementSpan.attr('title', achievementCounts.achieved + '/' + achievementCounts.total + ' achievements');
if(achievementCounts.achieved == achievementCounts.total) {
$achievementSpan.attr('style', "font-weight: bold; color: rgb(91, 192, 222)");
} else {
$achievementSpan.addClass("giveaway__column--positive");
}
}
}
};
var enhanceWonGames = function() {
var $rows = $(".giveaway__row-inner-wrap");
$rows.each(function() {
var $this = $(this), $heading = $this.find(".giveaway__heading"),
$ga_icon = $this.find("a.giveaway__icon:has(i.fa-steam)");
if($ga_icon && $ga_icon.attr("href")) {
var id = $ga_icon.attr("href").match(/https?:\/\/store.steampowered.com\/([^\/]*)\/([0-9]*)/);
if(id[1] == "sub" || id[1] == "subs") {
var totalMinutes = 0, totalAchievements = {achieved: 0, total: 0}, bestAchievementAppid = null, topCompletion = null,
minExpectedPlaytime = 0, maxExpectedPlaytime = 0, highestExpectedPlaytime = null, bestPlaytimeId = null, bestPlaytimeGame = null;
if(subAppIdsCache['s'+id[2]]) {
var appids = subAppIdsCache['s'+id[2]];
for(var i = 0; i < appids.length; i++) {
if(playtimeCache['a'+appids[i]]) {
totalMinutes += playtimeCache['a'+appids[i]];
}
if(achievementCache['a'+appids[i]]) {
totalAchievements.achieved += achievementCache['a'+appids[i]].achieved;
totalAchievements.total += achievementCache['a'+appids[i]].total;
if(topCompletion === null || achievementCache['a'+appids[i]].achieved / achievementCache['a'+appids[i]].total > topCompletion) {
topCompletion = achievementCache['a'+appids[i]].achieved / achievementCache['a'+appids[i]].total;
bestAchievementAppid = appids[i];
}
}
if(expectedPlaytimeCache['a'+appids[i]]) {
var substats = summaryStats(expectedPlaytimeCache['a'+appids[i]].times);
minExpectedPlaytime += substats.min;
maxExpectedPlaytime += substats.max;
if(highestExpectedPlaytime === null || maxExpectedPlaytime > highestExpectedPlaytime) {
highestExpectedPlaytime = maxExpectedPlaytime;
bestPlaytimeId = expectedPlaytimeCache['a'+appids[i]].hltb_id;
bestPlaytimeGame = expectedPlaytimeCache['a'+appids[i]].hltb_game;
}
}
}
}
enhanceRow($heading, totalMinutes, totalAchievements, minExpectedPlaytime, maxExpectedPlaytime, bestAchievementAppid, bestPlaytimeId, bestPlaytimeGame);
}
if(id[1] == "app" || id[1] == "apps") {
if(expectedPlaytimeCache['a'+id[2]]) {
var stats = {};
if(expectedPlaytimeCache['a'+id[2]].times.length > 0) {
stats = summaryStats(expectedPlaytimeCache['a'+id[2]].times);
}
enhanceRow($heading, playtimeCache['a'+id[2]], achievementCache['a'+id[2]], stats.min, stats.max, id[2], expectedPlaytimeCache['a'+id[2]].hltb_id, expectedPlaytimeCache['a'+id[2]].hltb_game);
} else {
enhanceRow($heading, playtimeCache['a'+id[2]], achievementCache['a'+id[2]], undefined, undefined, id[2], undefined, undefined);
}
}
}
});
};
var updateTableStats = function() {
var achievement_percentage_sum = 0, achievement_game_count = 0, achieved_game_count = 0,
achieved_game_count_25 = 0, achieved_game_count_100 = 0, achieved_game_cumulative = [],
playtime_total = 0, playtime_game_count = 0, playtime_game_count_5h = 0, playtime_game_count_10h = 0,
win_count = 0, achievement_playtime_total = 0, achievement_playtime_count = 0,
expected_less_than_min = 0, expected_total_less_distance = 0, expected_between_min_max = 0, expected_greater_than_max = 0, expected_total_greater_distance = 0,
expected_playtime_count = 0, expected_below = "", expected_between = "", expected_above = "";
var i = 0;
while(i < 101) {
achieved_game_cumulative[i] = 0;
i++;
}
$.each(winsCache, function(aid, details) {
var achievement_counts = achievementCache[aid];
if(achievement_counts && achievement_counts.total > 0) {
achievement_game_count += 1;
if(achievement_counts.achieved > 0) {
var ratio = achievement_counts.achieved / achievement_counts.total;
achievement_percentage_sum += ratio;
achieved_game_count += 1;
if(achievement_counts.achieved >= (achievement_counts.total / 4)) {
achieved_game_count_25 += 1;
}
if(achievement_counts.achieved === achievement_counts.total) {
achieved_game_count_100 += 1;
}
}
var j = 0, percentage = Math.round(achievement_counts.achieved / achievement_counts.total * 100);
while(j <= percentage) {
achieved_game_cumulative[j] += 1;
j++;
}
}
if(playtimeCache[aid] !== undefined) {
win_count += 1;
playtime_total += playtimeCache[aid];
if(playtimeCache[aid] > 0) {
playtime_game_count += 1;
}
if(playtimeCache[aid] >= 300) {
playtime_game_count_5h += 1;
}
if(playtimeCache[aid] >= 600) {
playtime_game_count_10h += 1;
}
if(playtimeCache[aid] > 0 && expectedPlaytimeCache[aid] && expectedPlaytimeCache[aid].times.length > 0) {
expected_playtime_count += 1;
var stats = summaryStats(expectedPlaytimeCache[aid].times);
if(playtimeCache[aid] < stats.min) {
expected_less_than_min += 1;
expected_total_less_distance += stats.min - playtimeCache[aid];
expected_below += details.name + "\n";
} else if(playtimeCache[aid] > stats.max) {
expected_greater_than_max += 1;
expected_total_greater_distance += playtimeCache[aid] - stats.max;
expected_above += details.name + "\n";
} else {
expected_between_min_max += 1;
expected_between += details.name + "\n";
}
}
}
if(achievement_counts && achievement_counts.total > 0 && playtimeCache[aid]) {
achievement_playtime_total += playtimeCache[aid];
achievement_playtime_count += achievement_counts.achieved;
}
});
if(achieved_game_count > 0) {
$percentage.text(formatPercentage(achievement_percentage_sum, achieved_game_count, 3));
} else {
$percentage.text("N/A");
}
if(expected_playtime_count > 0) {
$playtime_expectation.empty();
$playtime_expectation.append($playtime_expectation_below);
$playtime_expectation.append($playtime_expectation_between);
$playtime_expectation.append($playtime_expectation_above);
$playtime_expectation_below.text(expected_less_than_min + ' below (' + formatMinutes(expected_total_less_distance / expected_less_than_min) + ' avg.), ');
$playtime_expectation_below.attr("title", expected_below.replace(/\n$/, ""));
$playtime_expectation_between.text(expected_between_min_max + ' between, ');
$playtime_expectation_between.attr("title", expected_between.replace(/\n$/, ""));
$playtime_expectation_above.text(expected_greater_than_max + ' above (' + formatMinutes(expected_total_greater_distance / expected_greater_than_max) + ' avg.)');
$playtime_expectation_above.attr("title", expected_above.replace(/\n$/, ""));
} else {
$playtime_expectation.empty();
$playtime_expectation.text("N/A");
}
if(playtime_game_count !== win_count) {
$average_total_playtime.text(formatMinutes(playtime_total / win_count) + ' per win, ' +
formatMinutes(playtime_total / playtime_game_count) + ' per played win, ' +
formatMinutes(playtime_total) + ' total');
} else {
$average_total_playtime.text(formatMinutes(playtime_total / win_count) + ' in all wins, ' +
formatMinutes(playtime_total) + ' total');
}
$playtime_any_counts.text(formatPercentage(playtime_game_count, win_count, 3) +
' (' + playtime_game_count + '/' + win_count + ')');
$playtime_5_10_counts.text('≥5 hours: ' + formatPercentage(playtime_game_count_5h, win_count, 3) +
' (' + playtime_game_count_5h + '/' + win_count +
'), ≥10 hours: ' + formatPercentage(playtime_game_count_10h, win_count, 3) +
' (' + playtime_game_count_10h + '/' + win_count + ')');
$achievement_any_counts.text(formatPercentage(achieved_game_count, achievement_game_count, 3) +
' (' + achieved_game_count + '/' + achievement_game_count + ')');
$achievement_counts_chart.sparkline(
achieved_game_cumulative,
{'type': 'line', 'lineColor': 'rgba(255, 255, 255, 0.6)', 'fillColor': 'rgba(255, 255, 255, 0.4)', 'chartRangeMin': 0, 'height': 18,
'spotColor': 'rgb(153,204,102)', 'minSpotColor': 'rgb(153,204,102)', 'maxSpotColor': 'rgb(153,204,102)', 'tooltipOffsetX': -60, 'tooltipOffsetY': 25,
'tooltipFormatter': function(sparkline, options, fields) {
return maxIndex(achieved_game_cumulative, fields.y) + '% complete: ' + formatPercentage(fields.y, achievement_game_count, 3) + ' (' + fields.y + '/' + achievement_game_count + ')';
}});
$achievement_counts_chart.css(
'background',
'linear-gradient(to right, transparent calc(25%), rgba(255,0,0,0.5) calc(25% + 2px), transparent calc(25% + 4px), transparent calc(50% - 2px), rgba(255,0,0,0.5) calc(50%), transparent calc(50% + 2px), transparent calc(75% - 3px), rgba(255,0,0,0.5) calc(75% - 1px), transparent calc(75% + 1px))');
$achievement_25_100_counts.text(
'≥25% complete: ' + formatPercentage(achieved_game_count_25, achievement_game_count, 3) +
' (' + achieved_game_count_25 + '/' + achievement_game_count +
'), completed: ' + formatPercentage(achieved_game_count_100, achievement_game_count, 3) +
' (' + achieved_game_count_100 + '/' + achievement_game_count + ')');
if(GM_getValue(DISABLE_HLTB_KEY, false)) {
$hltb_left_row.css("display", "none");
} else {
$hltb_left_row.css("display", "flex");
}
};
var updateDisplayedCacheDate = function(t) {
if(t) {
$last_updated.text('Last retrieved: ' + t.toLocaleDateString() + (errorCount > 0 ? ", with " + errorCount + " API query errors" : ""));
$last_updated.attr('title', t.toLocaleString());
}
};
var displayButtons = function() {
if(!API_KEY_REGEXP.test(STEAM_API_KEY)) {
$button_container.show();
$hltb_status_container.hide();
$progress_container.hide();
$key_button.show();
$fetch_button.hide();
$last_updated.empty();
$last_updated.attr("title", "");
$last_updated.show();
$last_updated.append('<a style="color: rgba(255,255,255,0.6)" target="_blank" href="https://steamcommunity.com/dev/apikey">Click here to obtain a Steam API key</a>');
$rm_key_link.hide();
} else if(run_status == "STOPPED") {
$button_container.show();
$hltb_status_container.show();
$progress_container.hide();
$fetch_button.show();
$key_button.hide();
$last_updated.empty(); // will be updated by updateDisplayedCacheDate
$last_updated.show();
if(GM_getValue(LAST_CACHE_KEY)) {
$fetch_button.text("Update Playing Info");
updateDisplayedCacheDate(new Date(GM_getValue(LAST_CACHE_KEY)));
} else {
$fetch_button.text("Fetch Playing Info");
}
$rm_key_link.show();
} else {
$button_container.hide();
$hltb_status_container.hide();
$progress_container.show();
if(run_status == "PLAYTIMES") {
$progress_text.text("Retrieving " + username + "'s logged playing times");
} else if(run_status == "WON_GAMES") {
$progress_text.text("Retrieving " + username + "'s won games");
} else if(run_status == "ACHIEVEMENTS") {
$progress_text.text("Retrieving " + username + "'s achievement " + (GM_getValue(DISABLE_HLTB_KEY, false) ? '' : 'and HLTB ') + "progress (" + activeRequests + " games left to check)");
}
$last_updated.hide();
$rm_key_link.hide();
}
};
var updatePage = function(update_time) {
enhanceWonGames();
updateTableStats();
displayButtons();
updateDisplayedCacheDate(update_time);
};
var extractSubGames = function(sub, page) {
subAppIdsCache['s'+sub] = [];
$(".tab_item", page).each(function(i, e) {
var $this = $(e),
appId = $this.attr("data-ds-appid"),
name = $this.find(".tab_item_name").text(),
$link = $this.find(".tab_item_overlay");
if($link.attr("href") && (!winsCache['a'+appId] || !winsCache['a'+appId].appid)) {
var type = $link.attr("href").match(/https?:\/\/store.steampowered.com\/([^\/]*)\/[0-9]*/);
winsCache['a'+appId] = {'appid': appId, 'name': name};
}
subAppIdsCache['s'+sub].push(appId);
});
};
var extractWon = function(page) {
var extractCount = 0;
$(".giveaway__row-inner-wrap", page)
.filter(function(i) {
return $(this).find("div.giveaway__column--positive").length == 1;
})
.each(function(i, e) {
var $ga_name = $(e).find("a.giveaway__heading__name"),
$ga_icon = $(e).find("a.giveaway__icon:has(i.fa-steam)");
if($ga_icon.length === 1 && $ga_icon.attr("href")) {
var url = $ga_icon.attr("href"),
id = url.match(/https?:\/\/store.steampowered.com\/([^\/]*)\/([0-9]*)/),
name = $ga_name.text();
if(name.endsWith("...") && name.length > 3) {
name = name.substr(0, name.length - 3);
}
if((id[1] == "sub" || id[1] == "subs") && (!subAppIdsCache['s'+id[2]] || subAppIdsCache['s'+id[2]].length === 0|| !subAppIdsCache['s'+id[2]][0].appid)) { // only fetch appids for uncached-subs - do subs ever change? Probably...
activeRequests += 1;
GM_xmlhttpRequest({
"method": "GET",
"url": url,
"onload": function(response) {
if(response.finalUrl === url) { // if not, probably got redirected to Steam homepage
extractSubGames(id[2], response.responseText);
} else {
console.log("Could not get details for sub " + id[2]);
}
activeRequests -= 1;
},
"onabort": errorFn,
"onerror": errorFn,
"ontimeout": errorFn
});
extractCount += 1;
} else if((id[1] == "app" || id[1] == "apps") && (!winsCache['a'+id[2]] || !winsCache['a'+id[2]].appid)) {
winsCache['a'+id[2]] = {'appid': id[2], 'name': name};
extractCount += 1;
}
}
});
return extractCount;
};
var fetchWon = function(page, callback) {
activeRequests += 1;
GM_xmlhttpRequest({
"method": "GET",
"url": WINS_URL + "?page=" + page,
"onload": function(response) {
var count = extractWon(response.responseText);
// stop fetching pages if no new wins found on current page
if($("div.pagination__navigation > a > span:contains('Next')", response.responseText).length === 1 && count > 0) {
setTimeout(function() {
fetchWon(page + 1, callback);
}, WAIT_MILLIS);
} else {
callback();
}
activeRequests -= 1;
},
"onabort": errorFn,
"onerror": errorFn,
"ontimeout": errorFn
});
};
var fetchGamePlaytimes = function(steamID64, callback) {
activeRequests += 1;
GM_xmlhttpRequest({
"method": "GET",
"url": PLAYTIME_URL + "?steamid=" + steamID64 + "&key=" + STEAM_API_KEY,
"onload": function(response) {
var data;
try {
data = JSON.parse(response.responseText);
} catch(err) {
errorFn({"status": response.status, "responseText": response.responseText});
}
if(data) {
var games = data.response.games;
if(games) {
for(var i = 0; i < games.length; i++) {
playtimeCache["a"+games[i].appid] = games[i].playtime_forever;
}
}
activeRequests -= 1;
callback();
}
},
"onabort": errorFn,
"onerror": errorFn,
"ontimeout": errorFn
});
};
var fetchAchievementStatsFn = function(appid, steamID64) {
return function() {
GM_xmlhttpRequest({
"method": "GET",
"url": ACHIEVEMENTS_URL + "?appid=" + appid + "&steamid=" + steamID64 + "&key=" + STEAM_API_KEY,
"onload": function(response) {
var data;
try {
data = JSON.parse(response.responseText);
} catch(err) {
errorFn({"status": response.status, "responseText": response.responseText});
}
if(data) {
achievements = data.playerstats.achievements;
if(achievements) {
var achieved = achievements.filter(function(achievement) { return achievement.achieved == 1; }).length;
var total = achievements.length;
achievementCache["a"+appid] = {"achieved": achieved, "total": total};
} else {
achievementCache["a"+appid] = {"achieved": 0, "total": 0};
}
activeRequests -= 1;
}
},
"onabort": errorFn,
"onerror": errorFn,
"ontimeout": errorFn
});
};
};
var fetchExpectedPlaytimes = function(appid, game_name) {
return function() {
if(game_name.match(/[^\w\s-_:]/)) {
game_name = game_name.replace(/[^-\w\s_:]/g, "");
}
GM_xmlhttpRequest({
"method": "POST",
"url": HLTB_URL + '?page=1',
"headers": {
'Content-Type': 'application/x-www-form-urlencoded',
'accept': '*/*'
},
"data": 'queryString='+encodeURIComponent(game_name)+'&t=games&sorthead=popular&sortd=Normal&20Order&plat=PC&length_type=main&length_min=&length_max=&detail=0',
"onload": function(response) {
var data = {"cache_date": Date.now()};
try {
var $result = $('<ul>' + response.responseText + '</ul>').find('li:first');
if($result.text().startsWith("No results for")) {
data.search_term = $result.find("strong:first").text();
data.times = [];
console.log("Could not find details for:", data.search_term);
} else {
var times = $result.find(".search_list_details_block .search_list_tidbit").filter(":odd").map(function() { return $(this).text(); }).get(),
$game_link = $result.find("h3.shadow_text a");
data.search_term = game_name;
data.hltb_game = $game_link.text();
data.hltb_id = $game_link.attr('href').replace(/^game\.php\?id=/g, '');
data.times = times.map(parseHLTBPlaytime).filter(function(x) { return x !== null; });
}
expectedPlaytimeCache['a'+appid] = data;
activeRequests -= 1;
} catch(err) {
errorFn({"status": response.status, "responseText": response.responseText, "error": err.message});
}
},
"onabort": errorFn,
"onerror": errorFn,
"ontimeout": errorFn
});
};
};
var cacheJSONValue = function(key, value) {
GM_setValue(key, JSON.stringify(value));
var updateTime = new Date();
GM_setValue(LAST_CACHE_KEY, updateTime.getTime());
updatePage(updateTime);
};
(function() {
'use strict';
var $featured_table = $(".featured__table"),
$featured_table_col1 = $featured_table.children(":first-child"),
$featured_table_col2 = $featured_table.children(":last-child");
var $left_row_1 = $('<div class="featured__table__row"></div>'),
$left_row_2 = $('<div class="featured__table__row"></div>'),
$left_row_3 = $('<div class="featured__table__row"></div>'),
$right_row_1 = $('<div class="featured__table__row"></div>'),
$right_row_2 = $('<div class="featured__table__row"></div>'),
$right_row_3 = $('<div class="featured__table__row"></div>');
$toolbar.append($button_container);
$button_container.append($key_button);
$button_container.append($fetch_button);
$toolbar.append($hltb_status_container);
$hltb_status_container.append($disable_hltb);
$hltb_status_container.append('<label style="margin-right: 0.5em" for="disable_hltb">Disable HLTB enrichment?</label>');
$toolbar.append($progress_container);
$progress_container.append($progress_text);
$toolbar.append($last_updated);
$toolbar.append($rm_key_link);
$left_row_1.append('<div class="featured__table__row__left">Average and Total Playtime</div>');
$left_row_1.append($average_total_playtime);
$left_row_2.append('<div class="featured__table__row__left">Games with any Playtime</div>');
$left_row_2.append($playtime_any_counts);
$left_row_3.append('<div class="featured__table__row__left">Games with Playtime...</div>');
$left_row_3.append($playtime_5_10_counts);
$hltb_left_row.append('<div class="featured__table__row__left">Compared to HLTB Estimates</div>');
$hltb_left_row.append($playtime_expectation);
$right_row_1.append('<div class="featured__table__row__left">Avg. Achievement Percentage</div>');
$right_row_1.append($percentage);
$right_row_2.append('<div class="featured__table__row__left">Games with ≥1 Achievement</div>');
$right_row_2.append($achievement_any_counts);
var $achievement_games = $('<div class="featured__table__row__left">Achievement Rates </div>');
$achievement_games.append($chart_text_switch);
$right_row_3.append($achievement_games);
$right_row_3.append($achievement_25_100_counts);
$right_row_3.append($achievement_counts_chart);
if(GM_getValue(CHART_TEXT_PREFERENCE, "text") == "text") {
$achievement_counts_chart.hide();
$chart_text_switch.text('chart');
} else {
$achievement_25_100_counts.hide();
$chart_text_switch.text('text');
}
if(GM_getValue(DISABLE_HLTB_KEY, false)) {
$disable_hltb.prop("checked", true);
} else {
$disable_hltb.prop("checked", false);
}
$featured_table_col1.append($left_row_1).append($left_row_2).append($left_row_3).append($hltb_left_row);
$featured_table_col2.append($right_row_1).append($right_row_2).append($right_row_3);
$featured_table.after($toolbar);
updatePage(GM_getValue(LAST_CACHE_KEY) ? new Date(GM_getValue(LAST_CACHE_KEY)) : null);
$disable_hltb.change(function(e) {
GM_setValue(DISABLE_HLTB_KEY, this.checked);
updatePage();
});
$chart_text_switch.click(function(e) {
e.preventDefault();
if(GM_getValue(CHART_TEXT_PREFERENCE, "text") == "text") {
// switch to chart
$achievement_counts_chart.show();
$achievement_25_100_counts.hide();
$.sparkline_display_visible();
GM_setValue(CHART_TEXT_PREFERENCE, "chart");
$chart_text_switch.text("text");
} else {
// switch to text
$achievement_counts_chart.hide();
$achievement_25_100_counts.show();
GM_setValue(CHART_TEXT_PREFERENCE, "text");
$chart_text_switch.text("chart");
}
updateTableStats();
});
$key_button.click(function(e) {
e.preventDefault();
STEAM_API_KEY = prompt('Please provide your Steam API key');
while(STEAM_API_KEY !== "" && STEAM_API_KEY !== null && !API_KEY_REGEXP.test(STEAM_API_KEY)) {
STEAM_API_KEY = prompt('Please provide your valid Steam API key');
}
if(API_KEY_REGEXP.test(STEAM_API_KEY)) {
GM_setValue("DYEPB_API_KEY", STEAM_API_KEY);
displayButtons();
}
});
$rm_key_link.click(function(e) {
e.preventDefault();
GM_deleteValue("DYEPB_API_KEY");
GM_deleteValue(PLAYTIME_CACHE_KEY);
GM_deleteValue(ACHIEVEMENT_CACHE_KEY);
GM_deleteValue(WINS_CACHE_KEY);
GM_deleteValue(LAST_CACHE_KEY);
GM_deleteValue(USER_CACHE_VERSION_KEY);
GM_deleteValue(SUB_APPID_CACHE_KEY);
GM_deleteValue(SUB_APPID_CACHE_VERSION_KEY);
GM_deleteValue(EXPECTED_PLAYTIME_CACHE_KEY);
STEAM_API_KEY = "";
playtimeCache = {};
achievementCache = {};
winsCache = {};
subAppIdsCache = {};
expectedPlaytimeCache = {};
displayButtons();
});
$fetch_button.click(function(e) {
e.preventDefault();
activeRequests = 0;
errorCount = 0;
run_status = "PLAYTIMES";
displayButtons();
fetchGamePlaytimes(userID64, function() {
run_status = "WON_GAMES";
cacheJSONValue(PLAYTIME_CACHE_KEY, playtimeCache);
fetchWon(1, function() {
var intervalId = setInterval(function() {
if(activeRequests === 0) {
clearInterval(intervalId);
run_status = "ACHIEVEMENTS";
cacheJSONValue(WINS_CACHE_KEY, winsCache);
cacheJSONValue(SUB_APPID_CACHE_KEY, subAppIdsCache);
GM_setValue(SUB_APPID_CACHE_VERSION_KEY, JSON.stringify(CURRENT_VERSION));
var i = 0;
$.each(winsCache, function(id, details) {
activeRequests += 1;
if(details.name) {
setTimeout(fetchAchievementStatsFn(details.appid, userID64), i * 50);
// only update individual games expected playtime cache if tha data is missing or >30 days old
if(!GM_getValue(DISABLE_HLTB_KEY, false) && (!expectedPlaytimeCache['a'+details.appid] || expectedPlaytimeCache['a'+details.appid].cache_date < Date.now() - 2592000000)) {
activeRequests += 1;
setTimeout(fetchExpectedPlaytimes(details.appid, details.name), i * 1000); // rate limit data requests
}
} else if(details.appid) {
setTimeout(fetchAchievementStatsFn(details.appid, userID64), i * 50);
} else { // if details is not an object with an attribute name or an appid, it's probably an appid itself
setTimeout(fetchAchievementStatsFn(details, userID64), i * 50);
}
// increment delay to try to prevent overloading of Steam API
i += 1;
});
intervalId = setInterval(function() {
if(activeRequests === 0) {
clearInterval(intervalId);
run_status = "STOPPED";
cacheJSONValue(ACHIEVEMENT_CACHE_KEY, achievementCache);
cacheJSONValue(EXPECTED_PLAYTIME_CACHE_KEY, expectedPlaytimeCache);
GM_setValue(USER_CACHE_VERSION_KEY, JSON.stringify(CURRENT_VERSION));
console.log("Errors during API queries:", errorCount);
} else {
displayButtons();
console.log("Active achievement requests:", activeRequests);
}
}, 500);
} else {
displayButtons();
console.log("Active game requests:", activeRequests);
}
}, 250);
});
});
});
})();