-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrace-info-footer.js
968 lines (905 loc) · 36.6 KB
/
race-info-footer.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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
// ==UserScript==
// @name Nitro Type - Race Info Footer
// @namespace https://github.com/rickstaa/nitro-type-racing-stats-footer
// @version 1.1.2
// @description Adds an information footer to the race page displaying user stats, season information, and daily challenges.
// @author Toonidy, Ray Adams/Nate Dogg, Follycakes, Rick Staa
// @match *://*.nitrotype.com/race
// @match *://*.nitrotype.com/race/*
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACwAAAAsCAIAAACR5s1WAAABhGlDQ1BJQ0MgcHJvZmlsZQAAKJF9kT1Iw0AcxV9bpVIqilYQcchQnSyIijhqFYpQIdQKrTqYXPoFTRqSFBdHwbXg4Mdi1cHFWVcHV0EQ/ABxc3NSdJES/5cUWsR4cNyPd/ced+8Af73MVLNjHFA1y0gl4kImuyoEXxFCL/ogYEBipj4nikl4jq97+Ph6F+NZ3uf+HN1KzmSATyCeZbphEW8QT29aOud94ggrSgrxOfGYQRckfuS67PIb54LDfp4ZMdKpeeIIsVBoY7mNWdFQiaeIo4qqUb4/47LCeYuzWq6y5j35C8M5bWWZ6zSHkcAiliBSRzKqKKEMCzFaNVJMpGg/7uEfcvwiuWRylcDIsYAKVEiOH/wPfndr5icn3KRwHOh8se2PESC4CzRqtv19bNuNEyDwDFxpLX+lDsx8kl5radEjoGcbuLhuafIecLkDDD7pkiE5UoCmP58H3s/om7JA/y0QWnN7a+7j9AFIU1fJG+DgEBgtUPa6x7u72nv790yzvx9fO3KfqkKlgwAAAAlwSFlzAAAuIwAALiMBeKU/dgAAAAd0SU1FB+ULCBEQCo/KC2cAAAAZdEVYdENvbW1lbnQAQ3JlYXRlZCB3aXRoIEdJTVBXgQ4XAAAABmJLR0QAcgACAAKX272SAAAHfklEQVRYw82YCVATVxjHX2mr1tLSghTUaqFY7CFqK9RjqLWdsdfYqaP2omU6bUdbZ7D1KkjAOkWtNIBQETk8KAiCEGQUuSyHIQQJLLnI5lpCyLUhJNyIgJX04eIzkihEpHbnP5D98vJ9v/3e977dfcAcH//QBah/Ox0cHopuQzwsAsQxCpHx/IL706k5ninuLyBNCuKi12viJasmKZ7P8klBhE2fy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E+2ErqtSoJUS6vzjx8P8/KZqu+huCl+1qhHDyI6Bkloi9ocfpnbPaox2OTrS/PyKMzMJHE8oaITPVacPHZryjTMkIcaXSZtlKiOhac+pFCtbe7jsmri7lOFUQRAKLaFqU7S04gK8NCvrSFDQf7SF+L/YTH3o+hfertB4W63rtAAAAABJRU5ErkJggg==
// @grant none
// @require https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js#sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==
// @require https://cdnjs.cloudflare.com/ajax/libs/dexie/3.2.1/dexie.min.js#sha512-ybuxSW2YL5rQG/JjACOUKLiosgV80VUfJWs4dOpmSWZEGwdfdsy2ldvDSQ806dDXGmg9j/csNycIbqsrcqW6tQ==
// @require https://greasyfork.org/scripts/443718-nitro-type-userscript-utils/code/Nitro%20Type%20Userscript%20Utils.js?version=1042360
// @license MIT
// ==/UserScript==
/* global Dexie moment NTGLOBALS createLogger findReact */
const logging = createLogger("Nitro Type Race Info Footer");
// == Config storage ==
const db = new Dexie("NTRacingStats");
db.version(1).stores({
backupStatData: "userID",
});
db.open().catch((e) => {
logging.error("Init")(
"Failed to open up the race info footer cache database",
e
);
});
////////////
// Init //
////////////
// Retrieve race track.
const RACE_CONTAINER = document.getElementById("raceContainer");
const RACE_OBJ = RACE_CONTAINER ? findReact(RACE_CONTAINER) : null;
const SERVER = RACE_OBJ?.server;
const CURRENT_USER = RACE_OBJ?.props.user;
if (!RACE_CONTAINER || !RACE_OBJ) {
logging.error("Init")("Could not find the race track");
return;
}
if (!CURRENT_USER?.loggedIn) {
logging.error("Init")("Not available for Guest Racing");
return;
}
//////////////////
// Helpers //
//////////////////
/**
* Merge daily challenge data with user progress.
* @param {Array} progress - Array of user progress objects.
* @returns {Array} - Array of challenge objects with merged user progress.
*/
const mergeDailyChallengeData = (progress) => {
const { CHALLENGES, CHALLENGE_TYPES } = NTGLOBALS;
const now = Math.floor(Date.now() / 1000);
return CHALLENGES.filter((c) => c.expiration > now)
.slice(0, 3)
.map((c, _) => {
const userProgress = progress.find(
(p) => p.challengeID === c.challengeID
);
const [title, field] = CHALLENGE_TYPES[c.type];
return {
...c,
title: title
.replace(/\$\{goal\}/, c.goal)
.replace(/\$\{field\}/, `${field}${c.goal !== 1 ? "s" : ""}`),
field,
goal: c.goal,
progress: userProgress?.progress || 0,
};
});
};
/**
* Retrieve Race Info Footer from various sources.
* @returns {Promise} - Promise that resolves to an object containing the user stats.
*/
const getStats = async () => {
let backupUserStats = null;
try {
backupUserStats = await db.backupStatData.get(CURRENT_USER.userID);
} catch (ex) {
logging.warn("Update")("Unable to get backup stats", ex);
}
try {
const persistStorageStats = JSON.parse(
JSON.parse(localStorage.getItem("persist:nt")).user
);
const user =
!backupUserStats ||
typeof backupUserStats.lastConsecRace !== "number" ||
persistStorageStats.lastConsecRace >= backupUserStats.lastConsecRace
? persistStorageStats
: backupUserStats;
const dailyChallenges = mergeDailyChallengeData(user.challenges);
return { user, dailyChallenges };
} catch (ex) {
logging.error("Update")("Unable to get stats", ex);
return Promise.reject(new Error("Unable to get stats"));
}
};
/**
* Retrieve team stats.
* @returns {Promise} - Promise that resolves to an object containing the team stats.
*/
const getTeamStats = async () => {
const tag = CURRENT_USER?.tag ?? null;
if (!tag) {
console.log("User is not in a team.");
return null;
}
// Retrieve team stats.
try {
const authToken = localStorage.getItem("player_token");
const response = await fetch(`/api/v2/teams/${tag}`, {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
const { results } = await response.json();
const { leaderboard, motd, info, stats, members, season } = results;
const member = members?.find((u) => u.userID === CURRENT_USER.userID);
const seasonStats = season?.find((u) => u.userID === CURRENT_USER.userID);
return { leaderboard, motd, info, stats, member, season: seasonStats };
} catch (error) {
console.error("Error getting team stats:", error);
return null;
}
};
/**
* Retrieve Summary Stats.
* @returns {Promise} - Promise that resolves to an object containing the summary stats.
*/
const getSummaryStats = async () => {
const authToken = localStorage.getItem("player_token");
try {
const response = await fetch("/api/v2/stats/summary", {
headers: {
Authorization: `Bearer ${authToken}`,
},
});
const { results } = await response.json();
const seasonBoard = results.racingStats?.find((b) => b.board === "season");
const dailyBoard = results.racingStats?.find((b) => b.board === "daily");
return { seasonBoard, dailyBoard };
} catch (error) {
console.error("Error getting summary stats:", error);
return null;
}
};
//////////////////
// Components //
//////////////////
// Inject components styles.
const style = document.createElement("style");
style.innerHTML = `
#raceContainer {
margin-bottom: 0;
}
.nt-stats-root {
background-color: #222;
}
.nt-stats-body {
display: flex;
justify-content: space-between;
padding: 8px;
}
.nt-stats-left-section, .nt-stats-right-section {
display: flex;
flex-direction: column;
row-gap: 8px;
}
.nt-stats-toolbar {
display: flex;
justify-content: space-between;
align-items: center;
padding-left: 8px;
color: rgba(255, 255, 255, 0.8);
background-color: #03111a;
font-size: 12px;
}
.nt-stats-toolbar-status {
display: flex;
}
.nt-stats-toolbar-status .nt-stats-toolbar-status-item {
padding: 0 8px;
background-color: #0a2c42;
}
.nt-stats-toolbar-status .nt-stats-toolbar-status-item-alt {
padding: 0 8px;
background-color: #22465c;
}
.nt-stats-daily-challenges {
width: 350px;
}
.nt-stats-daily-challenges .daily-challenge-progress--badge {
z-index: 0;
}
.nt-stats-season-progress {
padding: 8px;
margin: 0 auto;
border-radius: 8px;
background-color: #1b83d0;
box-shadow: 0 28px 28px 0 rgb(2 2 2 / 5%), 0 17px 17px 0 rgb(2 2 2 / 20%), 0 8px 8px 0 rgb(2 2 2 / 15%);
}
.nt-stats-season-progress .season-progress-widget {
width: 350px;
}
.nt-stats-season-progress .season-progress-widget--level-progress-bar {
transition: width 0.3s ease;
}
.nt-stats-info {
text-align: center;
color: #eee;
font-size: 14px;
}
.nt-stats-metric-row {
margin-bottom: 4px;
}
.nt-stats-metric-value, .nt-stats-metric-suffix {
font-weight: 600;
}
.nt-stats-metric-suffix {
color: #aaa;
}
.nt-stats-right-section {
flex-grow: 1;
margin-left: 15px;
}`;
document.head.appendChild(style);
/**
* Creates a small toolbar widget component that displays information about the user's userscript, NT Cash, and Mystery Box status.
* @param {Object} user - The current user object.
* @returns {Object} An object containing the root element of the toolbar and an `updateStats` method that can be used to update the toolbar stats.
*/
const ToolbarWidget = ((user) => {
const root = document.createElement("div");
root.classList.add("nt-stats-toolbar");
root.innerHTML = `
<div>
NOTE: Team Stats and Season Stats are cached.
</div>
<div class="nt-stats-toolbar-status">
<div class="nt-stats-toolbar-status-item">
<span class="nt-cash-status as-nitro-cash--prefix">N/A</span>
</div>
<div class="nt-stats-toolbar-status-item-alt">
📦 Mystery Box: <span class="mystery-box-status">N/A</span>
</div>
</div>`;
// == Mystery Box ==
const { rewardCountdown } = user;
const mysteryBoxStatus = root.querySelector(".mystery-box-status");
let isDisabled = Date.now() < rewardCountdown * 1e3;
let timer = null;
// Sync countdown.
const syncCountdown = () => {
isDisabled = Date.now() < rewardCountdown * 1e3;
if (!isDisabled) {
if (timer) {
clearInterval(timer);
}
mysteryBoxStatus.textContent = "Claim Now!";
return;
}
mysteryBoxStatus.textContent = moment(rewardCountdown * 1e3).fromNow(false);
};
if (isDisabled) {
syncCountdown();
timer = setInterval(syncCountdown, 6e3);
}
// == NT Cash ==
const amountNode = root.querySelector(".nt-cash-status");
// Return an object with the widget root element and a method to update the widget's stats.
return {
root,
updateStats: ({ money }) => {
if (typeof money === "number") {
amountNode.textContent = `$${money.toLocaleString()}`;
}
},
};
})(RACE_OBJ.props.user);
/**
* Creates a widget that displays the daily challenges.
* @returns {Object} An object containing the root element of the widget and an `updateStats` method that can be used to update the widget stats.
*/
const DailyChallengeWidget = (() => {
// Create the root element of the widget.
const root = document.createElement("div");
root.classList.add(
"nt-stats-daily-challenges",
"profile-dailyChallenges",
"card",
"card--open",
"card--d",
"card--grit",
"card--shadow-l"
);
root.innerHTML = `
<div class="daily-challenge-list--heading">
<h4>Daily Challenges</h4>
<div class="daily-challenge-list--arriving">
<div class="daily-challenge-list--arriving-label">
<svg class="icon icon-recent-time"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/dist/site/images/icons/icons.css.1494.svg#icon-recent-time"></use></svg>
New <span></span>
</div>
</div>
</div>
<div class="daily-challenge-list--challenges"></div>`;
// Get references to the daily challenges container and expiry label.
const challengesContainer = root.querySelector(
".daily-challenge-list--challenges"
);
const expiryLabel = root.querySelector(
".daily-challenge-list--arriving-label span"
);
// Create a template for the daily challenge item.
const challengeItemTemplate = document.createElement("div");
challengeItemTemplate.classList.add("raceResults--dailyChallenge");
challengeItemTemplate.innerHTML = `
<div class="daily-challenge-progress">
<div class="daily-challenge-progress--info">
<div class="daily-challenge-progress--requirements">
<div class="daily-challenge-progress--name">
<div style="height: 19px;">
<div align="left" style="white-space: nowrap; pavgSpeedosition: absolute; transform: translate(0%, 0px) scale(1, 1); left: 0px;">
</div>
</div>
</div>
<div class="daily-challenge-progress--status"></div>
</div>
<div class="daily-challenge-progress--progress">
<div class="daily-challenge-progress--progress-bar-container">
<div class="daily-challenge-progress--progress-bar" style="width: 40%"></div>
<div class="daily-challenge-progress--progress-bar--earned" style="width: 40%"></div>
</div>
</div>
</div>
<div class="daily-challenge-progress--badge">
<div class="daily-challenge-progress--success"></div>
<div class="daily-challenge-progress--xp">
<span class="daily-challenge-progress--value"></span><span class="daily-challenge-progress--divider">/</span><span class="daily-challenge-progress--target"></span>
</div>
<div class="daily-challenge-progress--label"></div>
</div>
</div>`;
/**
* Get the daily challenges.
* @returns {Promise} - Promise that resolves to an array of challenge objects.
*/
const getDailyChallenges = async () => {
const { dailyChallenges } = await getStats();
return dailyChallenges;
};
/**
* Creates a challenge node for each challenge.
* @returns {Promise} - Promise that resolves to an array of challenge nodes.
*/
const createChallengeNodes = async () => {
const dailyChallenges = await getDailyChallenges();
const challengeFragment = document.createDocumentFragment();
// Create a challenge node for each challenge.
const challengeNodes = Array.from(dailyChallenges, (challenge) => {
const node = challengeItemTemplate.cloneNode(true);
updateChallengeNode(node, challenge);
challengeFragment.append(node);
return node;
});
challengesContainer.append(challengeFragment);
return challengeNodes;
};
let challengeNodesPromise = createChallengeNodes();
/*
* Updates a challenge node with new data.
* @param {HTMLElement} node The challenge node to update.
* @param {Object} challenge The challenge data to update the node with.
*/
const updateChallengeNode = (node, challenge) => {
const { title, field, progress, goal, reward } = challenge;
// Update each element in the challenge node.
let progressPercentage = goal > 0 ? (progress / goal) * 100 : 0;
if (progress === goal) {
progressPercentage = 100;
node
.querySelector(".daily-challenge-progress")
.classList.add("is-complete");
} else {
node
.querySelector(".daily-challenge-progress")
.classList.remove("is-complete");
}
node.querySelector(".daily-challenge-progress--name div div").textContent =
title;
node.querySelector(
".daily-challenge-progress--label"
).textContent = `${field}s`;
node.querySelector(".daily-challenge-progress--value").textContent =
progress;
node.querySelector(".daily-challenge-progress--target").textContent = goal;
node.querySelector(
".daily-challenge-progress--status"
).textContent = `Earn ${Math.floor(reward / 100) / 10}k XP`;
node
.querySelectorAll(
".daily-challenge-progress--progress-bar, .daily-challenge-progress--progress-bar--earned"
)
.forEach((bar) => {
bar.style.width = `${progressPercentage}%`;
});
};
/**
* Upates the challenge widget with new data.
* @param {Array} data - An array of challenge objects.
*/
const updateStats = async (data) => {
if (!data || data.length === 0) {
return;
}
if (data[0] && data[0].expiration) {
const expiryTime = 1000 * data[0].expiration;
if (!isNaN(expiryTime)) {
expiryLabel.textContent = moment(expiryTime).fromNow();
}
}
const challengeNodes = await challengeNodesPromise;
data.forEach((challenge, i) => {
if (challengeNodes[i]) {
updateChallengeNode(challengeNodes[i], challenge);
}
});
};
// Return an object with the widget root element and a method to update the widget's stats.
return {
root,
updateStats,
};
})();
/**
* Creates a SeasonProgressWidget object that displays the user's progress in the current season.
* @param {Object} race_info - A Race information object.
* @param {Object} race_info.seasonRewards - An array of season rewards.
* @param {Object} race_info.user - The current user object.
* @returns {Object} An object with a root element and an updateStats method to update the widget's stats.
*/
const SeasonProgressWidget = (({ props }) => {
const { ACTIVE_SEASONS, SEASON_LEVELS, LOOT, CARS } = NTGLOBALS;
// Retrieve Season information.
const currentSeason = ACTIVE_SEASONS.find((s) => {
const now = Date.now();
return now >= s.startStamp * 1e3 && now <= s.endStamp * 1e3;
});
const { seasonRewards, user } = props ?? {};
// Create the root element for the widget.
const root = document.createElement("div");
root.classList.add("nt-stats-season-progress", "theme--pDefault");
root.innerHTML = `
<div class="season-progress-widget">
<div class="season-progress-widget--info">
<div class="season-progress-widget--title">Season Progress${
currentSeason ? "" : " (starting soon)"
}</div>
<div class="season-progress-widget--current-xp"></div>
<div class="season-progress-widget--current-level">
<div class="season-progress-widget--current-level--prefix">Level</div>
<div class="season-progress-widget--current-level--number"></div>
</div>
<div class="season-progress-widget--level-progress">
<div class="season-progress-widget--level-progress-bar" style="width: 0%;"></div>
</div>
</div>
<div class="season-progress-widget--next-reward">
<div class="season-progress-widget--next-reward--display">
<div class="season-reward-mini-preview">
<div class="season-reward-mini-preview--locked">
<div class="tooltip--season tooltip--xs tooltip--c" data-ttcopy="Upgrade to Nitro Gold to Unlock!">
<svg class="icon icon-lock"><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/dist/site/images/icons/icons.css.svg#icon-lock"></use></svg>
</div>
</div>
<a class="season-reward-mini-preview" href="/season">
<div class="season-reward-mini-preview--frame">
<div class="rarity-frame rarity-frame--small">
<div class="rarity-frame--extra"></div>
<div class="rarity-frame--content">
<div class="season-reward-mini-preview--preview"></div>
<div class="season-reward-mini-preview--label"></div>
</div>
</div>
</div>
</a>
</div>
</div>
</div>
</div>`;
// Setup widget text nodes.
const xpTextNode = root.querySelector(".season-progress-widget--current-xp");
const xpProgressBarNode = root.querySelector(
".season-progress-widget--level-progress-bar"
);
const levelNode = root.querySelector(
".season-progress-widget--current-level--number"
);
const nextRewardRootNode = root.querySelector(".season-reward-mini-preview");
const nextRewardTypeLabelNode = root.querySelector(
".season-reward-mini-preview--label"
);
const nextRewardTypeLockedNode = root.querySelector(
".season-reward-mini-preview--locked"
);
const nextRewardTypePreviewNode = root.querySelector(
".season-reward-mini-preview--preview"
);
const nextRewardTypePreviewImgNode = document.createElement("img");
const nextRewardRarityFrameNode = root.querySelector(
".rarity-frame.rarity-frame--small"
);
nextRewardTypePreviewImgNode.classList.add("season-reward-mini-previewImg");
if (!currentSeason) {
nextRewardRootNode.remove();
}
/**
* Returns the next reward the user will receive.
* @param {number} currentXP - The user's current experience.
* @returns {Object} The next reward the user will receive.
*/
const getNextReward = (currentXP) => {
currentXP = currentXP || user?.experience;
if (!seasonRewards || seasonRewards.length === 0) {
return null;
}
if (user?.experience === 0) {
return seasonRewards[0]?.achievementID ?? null;
}
let nextReward = seasonRewards.find((r, i) => r.experience > currentXP);
if (!nextReward) {
nextReward = seasonRewards[seasonRewards.length - 1];
}
return nextReward ?? null;
};
/**
* Calculates the amount of experience required to reach a certain level and the total experience required to reach that level.
* @param {number} lvl - The level to calculate the required experience for.
* @returns {Array<number>} An array containing the amount of experience required to reach the specified level and the total experience required to reach that level.
*/
const getExperienceRequired = (lvl) => {
if (lvl < 1) {
lvl = 1;
}
let {
startingLevels,
experiencePerStartingLevel,
experiencePerAchievementLevel,
experiencePerExtraLevels,
} = SEASON_LEVELS;
if (startingLevels === 0) {
startingLevels = currentSeason?.totalRewards ?? 0;
}
let totalExpRequired = 0,
amountExpRequired = experiencePerStartingLevel;
for (let i = 1; i < lvl; i++) {
if (i <= startingLevels) {
totalExpRequired += experiencePerStartingLevel;
} else if (currentSeason && i > currentSeason.totalRewards) {
totalExpRequired += experiencePerExtraLevels;
amountExpRequired = experiencePerExtraLevels;
} else {
totalExpRequired += experiencePerAchievementLevel;
amountExpRequired = experiencePerAchievementLevel;
}
}
return [amountExpRequired, totalExpRequired];
};
/**
* Update the season progress widget with new data.
* @param {Object} data - An object containing the user's season stats.
* @param {number} data.experience - The user's current experience.
* @param {number} data.level - The user's current level.
*/
const updateStats = ({ experience, level }) => {
// Get next Reward.
const nextReward = getNextReward(experience);
if (!nextReward) {
return;
}
// Get experience required for next level.
const [amountExpRequired, totalExpRequired] = getExperienceRequired(
level + 1
);
// Calculate progress.
const progress =
((experience - (totalExpRequired - amountExpRequired)) /
amountExpRequired) *
100;
// Update progress text node if experience is available.
if (experience) {
xpTextNode.textContent = `${(
amountExpRequired -
(totalExpRequired - experience)
).toLocaleString()} / ${amountExpRequired / 1e3}k XP`;
xpProgressBarNode.style.width = `${progress}%`;
levelNode.textContent =
currentSeason && level > currentSeason.totalRewards + 1
? `∞${level - currentSeason.totalRewards - 1}`
: level || 1;
}
// Create next reward node.
const { type, value } = nextReward.reward;
if (["loot", "car"].includes(type)) {
const item =
type === "loot"
? LOOT.find((l) => l.lootID === value)
: CARS.find((l) => l.carID === value);
if (!item) {
logging.warn("Update")(
`Unable to find next reward ${type}`,
nextReward.reward
);
return;
}
nextRewardRootNode.className = `season-reward-mini-preview season-reward-mini-preview--${
type === "loot" ? item?.type : "car"
}`;
nextRewardTypeLabelNode.textContent =
type === "loot" ? item?.type ?? "???" : "car";
nextRewardRarityFrameNode.className = `rarity-frame rarity-frame--small${
item?.options?.rarity ? ` rarity-frame--${item.options.rarity}` : ""
}`;
if (item?.type === "title") {
nextRewardTypePreviewNode.textContent = `"${item.name}"`;
nextRewardTypePreviewImgNode.remove();
} else {
nextRewardTypePreviewImgNode.src =
type === "loot"
? item?.options?.src
: `/cars/${item?.options?.smallSrc}`;
nextRewardTypePreviewNode.append(nextRewardTypePreviewImgNode);
}
} else if (type === "money") {
nextRewardTypeLabelNode.innerHTML = `<div class="as-nitro-cash--prefix">$${value.toLocaleString()}</div>`;
nextRewardTypePreviewImgNode.src =
"/dist/site/images/pages/race/race-results-prize-cash.2.png";
nextRewardRootNode.className =
"season-reward-mini-preview season-reward-mini-preview--money";
nextRewardRarityFrameNode.className =
"rarity-frame rarity-frame--small rarity-frame--legendary";
nextRewardTypePreviewNode.append(nextRewardTypePreviewImgNode);
} else {
logging.warn("Update")(
`Unhandled next reward type ${type}`,
nextReward.reward
);
return;
}
if (!nextReward.free && user?.membership === "basic") {
nextRewardRootNode.firstElementChild.before(nextRewardTypeLockedNode);
} else {
nextRewardTypeLockedNode.remove();
}
};
// Return an object with the widget root element and a method to update the widget's stats.
return {
root,
updateStats,
};
})(RACE_OBJ);
/**
* Displays a widget that shows various statistics about the player.
* @returns {Object} An object containing the root element of the widget and an `updateStats` method that can be used to update the widget stats.
*/
const StatWidget = (() => {
// Create the root element for the widget.
const root = document.createElement("div");
root.classList.add("nt-stats-info");
// Create the HTML for the widget.
root.innerHTML = `
<div class="nt-stats-metric-row">
<span class="nt-stats-metric nt-stats-metric-total-races">
<span class="nt-stats-metric-heading">Total Races:</span>
<span class="nt-stats-metric-value">0</span>
</span>
<span class="nt-stats-metric-separator">|</span>
<span class="nt-stats-metric nt-stats-metric-season-races">
<span class="nt-stats-metric-heading">Season Races:</span>
<span class="nt-stats-metric-value">N/A</span>
</span>
${
CURRENT_USER.tag
? `<span class="nt-stats-metric-separator">|</span>
<span class="nt-stats-metric nt-stats-metric-team-races">
<span class="nt-stats-metric-heading">Team Races:</span>
<span class="nt-stats-metric-value">N/A</span>
</span>`
: ``
}
<span class="nt-stats-metric-separator">|</span>
<span class="nt-stats-metric nt-stats-metric-session-races">
<span class="nt-stats-metric-heading">Current Session:</span>
<span class="nt-stats-metric-value">0</span>
</span>
</div>
<div class="nt-stats-metric-row">
<span class="nt-stats-metric nt-stats-metric-avg-speed">
<span class="nt-stats-metric-heading">Avg Speed:</span>
<span class="nt-stats-metric-value">0</span>
<span class="nt-stats-metric-suffix">WPM</span>
</span>
<span class="nt-stats-metric-separator">|</span>
<span class="nt-stats-metric nt-stats-metric-avg-accuracy">
<span class="nt-stats-metric-heading">Avg Acc:</span>
<span class="nt-stats-metric-value">0</span>
<span class="nt-stats-metric-suffix nt-stats-metric-suffix-no-space">%</span>
</span>
</div>
`;
// Get references to the various elements in the widget.
const totalRacesElement = root.querySelector(
".nt-stats-metric-total-races .nt-stats-metric-value"
);
const sessionRacesElement = root.querySelector(
".nt-stats-metric-session-races .nt-stats-metric-value"
);
const teamRacesElement = CURRENT_USER.tag
? root.querySelector(".nt-stats-metric-team-races .nt-stats-metric-value")
: null;
const seasonRacesElement = root.querySelector(
".nt-stats-metric-season-races .nt-stats-metric-value"
);
const avgSpeedElement = root.querySelector(
".nt-stats-metric-avg-speed .nt-stats-metric-value"
);
const avgAccuracyElement = root.querySelector(
".nt-stats-metric-avg-accuracy .nt-stats-metric-value"
);
// Return an object with the widget root element and a method to update the widget's stats.
return {
root,
updateStats: (data) => {
const racesPlayed = data?.racesPlayed;
const sessionRaces = data?.sessionRaces;
const seasonRaces = data?.seasonRaces;
const teamRaces = data?.teamRaces;
const avgAcc = data?.avgAcc;
const avgSpeed = data?.avgSpeed ?? data?.avgScore;
// Update stats that were supplied.
if (racesPlayed) {
totalRacesElement.textContent = racesPlayed.toLocaleString();
}
if (sessionRaces) {
sessionRacesElement.textContent = sessionRaces.toLocaleString();
}
if (seasonRaces) {
const value = parseInt(seasonRaces, 10);
seasonRacesElement.textContent = Number.isNaN(value)
? seasonRaces
: value.toLocaleString();
}
if (teamRaces && teamRacesElement) {
teamRacesElement.textContent = teamRaces.toLocaleString();
}
if (avgAcc) {
avgAccuracyElement.textContent = avgAcc;
}
if (typeof avgSpeed === "number") {
avgSpeedElement.textContent = avgSpeed;
}
},
};
})();
////////////
// Main //
////////////
/**
* Adds stats to the race page with current values.
*/
const addStatsToRacePage = async () => {
// Get user and daily challenge stats.
const { user, dailyChallenges } = await getStats();
StatWidget.updateStats(user);
SeasonProgressWidget.updateStats(user);
DailyChallengeWidget.updateStats(dailyChallenges);
ToolbarWidget.updateStats(user);
logging.info("Update")("Start of race");
// Create Stats widget.
const root = document.createElement("div");
const body = document.createElement("div");
root.classList.add("nt-stats-root");
body.classList.add("nt-stats-body");
const leftSection = document.createElement("div");
leftSection.classList.add("nt-stats-left-section");
leftSection.append(DailyChallengeWidget.root);
const rightSection = document.createElement("div");
rightSection.classList.add("nt-stats-right-section");
rightSection.append(StatWidget.root, SeasonProgressWidget.root);
body.append(leftSection, rightSection);
root.append(body, ToolbarWidget.root);
RACE_CONTAINER.parentElement.append(root);
// Update team stats if user is in a team.
const teamStats = await getTeamStats();
if (teamStats) {
const { member } = teamStats;
StatWidget.updateStats({ teamRaces: member.played });
}
// Update season stats.
const summaryStats = await getSummaryStats();
if (summaryStats) {
const { seasonBoard } = summaryStats;
if (seasonBoard) {
StatWidget.updateStats({ seasonRaces: seasonBoard.played });
}
}
};
addStatsToRacePage();
/**
* Broadcasts a message to let other windows know that the stats have been updated.
* Listens for messages on the channel and updates the appropriate widgets based on the message type.
*/
const MESSAGE_LAST_RACE_UPDATED = "last_race_updated";
const MESSAGE_DAILY_CHALLENGE_UPDATED = "stats_daily_challenge_updated";
const MESSAGE_USER_STATS_UPDATED = "stats_user_updated";
const statChannel = new BroadcastChannel("NTRacingStats");
statChannel.onmessage = async (e) => {
const [type, payload] = e.data;
switch (type) {
case MESSAGE_LAST_RACE_UPDATED: {
const { user, dailyChallenges } = await getStats();
StatWidget.updateStats(user);
SeasonProgressWidget.updateStats(user);
DailyChallengeWidget.updateStats(dailyChallenges);
ToolbarWidget.updateStats(user);
break;
}
case MESSAGE_DAILY_CHALLENGE_UPDATED:
DailyChallengeWidget.updateStats(payload);
break;
case MESSAGE_USER_STATS_UPDATED:
StatWidget.updateStats(payload);
SeasonProgressWidget.updateStats(payload);
break;
}
};
/**
* Syncs the daily challenge data.
*/
SERVER.on("setup", async (e) => {
const dailyChallenges = mergeDailyChallengeData(e.challenges);
DailyChallengeWidget.updateStats(dailyChallenges);
statChannel.postMessage([MESSAGE_DAILY_CHALLENGE_UPDATED, dailyChallenges]);
});
/**
* Syncs some of the user's stats data.
*/
SERVER.on("joined", async (e) => {
if (e.userID !== CURRENT_USER.userID) {
return;
}
const payload = {
level: e.profile?.level,
racesPlayed: e.profile?.racesPlayed,
sessionRaces: e.profile?.sessionRaces,
avgSpeed: e.profile?.avgSpeed,
};
StatWidget.updateStats(payload);
SeasonProgressWidget.updateStats(payload);
statChannel.postMessage([MESSAGE_USER_STATS_UPDATED, payload]);
});
/**
* Store race results in IndexedDB when the race is complete.
*/
let hasCollectedResultStats = false;
SERVER.on("update", async (e) => {
const me = e?.racers?.find((r) => r.userID === CURRENT_USER.userID);
if (
me.progress.completeStamp > 0 &&
me.rewards?.current &&
!hasCollectedResultStats
) {
hasCollectedResultStats = true;
try {
const backupData = {
...me.rewards.current,
challenges: me.challenges,
userID: CURRENT_USER.userID,
};
await db.backupStatData.put(backupData);
statChannel.postMessage([MESSAGE_LAST_RACE_UPDATED]);
} catch (error) {
console.error(error);
}
}
});
/**
* Adds a mutation observer to check if the racing results have been shown.
* When the results are shown, updates various widgets with the user's stats.
*/
const resultObserver = new MutationObserver(([mutation], observer) => {
for (const node of mutation.addedNodes) {
if (node.classList?.contains("race-results")) {
observer.disconnect();
logging.info("Update")("Race Results received");
addStatsToRacePage();
break;
}
}
});
resultObserver.observe(RACE_CONTAINER, { childList: true });