-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
154 lines (141 loc) · 4.25 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
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
const PlayerInputGroup = (playerId) => {
return `
<div class="PlayersForm__player-input-group" id="player-${playerId}-input-group">
<h3>Player ${playerId}</h3>
<label>
Name
<input type="text" class="PlayersForm__player-input-name"/>
</label>
<label>
Rank
<select class="PlayersForm__player-input-rank">
<option value="BRONZE">Bronze</option>
<option value="SILVER">Silver</option>
<option value="GOLD">Gold</option>
<option value="PLAT">Plat</option>
<option value="DIAMOND">Diamond</option>
<option value="MASTERS">Masters</option>
<option value="GRANDMASTERS">Grandmasters</option>
<option value="CHALLENGER">Challenger</option>
</select>
</label>
<label>
Primary Role
<select class="PlayersForm__player-input-primary-role">
<option value="TOP">Top</option>
<option value="JUNGLE">Jungle</option>
<option value="MID">Mid</option>
<option value="BOT">Bot</option>
<option value="SUPPORT">Support</option>
<option value="FILL">Fill</option>
</select>
</label>
<label>
Secondary Role
<select class="PlayersForm__player-input-secondary-role">
<option value="TOP">Top</option>
<option value="JUNGLE">Jungle</option>
<option value="MID">Mid</option>
<option value="BOT">Bot</option>
<option value="SUPPORt">Support</option>
</select>
</label>
</div>
`
}
const TeamsTable = (teamOne, teamTwo) => {
let ret = `
<h3>Team 1 (${teamOne.teamValue}):</h3>
<table>
`
Object.entries(teamOne).forEach(([role, player]) => {
if (role === "teamValue")
return;
if (!player) {
ret += `
<tr>
<td>${role}</td>
<td>None</td>
<td>N/A</td>
</tr>
`
} else
ret += `
<tr>
<td>${role}</td>
<td>${player.name}</td>
<td>${player.rank}</td>
</tr>
`;
});
ret += `
</table>
<h3>Team 2 (${teamTwo.teamValue}):</h3>
<table>
`
Object.entries(teamTwo).forEach(([role, player]) => {
if (role === "teamValue")
return;
if (!player) {
ret += `
<tr>
<td>${role}</td>
<td>None</td>
<td>N/A</td>
</tr>
`
} else
ret += `
<tr>
<td>${role}</td>
<td>${player.name}</td>
<td>${player.rank}</td>
</tr>
`;
});
ret += "</table>"
return ret;
};
const teamsTablesWrapper = document.getElementById("team-tables-wrapper");
const playersForm = document.getElementById("players-form");
const playerInputGroups = playersForm.querySelector(".PlayersForm__player-input-groups");
// render player input groups
for (let i = 1; i <= 10; i++)
playerInputGroups.innerHTML += PlayerInputGroup(i);
const generateTeams = (players) => {
players = assignPlayerRankValues(players);
players = sortPlayersByRankValue(players);
const { teamOne, teamTwo } = generateTeamsByRole(players);
const { balancedTeamOne, balancedTeamTwo } = balanceTeamsByRank(
teamOne,
teamTwo
);
// render teams tables
const teamsHTML = TeamsTable(balancedTeamOne, balancedTeamTwo);
teamsTablesWrapper.innerHTML = teamsHTML;
window.scrollTo({top: 0, behavior: 'smooth'});
}
const handlePlayersFormSubmit = (e) => {
e.preventDefault();
const playerInputGroups = document.querySelectorAll(".PlayersForm__player-input-group");
const playersData = [];
playerInputGroups.forEach((inputGroup) => {
const nameEl = inputGroup.querySelector(".PlayersForm__player-input-name");
const rankEl = inputGroup.querySelector(".PlayersForm__player-input-rank");
const primaryRoleEl = inputGroup.querySelector(".PlayersForm__player-input-primary-role");
const secondaryRoleEl = inputGroup.querySelector(".PlayersForm__player-input-secondary-role");
const name = nameEl.value;
const rank = rankEl.value;
const primaryRole = primaryRoleEl.value;
const secondaryRole = secondaryRoleEl.value;
playersData.push({
name,
rank,
primaryRole,
secondaryRole
})
});
generateTeams(playersData);
}
// add event listeners
playersForm.addEventListener("submit", handlePlayersFormSubmit)