Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
308 lines (269 loc) · 11.6 KB
/
Copy pathscript.js
File metadata and controls
308 lines (269 loc) · 11.6 KB
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
let protocols = [];
let availablePool = [];
let player1Pool = [];
let player2Pool = [];
let draftType = null;
let selectedSets = new Set();
let currentPlayer = 1; // Tracks the current player (1 or 2)
let selectionsRemaining = 0; // Tracks how many selections the current player must make
// Load protocols.json
fetch('protocols.json')
.then(response => response.json())
.then(data => {
protocols = data;
initializeSetSelection();
});
// Initialize set selection
function initializeSetSelection() {
const setCheckboxes = document.getElementById('set-checkboxes');
const sets = new Set(protocols.map(p => p.Set));
// Convert Set to Array and sort alphabetically
const sortedSets = Array.from(sets).sort();
// Clear existing checkboxes first
setCheckboxes.innerHTML = '';
// Create checkboxes in sorted order
sortedSets.forEach(set => {
const label = document.createElement('label');
label.innerHTML = `<input type="checkbox" value="${set}" checked> ${set}`;
setCheckboxes.appendChild(label);
});
// Dynamically display protocols when sets are selected
document.querySelectorAll('#set-checkboxes input').forEach(input => {
input.addEventListener('change', () => {
selectedSets = new Set([...document.querySelectorAll('#set-checkboxes input:checked')].map(input => input.value));
displayProtocols();
});
});
// Display protocols initially (all sets selected by default)
selectedSets = new Set(sortedSets); // Update selectedSets with sorted order
displayProtocols();
// Attach event listener to the dynamically created Confirm Sets button
document.getElementById('confirm-sets').addEventListener('click', confirmSets);
}
// Confirm sets and proceed to drafting process
function confirmSets() {
selectedSets = new Set([...document.querySelectorAll('#set-checkboxes input:checked')].map(input => input.value));
availablePool = protocols.filter(p => selectedSets.has(p.Set));
// Error checking for minimum protocols
if ((draftType === 'standard' && availablePool.length < 7) || (draftType === 'blind-elimination' && availablePool.length < 9)) {
alert(`Not enough protocols in the selected sets. Required: ${draftType === 'standard' ? 7 : 9}`);
return;
}
document.getElementById('set-selection').classList.add('hidden'); // Hide set selection
document.getElementById('protocol-display').classList.add('hidden'); // Hide protocol display
document.getElementById('draft-process').classList.remove('hidden'); // Show drafting process
startDraftProcess(); // Start the drafting process
}
// Event listeners for draft type selection
document.getElementById('standard-draft').addEventListener('click', () => {
draftType = 'standard';
document.getElementById('draft-type').classList.add('hidden'); // Hide draft type selection
document.getElementById('set-selection').classList.remove('hidden'); // Show set selection
document.getElementById('set-selection').innerHTML = `
<h3>Standard Draft - Select Sets</h3>
<div id="set-checkboxes"></div>
<button id="confirm-sets">Confirm Sets</button>
<div id="protocol-cards"></div>
<button id="start-over-button">Start Over</button>
`;
initializeSetSelection(); // Reinitialize set selection
selectedSets = new Set(protocols.map(p => p.Set)); // Default to all sets
displayProtocols(); // Display protocols dynamically
});
document.getElementById('blind-elimination-draft').addEventListener('click', () => {
draftType = 'blind-elimination';
document.getElementById('draft-type').classList.add('hidden'); // Hide draft type selection
document.getElementById('set-selection').classList.remove('hidden'); // Show set selection
document.getElementById('set-selection').innerHTML = `
<h3>Blind Elimination Draft - Select Sets</h3>
<div id="set-checkboxes"></div>
<button id="confirm-sets">Confirm Sets</button>
<div id="protocol-cards"></div>
<button id="start-over-button">Start Over</button>
`;
initializeSetSelection(); // Reinitialize set selection
selectedSets = new Set(protocols.map(p => p.Set)); // Default to all sets
displayProtocols(); // Display protocols dynamically
});
// Display protocols dynamically
function displayProtocols() {
const protocolCards = document.getElementById('protocol-cards');
protocolCards.innerHTML = ''; // Clear previous content
const filteredProtocols = protocols.filter(p => selectedSets.has(p.Set));
filteredProtocols.forEach(protocol => {
const card = document.createElement('div');
card.className = `protocol-card ${protocol.Protocol.toLowerCase().replace(/\s+/g, '-')}`; // Add protocol-specific class
card.innerHTML = `
<strong>${protocol.Protocol}</strong><br>
${protocol.Top}<br>
${protocol.Bottom}<br>
<em><b>Set: ${protocol.Set}</b></em>
`;
protocolCards.appendChild(card);
});
}
// Start the drafting process
function startDraftProcess() {
const draftActions = document.getElementById('draft-actions');
if (draftType === 'standard') {
standardDraft();
} else if (draftType === 'blind-elimination') {
blindEliminationDraft();
}
}
// Standard Draft logic
function standardDraft() {
const draftActions = document.getElementById('draft-actions');
if (currentPlayer === 1) {
if (player1Pool.length === 0) {
selectionsRemaining = 1; // Player 1 selects 1 protocol
} else if (player1Pool.length === 1) {
selectionsRemaining = 2; // Player 1 selects 2 protocols
}
} else if (currentPlayer === 2) {
if (player2Pool.length === 0) {
selectionsRemaining = 2; // Player 2 selects 2 protocols
} else if (player2Pool.length === 2) {
selectionsRemaining = 1; // Player 2 selects 1 protocol
}
}
draftActions.innerHTML = `<p>Player ${currentPlayer}'s turn. <br>Select ${selectionsRemaining} protocol(s):</p>`;
// Display available protocols for selection
availablePool.forEach((protocol, index) => {
const card = document.createElement('div');
card.className = `protocol-card ${protocol.Protocol.toLowerCase().replace(/\s+/g, '-')}`; // Add protocol-specific class
card.innerHTML = `
<strong>${protocol.Protocol}</strong><br>
${protocol.Top}<br>
${protocol.Bottom}<br>
<em><b>Set: ${protocol.Set}</b></em>
`;
// Add click event to select the protocol
card.addEventListener('click', () => {
if (currentPlayer === 1) {
player1Pool.push(protocol); // Add to Player 1's pool
} else {
player2Pool.push(protocol); // Add to Player 2's pool
}
availablePool.splice(index, 1); // Remove from available pool
selectionsRemaining--;
// Check if the current player's turn is complete
if (selectionsRemaining === 0) {
// Switch players
currentPlayer = currentPlayer === 1 ? 2 : 1;
// Check if drafting is complete
if (player1Pool.length + player2Pool.length === 6) {
endDraft();
} else {
standardDraft(); // Continue drafting
}
} else {
standardDraft(); // Continue current player's turn
}
});
draftActions.appendChild(card);
});
}
// Blind Elimination Draft logic
function blindEliminationDraft() {
const draftActions = document.getElementById('draft-actions');
// Randomly eliminate all but 9 protocols
while (availablePool.length > 9) {
const randomIndex = Math.floor(Math.random() * availablePool.length);
availablePool.splice(randomIndex, 1);
}
// Prompt Player 1 to eliminate 1 protocol
if (currentPlayer === 1) {
draftActions.innerHTML = `<p>Player 1's turn.<br> Select 1 protocol to eliminate:</p><br>`;
}
// Prompt Player 2 to eliminate 1 protocol
else if (currentPlayer === 2) {
draftActions.innerHTML = `<p>Player 2's turn.<br> Select 1 protocol to eliminate:</p>`;
}
// Display available protocols for elimination
availablePool.forEach((protocol, index) => {
const card = document.createElement('div');
card.className = `protocol-card ${protocol.Protocol.toLowerCase().replace(/\s+/g, '-')}`; // Add protocol-specific class
card.innerHTML = `
<strong>${protocol.Protocol}</strong><br>
${protocol.Top}<br>
${protocol.Bottom}<br>
<em><b>Set: ${protocol.Set}</b></em>
`;
// Add click event to eliminate the protocol
card.addEventListener('click', () => {
availablePool.splice(index, 1); // Eliminate the selected protocol
currentPlayer = currentPlayer === 1 ? 2 : 1; // Switch players
// Check if elimination phase is complete
if (availablePool.length === 7) {
standardDraft(); // Proceed to standard draft
} else {
blindEliminationDraft(); // Continue elimination
}
});
draftActions.appendChild(card);
});
}
// End the draft and display results
function endDraft() {
const results = document.getElementById('results');
const draftProcess = document.getElementById('draft-process');
// Clear the available pool and hide the draft process UI
availablePool = [];
draftProcess.classList.add('hidden');
// Display results
results.classList.remove('hidden');
results.innerHTML = `
<h3>Draft Results</h3>
<h4>Player 1 Pool</h4>
<div id="player1-pool">
${player1Pool.map(p => `
<div class="protocol-card ${p.Protocol.toLowerCase().replace(/\s+/g, '-')}">
<strong>${p.Protocol}</strong><br>
${p.Top}<br>
${p.Bottom}<br>
<em><b>Set: ${p.Set}</b></em>
</div>
`).join('')}
</div>
<h4>Player 2 Pool</h4>
<div id="player2-pool">
${player2Pool.map(p => `
<div class="protocol-card ${p.Protocol.toLowerCase().replace(/\s+/g, '-')}">
<strong>${p.Protocol}</strong><br>
${p.Top}<br>
${p.Bottom}<br>
<em><b>Set: ${p.Set}</b></em>
</div>
`).join('')}
</div>
<button id="start-over-button">Start Over</button>
`;
}
// Reset the app to its initial state
function resetApp() {
// Reset state variables (except protocols)
availablePool = [];
player1Pool = [];
player2Pool = [];
draftType = null;
selectedSets = new Set();
currentPlayer = 1;
selectionsRemaining = 0;
// Reset the UI
document.getElementById('draft-type').classList.remove('hidden');
document.getElementById('set-selection').classList.add('hidden');
document.getElementById('protocol-display').classList.add('hidden');
document.getElementById('draft-process').classList.add('hidden');
document.getElementById('results').classList.add('hidden');
// Clear the results section's inner HTML
document.getElementById('results').innerHTML = '';
// Reinitialize set selection
initializeSetSelection();
}
// Add event listener for the "Start Over" button
document.addEventListener('click', function (event) {
if (event.target && event.target.id === 'start-over-button') {
resetApp();
}
});