-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
94 lines (77 loc) · 3.57 KB
/
index.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
import { retrieveCountersDriver } from './utilities/RetrieveCounters.js';
const calculateBtn = document.querySelector('.calculate');
const selectors = document.querySelectorAll('.select-hero');
const roleSelector = document.getElementById('select-role');
const roleSelectorParent = document.querySelector('.role-select-wrapper');
calculateBtn.addEventListener('click', () => { //Calculate and display counters
//Retrieve the 5 selected heroes from the HTML selectors
let selectedHeroes =[];
selectors.forEach(selector => selectedHeroes.push(selector.value))
selectedHeroes = selectedHeroes.map(hero => hero.replace(/-/g, ' '));
const selectedRole = roleSelector.value;
if(selectedRole === 'none') {
roleSelectorParent.classList.add('please-select');
setTimeout(() => {
alert('Please select a role');
}, 10);
return;
}
roleSelectorParent.classList.remove('please-select');
const counters = retrieveCountersDriver(selectedRole, selectedHeroes)
displayCounters(counters);
})
selectors.forEach(selector => {
selector.addEventListener('change', (e) => {
const parentHeroCard = e.target.closest('.hero-card');
const heroImg = parentHeroCard.querySelector('img');
let heroName = selector.value;
if(heroName) {
if(heroName === 'Soldier:76') heroName = 'Soldier-76';
if(heroName === 'D.Va') heroName = 'Dva';
heroName = heroName.toLowerCase();
heroImg.src = `./assets/hero_icons/${heroName}.png`;
heroImg.alt = heroName;
}else { //No character chosen
heroName = 'default-icon';
heroImg.src = './assets/overwatch-logo.png';
heroImg.alt = 'overwatch-logo';
}
})
})
const displayCounters = (counters) => {
const displayCounterEl = document.querySelector('.display-counters');
displayCounterEl.classList.remove('no-show')
if(counters.length < 1) {
displayCounterEl.innerHTML = '<h1>No counters found</h1>';
return;
}
// Generate the inner HTML string for counters
const countersHTML = counters.map(counter => {
const counterAlt = counter[0]; //Counter name before editing used for display
let counterName = counter[0];
counterName = counterName.replace(' ', '-');
if(counterName === 'D.Va') counterName = 'dva';
if(counterName === 'Soldier:76') counterName = 'soldier-76';
return `
<div class="hero-card">
<img src="./assets/hero_icons/${counterName.toLowerCase()}.png" alt="${counterAlt}">
<h3>${counterAlt}</h3>
<h3>Score: ${counter[1]}</h3>
</div>
`;
}).join(''); // Join the array elements into a single string without commas
displayCounterEl.innerHTML = `
<div class="cards-container">
${countersHTML}
</div>
`;
}
const imgHeroNames = ["ana", "ashe", "baptiste", "bastion", "brigitte", "cassidy", "dva", "doomfist", "echo", "genji", "hanzo", "illari", "junker-queen", "junkrat", "kiriko", "lifeweaver", "lucio", "mauga", "mei", "mercy", "moira", "orisa", "pharah", "ramattra", "reaper", "reinhardt", "roadhog", "sigma", "soldier-76", "sombra", "symmetra", "torbjorn", "tracer", "widowmaker", "winston", "wrecking-ball", "zarya", "zenyatta"];
const preloadHeroImages = () => {
imgHeroNames.forEach(heroName => {
const src = `./assets/hero_icons/${heroName}.png`;
const img = new Image();
img.src = src;
});
}
document.addEventListener('DOMContentLoaded', preloadHeroImages);