-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
235 lines (196 loc) · 7.56 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
//#region Player Class
class Player {
constructor(name, skillLevel) {
this.name = name;
this.skillLevel = skillLevel;
}
}
//#endregion
//#region DOM Element retrieval
const playerListDiv = document.getElementById("playerList");
const btnAddPlayer = document.getElementById("btnAddPlayer");
const btnGetTeams = document.getElementById("btnGetTeams");
const team1Table = document.getElementById("table1");
const team2Table = document.getElementById("table2");
const outputTables = document.getElementById("outputTables");
let classNames = Array.from(btnGetTeams.classList);
//#endregion
//#region Initial Stats
let playerCount = 1;
const team1 = [];
const team2 = [];
let totalSkillTeam1 = 0;
let totalSkillTeam2 = 0;
const MINIMUM_PLAYER_COUNT = 3;
//#endregion
function addPlayerFields() {
//#region Build InputGroup String
const inputGroup = `<div class="row justify-content-md-center">
<div class="col col-md-6 col-sm-12">
<div class="input-group mb-6 p-1">
<span class="input-group-text">${playerCount}</span>
<input type="text" placeholder="Player Name" class="form-control"
aria-label="Text input with dropdown button">
<select class="form-select" aria-label="Default select example">
<option value="default" selected>Player Skill Level</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
</div>
</div>`;
//#endregion
//#region Prepend element to DOM
let row = document.createElement('div');
row.innerHTML = inputGroup;
row = row.firstChild;
// playerListDiv.appendChild(row);
playerListDiv.prepend(row);
//#endregion
playerCount++;
if (playerCount >= MINIMUM_PLAYER_COUNT && classNames.includes('d-none')) btnGetTeams.classList.remove("d-none");
}
function validatePlayerInputs() {
const inputs = document.querySelectorAll('input[type="text"]');
const selects = document.querySelectorAll('select');
// Check if all input fields are not empty
for (const input of inputs) {
if (input.value.trim() === '') return false;
}
// Check if all select elements have a selected value other than the default value
for (const select of selects) {
if (select.value === 'default') return false;
}
return true;
}
function getPlayersFromInput() {
const playerElements = document.querySelectorAll('#playerList .input-group');
const players = [];
playerElements.forEach(playerElement => {
const playerName = playerElement.querySelector('input[type="text"]').value;
const level = parseInt(playerElement.querySelector('select').value);
const player = new Player(playerName, level);
players.push(player);
});
return players;
}
// Skill First Approach
function distributeTeams(players) {
players.sort((a, b) => b.skillLevel - a.skillLevel);
// TODO: what if skills are: [3,3,4,10]
// Count or Fairly distributed skills?
for (let i = 0; i < players.length; i++) {
const currentPlayer = players[i];
if (totalSkillTeam1 <= totalSkillTeam2) {
team1.push(currentPlayer);
totalSkillTeam1 += currentPlayer.skillLevel;
} else {
team2.push(currentPlayer);
totalSkillTeam2 += currentPlayer.skillLevel;
}
}
}
function printTeams() {
team1Table.innerHTML = generateTeamTable(team1, totalSkillTeam1);
team2Table.innerHTML = generateTeamTable(team2, totalSkillTeam2);
}
function generateTeamTable(team, skillTotal) {
let rows = '';
for (const [index, player] of team.entries()) {
rows += `<tr>
<th scope="row">${index + 1}</th>
<td>${player.name}</td>
<td>${player.skillLevel}</td>
</tr>`;
}
let table = `<table class="table table-striped">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Player</th>
<th scope="col">Skill Level</th>
</tr>
</thead>
<tbody>
${rows}
</tbody>
<tfoot>
<tr>
<td colspan="2"></td>
<td class="text-danger fw-bold">Total Skill: ${skillTotal}</td>
</tr>
</tfoot>
</table>`;
return table;
}
function resetGameStats() {
team1.length = 0;
team2.length = 0;
totalSkillTeam1 = 0;
totalSkillTeam2 = 0;
team1Table.innerHTML = '';
team2Table.innerHTML = '';
}
function processTeamDistribution() {
resetGameStats();
let areInputsValid = validatePlayerInputs();
if (!areInputsValid) {
var myModal = new bootstrap.Modal(document.getElementById('myModal'));
myModal.show();
return;
}
let playerList = getPlayersFromInput();
let median = Math.floor(playerList.length / 2);
let firstPart = playerList.slice(0, median);
let secondPart = playerList.slice(median);
const teams = balanceTeamsBySkillLevel(firstPart, secondPart);
// console.log({ output});
const teamA = teams[0];
const teamB = teams[1];
for (const p of teamA) {
team1.push(p);
totalSkillTeam1 += p.skillLevel;
}
for (const p of teamB) {
team2.push(p);
totalSkillTeam2 += p.skillLevel;
}
// distributeTeams(playerList);
printTeams();
outputTables.scrollIntoView();
}
// Gets evenly distributed teams based on skill level
// This is where all the magic happens
function balanceTeamsBySkillLevel(arr1, arr2) {
let diff = Math.abs(
arr1.reduce((a, b) => a + b.skillLevel, 0) -
arr2.reduce((a, b) => a + b.skillLevel, 0)
);
for (let i = 0; i < arr1.length; i++) {
for (let j = 0; j < arr2.length; j++) {
let newDiff = Math.abs(
arr1.reduce((a, b) => a + b.skillLevel, 0) - arr1[i].skillLevel + arr2[j].skillLevel -
(arr2.reduce((a, b) => a + b.skillLevel, 0) - arr2[j].skillLevel + arr1[i].skillLevel)
);
if (newDiff < diff) {
let temp1 = arr1[i];
let temp2 = arr2[j];
arr1[i] = temp2;
arr2[j] = temp1;
return balanceTeamsBySkillLevel(arr1, arr2);
}
}
}
return [arr1, arr2];
}
// Event handlers
btnAddPlayer.addEventListener("click", addPlayerFields);
btnGetTeams.addEventListener("click", processTeamDistribution);