-
Notifications
You must be signed in to change notification settings - Fork 0
/
tactics-selector.user.js
1449 lines (1187 loc) · 43.7 KB
/
tactics-selector.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 MZ Tactics Selector
// @namespace douglaskampl
// @version 7.3
// @description Adds a dropdown menu with overused tactics and lets you save your own tactics for quick access later on.
// @author Douglas Vieira
// @match https://www.managerzone.com/?p=tactics
// @match https://www.managerzone.com/?p=national_teams&sub=tactics&type=*
// @match https://www.managerzone.com/?p=players
// @icon https://www.google.com/s2/favicons?sz=64&domain=managerzone.com
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_addStyle
// @require https://unpkg.com/jssha@3.3.0/dist/sha256.js
// @require https://unpkg.com/i18next@21.6.3/i18next.min.js
// @license MIT
// ==/UserScript==
GM_addStyle(
"@import url('https://fonts.googleapis.com/css2?family=Montserrat&display=swap');"
);
(function () {
"use strict";
let dropdownMenuTactics = [];
const defaultTacticsDataUrl =
"https://raw.githubusercontent.com/douglasdotv/tactics-selector/main/json/tactics.json?callback=?";
let activeLanguage;
const flagsDataUrl = {
gb: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/gb.svg",
br: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/br.svg",
cn: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/cn.svg",
se: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/se.svg",
no: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/no.svg",
dk: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/dk.svg",
ar: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/ar.svg",
pl: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/pl.svg",
nl: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/nl.svg",
id: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/id.svg",
de: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/de.svg",
it: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/it.svg",
fr: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/fr.svg",
ro: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/ro.svg",
tr: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/tr.svg",
kr: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/kr.svg",
ru: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/ru.svg",
sa: "https://raw.githubusercontent.com/lipis/flag-icons/d6785f2434e54e775d55a304733d17b048eddfb5/flags/4x3/sa.svg",
};
const languages = [
{ code: "en", name: "English", flag: flagsDataUrl.gb },
{ code: "pt", name: "Português", flag: flagsDataUrl.br },
{ code: "zh", name: "中文", flag: flagsDataUrl.cn },
{ code: "sv", name: "Svenska", flag: flagsDataUrl.se },
{ code: "no", name: "Norsk", flag: flagsDataUrl.no },
{ code: "da", name: "Dansk", flag: flagsDataUrl.dk },
{ code: "es", name: "Español", flag: flagsDataUrl.ar },
{ code: "pl", name: "Polski", flag: flagsDataUrl.pl },
{ code: "nl", name: "Nederlands", flag: flagsDataUrl.nl },
{ code: "id", name: "Bahasa Indonesia", flag: flagsDataUrl.id },
{ code: "de", name: "Deutsch", flag: flagsDataUrl.de },
{ code: "it", name: "Italiano", flag: flagsDataUrl.it },
{ code: "fr", name: "Français", flag: flagsDataUrl.fr },
{ code: "ro", name: "Română", flag: flagsDataUrl.ro },
{ code: "tr", name: "Türkçe", flag: flagsDataUrl.tr },
{ code: "ko", name: "한국어", flag: flagsDataUrl.kr },
{ code: "ru", name: "Русский", flag: flagsDataUrl.ru },
{ code: "ar", name: "العربية", flag: flagsDataUrl.sa },
];
const strings = {
addButton: "",
deleteButton: "",
renameButton: "",
updateButton: "",
clearButton: "",
resetButton: "",
importButton: "",
exportButton: "",
usefulLinksButton: "",
aboutButton: "",
tacticNamePrompt: "",
addAlert: "",
deleteAlert: "",
renameAlert: "",
updateAlert: "",
clearAlert: "",
resetAlert: "",
deleteConfirmation: "",
updateConfirmation: "",
clearConfirmation: "",
resetConfirmation: "",
invalidTacticError: "",
noTacticNameProvidedError: "",
alreadyExistingTacticNameError: "",
tacticNameLengthError: "",
noTacticSelectedError: "",
duplicateTacticError: "",
modalContentInfoText: "",
modalContentFeedbackText: "",
usefulContent: "",
tacticsDropdownMenuLabel: "",
languageDropdownMenuLabel: "",
};
const elementStringKeys = {
add_tactic_button: "addButton",
delete_tactic_button: "deleteButton",
rename_tactic_button: "renameButton",
update_tactic_button: "updateButton",
clear_tactics_button: "clearButton",
reset_tactics_button: "resetButton",
import_tactics_button: "importButton",
export_tactics_button: "exportButton",
about_button: "aboutButton",
tactics_dropdown_menu_label: "tacticsDropdownMenuLabel",
language_dropdown_menu_label: "languageDropdownMenuLabel",
info_modal_info_text: "modalContentInfoText",
info_modal_feedback_text: "modalContentFeedbackText",
useful_links_button: "usefulLinksButton",
useful_content: "usefulContent",
};
let infoModal;
let usefulLinksModal;
const tacticsBox = document.getElementById("tactics_box");
const tacticsPreset = document.getElementById("tactics_preset");
const outfieldPlayersSelector =
".fieldpos.fieldpos-ok.ui-draggable:not(.substitute):not(.goalkeeper):not(.substitute.goalkeeper), .fieldpos.fieldpos-collision.ui-draggable:not(.substitute):not(.goalkeeper):not(.substitute.goalkeeper)";
const goalkeeperSelector = ".fieldpos.fieldpos-ok.goalkeeper.ui-draggable";
const formationTextSelector = "#formation_text";
const tacticSlotSelector =
".ui-state-default.ui-corner-top.ui-tabs-selected.ui-state-active.invalid";
const minOutfieldPlayers = 10;
const maxTacticNameLength = 50;
async function initialize() {
if (tacticsBox) {
activeLanguage = getActiveLanguage();
i18next
.init({
lng: activeLanguage,
resources: {
[activeLanguage]: {
translation: await (
await fetch(
`https://raw.githubusercontent.com/douglasdotv/tactics-selector/main/json/lang/${activeLanguage}.json?callback=?`
)
).json(),
},
},
})
.then(() => {
const tacticsSelectorDiv = createTacticsSelectorDiv();
if (isFootball()) {
insertAfterElement(tacticsSelectorDiv, tacticsBox);
}
const firstRow = createRow("tactics_selector_div_first_row");
const secondRow = createRow("tactics_selector_div_second_row");
appendChildren(tacticsSelectorDiv, [
firstRow,
secondRow,
createHiddenTriggerButton(),
]);
setupFirstRow();
setupSecondRow();
fetchTacticsFromGMStorage()
.then((data) => {
const tacticsDropdownMenu = document.getElementById(
"tactics_dropdown_menu"
);
dropdownMenuTactics = data.tactics;
dropdownMenuTactics.sort((a, b) => {
return a.name.localeCompare(b.name);
});
addTacticsToDropdownMenu(
tacticsDropdownMenu,
dropdownMenuTactics
);
tacticsDropdownMenu.addEventListener("change", function () {
handleTacticsSelection(this.value);
});
})
.catch((err) => {
console.error("Couldn't fetch data from json: ", err);
});
setModals();
updateTranslation();
});
}
applyUxxFilter();
}
window.addEventListener("load", function () {
initialize().catch((err) => {
console.error("Init error: ", err);
});
});
// _____Tactics Dropdown Menu_____
function createTacticsDropdownMenu() {
const dropdown = document.createElement("select");
setupDropdownMenu(dropdown, "tactics_dropdown_menu");
appendChildren(dropdown, [createPlaceholderOption()]);
return dropdown;
}
function createHiddenTriggerButton() {
/*
* The purpose of this button is to trigger the tactics preset change event: it changes the tactic to the default 5-3-2.
* It's a workaround to put 10 players on the field when the user has less than 10 players on the field and selects a tactic.
* If that happens, the pitch will be filled with 10 players in a 5-3-2 formation, then they will be rearranged to the selected tactic.
*/
const button = document.createElement("button");
button.id = "hidden_trigger_button";
button.textContent = "";
button.style.visibility = "hidden";
button.addEventListener("click", function () {
tacticsPreset.value = "5-3-2";
tacticsPreset.dispatchEvent(new Event("change"));
});
return button;
}
async function fetchTacticsFromGMStorage() {
const storedTactics = GM_getValue("ls_tactics");
if (storedTactics) {
return storedTactics;
} else {
const jsonTactics = await fetchTacticsFromJson();
storeTacticsInGMStorage(jsonTactics);
return jsonTactics;
}
}
async function fetchTacticsFromJson() {
const response = await fetch(defaultTacticsDataUrl);
return await response.json();
}
function storeTacticsInGMStorage(data) {
GM_setValue("ls_tactics", data);
}
function addTacticsToDropdownMenu(dropdown, tactics) {
for (const tactic of tactics) {
const option = document.createElement("option");
option.value = tactic.name;
option.text = tactic.name;
dropdown.appendChild(option);
}
}
function handleTacticsSelection(tactic) {
const outfieldPlayers = Array.from(
document.querySelectorAll(outfieldPlayersSelector)
);
const selectedTactic = dropdownMenuTactics.find(
(tacticData) => tacticData.name === tactic
);
if (selectedTactic) {
if (outfieldPlayers.length < minOutfieldPlayers) {
const hiddenTriggerButton = document.getElementById(
"hidden_trigger_button"
);
hiddenTriggerButton.click();
setTimeout(() => rearrangePlayers(selectedTactic.coordinates), 1);
} else {
rearrangePlayers(selectedTactic.coordinates);
}
}
}
function rearrangePlayers(coordinates) {
const outfieldPlayers = Array.from(
document.querySelectorAll(outfieldPlayersSelector)
);
findBestPositions(outfieldPlayers, coordinates);
for (let i = 0; i < outfieldPlayers.length; ++i) {
outfieldPlayers[i].style.left = coordinates[i][0] + "px";
outfieldPlayers[i].style.top = coordinates[i][1] + "px";
removeCollision(outfieldPlayers[i]);
}
removeTacticSlotInvalidStatus();
updateFormationText(getFormation(coordinates));
}
function findBestPositions(players, coordinates) {
players.sort((a, b) => parseInt(a.style.top) - parseInt(b.style.top));
coordinates.sort((a, b) => a[1] - b[1]);
}
function removeCollision(player) {
if (player.classList.contains("fieldpos-collision")) {
player.classList.remove("fieldpos-collision");
player.classList.add("fieldpos-ok");
}
}
function removeTacticSlotInvalidStatus() {
const slot = document.querySelector(tacticSlotSelector);
if (slot) {
slot.classList.remove("invalid");
}
}
function updateFormationText(formation) {
const formationTextElement = document.querySelector(formationTextSelector);
formationTextElement.querySelector(".defs").textContent =
formation.defenders;
formationTextElement.querySelector(".mids").textContent =
formation.midfielders;
formationTextElement.querySelector(".atts").textContent =
formation.strikers;
}
function getFormation(coordinates) {
let strikers = 0;
let midfielders = 0;
let defenders = 0;
for (const coo of coordinates) {
const y = coo[1];
if (y < 103) {
strikers++;
} else if (y <= 204) {
midfielders++;
} else {
defenders++;
}
}
return { strikers, midfielders, defenders };
}
// _____Add new tactic_____
function createAddNewTacticButton() {
const button = document.createElement("button");
setupButton(button, "add_tactic_button", strings.addButton);
button.addEventListener("click", function () {
addNewTactic().catch(console.error);
});
return button;
}
async function addNewTactic() {
const outfieldPlayers = Array.from(
document.querySelectorAll(outfieldPlayersSelector)
);
const tacticsDropdownMenu = document.getElementById(
"tactics_dropdown_menu"
);
const tacticCoordinates = outfieldPlayers.map((player) => [
parseInt(player.style.left),
parseInt(player.style.top),
]);
if (!validateTacticPlayerCount(outfieldPlayers)) {
return;
}
const tacticId = generateUniqueId(tacticCoordinates);
const isDuplicate = await validateDuplicateTactic(tacticId);
if (isDuplicate) {
alert(strings.duplicateTacticError);
return;
}
const tacticName = prompt(strings.tacticNamePrompt);
const isValidName = await validateTacticName(tacticName);
if (!isValidName) {
return;
}
const tactic = {
name: tacticName,
coordinates: tacticCoordinates,
id: tacticId,
};
saveTacticToStorage(tactic).catch(console.error);
addTacticsToDropdownMenu(tacticsDropdownMenu, [tactic]);
dropdownMenuTactics.push(tactic);
tacticsDropdownMenu.value = tactic.name;
handleTacticsSelection(tactic.name);
alert(strings.addAlert.replace("{}", tactic.name));
}
function validateTacticPlayerCount(outfieldPlayers) {
const isGoalkeeper = document.querySelector(goalkeeperSelector);
outfieldPlayers = outfieldPlayers.filter(
(player) => !player.classList.contains("fieldpos-collision")
);
if (outfieldPlayers.length < minOutfieldPlayers || !isGoalkeeper) {
alert(strings.invalidTacticError);
return false;
}
return true;
}
async function validateDuplicateTactic(id) {
const tacticsData = (await GM_getValue("ls_tactics")) || { tactics: [] };
return tacticsData.tactics.some((tactic) => tactic.id === id);
}
async function validateTacticName(name) {
if (!name) {
alert(strings.noTacticNameProvidedError);
return false;
}
const tacticsData = (await GM_getValue("ls_tactics")) || { tactics: [] };
if (tacticsData.tactics.some((t) => t.name === name)) {
alert(strings.alreadyExistingTacticNameError);
return false;
}
if (name.length > maxTacticNameLength) {
alert(strings.tacticNameLengthError);
return false;
}
return true;
}
async function saveTacticToStorage(tactic) {
const tacticsData = (await GM_getValue("ls_tactics")) || { tactics: [] };
tacticsData.tactics.push(tactic);
await GM_setValue("ls_tactics", tacticsData);
}
// _____Delete tactic_____
function createDeleteTacticButton() {
const button = document.createElement("button");
setupButton(button, "delete_tactic_button", strings.deleteButton);
button.addEventListener("click", function () {
deleteTactic().catch(console.error);
});
return button;
}
async function deleteTactic() {
const tacticsDropdownMenu = document.getElementById(
"tactics_dropdown_menu"
);
const selectedTactic = dropdownMenuTactics.find(
(tactic) => tactic.name === tacticsDropdownMenu.value
);
if (!selectedTactic) {
alert(strings.noTacticSelectedError);
return;
}
const confirmed = confirm(
strings.deleteConfirmation.replace("{}", selectedTactic.name)
);
if (!confirmed) {
return;
}
const tacticsData = (await GM_getValue("ls_tactics")) || { tactics: [] };
tacticsData.tactics = tacticsData.tactics.filter(
(tactic) => tactic.id !== selectedTactic.id
);
await GM_setValue("ls_tactics", tacticsData);
dropdownMenuTactics = dropdownMenuTactics.filter(
(tactic) => tactic.id !== selectedTactic.id
);
const selectedOption = Array.from(tacticsDropdownMenu.options).find(
(option) => option.value === selectedTactic.name
);
tacticsDropdownMenu.remove(selectedOption.index);
if (tacticsDropdownMenu.options[0]?.disabled) {
tacticsDropdownMenu.selectedIndex = 0;
}
alert(strings.deleteAlert.replace("{}", selectedTactic.name));
}
// _____Rename tactic_____
function createRenameTacticButton() {
const button = document.createElement("button");
setupButton(button, "rename_tactic_button", strings.renameButton);
button.addEventListener("click", function () {
renameTactic().catch(console.error);
});
return button;
}
async function renameTactic() {
const tacticsDropdownMenu = document.getElementById(
"tactics_dropdown_menu"
);
const selectedTactic = dropdownMenuTactics.find(
(tactic) => tactic.name === tacticsDropdownMenu.value
);
if (!selectedTactic) {
alert(strings.noTacticSelectedError);
return;
}
const oldName = selectedTactic.name;
const newName = prompt(strings.tacticNamePrompt);
const isValidName = await validateTacticName(newName);
if (!isValidName) {
return;
}
const selectedOption = Array.from(tacticsDropdownMenu.options).find(
(option) => option.value === selectedTactic.name
);
const tacticsData = (await GM_getValue("ls_tactics")) || { tactics: [] };
tacticsData.tactics = tacticsData.tactics.map((tactic) => {
if (tactic.id === selectedTactic.id) {
tactic.name = newName;
}
return tactic;
});
await GM_setValue("ls_tactics", tacticsData);
dropdownMenuTactics = dropdownMenuTactics.map((tactic) => {
if (tactic.id === selectedTactic.id) {
tactic.name = newName;
}
return tactic;
});
selectedOption.value = newName;
selectedOption.textContent = newName;
const replacements = [oldName, newName];
alert(strings.renameAlert.replace(/\{\}/g, () => replacements.shift()));
}
// _____Update tactic_____
function createUpdateTacticButton() {
const button = document.createElement("button");
setupButton(button, "update_tactic_button", strings.updateButton);
button.addEventListener("click", function () {
updateTactic().catch(console.error);
});
return button;
}
async function updateTactic() {
const outfieldPlayers = Array.from(
document.querySelectorAll(outfieldPlayersSelector)
);
const tacticsDropdownMenu = document.getElementById(
"tactics_dropdown_menu"
);
const selectedTactic = dropdownMenuTactics.find(
(tactic) => tactic.name === tacticsDropdownMenu.value
);
if (!selectedTactic) {
alert(strings.noTacticSelectedError);
return;
}
const updatedCoordinates = outfieldPlayers.map((player) => [
parseInt(player.style.left),
parseInt(player.style.top),
]);
const newId = generateUniqueId(updatedCoordinates);
const tacticsData = (await GM_getValue("ls_tactics")) || { tactics: [] };
const validationOutcome = await validateDuplicateTacticWithUpdatedCoord(
newId,
selectedTactic,
tacticsData
);
switch (validationOutcome) {
case "unchanged":
return;
case "duplicate":
alert(strings.duplicateTacticError);
return;
case "unique":
break;
default:
return;
}
const confirmed = confirm(
strings.updateConfirmation.replace("{}", selectedTactic.name)
);
if (!confirmed) {
return;
}
for (const tactic of tacticsData.tactics) {
if (tactic.id === selectedTactic.id) {
tactic.coordinates = updatedCoordinates;
tactic.id = newId;
}
}
for (const tactic of dropdownMenuTactics) {
if (tactic.id === selectedTactic.id) {
tactic.coordinates = updatedCoordinates;
tactic.id = newId;
}
}
await GM_setValue("ls_tactics", tacticsData);
alert(strings.updateAlert.replace("{}", selectedTactic.name));
}
async function validateDuplicateTacticWithUpdatedCoord(
newId,
selectedTac,
tacticsData
) {
if (newId === selectedTac.id) {
return "unchanged";
} else if (tacticsData.tactics.some((tac) => tac.id === newId)) {
return "duplicate";
} else {
return "unique";
}
}
// _____Clear tactics_____
function createClearTacticsButton() {
const button = document.createElement("button");
setupButton(button, "clear_tactics_button", strings.clearButton);
button.addEventListener("click", function () {
clearTactics().catch(console.error);
});
return button;
}
async function clearTactics() {
const confirmed = confirm(strings.clearConfirmation);
if (!confirmed) {
return;
}
await GM_setValue("ls_tactics", { tactics: [] });
dropdownMenuTactics = [];
const tacticsDropdownMenu = document.getElementById(
"tactics_dropdown_menu"
);
tacticsDropdownMenu.innerHTML = "";
tacticsDropdownMenu.disabled = true;
alert(strings.clearAlert);
}
// _____Reset default settings_____
function createResetTacticsButton() {
const button = document.createElement("button");
setupButton(button, "reset_tactics_button", strings.resetButton);
button.addEventListener("click", function () {
resetTactics().catch(console.error);
});
return button;
}
async function resetTactics() {
const confirmed = confirm(strings.resetConfirmation);
if (!confirmed) {
return;
}
const response = await fetch(defaultTacticsDataUrl);
const data = await response.json();
const defaultTactics = data.tactics;
await GM_setValue("ls_tactics", { tactics: defaultTactics });
dropdownMenuTactics = defaultTactics;
const tacticsDropdownMenu = document.getElementById(
"tactics_dropdown_menu"
);
tacticsDropdownMenu.innerHTML = "";
tacticsDropdownMenu.appendChild(createPlaceholderOption());
addTacticsToDropdownMenu(tacticsDropdownMenu, dropdownMenuTactics);
tacticsDropdownMenu.disabled = false;
alert(strings.resetAlert);
}
// _____Import/Export_____
function createImportTacticsButton() {
const button = document.createElement("button");
setupButton(button, "import_tactics_button", strings.importButton);
button.addEventListener("click", function () {
importTactics().catch(console.error);
});
return button;
}
function createExportTacticsButton() {
const button = document.createElement("button");
setupButton(button, "export_tactics_button", strings.exportButton);
button.addEventListener("click", exportTactics);
return button;
}
async function importTactics() {
const input = document.createElement("input");
input.type = "file";
input.accept = ".json";
input.onchange = async function (event) {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = async function (event) {
const importedTactics = JSON.parse(event.target.result).tactics;
let existingTactics = await GM_getValue("ls_tactics", { tactics: [] });
existingTactics = existingTactics.tactics;
const mergedTactics = [...existingTactics];
for (const importedTactic of importedTactics) {
if (
!existingTactics.some((tactic) => tactic.id === importedTactic.id)
) {
mergedTactics.push(importedTactic);
}
}
await GM_setValue("ls_tactics", { tactics: mergedTactics });
mergedTactics.sort((a, b) => a.name.localeCompare(b.name));
dropdownMenuTactics = mergedTactics;
const tacticsDropdownMenu = document.getElementById(
"tactics_dropdown_menu"
);
tacticsDropdownMenu.innerHTML = "";
tacticsDropdownMenu.append(createPlaceholderOption());
addTacticsToDropdownMenu(tacticsDropdownMenu, dropdownMenuTactics);
tacticsDropdownMenu.disabled = false;
};
reader.readAsText(file);
};
input.click();
}
function exportTactics() {
const tactics = GM_getValue("ls_tactics", { tactics: [] });
const tacticsJson = JSON.stringify(tactics);
const blob = new Blob([tacticsJson], { type: "application/json" });
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = "tactics.json";
link.click();
URL.revokeObjectURL(url);
}
// _____Useful Links Button_____
function createUsefulLinksButton() {
const button = document.createElement("button");
setupButton(button, "useful_links_button", strings.usefulLinksButton);
button.addEventListener("click", function (event) {
event.stopPropagation();
toggleModal(usefulLinksModal);
});
return button;
}
function createUsefulLinksModal() {
const modal = document.createElement("div");
setupModal(modal, "useful_links_modal");
const modalContent = createUsefulLinksModalContent();
modal.appendChild(modalContent);
return modal;
}
function createUsefulLinksModalContent() {
const modalContent = document.createElement("div");
styleModalContent(modalContent);
const usefulContent = createUsefulContent();
const hrefs = new Map([
["gewlaht - BoooM", "https://www.managerzone.com/?p=forum&sub=topic&topic_id=11415137&forum_id=49&sport=soccer"],
["honken91 - taktikskola", "https://www.managerzone.com/?p=forum&sub=topic&topic_id=12653892&forum_id=4&sport=soccer"],
["peto - mix de dibujos", "https://www.managerzone.com/?p=forum&sub=topic&topic_id=12196312&forum_id=255&sport=soccer"],
["The Zone Chile", "https://www.managerzone.com/thezone/paper.php?paper_id=18036&page=9&sport=soccer"],
]);
const usefulLinksList = createLinksList(hrefs);
modalContent.appendChild(usefulContent);
modalContent.appendChild(usefulLinksList);
return modalContent;
}
function createUsefulContent() {
const usefulContent = document.createElement("p");
usefulContent.id = "useful_content";
usefulContent.textContent = strings.usefulContent;
return usefulContent;
}
function createLinksList(hrefs) {
const list = document.createElement("ul");
hrefs.forEach((href, title) => {
const listItem = document.createElement("li");
const link = document.createElement("a");
link.href = href;
link.textContent = title;
listItem.appendChild(link);
list.appendChild(listItem);
});
return list;
}
function setUsefulLinksModal() {
usefulLinksModal = createUsefulLinksModal();
document.body.appendChild(usefulLinksModal);
}
// _____About button_____
function createAboutButton() {
const button = document.createElement("button");
setupButton(button, "about_button", strings.aboutButton);
button.addEventListener("click", function (event) {
event.stopPropagation();
toggleModal(infoModal);
});
return button;
}
function createInfoModal() {
const modal = document.createElement("div");
setupModal(modal, "info_modal");
const modalContent = createModalContent();
modal.appendChild(modalContent);
return modal;
}
function createModalContent() {
const modalContent = document.createElement("div");
styleModalContent(modalContent);
const title = createTitle();
const infoText = createInfoText();
const feedbackText = createFeedbackText();
modalContent.appendChild(title);
modalContent.appendChild(infoText);
modalContent.appendChild(feedbackText);
return modalContent;
}
function createTitle() {
const title = document.createElement("h2");
title.id = "info_modal_title";
title.textContent = "MZ Tactics Selector";
title.style.fontSize = "24px";
title.style.fontWeight = "bold";
title.style.marginBottom = "20px";
return title;
}
function createInfoText() {
const infoText = document.createElement("p");
infoText.id = "info_modal_info_text";
infoText.innerHTML = strings.modalContentInfoText;
return infoText;
}
function createFeedbackText() {
const feedbackText = document.createElement("p");
feedbackText.id = "info_modal_feedback_text";
feedbackText.innerHTML = strings.modalContentFeedbackText;
return feedbackText;
}
function setInfoModal() {
infoModal = createInfoModal();
document.body.appendChild(infoModal);
}
// _____Audio button_____
const createAudioButton = () => {
const button = document.createElement("button");
setupButton(button, "audio_button", "🔊");
const audioUrls = [
"https://ia802609.us.archive.org/16/items/w-w-w-d-e-e-p-d-i-v-e-c-o-m-wg7bkk/Webinar%E2%84%A2%20-%20w%20w%20w%20.%20d%20e%20e%20p%20d%20i%20v%20e%20.%20c%20o%20m%20-%2005%20URL%20%E6%B9%96.mp3",
"https://ia802306.us.archive.org/15/items/remember-this-night-w6kvvl/COSMIC%20CYCLER%20-%20Remember%20This%20Night%20-%2009%20Night%20Breeze.mp3",
"https://ia802300.us.archive.org/32/items/01.-dj-mixed-by-atsushi-ohara/Ridge%20Racer%20Soundtrack%20Collection/2006%20-%20Ridge%20Racers%202%20Direct%20Audio/11.%20Quiet%20Curves.mp3",
"https://ia801701.us.archive.org/18/items/architectureintokyo-singles2013-2018/architecture%20in%20tokyo%20-%20singles%20%282013-2018%29%20-%2005%20marble%20%28ft.%20ULTRA%20%E3%82%A6%E3%83%AB%E3%83%88%E3%83%A9%29.mp3",
"https://ia802306.us.archive.org/28/items/remember-this-night-w9vygv/COSMIC%20CYCLER%20-%20Remember%20This%20Night%20-%2006%20Living%20in%20Your%20Eyes.mp3",
"https://ia801403.us.archive.org/24/items/ElisTom1AguasDeMarco/ElisTom1Aguas%20de%20marco.mp3",
"https://ia800103.us.archive.org/18/items/azzahradibaku_gmail_Gana/Elis%20Regina%20e%20Adoniran%20Barbosa%20-%20Tiro%20ao%20%C3%81lvaro.mp3",
"https://ia601404.us.archive.org/29/items/flying-beagle/The%20Second%20Summer.mp3",
"https://ia801001.us.archive.org/25/items/101AirLaFemmeDargent/108-air_-_ce_matin_la.mp3",
"https://ia804500.us.archive.org/7/items/special-night-w9vkyb/Cosmic%20Cycler%20-%20Special%20Night%20-%2003%20Trying%20to%20Relax.mp3",
];
const audios = audioUrls.map(url => new Audio(url));
let isPlaying = false;
let currentAudio = null;
button.addEventListener("click", function () {