-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
729 lines (603 loc) · 21.8 KB
/
app.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
let lftCards = [];
let rgtCards = [];
let midCards = [];
let draggedCard = null;
let dragOrigin = null;
let pickRoutine = [[1, 2, 2, 1], [1, 2, 1]]
let mapRoutine = []
let currentTurn = null;
let randomMaps = [];
let reroll = [1, 1];
document.addEventListener('DOMContentLoaded', function () {
// Prevenir comportamento padrão de arrastar em elementos não-arrastáveis
document.addEventListener('dragstart', function (event) {
if (!event.target.classList.contains('card')) {
event.preventDefault();
}
});
// Adicionar evento para prevenir seleção de texto no container de cards
document.getElementById("cards-container").addEventListener("mousedown", function (event) {
if (event.target === this) {
event.preventDefault();
}
});
});
const maps = [
"alterac-pass",
"battlefield-of-eternity",
// "blackhearts-bay",
"braxis-holdout",
"cursed-hollow",
"dragon-shire",
"garden-of-terror",
"hanamura-temple",
// "haunted-mines",
"infernal-shrines",
"sky-temple",
"tomb-of-the-spider-queen",
"towers-of-doom",
"volskaya-foundry",
// "warhead-junction"
];
function sortHeroesByRole(heroes) {
const roleOrder = ["tank", "ranged", "melee", "healer", "bruiser"];
const sortedHeroes = {
tank: [],
ranged: [],
melee: [],
healer: [],
bruiser: [],
};
heroes.forEach((hero) => {
const role = heroRoles[hero.match(/(?<=,)[^\]]+/)[0]];
sortedHeroes[role].push(hero);
});
let orderedHeroes = [];
roleOrder.forEach((role) => {
orderedHeroes = orderedHeroes.concat(sortedHeroes[role]);
});
return orderedHeroes;
}
function displayTeamCards(teamCards, containerId) {
const sortedCards = sortHeroesByRole(teamCards);
const container = document.getElementById(containerId);
if (containerId === "left-draft-container" || containerId === "right-draft-container") {
// Get existing rows or create them if they don't exist
let topRow = container.querySelector('.draft-row:first-child');
let bottomRow = container.querySelector('.draft-row:last-child');
if (!topRow) {
topRow = document.createElement("div");
topRow.className = "draft-row";
container.appendChild(topRow);
}
if (!bottomRow) {
bottomRow = document.createElement("div");
bottomRow.className = "draft-row";
container.appendChild(bottomRow);
}
// Clear existing cards, keeping placeholders
topRow.querySelectorAll('.card').forEach(card => card.remove());
bottomRow.querySelectorAll('.card').forEach(card => card.remove());
// Ensure there are enough placeholders
while (topRow.children.length < 2) {
const placeholder = document.createElement("div");
placeholder.className = "card-placeholder";
topRow.appendChild(placeholder);
}
while (bottomRow.children.length < 3) {
const placeholder = document.createElement("div");
placeholder.className = "card-placeholder";
bottomRow.appendChild(placeholder);
}
// Add sorted cards, replacing placeholders
for (let i = 0; i < sortedCards.length; i++) {
const talentCode = sortedCards[i];
const card = createTalentCard();
displayTalentImages(talentCode, card, containerId);
card.classList.add('card');
if (i < 2) {
topRow.children[i].replaceWith(card);
} else {
bottomRow.children[i - 2].replaceWith(card);
}
}
} else {
// For cards-container, keep the original layout
container.innerHTML = ""; // Clear previous cards
for (let i = 0; i < sortedCards.length; i++) {
const talentCode = sortedCards[i];
const card = createTalentCard();
displayTalentImages(talentCode, card, containerId);
card.classList.add('card');
container.appendChild(card);
}
}
}
// Function to generate a random integer between min and max
function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
// Function to get a random hero from the heroes array
function getRandomHero() {
const randomIndex = getRandomInt(0, heroes.length - 1);
return heroes[randomIndex];
}
function remove_from_list(list, value) {
for (var i = 0; i < list.length; i++) {
if (list[i] === value) {
list.splice(i, 1);
i--;
}
}
return list;
}
function generateRandomString(usedHeroes) {
let randomHero;
// Generate a random hero until it is not in the usedHeroes array
do {
randomHero = getRandomHero();
} while (usedHeroes.includes(randomHero));
// Add the new hero to the usedHeroes array
usedHeroes.push(randomHero);
// Get the talents of the hero
const heroTalents = heroes_talents[randomHero];
// Generate a random number for each talent
let randomNumbers = "";
for (let i = 0; i < heroTalents.length; i++) {
const maxTalentIndex = heroTalents[i].length;
// Apply the talent limitations
let chosenTalent;
let invalidTalents = [];
do {
chosenTalent = getRandomInt(1, maxTalentIndex);
if (i === 6) {
const Ultimate = randomNumbers[3];
// Check for hero-specific limitations
switch (randomHero) {
case "fenix":
if (Ultimate === "1") {
invalidTalents = [1];
}
break;
case "zeratul":
if (Ultimate === "2") {
invalidTalents = [1];
}
break;
case "varian":
invalidTalents = remove_from_list([1, 2, 3], parseInt(randomNumbers[1]));
break;
case "garrosh":
invalidTalents = remove_from_list([1, 2], parseInt(Ultimate))
if (randomNumbers[0] !== "3") {
invalidTalents += 4;
}
break;
default:
if (randomHero !== "deathwing" && randomHero !== "tracer" && randomHero !== "maiev") {
invalidTalents = remove_from_list([1, 2], parseInt(Ultimate));
}
break;
}
}
} while ((chosenTalent > maxTalentIndex) || (invalidTalents.includes(chosenTalent)));
randomNumbers += chosenTalent;
}
// Return the generated string
return `[T${randomNumbers},${randomHero}]`;
}
// Function to create a talent card element
function createTalentCard() {
const card = document.createElement("div");
card.className = "card";
card.setAttribute("draggable", "true");
card.addEventListener("mousedown", function (event) {
// Prevenir a propagação do evento para evitar seleção de texto
event.stopPropagation();
});
card.addEventListener("dragstart", function (event) {
this.classList.add('dragging');
dragOrigin = this.closest('#left-draft-container, #right-draft-container, #cards-container').id;
draggedCard = this.getAttribute("data-talent-code");
});
card.addEventListener("dragend", function (event) {
this.classList.remove('dragging');
draggedCard = null;
dragOrigin = null;
});
return card;
}
// Functions to get the image source of a talent or hero
function getImageSrc(talent) {
return `abilitytalents/${talent}.png`;
}
function getHeroImageSrc(heroName) {
return `heroportraits/ui_targetportrait_hero_${heroName}.png`;
}
// Function to display the images of a talent code on a card
function displayTalentImages(talentCode, card, containerId) {
const heroName = talentCode.match(/(?<=,)[^\]]+/)[0];
const talents = heroes_talents[heroName];
const talentNumbers = talentCode.match(/\d+/g)[0];
card.innerHTML = "";
// Create the first row of the card
const firstRow = document.createElement("div");
firstRow.className = "firstRow";
card.appendChild(firstRow);
// Add the hero image
const heroImg = document.createElement("img");
heroImg.src = getHeroImageSrc(heroName);
heroImg.alt = heroName;
heroImg.className = "hero";
firstRow.appendChild(heroImg);
// Add the ultimate talent image
const ult = talents[3][(parseInt(talentNumbers[3]) - 1)];
const ultImg = document.createElement("img");
ultImg.src = getImageSrc(ult);
ultImg.alt = ult;
ultImg.className = "ult";
firstRow.appendChild(ultImg);
// Create the container for the other talents
const talentsContainer = document.createElement("div");
talentsContainer.className = "talents";
card.appendChild(talentsContainer);
// Add the other talent images in 2 rows of 3
const talentOrder = [0, 1, 2, 4, 5, 6]; // Skip index 3 (ultimate)
for (let i = 0; i < talentOrder.length; i++) {
const talentIndex = parseInt(talentNumbers[talentOrder[i]]) - 1;
const talent = talents[talentOrder[i]][talentIndex];
const img = document.createElement("img");
img.src = getImageSrc(talent);
img.alt = talent;
talentsContainer.appendChild(img);
}
card.setAttribute("data-talent-code", talentCode);
// Add the role icon only for cards in the cards-container
if (containerId === "cards-container") {
const roleIconContainer = document.createElement("div");
roleIconContainer.className = "role-icon-container";
const roleIcon = document.createElement("img");
roleIcon.src = getRoleImageSrc(heroName);
roleIcon.alt = `${heroRoles[heroName]} role`;
roleIcon.className = "role-icon";
roleIconContainer.appendChild(roleIcon);
card.appendChild(roleIconContainer);
}
// Remover os event listeners existentes, se houver
card.removeEventListener("dragstart", card.dragStartHandler);
card.removeEventListener("dragend", card.dragEndHandler);
// Adicionar novos event listeners
card.dragStartHandler = (event) => {
dragOrigin = containerId;
draggedCard = talentCode;
event.target.classList.add("dragging");
};
card.dragEndHandler = (event) => {
event.target.classList.remove("dragging");
draggedCard = null;
dragOrigin = null;
};
card.addEventListener("dragstart", card.dragStartHandler);
card.addEventListener("dragend", card.dragEndHandler);
}
function getRoleImageSrc(heroName) {
const role = heroRoles[heroName];
return `roleportraits/${role}.png`;
}
function getNeededRoles() {
const requiredRoles = {
'melee': 1,
'healer': 1,
'tank': 1,
'bruiser': 1,
'ranged': 2,
};
const rolesPresent = midCards.map(
(card) => heroRoles[card.match(/(?<=,)[^\]]+/)[0]]
);
const roleCounts = rolesPresent.reduce((acc, role) => {
acc[role] = (acc[role] || 0) + 1;
return acc;
}, {});
const neededRoles = Object.entries(requiredRoles).filter(([role, requiredCount]) => {
const presentCount = roleCounts[role] || 0;
return presentCount < requiredCount;
}).map(([role, _]) => role);
return neededRoles;
}
function midNewCard() {
const usedHeroes = midCards
.filter((card) => card)
.map((card) => card.match(/(?<=,)[^\]]+/)[0]);
const leftHeroes = lftCards
.filter((card) => card)
.map((card) => card.match(/(?<=,)[^\]]+/)[0]);
const rightHeroes = rgtCards
.filter((card) => card)
.map((card) => card.match(/(?<=,)[^\]]+/)[0]);
const allUsedHeroes = usedHeroes.concat(leftHeroes, rightHeroes);
const neededRoles = getNeededRoles()
let newTalentCode;
do {
newTalentCode = generateRandomString(allUsedHeroes);
const newHero = newTalentCode.match(/(?<=,)[^\]]+/)[0];
const newHeroRole = heroRoles[newHero];
if (neededRoles.includes(newHeroRole) || (neededRoles.length === 0)) {
midCards.push(newTalentCode);
displayTeamCards(midCards, "cards-container");
break;
}
} while ((true));
}
// Event listener for the generate button
document.getElementById("btn-lft").addEventListener("click", () => {
if ((reroll[0] > 0) && (currentTurn !== "right")) {
reroll[0] -= 1
const cardsContainer = document.getElementById("cards-container");
cardsContainer.innerHTML = ""; // Clear previous cards
midCards.length = 0
// Generate 5 talent codes
rerollCards()
updateRerollButtons()
}
});
document.getElementById("btn-rgt").addEventListener("click", () => {
if ((reroll[1] > 0) && (currentTurn !== "left")) {
reroll[1] -= 1
const cardsContainer = document.getElementById("cards-container");
cardsContainer.innerHTML = ""; // Clear previous cards
midCards.length = 0
rerollCards()
updateRerollButtons()
}
});
function displayMapIcons(containerId) {
const container = document.getElementById(containerId);
container.innerHTML = ""; // Clear previous content
// Add map-container class
container.classList.add('map-container');
// Display the maps of that index
randomMaps.forEach((index) => {
const mapIcon = document.createElement("img");
mapIcon.src = 'map_icons/' + maps[index] + '.jpg';
mapIcon.alt = `map${index + 1}`;
mapIcon.className = "map-icon";
mapIcon.setAttribute("data-map-index", index);
mapIcon.addEventListener("click", (event) => {
handleMapBan(index);
});
container.appendChild(mapIcon);
});
}
function removeElement(arr, elem) {
return arr.filter(item => item !== elem);
}
function handleMapBan(mapIndex) {
if (randomMaps.length === 1) {
return;
}
randomMaps = removeElement(randomMaps, mapIndex);
displayMapIcons("cards-container");
mapRoutine[0] -= 1;
if (mapRoutine[0] === 0) {
mapRoutine.shift();
currentTurn = currentTurn === 'right' ? 'left' : 'right';
if (mapRoutine.length === 0) {
const cardsContainer = document.getElementById("cards-container");
cardsContainer.innerHTML = ""; // Clear previous cards
cardsContainer.classList.remove('map-container'); // Remove map-container class
midCards.length = 0;
rerollCards();
reroll = [1, 1];
updateRerollButtons();
console.log("finished map phase");
// Display the chosen map
displayChosenMap(randomMaps[0]);
displayTeamCards(midCards, "cards-container");
}
}
updateDraftHighlight();
}
function handleTeamDrop(event, targetDrop) {
event.preventDefault();
if (draggedCard === null) {
return;
}
if (randomMaps.length > 1) {
return;
}
// Check if the current turn matches the target drop container or if the current turn is null
if ((currentTurn === null) || (currentTurn === "left" && targetDrop === "left-draft-container") || (currentTurn === "right" && targetDrop === "right-draft-container")) {
if (targetDrop !== dragOrigin) {
if (targetDrop === "left-draft-container") {
lftCards.push(draggedCard);
displayTeamCards(lftCards, targetDrop);
} else if (targetDrop === "right-draft-container") {
rgtCards.push(draggedCard);
displayTeamCards(rgtCards, targetDrop);
}
if (dragOrigin === "cards-container") {
midCards = midCards.filter((code) => code !== draggedCard);
midNewCard();
displayTeamCards(midCards, dragOrigin);
} else if (dragOrigin === "left-draft-container") {
lftCards = lftCards.filter((code) => code !== draggedCard);
displayTeamCards(lftCards, dragOrigin);
} else if (dragOrigin === "right-draft-container") {
rgtCards = rgtCards.filter((code) => code !== draggedCard);
displayTeamCards(rgtCards, dragOrigin);
}
draggedCard = null;
dragOrigin = null;
if ((currentTurn === null) && (targetDrop != "cards-container")) {
currentTurn = targetDrop === "left-draft-container" ? 'left' : 'right';
}
pickRoutine[0][0] -= 1;
// If current player's pick turn has ended
if (pickRoutine[0][0] === 0) {
// Remove current turn from turn list
pickRoutine[0].shift();
// Invert player turn
currentTurn = targetDrop === "left-draft-container" ? 'right' : targetDrop === "right-draft-container" ? 'left' : currentTurn;
// If a phase ends
if (pickRoutine[0].length === 0) {
// Creates 3 maps
currentTurn = currentTurn === 'right' ? 'left' : 'right';
reroll = [0, 0];
pickRoutine.shift();
if (pickRoutine.length === 0) {
currentTurn = null;
updateRerollButtons();
updateDraftHighlight();
const container = document.getElementById("cards-container");
container.innerHTML = ""; // Clear previous content
return;
} else {
// reroll = [0, 0]
mapRoutine = [1, 1];
while (randomMaps.length < 3) {
let randomNumber = Math.floor(Math.random() * maps.length);
if (!randomMaps.includes(randomNumber)) {
randomMaps.push(randomNumber);
}
}
}
displayMapIcons("cards-container");
console.log("finished first pick phase");
}
}
}
}
updateDraftHighlight();
updateRerollButtons();
}
document.getElementById("left-draft").addEventListener("dragover", (event) => {
event.preventDefault();
});
document.getElementById("right-draft").addEventListener("dragover", (event) => {
event.preventDefault();
});
document.getElementById("cards-container").addEventListener("dragover", (event) => {
event.preventDefault();
});
document.getElementById("left-draft").addEventListener("drop", (event) => {
handleTeamDrop(event, "left-draft-container");
});
document.getElementById("right-draft").addEventListener("drop", (event) => {
handleTeamDrop(event, "right-draft-container");
});
document.getElementById("cards-container").addEventListener("drop", (event) => {
handleTeamDrop(event, "cards-container");
});
async function copyTalents(teamSide) {
try {
const draftContainer = document.getElementById(`${teamSide}-draft-container`);
const cards = Array.from(draftContainer.querySelectorAll('.card'));
const talents = cards.map((card) => card.getAttribute("data-talent-code"));
let message = `${teamSide.charAt(0).toUpperCase() + teamSide.slice(1)} Team - \n`;
talents.forEach((code) => {
if (code) {
message += `:ghostboogie: ${code}\n`;
}
});
await navigator.clipboard.writeText(message);
// Provide visual feedback
const icon = document.getElementById(`${teamSide}-draft-icon`);
const originalSrc = icon.src;
icon.src = "check.svg"; // Assume you have a check icon
setTimeout(() => {
icon.src = originalSrc;
}, 2000);
console.log(`${teamSide} team talents copied to clipboard.`);
} catch (err) {
console.error(`Failed to copy ${teamSide} team talents to clipboard.`, err);
// Provide error feedback
const icon = document.getElementById(`${teamSide}-draft-icon`);
const originalSrc = icon.src;
icon.src = "error.svg"; // Assume you have an error icon
setTimeout(() => {
icon.src = originalSrc;
}, 2000);
}
}
// Update event listeners for the draft icons
document.getElementById("left-draft-icon").addEventListener("click", () => copyTalents("left"));
document.getElementById("right-draft-icon").addEventListener("click", () => copyTalents("right"));
function updateRerollButtons() {
const leftRerollButton = document.getElementById("btn-lft");
const rightRerollButton = document.getElementById("btn-rgt");
// Update left reroll button
if (reroll[0] > 0 && currentTurn !== "right") {
leftRerollButton.style.backgroundColor = "#0eaf9b";
leftRerollButton.style.cursor = "pointer";
} else {
leftRerollButton.style.backgroundColor = "gray";
leftRerollButton.style.cursor = "not-allowed";
}
// leftRerollButton.querySelector(".reroll-count").textContent = reroll[0];
// Update right reroll button
if (reroll[1] > 0 && currentTurn !== "left") {
rightRerollButton.style.backgroundColor = "#0eaf9b";
rightRerollButton.style.cursor = "pointer";
} else {
rightRerollButton.style.backgroundColor = "gray";
rightRerollButton.style.cursor = "not-allowed";
}
//rightRerollButton.querySelector(".reroll-count").textContent = reroll[1];
}
// Call the updateRerollButtons function initially
updateRerollButtons();
// Generate 5 talent codes
function rerollCards() {
for (let i = 0; i < 6; i++) {
midNewCard()
}
displayTeamCards(midCards, "cards-container");
document.getElementById("cards-container").classList.remove('map-container');
}
rerollCards()
// Substitua a função updateDraftHighlight existente por esta versão atualizada
function updateDraftHighlight() {
const body = document.body;
const turnMessage = document.getElementById("turn-message");
// Remova as linhas que manipulam leftTurnText e rightTurnText, pois não serão mais usadas
let message = "";
if (randomMaps.length > 1) { // Fase de banimento de mapas
if (currentTurn === "left") {
message = "Left team turn to ban";
} else if (currentTurn === "right") {
message = "Right team turn to ban";
}
} else { // Fase de escolha de heróis
if (currentTurn === "left") {
message = "Left team turn to pick";
} else if (currentTurn === "right") {
message = "Right team turn to pick";
}
}
if (message) {
turnMessage.textContent = message;
turnMessage.style.display = "block";
} else {
turnMessage.style.display = "none";
}
if (currentTurn === "left") {
body.style.backgroundImage = "linear-gradient(90deg, #22262f 0%, #22262f 25%, #2e222f 35%, #2e222f 100%)";
} else if (currentTurn === "right") {
body.style.backgroundImage = "linear-gradient(270deg, #22262f 0%, #22262f 25%, #2e222f 35%, #2e222f 100%)";
} else {
body.style.backgroundImage = "linear-gradient(90deg, #22262f 0%, #22262f 25%, #2e222f 35%, #2e222f 65%, #22262f 75%, #22262f 100%)";
}
}
// Remova ou comente as seguintes linhas no seu HTML:
// <div id="left-turn-text" style="display: none;">YOUR TURN TO PICK</div>
// <div id="right-turn-text" style="display: none;">YOUR TURN TO PICK</div>
function displayChosenMap(mapIndex) {
const choosenMapContainer = document.getElementById("choosen-map-container");
choosenMapContainer.innerHTML = ""; // Clear previous content
const mapIcon = document.createElement("img");
mapIcon.src = 'map_icons/' + maps[mapIndex] + '.jpg';
mapIcon.alt = `Chosen Map: ${maps[mapIndex]}`;
mapIcon.className = "map-icon";
choosenMapContainer.appendChild(mapIcon);
}