-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
333 lines (305 loc) · 9.37 KB
/
index.js
File metadata and controls
333 lines (305 loc) · 9.37 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
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
// Initializing player list
let players = [];
const specialCards = [
"Queen", // runner up (2nd)
"Jack", // Trailer (Last)
"Joker", // Choice
"King", // First
"Ace", // Pass to next player
"Mirror", // Reverse
"Bomb"
];
const specialCardsRules = [
{
card: "Queen: <i>Patience</i>",
description: `
<p>Place player behind the first player</p>
`,
},
{
card: "Jack: <i>Trailer</i>",
description: `
<p>
Place player behind the last place player. If that would cause
collision, place them in spot 1.
</p>
`,
},
{
card: "Joker: <i>Choice</i>",
description: `
<p>Player chooses the draft position they would like.</p>
`,
},
{
card: "King: <i>Coup</i>",
description: `
<p>
Place player in spot 1. If that would cause a collision, remove the
existing player from the board.
</p>
`,
},
{
card: "Ace: <i>Pass</i>",
description: `
<p>Skip the player's turn.</p>
`,
},
{
card: "Mirror: <i>Reverse</i>",
description: `
<p>Reverse the order of the players. Player who drew draws again.</p>
`,
},
{
card: "Bomb: <i>Boom</i>",
description: `
<p>
Remove all players from the board and place them back in the queue.
</p>
`,
},
];
document.getElementById("rulesList").innerHTML = specialCardsRules
.map((card) => `<li><h4>${card.card}</h4>${card.description}</li>`)
.join("");
let movedPlayers = [];
let cards = [];
let playerSlots = Array(players.length).fill(null);
let ledger = [];
function shuffle() {
cards = [...specialCards];
for (let i = 1; i <= players.length; i++) {
cards.push(i);
}
}
shuffle();
function addPlayer() {
const playerNameInput = document.getElementById("playerNameInput");
const playerName = playerNameInput.value.trim();
if (playerName) {
players.push(playerName);
playerNameInput.value = ""; // Clear the input field
populateLists(); // Update the player list display
createTableSpaces(players.length); // Update the table spaces
shuffle();
playerSlots = Array(players.length).fill(null);
}
}
// Populate the initial list of players
function populateLists() {
document.getElementById("remainingPlayersList").innerHTML = players
.map((player) => `<li>${player}</li>`)
.join("");
document.getElementById("movedPlayersList").innerHTML = movedPlayers
.map((player) => `<li>${player}</li>`)
.join("");
if (players.length === 0) {
document.querySelector(".active-player").textContent = `✨ Finished ✨`;
} else {
document.querySelector(".active-player").textContent = `Active Player: ✨${
players[0] || "None"
}✨`;
}
document.getElementById("ledger").innerHTML = ledger
.map((entry) => `<li>${entry}</li>`)
.join("");
}
// Randomize players list
function randomizePlayers() {
players.sort(() => Math.random() - 0.5);
populateLists();
}
function drawCard() {
if (players.length === 0) return;
if (cards.length === 0) {
alert("No more cards left in the deck. Shuffling...");
shuffle();
}
const randomIndex = Math.floor(Math.random() * cards.length);
const drawnCard = cards[randomIndex];
cards.splice(randomIndex, 1); // Remove the drawn card from deck
// Get active player and move them to moved list
let activePlayer = players.shift();
ledger.push(`${activePlayer} drew ${drawnCard}`);
movedPlayers.push(activePlayer);
document.getElementById("mostRecentCard").textContent = `${drawnCard}`;
if (typeof drawnCard !== "number") {
document.getElementById("card").classList.add("specialCard");
} else {
document.getElementById("card").classList.remove("specialCard");
}
handleCard(drawnCard, activePlayer);
populateLists();
}
function handleCard(card, player) {
// check if card is a number or a string
if (typeof card === "number") {
bumpAndAssign(card - 1, player);
} else {
handleSpecialCard(card, player);
}
updateTable();
}
// Bump function to handle placement collisions
function bumpAndAssign(index, player) {
if (index >= playerSlots.length) {
index = 0;
}
if (index < 0) {
index = playerSlots.length - 1;
}
let currentIndex = index;
while (true) {
if (!playerSlots[currentIndex]) {
// Spot is empty
playerSlots[currentIndex] = player;
break;
} else {
let tempPlayer = playerSlots[currentIndex];
playerSlots[currentIndex] = player;
// Move onto next spot (consider wrapping)
currentIndex--;
if (currentIndex < 0) currentIndex = playerSlots.length - 1;
if (currentIndex === index) break; // Full cycle completed
player = tempPlayer; // Continue with bumped-out player.
}
}
}
function handleSpecialCard(card, player) {
let index = 0;
switch (card) {
case "Queen":
// put player behind the first player
index = 0;
while (index < playerSlots.length) {
if (playerSlots[index] === null) {
index++;
} else {
index++;
bumpAndAssign(index, player);
return;
}
}
// it didn't find any player in the list. So, it will place the player in the first spot
bumpAndAssign(0, player);
return;
case "Jack":
// Place player behind the last player
index = playerSlots.length - 1;
while (index >= 0) {
if (playerSlots[index] === null) {
index--;
} else {
index++;
bumpAndAssign(index, player);
return;
}
}
// it didn't find any player in the list. So, it will place the player in the first spot
bumpAndAssign(0, player);
return;
case "Joker":
while (true) {
// Prompt the user to enter a number between 1 and playerSlots.length
let choice = parseInt(
prompt(`${player} pick a position (1 - ${playerSlots.length})`),
10
);
// Validate the input to ensure it's within the correct range
if (choice >= 1 && choice <= playerSlots.length) {
bumpAndAssign(choice - 1, player);
ledger.push(`${player} chose spot ${choice}`);
return;
} else {
alert(
"Invalid choice. Please enter a number within the specified range."
);
}
}
case "King":
// Place player in first spot
// Bump the existing player to the queue
if (playerSlots[0]) {
removeFromChillList(playerSlots[0]);
}
playerSlots[0] = player;
return;
case "Ace":
removeFromChillList(player);
return;
case "Mirror":
// Reverse the order of the players
playerSlots.reverse();
// Add the player back to the front of the queue
takeAnotherTurn(player);
return;
case "Bomb":
// Remove all players from the board and place them back in the queue
takeAnotherTurn(player);
players = [...players, ...movedPlayers];
playerSlots = Array(playerSlots.length).fill(null);
updateTable();
return;
default:
alert("Invalid card, ", card);
return;
}
}
function removeFromChillList(player) {
players.push(player);
let indexOfCollisionPlayer = movedPlayers.indexOf(player);
if (indexOfCollisionPlayer !== -1) {
movedPlayers.splice(indexOfCollisionPlayer, 1);
}
}
function takeAnotherTurn(player) {
players.unshift(player);
let indexOfCollisionPlayer = movedPlayers.indexOf(player);
if (indexOfCollisionPlayer !== -1) {
movedPlayers.splice(indexOfCollisionPlayer, 1);
}
}
// Update table slots display
function updateTable() {
const rowCells = document.querySelectorAll("#playerTable tbody tr td");
rowCells.forEach((cell, idx) => {
const existingNameDiv = cell.querySelector(".name-div"); // Find the existing name div
if (existingNameDiv) {
cell.removeChild(existingNameDiv); // Remove the existing name div if it exists
}
const nameDiv = document.createElement("div"); // Create a new div for the name
nameDiv.className = "name-div"; // Add a class name to the new div
nameDiv.textContent = `${playerSlots[idx] || ""}`; // Set the text content of the name div
cell.appendChild(nameDiv); // Append the name div to the td
if (playerSlots[idx]) {
cell.classList.add("filled");
} else {
cell.classList.remove("filled");
}
});
}
function createTableSpaces(number) {
// update the table header width
var thElement = document.getElementById("draft-order");
thElement.setAttribute("colspan", number);
var trElement = document.getElementById("draftersRow");
trElement.innerHTML = "";
for (let i = 1; i <= number; i++) {
const td = document.createElement("td");
const div = document.createElement("div"); // Create a div for the number label
div.textContent = i; // Set the text content of the div to the number
td.appendChild(div); // Append the text node to the td
trElement.appendChild(td);
}
// set the width of each cell
const width = 100 / number + "%";
let elements = document.querySelectorAll("th"); // Replace with your actual class or selector
elements = [...elements, ...document.querySelectorAll("td")]; // Replace with your actual class or selector
elements.forEach((element) => {
element.style.width = width;
});
}
createTableSpaces(players.length); // Creating spaces for number range you mentioned (1 to number)
populateLists(); // Populate lists initially