-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsh.user.js
6818 lines (6070 loc) · 325 KB
/
sh.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
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
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==UserScript==
// @name SurfHeaven ranks Ext
// @namespace http://tampermonkey.net/
// @version 4.2.20
// @description More stats and features for SurfHeaven.eu
// @author kalle, Link
// @updateURL https://github.com/Kalekki/SurfHeaven_Extended/raw/main/sh.user.js
// @downloadURL https://github.com/Kalekki/SurfHeaven_Extended/raw/main/sh.user.js
// @require https://cdnjs.cloudflare.com/ajax/libs/chartist/0.11.4/chartist.min.js
// @match https://surfheaven.eu/*
// @icon https://www.google.com/s2/favicons?domain=surfheaven.eu
// @connect raw.githubusercontent.com
// @connect surfheaven.eu
// @connect iloveur.mom
// @grant GM_xmlhttpRequest
// @grant GM_addStyle
// @grant GM.getValue
// @grant GM.setValue
// @grant GM_info
// @license MIT
// ==/UserScript==
(async function () {
'use strict';
const VERSION = GM_info.script.version;
var use_custom = await GM.getValue('sh_ranks_use_custom_id', false);
var custom_id = await GM.getValue('sh_ranks_custom_id', unsafeWindow.localStorage.getItem('cached_id'));
let showed_id_prompt = false;
var current_page = "";
var url_path = window.location.pathname.split('/');
var api_call_count = 0;
var scraped_maps = [];
var map_completions = {};
var map_types = {};
var map_tiers = {};
let map_dates = {};
var bonus_completions = {};
let fastest_time = -1;
let in_session = null
let session_check_count = 0
let current_session = null
// season specific
let season_time_left = [];
const season_map_times = [30, 45, 60]
let servers_load_time
const now = new Date();
const cest = { timeZone: 'Europe/Paris' };
const cest_string = now.toLocaleString('en-US', cest);
const cest_time = new Date(cest_string);
const cest_season_end = new Date(Date.parse('2023-07-23T18:00:00+02:00'));
const is_season = cest_time < cest_season_end;
const easy_maps = ["surf_spaceracer","surf_aesthetic","surf_beyond2","surf_melatonin"]
const medium_maps = ["surf_eos","surf_kala","surf_nibiru2","surf_njv"]
const hard_maps = ["surf_salmari","surf_barbies_malibu_adventure","surf_korovamilkbar","surf_rebirth"]
const custom_css = document.createElement('link');
custom_css.rel = 'stylesheet';
custom_css.href = 'https://iloveur.mom/surfheaven/styles.css?d=' + now.getHours();
document.head.appendChild(custom_css);
// colors are approximate and might be wrong, let me know
const GROUP_THRESHOLDS = [1, 2, 3, 10, 25, 50, 75, 100, 150, 250, 500, 750, 1000, 1500, 2000, 3000, 6000, 15000, 25000]
const GROUP_NAMES = ["#1", "#2", "#3", "Master", "Elite", "Veteran", "Expert", "Pro", "TheSteve","Hotshot", "Skilled", "Intermediate", "Casual", "Amateur", "Regular", "Potato", "Beginner", "Burrito", "Calzone", "New"]
const GROUP_COLORS = ["gold", "gold", "gold", "#b57fe5", "red", "#d731eb", "#6297d1","#6297d1", "#E94A4B", "#55ff4b", "#aef25d", "#ad8adc", "#ebe58d","#b4c5d9", "#6297d1", "#dfa746","#ccccd4", "#649ad8", "#ccccd4", "#FFFFFF"]
const AU_SERVERS = {
12 : "51.161.199.33:27015", //#1
13 : "51.161.199.33:27016", //#2
14 : "51.161.199.33:27017", //#3
15 : "51.161.199.33:27018", //#4
16 : "51.161.199.33:27019", //#5
17 : "51.161.199.33:27020", //#6
18 : "51.161.199.33:27021", //#7
19 : "51.161.199.33:27022", //#8
}
// SETTINGS
let settings
if (unsafeWindow.localStorage.getItem('settings') == null) {
// defaults
settings = {
flags: true,
follow_list: true,
update_check: true,
cp_chart: true,
steam_avatar: true,
completions_by_tier: true,
country_top_100: true,
hover_info: true,
map_cover_image: true,
points_per_rank: true,
completions_bar_chart: true,
user_ratings_table: true,
user_ratings: true,
user_effects: true,
comments: true,
map_recommendations: true,
record_diffs: true
}
unsafeWindow.localStorage.setItem('settings', JSON.stringify(settings));
}else{
settings = JSON.parse(unsafeWindow.localStorage.getItem('settings'));
validate_settings();
}
const settings_labels = {
flags: "Country flags",
follow_list: "Follow list",
update_check: "Automatic update check",
cp_chart: "Checkpoint chart",
steam_avatar: "Show Steam avatar",
completions_by_tier: "Completions by tier",
country_top_100: "Country top 100 table",
hover_info: "Player/map info on hover",
map_cover_image: "Map cover image",
points_per_rank: "Show points per rank",
completions_bar_chart: "Show completions as bar chart",
toasts: "Show debug toasts",
user_ratings_table: "Show user rated maps",
user_ratings: "Show user ratings",
user_effects: "Show user effects",
comments: "Show map comments",
map_recommendations: "Show map recommendations",
record_diffs: "Show record time difference in recents"
}
const settings_categories = {
"Global" : ["flags","follow_list", "hover_info", "update_check", "toasts", "user_effects", "record_diffs"],
"Dashboard" : ["country_top_100", "user_ratings_table"],
"Map page" : ["cp_chart","points_per_rank","map_cover_image","user_ratings","comments"],
"Profile" : ["steam_avatar", "completions_by_tier", "completions_bar_chart", "map_recommendations"]
}
function validate_settings(){
if (settings.flags == null) settings.flags = true;
if (settings.follow_list == null) settings.follow_list = true;
if (settings.update_check == null) settings.update_check = true;
if (settings.cp_chart == null) settings.cp_chart = true;
if (settings.steam_avatar == null) settings.steam_avatar = true;
if (settings.completions_by_tier == null) settings.completions_by_tier = true;
if (settings.country_top_100 == null) settings.country_top_100 = true;
if (settings.hover_info == null) settings.hover_info = true;
if (settings.map_cover_image == null) settings.map_cover_image = true;
if (settings.points_per_rank == null) settings.points_per_rank = true;
if (settings.completions_bar_chart == null) settings.completions_bar_chart = true;
if (settings.toasts == null) settings.toasts = false;
if (settings.user_ratings_table == null) settings.user_ratings_table = true;
if (settings.user_ratings == null) settings.user_ratings = true;
if (settings.user_effects == null) settings.user_effects = true;
if (settings.comments == null) settings.comments = true;
if (settings.map_recommendations == null) settings.map_recommendations = true;
if (settings.record_diffs == null) settings.record_diffs = true;
}
// USER EFFECTS
let user_effects = {};
let last_updated_effects = unsafeWindow.localStorage.getItem('user_effects_last_updated');
if (last_updated_effects == null || Date.now() - Number(last_updated_effects) > 1000 * 60 * 5 ) {
console.log("Updating user_effects.json")
unsafeWindow.localStorage.setItem('user_effects_last_updated', Date.now());
fetch("https://iloveur.mom/surfheaven/user_effects.json", {cache: "no-cache"})
.then(response => response.json())
.then(data => {
for (let user in data) {
if (data[user] == "candycane-custom-") {
delete data[user]; // remove empty custom styles
}
}
console.log("Updated user_effects.json")
unsafeWindow.localStorage.setItem('user_effects', JSON.stringify(data));
user_effects = data;
});
}
user_effects = JSON.parse(unsafeWindow.localStorage.getItem('user_effects'));
for (let user in user_effects) {
if (user_effects != null && user_effects[user] != null) {
if (user_effects[user].startsWith("candycane-custom") && user_effects[user] != "candycane-custom-") {
create_custom_candycane_style(user_effects[user]);
}
}
}
function create_custom_candycane_style(style_name){
let colors = style_name.split("-").slice(2);
if (colors.length == 1){
// single color
//console.log(`Creating custom style: ${colors[0]}`);
GM_addStyle(`.candycane-custom-${colors[0]} {
color: ${colors[0]};
}`);
return;
};
let cssColors = colors.map((color, index) => {
if (index === 0) {
return `${color}, ${color} 10px,`;
} else if (index === colors.length - 1) {
return `${color} ${index * 10}px, ${color} ${(index + 1) * 10}px`;
}
return `${color} ${index * 10}px, ${color} ${(index + 1) * 10}px,`;
}).join(' ');
//console.log(`Creating custom style: ${colors.join(' ')}`);
//TODO: Fix this to make the looping perfect
GM_addStyle(`.candycane-custom-${colors.join('-')} {
background: repeating-linear-gradient(45deg, ${cssColors});
background-size: 4800%;
color: transparent;
text-shadow: 0px 0px 0 rgba(0,0,0,0);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
-webkit-animation: 120s linear 0s infinite move;
animation: 120s linear 0s infinite move;
font-weight: bold;
}`);
//console.log(`.candycane-custom-${colors.join('-')} {
// background: repeating-linear-gradient(45deg, ${cssColors});
// `)
}
function apply_user_effect(id, element){
if(!settings.user_effects) return;
let effect = '';
if(id in user_effects){
effect = user_effects[id];
element.classList.remove('vip-name')
}
let text = element.textContent;
let flag_img = element.childNodes[0];
let steam_link = element.childNodes[2];
element.innerHTML = '';
element.appendChild(flag_img);
element.innerHTML += '<span class='+effect+'> ' + text + ' </span>';
element.appendChild(steam_link);
}
// Text under "SurfHeaven"
let logo = document.querySelector(".navbar-brand");
let logo_text = document.createElement("div");
logo_text.id = "logo_text";
logo_text.innerHTML = "<a style='color:#FFFFFF;' href='https://github.com/Kalekki/SurfHeaven_Extended' target='_blank'>Extended</a>";
logo_text.style.position = "absolute";
logo_text.style.bottom = "0px";
logo_text.style.fontSize = "10px";
logo_text.style.color = "#FFFFFF";
logo_text.style.padding = "0px 0px";
logo_text.style.zIndex = "100";
logo_text.style.bottom = "5px";
logo_text.style.left = "95px"
logo.appendChild(logo_text);
// Replace "My Profile" link with custom id
if(use_custom){
let profile_link = document.querySelector(".nav > li:nth-child(1) > a:nth-child(1)")
if(profile_link.text == "MY PROFILE"){
console.log("Replacing profile link with custom id")
profile_link.href = "/player/" + custom_id;
}else{
console.log("Creating profile link with custom id")
let new_profile_link = document.createElement("li");
new_profile_link.innerHTML = "<a href='/player/" + custom_id + "'>MY PROFILE</a>";
document.querySelector(".nav").insertBefore(new_profile_link, document.querySelector(".nav > li:nth-child(1)"));
}
}
// Surf map tier cache
function cache_map_tiers(){
let last_updated_map_tiers = unsafeWindow.localStorage.getItem('map_tiers_last_updated')
if(!last_updated_map_tiers || Date.now() - Number(last_updated_map_tiers) > 1000 * 60 * 60 * 6){
unsafeWindow.localStorage.setItem('map_tiers_last_updated', String(Date.now()))
make_request("https://api.surfheaven.eu/api/maps", (data) => {
let map_tiers = {};
for (let i = 0; i < data.length; i++) {
map_tiers[data[i].map] = data[i].tier;
}
unsafeWindow.localStorage.setItem('map_tiers', JSON.stringify(map_tiers));
console.log("Updated map tiers cache");
console.log(map_tiers);
})
}
}
// SERVERS PAGE
if (window.location.pathname.endsWith("/servers/")) {
current_page = "servers";
servers_page();
}
// PROFILE PAGE
else if (url_path[url_path.length - 2] == "player") {
current_page = "profile";
profile_page();
}
// MAP PAGE
else if (url_path[url_path.length - 2] == "map") {
current_page = "map";
var current_map_name = url_path[url_path.length - 1];
map_page(current_map_name);
}
// DASHBOARD
else if (window.location.pathname == "/") {
current_page = "dashboard";
dashboard_page();
}
else if(url_path[url_path.length - 2] == "search"){
current_page = "search";
let search_term = url_path[url_path.length - 1];
search_term = decodeURIComponent(search_term);
search_page(search_term);
}
else{
current_page = url_path[url_path.length - 2];
if(current_page == "donate"){
// Gift vip
if (unsafeWindow.localStorage.getItem('gift_vip_steamid') != null) {
document.getElementById("authid").value = "http://steamcommunity.com/profiles/"+unsafeWindow.localStorage.getItem('gift_vip_steamid');
unsafeWindow.localStorage.removeItem('gift_vip_steamid');
unsafeWindow.checker();
}
}
}
recent_activity_times()
function recent_activity_times(){
if(!settings.record_diffs) return;
let recs = [];
let comparison_recs = [];
let recents = document.getElementsByClassName("row-recentactivity");
for(let i = 0; i < recents.length; i++){
recents[i].id = "rec_" + i
//console.log(recents[i].textContent);
let rec_string = recents[i].textContent;
let id = recents[i].querySelectorAll("a")[0].href.split("/").pop();
let map = recents[i].querySelectorAll("a")[1].href.split("/").pop();
let track = 0;
rec_string = rec_string.substring(rec_string.indexOf("set a new"));
if(recents[i].textContent.includes("new Map WR")){
track = 0;
}else if(recents[i].textContent.includes("new Bonus ")){
let _regex = /\d+/g;
track = rec_string.match(_regex)[0];
}else{
track = null
}
recs.push([id, map, track]);
//console.log(id, map, track);
}
//console.log(recs);
for (let i = 0; i < recs.length; i++) {
let map = recs[i][1]
let track = recs[i][2]
if (track != null) {
make_request("https://api.surfheaven.eu/api/records/"+map+"/" + track, (data) => {
if (data) {
let diff
if(data.length <=1){
diff = "👑"
comparison_recs.push([i,diff]);
}else{
data.sort((a, b) => a.time - b.time);
diff = (data[0].time - data[1].time).toFixed(3);
comparison_recs.push([i,diff]);
}
check_if_ready();
}
})
}else{
comparison_recs.push([i, null]);
check_if_ready();
}
}
function check_if_ready(){
if(comparison_recs.length == recs.length){
comparison_recs.sort((a, b) => a[0] - b[0]);
//console.log(comparison_recs);
for(let i = 0; i < comparison_recs.length; i++){
if(comparison_recs[i][1] != null){
let target_div = document.getElementById("rec_" + i);
let target_node = target_div.querySelector("div:nth-child(1) > div:nth-child(1) > div:nth-child(1) > div:nth-child(3)")
target_node.innerHTML += " <span style='color:lightgreen'><small>" + comparison_recs[i][1] + "</small></span>";
}
}
}
}
}
function search_page(search_term){
// Maps by author
make_request("https://api.surfheaven.eu/api/maps", (data) => {
let search_results = [];
data.forEach((map) => {
if(map.author.toLowerCase().includes(search_term.toLowerCase())){
search_results.push(map);
}
// the_ancient_one patch
if(search_term.includes("_")){
if(map.author.toLowerCase().includes(search_term.replace(/_/g, " ").toLowerCase())){
search_results.push(map);
}
}
// vice versa
if(search_term.includes(" ")){
if(map.author.toLowerCase().includes(search_term.replace(/ /g, "_").toLowerCase())){
search_results.push(map);
}
}
});
console.log(search_results)
let table_data = [];
search_results.forEach((map) => {
table_data.push([
`<a href="https://surfheaven.eu/map/${map.map}">${map.map}</a>`,
map.author,
map.tier,
map.type == 0 ? "Linear" : "Staged",
map.bonus,
map.date_added.split("T")[0]
]);
});
let table = create_table(["Map name", "Author", "Tier", "Type", "Bonuses", "Date Added"], table_data,"author_maps");
let div = create_div('Maps by "'+search_term+'"', table, 12, true);
let target_div = document.querySelector(".content > div:nth-child(1)")
target_div.appendChild(div);
$("#author_maps").DataTable({
"paging": "true",
"pagingType": "simple",
"lengthChange": false,
"info": true,
"searching": true,
"oLanguage": {
"sSearch": '<i class="fas fa-search"></i>',
"sInfo": "<small>Note: Maps with multiple authors are sometimes listed as 'collab' and might not show up in this list.</small>",
},
"order": [[ 5, "asc" ]]
});
})
}
function create_table(columns, data, id){
let table = document.createElement('table');
table.id = id;
table.className = "table table-striped table-hover";
let thead = document.createElement('thead');
let tbody = document.createElement('tbody');
let thead_tr = document.createElement('tr');
columns.forEach((column) => {
let th = document.createElement('th');
th.innerHTML = column;
thead_tr.appendChild(th);
});
thead.appendChild(thead_tr);
table.appendChild(thead);
data.forEach((row) => {
let tr = document.createElement('tr');
row.forEach((cell) => {
let td = document.createElement('td');
td.innerHTML = cell;
tr.appendChild(td);
});
tbody.appendChild(tr);
});
table.appendChild(tbody);
return table;
}
function create_div(title, content, size = 12){
let div = document.createElement('div');
div.className = "col-md-"+size;
let panel = document.createElement('div');
panel.className = "panel panel-filled";
let panel_heading = document.createElement('div');
panel_heading.className = "panel-heading";
panel_heading.innerHTML = "<span>"+title+"</span>";
let panel_body = document.createElement('div');
panel_body.className = "panel-body";
panel_body.appendChild(content);
panel.appendChild(panel_heading);
panel.appendChild(panel_body);
div.appendChild(panel);
return div;
}
// Navbar crowded fix
if (document.getElementById("navbar").clientHeight > 60) {
make_navbar_compact();
}
window.addEventListener('resize', function () {
if (document.getElementById("navbar").clientHeight > 60) {
make_navbar_compact();
}
});
// Update check
if(settings.update_check){
if (unsafeWindow.localStorage.getItem('update_last_checked') == null) {
unsafeWindow.localStorage.setItem('update_last_checked', Date.now());
}
else if (Date.now() - unsafeWindow.localStorage.getItem('update_last_checked') > 1000*60*5) {
unsafeWindow.localStorage.setItem('update_last_checked', Date.now());
check_for_updates();
}
}
function check_for_updates(){
GM_xmlhttpRequest({
method: "GET",
url: "https://raw.githubusercontent.com/Kalekki/SurfHeaven_Extended/main/changelog.txt",
onload: function (response) {
if(response.status != 200) return;
var latest_version = response.responseText.split("___")[1];
console.log("Current version: " + VERSION + " | Latest version: " + latest_version)
if (latest_version != VERSION) {
let update_url = "https://github.com/Kalekki/SurfHeaven_Extended/raw/main/sh.user.js"
let modal = document.createElement('div');
modal.innerHTML = `
<div class="modal fade" id="update_modal" tabindex="-1" role="dialog" style="display: flex; z-index:99999">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-body" style="padding: 1rem;">
<h5 class="modal-title" style="margin-bottom:1rem;">SH Extended update available!</h5>
<p>Version <span style="color:salmon;">${VERSION}</span> -> <span style="color: lightgreen">${latest_version}</span</p>
<p style="color:white;">What's new:</p>
<textarea readonly style="width:100%;height:80px; background-color:#21242a; color:white;">${response.responseText.split("___")[2]}</textarea>
</div>
<div class="modal-footer" style="padding:7px;">
<small style="text-align: left;">You can disable this message in the settings.</small>
<button type="button" class="btn btn-secondary btn-danger" data-dismiss="modal">Close</button>
<a href="${update_url}" target="_blank" onclick="$('#update_modal').modal('hide');" class="btn btn-primary btn-success">Update</a>
</div>
</div>
</div>
</div>
`;
document.body.appendChild(modal);
$('#update_modal').modal('show');
}
}
});
}
// Follow list
if (settings.follow_list) {
const sidebar_div = document.querySelector('.navigation');
const follow_list_root_div = document.createElement('div');
const follow_list_row_div = document.createElement('div');
const follow_list_panel_div = document.createElement('div');
const follow_list_panel_body_div = document.createElement('div');
const follow_h5 = document.createElement('h5');
follow_h5.className = "text-center";
follow_h5.innerHTML = "<a href='#' style='color:white;'>FOLLOWED PLAYERS</a>";
follow_h5.addEventListener("click", follow_list_manager);
follow_h5.classList.add("text-white");
follow_list_root_div.className = "row-recentactivity";
follow_list_row_div.className = "col-sm-12";
follow_list_panel_div.className = "panel panel-filled";
follow_list_panel_body_div.className = "panel-body";
follow_list_panel_body_div.id = "follow_list";
follow_list_panel_body_div.style = "padding: 5px;";
follow_list_root_div.appendChild(follow_list_row_div);
follow_list_row_div.appendChild(follow_list_panel_div);
follow_list_panel_div.appendChild(follow_list_panel_body_div);
sidebar_div.insertBefore(follow_list_root_div, sidebar_div.firstChild);
sidebar_div.insertBefore(follow_h5, sidebar_div.firstChild);
insert_flags_to_profiles();
refresh_follow_list();
// refresh follow list
const follow_list_refresh_interval = 60*1000;
setInterval(() => {
refresh_follow_list();
}, follow_list_refresh_interval);
}else{
insert_flags_to_profiles();
}
function follow_list_manager(){
let follow_list = get_follow_list();
const follow_list_root = document.createElement('div');
follow_list_root.style.overflowY = "scroll";
follow_list_root.style.height = "600px";
for(let i = 0; i < follow_list.length; i++){
make_request(`https://api.surfheaven.eu/api/playerinfo/${follow_list[i]}`, (data) => {
let follow_list_item = document.createElement('div');
follow_list_item.style = "white-space: nowrap; overflow: hidden; text-overflow: ellipsis;";
let name = data[0].name;
let last_online = data[0].lastplay;
const profile_link = document.createElement('a');
profile_link.href = `https://surfheaven.eu/player/${follow_list[i]}`;
profile_link.innerHTML = name != "" ? name : follow_list[i];
profile_link.style = "width:220px; float: left;";
const last_online_span = document.createElement('span');
last_online_span.style = "float: right;";
last_online_span.innerHTML = `Last play ${format_date(last_online)} `;
last_online_span.setAttribute("data-last-online", last_online); // for sorting
const unfollow_button = document.createElement('button');
unfollow_button.className = "btn btn-danger btn-xs float-right";
unfollow_button.style.marginTop = "1px";
unfollow_button.style.marginLeft = "1rem";
unfollow_button.style.marginRight = "0.5rem";
unfollow_button.style.marginBottom = "1px";
unfollow_button.innerHTML = "Unfollow";
unfollow_button.onclick = () => {
follow_list_root.removeChild(follow_list_item);
follow_user(follow_list[i]);
};
follow_list_item.appendChild(profile_link)
last_online_span.appendChild(unfollow_button);
follow_list_item.appendChild(last_online_span);
follow_list_root.appendChild(follow_list_item);
insert_flags_to_profiles();
if(follow_list_root.querySelectorAll('div').length == follow_list.length){
let follow_list_items = follow_list_root.children;
let follow_list_items_array = [];
for(let j = 0; j < follow_list_items.length; j++){
follow_list_items_array.push(follow_list_items[j]);
}
follow_list_items_array.sort((a, b) => {
let a_last_online = a.querySelector('span').getAttribute("data-last-online");
let b_last_online = b.querySelector('span').getAttribute("data-last-online");
return new Date(b_last_online) - new Date(a_last_online);
});
while (follow_list_root.firstChild) {
follow_list_root.removeChild(follow_list_root.firstChild);
}
follow_list_items_array.forEach((item, index) => {
follow_list_root.appendChild(item);
if (index % 2 == 0) {
item.style.backgroundColor = "#19202B";
}
});
insert_flags_to_profiles();
}
});
}
show_overlay_window("Followed players", follow_list_root);
}
function refresh_follow_list(){
console.log("Refreshing follow list")
let follow_list = get_follow_list();
let follow_list_panel_body_div = document.querySelector('div.row-recentactivity:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)');
if (follow_list != null && follow_list[0] != "") {
make_request("https://api.surfheaven.eu/api/online/", (data) => {
let online_players = [];
let friends_online = false;
let self_in_server = false;
let self_stats
data.forEach((player) => {
online_players.push([player.steamid, player.name, player.server, player.map, player.region]);
});
online_players.forEach((player) => {
if (follow_list.includes(player[0])) {
friends_online = true;
}
if(player[0] == get_id()){
self_in_server = true;
self_stats = player
}
});
// session shit
if(current_page == "servers"){
if (self_in_server) {
console.log("self in server");
if (!in_session) {
console.log("starting session");
current_session = {
start_time: Date.now(),
server: self_stats[2],
maps: [self_stats[3]],
starting_ranks: [],
starting_points: [],
starting_bonus_ranks: [],
starting_bonus_points: [],
starting_account_points: 0
};
get_rank_in_map(self_stats[3], get_id(), 0, true, (stats) => {
current_session.starting_ranks.push(stats.rank);
current_session.starting_points.push(stats.points);
});
get_mapinfo(self_stats[3], (map_data) => {
if (!map_data || typeof map_data !== "object" || !map_data.bonus) {
console.log("no bonus tracks found for", self_stats[3]);
current_session.starting_bonus_ranks.push([]);
current_session.starting_bonus_points.push([]);
return;
}
const bonus_count = map_data.bonus;
console.log("bonus count for", self_stats[3], ":", bonus_count);
let bonus_ranks = [];
let bonus_points = [];
for (let b = 1; b <= bonus_count; b++) {
get_rank_in_map(self_stats[3], get_id(), b, true, (bonus_stats) => {
bonus_ranks.push(bonus_stats.rank ?? 0);
bonus_points.push(bonus_stats.points ?? 0);
if (bonus_ranks.length === bonus_count) {
current_session.starting_bonus_ranks.push(bonus_ranks);
current_session.starting_bonus_points.push(bonus_points);
}
});
}
});
get_playerinfo(get_id(), (data) => {
current_session.starting_account_points = data.points;
});
if (current_page == "servers") {
const session_overlay = create_session_overlay();
session_overlay.stats_button.addEventListener('click', () => {
display_session_modal(current_session);
});
}
} else {
console.log("updating session");
if (current_session.maps[current_session.maps.length - 1] != self_stats[3]) {
current_session.maps.push(self_stats[3]);
current_session.starting_ranks.push(0);
current_session.starting_points.push(0);
current_session.starting_bonus_ranks.push([]);
current_session.starting_bonus_points.push([]);
get_rank_in_map(self_stats[3], get_id(), 0, true, (stats) => {
current_session.starting_ranks[current_session.starting_ranks.length - 1] = stats.rank;
current_session.starting_points[current_session.starting_points.length - 1] = stats.points;
});
get_mapinfo(self_stats[3], (map_data) => {
if (!map_data || typeof map_data !== "object" || !map_data.bonus) return;
const bonus_count = map_data.bonus;
let bonus_ranks = [];
let bonus_points = [];
for (let b = 1; b <= bonus_count; b++) {
get_rank_in_map(self_stats[3], get_id(), b, true, (bonus_stats) => {
bonus_ranks.push(bonus_stats.rank ?? 0);
bonus_points.push(bonus_stats.points ?? 0);
if (bonus_ranks.length === bonus_count) {
current_session.starting_bonus_ranks[current_session.starting_bonus_ranks.length - 1] = bonus_ranks;
current_session.starting_bonus_points[current_session.starting_bonus_points.length - 1] = bonus_points;
}
});
}
});
}
}
in_session = true;
session_check_count = 0;
} else {
if (session_check_count > 0) {
in_session = false;
if (current_session != null) {
console.log("ending session");
}
} else {
session_check_count++;
console.log("cant find self in server, waiting for next check " + session_check_count);
}
}
}
online_players.sort((a, b) => {
return a[2] - b[2];
});
if (friends_online) {
follow_list_panel_body_div.innerHTML = "";
online_players.forEach((player) => {
if (follow_list.includes(player[0])) {
let follow_list_item = document.createElement('h5');
if(player[4] == "AU"){
follow_list_item.innerHTML = `<a href="https://surfheaven.eu/player/${player[0]}">${player[1]}</a> in <a href="https://surfheaven.eu/map/${player[3]}" title="${player[3]}" style="color:rgb(0,255,0)">#${player[2]-13} (AU)</a>`
} else {
follow_list_item.innerHTML = `<a href="https://surfheaven.eu/player/${player[0]}">${player[1]}</a> in <a href="https://surfheaven.eu/map/${player[3]}" title="${player[3]}" style="color:rgb(0,255,0)">#${player[2]}</a>`
}
follow_list_panel_body_div.appendChild(follow_list_item);
}
});
insert_flags_to_profiles();
}
else{
follow_list_panel_body_div.innerHTML = "";
let follow_list_item = document.createElement('h5');
follow_list_item.innerHTML = "No friends online :(";
follow_list_panel_body_div.appendChild(follow_list_item);
}
});
}
}
document.addEventListener('click', (e) => {
if (e.target.tagName == "A" && current_page != "servers") {
insert_flags_to_profiles();
}
});
function create_session_overlay() {
const overlay = document.createElement('div');
overlay.style.position = "fixed";
overlay.style.top = "80px";
overlay.style.left = "50%";
overlay.style.transform = "translateX(-50%)";
overlay.style.zIndex = "9999";
overlay.style.backgroundColor = "#2d3748";
overlay.style.border = "1px solid #4a5568";
overlay.style.borderRadius = "0.5rem";
overlay.style.boxShadow = "0px 2px 15px rgba(0,0,0,0.3)";
overlay.style.padding = "0.5rem";
overlay.style.minWidth = "300px";
overlay.style.color = "#e2e8f0";
const title_bar = document.createElement('div');
title_bar.style.display = "flex";
title_bar.style.justifyContent = "space-between";
title_bar.style.alignItems = "center";
title_bar.style.padding = "0.5rem";
title_bar.style.cursor = "move";
//title_bar.style.borderBottom = "1px solid #4a5568";
const title = document.createElement('h4');
title.textContent = "In session";
title.style.margin = "0";
title.style.padding = "0";
title.style.fontSize = "1.1rem";
title.style.fontWeight = "600";
const stats_button = document.createElement('button');
stats_button.textContent = "Show session stats";
stats_button.style.cursor = "pointer";
stats_button.style.backgroundColor = "transparent";
stats_button.style.border = "1px solid #4a5568";
stats_button.style.borderRadius = "0.25rem";
stats_button.style.color = "#e2e8f0";
stats_button.style.padding = "0.25rem 0.75rem";
stats_button.style.marginRight = "0.5rem";
stats_button.style.transition = "all 0.2s ease";
stats_button.addEventListener('mouseover', () => {
stats_button.style.backgroundColor = "#4a5568";
stats_button.style.borderColor = "#718096";
});
stats_button.addEventListener('mouseout', () => {
stats_button.style.backgroundColor = "transparent";
stats_button.style.borderColor = "#4a5568"
});
const close_button = document.createElement('button');
close_button.innerHTML = "×";
close_button.style.cursor = "pointer";
close_button.style.backgroundColor = "transparent";
close_button.style.border = "1px solid #4a5568";
close_button.style.borderRadius = "0.25rem";
close_button.style.color = "#e2e8f0";
close_button.style.padding = "0.1rem 0.5rem";
close_button.style.transition = "all 0.2s ease";
close_button.addEventListener('mouseover', () => {
close_button.style.backgroundColor = "#4a5568";
close_button.style.borderColor = "#718096";
});
close_button.addEventListener('mouseout', () => {
close_button.style.backgroundColor = "transparent";
close_button.style.borderColor = "#4a5568";
});
title_bar.appendChild(title);
title_bar.appendChild(stats_button);
title_bar.appendChild(close_button);
let is_dragging = false;
let start_x, start_y, initial_left, initial_top;
title_bar.addEventListener('mousedown', (e) => {
if (e.target.tagName === 'BUTTON') return;
is_dragging = true;
const rect = overlay.getBoundingClientRect();
initial_left = rect.left;
initial_top = rect.top;
start_x = e.clientX;
start_y = e.clientY;
document.addEventListener('mousemove', handle_mouse_move);
document.addEventListener('mouseup', () => {
is_dragging = false;
document.removeEventListener('mousemove', handle_mouse_move);
});
});
function handle_mouse_move(e) {
if (!is_dragging) return;
const dx = e.clientX - start_x;
const dy = e.clientY - start_y;
overlay.style.left = `${initial_left + dx}px`;
overlay.style.top = `${initial_top + dy}px`;
overlay.style.transform = 'none';
}
close_button.addEventListener('click', () => {
overlay.remove();
});
overlay.appendChild(title_bar);
document.body.appendChild(overlay);
return {
overlay_element: overlay,
stats_button: stats_button,
close_button: close_button
};
}
function get_rank_in_map(map, id, track = 0, full_response = false, callback) {
let url = "https://api.surfheaven.eu/api/maprecord/" + map + "/" + id + "/" + track;
make_request(url, (data) => {
let rank = 0;
if (data.length > 0) {
if (full_response) {
callback(data[0]);
return;
}
rank = data[0].rank || 0;
}
callback(rank);
});
}
function get_playerinfo(id, callback) {
let url = "https://api.surfheaven.eu/api/playerinfo/" + id;
make_request(url, (data) => {
callback(data[0]);
});
}
function get_mapinfo(map, callback) {
let url = "https://api.surfheaven.eu/api/mapinfo/" + map;
make_request(url, (data) => {
callback(data[0]);
});
}
function display_session_modal(original_session) {
const session = JSON.parse(JSON.stringify(original_session));
let comp_points = 0;
session.starting_points = session.starting_points.map((point) => point ?? 0);
session.starting_ranks = session.starting_ranks.map((rank) => rank ?? 0);
const session_element = document.createElement('div');
session_element.style.minWidth = '300px';
const session_end_time = Date.now();
const session_duration = session_end_time - session.start_time;
const session_length = format_time_noms(Math.floor(session_duration / 1000));
const session_length_element = document.createElement('p');
session_length_element.textContent = `Session Length: ${session_length}`;
session_element.appendChild(session_length_element);
const points_element = document.createElement('p');
// total points from api