-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.js
464 lines (405 loc) · 14.5 KB
/
script.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
// Game State
const numberOfCards = 4;
let amtcards = 4;
let turnCounter = 0;
let undoStack = [];
let baseState;
let ignoreEventListener = false;
let isInputArea = true;
let mulliganPhase = true;
// UI Elements
const cardContainer = document.getElementById('flex-card-image-container');
//initialize
function init() {
// Perform initialization tasks here
initializeMulligan();
initializeKeybindings();
initializeButtons();
return {
message: 'Initialization completed successfully!',
};
}
function initializeMulligan() {
//generate initial set of 4 cards
for (let i = 1; i <= numberOfCards; i++) {
const imgCardDiv = document.createElement('div');
imgCardDiv.className = 'img-cards';
imgCardDiv.id = `img-cards-${i}`;
const img = document.createElement('img');
img.src = "Card-Back-Images/Summoner's-Rift.png";
img.alt = "LOR Card";
img.className = "LORCardClass";
img.style.width = "100%";
img.style.height = "100%";
img.id = `LORCard${i}`;
img.onclick = () => cardSelected(`img-cards-${i}`);
const label = document.createElement('p');
label.className = 'card-labels';
label.id = `LORCard-label${i}`;
label.innerHTML = `Card ${i}`;
let mulliganButton = document.createElement('img');
mulliganButton.src = "Button-Images/replaceButtonStandard.png";
mulliganButton.alt = "replaceButton";
mulliganButton.id = `replace${i}`;
mulliganButton.onclick = () => handleReplaceButton(i);
imgCardDiv.appendChild(label);
imgCardDiv.appendChild(img);
imgCardDiv.appendChild(mulliganButton);
cardContainer.appendChild(imgCardDiv);
//Add right click to delete onto the image (LORCard${i})
img.addEventListener('contextmenu', discardCard);
}
document.getElementById("cardCount").innerHTML = amtcards;
}
//toggles whether or not card should have the mulled or kept tag
function handleReplaceButton(cardNumber) {
let card = document.getElementById(`LORCard${cardNumber}`).src;
if(!card.includes("Card-Back-Images/Summoner's-Rift-faded.png")) {
document.getElementById(`LORCard${cardNumber}`).src = "Card-Back-Images/Summoner's-Rift-faded.png";
}
else if(card.includes("Card-Back-Images/Summoner's-Rift-faded.png")){
document.getElementById(`LORCard${cardNumber}`).src = "Card-Back-Images/Summoner's-Rift.png";
}
}
//function for startGame Button
function handleStartButton() {
for (let i = 1; i <= numberOfCards; i++) {
curCardImg = document.getElementById(`LORCard${i}`);
let label = document.getElementById(`LORCard-label${i}`);
if(curCardImg.src.includes("Card-Back-Images/Summoner's-Rift-faded.png")) {
label.innerHTML += '<br>Mulled';
curCardImg.src = "Card-Back-Images/Summoner's-Rift.png";
}
else {
label.innerHTML += '<br>Kept';
}
document.getElementById(`replace${i}`).remove();
}
baseState = currentState();
undoStack.push(baseState);
mulliganPhase = false;
updateDisplay();
}
//function for initializing q,w
function initializeKeybindings() {
document.addEventListener('keydown', function(event) {
if(ignoreEventListener) {
return;
}
switch (event.key) {
case 'q':
ButtonSelected('next');
break;
case 'w':
ButtonSelected('draw');
break;
// Add more cases for other keys if needed
default:
break;
}
});
}
//initializes all buttons
function initializeButtons() {
// List of buttons and their associated click actions (if any)
const buttons = [
{ id: 'next', clickAction: true },
{ id: 'draw', clickAction: true },
{ id: 'start-game' },
{ id: 'restart' },
{ id: 'undo', clickAction: true },
{ id: 'replace1', clickAction: true },
{ id: 'replace2', clickAction: true },
{ id: 'replace3', clickAction: true },
{ id: 'replace4', clickAction: true }
];
// Loop through the buttons and add event listeners
buttons.forEach(button => {
addButtonGraphics(button.id, button.clickAction);
});
}
initConfirmation = init();
console.log(initConfirmation);
//main function for undo button
function undoUpdateCards() {
const cardContainer = document.getElementById('flex-card-image-container');
if(undoStack.length == 0) {
undoStack.push(baseState);
}
let labelArr = undoStack.pop();
if(JSON.stringify(labelArr) === JSON.stringify(currentState())) {
labelArr = undoStack.pop()
}
if(JSON.stringify(currentState()) === JSON.stringify(baseState)) {
return;
}
while (cardContainer.firstChild) {
cardContainer.removeChild(cardContainer.firstChild);
}
for (let i = 1; i <= labelArr.length; i++) {
const imgCardDiv = document.createElement('div');
imgCardDiv.className = 'img-cards';
imgCardDiv.id = `img-cards-${i}`;
const img = document.createElement('img');
img.src = "Card-Back-Images/Summoner's-Rift.png";
img.alt = "LOR Card";
img.className = "LORCardClass";
img.style.width = "100%";
img.style.height = "100%";
img.id = `LORCard${i}`;
img.onclick = () => cardSelected(`img-cards-${i}`);
const label = document.createElement('p');
label.className = 'card-labels';
label.id = `LORCard-label${i}`;
//label.innerHTML = `Card ${i}`;
label.innerHTML = labelArr[i-1];
//TODO for every label in the undo stack, append it to the card
imgCardDiv.appendChild(label);
imgCardDiv.appendChild(img);
cardContainer.appendChild(imgCardDiv);
//Add right click to delete onto the image (LORCard${i})
img.addEventListener('contextmenu', discardCard);
}
amtcards = labelArr.length;
}
//add graphics to buttons
function addButtonGraphics(buttonId, clickAction) {
const button = document.getElementById(buttonId);
button.addEventListener('mouseover', function() {
ChangeButton(buttonId, 'Hover');
});
button.addEventListener('mouseout', function() {
ChangeButton(buttonId, 'Standard');
});
if (clickAction) {
button.addEventListener('click', function() {
ChangeButton(buttonId, 'Selected');
});
}
}
//function for button graphics
function ChangeButton(buttonId, functiontype) {
const prev = document.getElementById(buttonId).src;
let buttonIdNoNums = buttonId.replace(/\d+$/, '');
document.getElementById(buttonId).src = `Button-Images/${buttonIdNoNums}Button${functiontype}.png`;
if(functiontype == 'Selected') {
setTimeout(() => {
document.getElementById(buttonId).src = prev;
}, 100)
}
}
//turn counter function
function updateDisplay(){
turnCounter++;
document.getElementById("turnCount").innerHTML = turnCounter;
amtcards = drawCard(amtcards);
}
//stops right click menu from showing up
function preventDefault() {
let background = document.getElementById('flex-background');
background.addEventListener('contextmenu', (e) => {
e.preventDefault(); // Prevent default context menu
});
}
//delete a Card on Right Click
function discardCard(event) {
event.preventDefault(); // Prevent the default context menu
//get Id's
const clickedElement = event.target;
const imgId = clickedElement.id;
let cardNum = imgId.match(/\d+/g);
const joinedString = cardNum ? cardNum.join('') : '';
cardNum = parseInt(joinedString, 10);
//remove the specified card
const wrapper = document.getElementById(`img-cards-${cardNum}`);
const label = document.getElementById(`LORCard-label${cardNum}`);
const card = document.getElementById(`LORCard${cardNum}`);
card.remove();
label.remove();
wrapper.remove();
for (let i = cardNum+1; i <= amtcards; i++) {
let currentCardDiv = document.getElementById(`img-cards-${i}`)
let currentCard = document.getElementById(`LORCard${i}`);
let currentCardLabel = document.getElementById(`LORCard-label${i}`);
const newCardNum = i - 1;
let innerHTMLLabel = currentCardLabel.innerHTML;
// Replace the numbers in the HTML content while preserving HTML tags
const updatedLabel = innerHTMLLabel.replace(/\b(?:[1-9]|10)\b/, newCardNum);
currentCard.onclick = () => cardSelected(`img-cards-${newCardNum}`);
//set the id's to be reduced by 1
currentCardLabel.innerHTML = updatedLabel;
currentCardDiv.id = `img-cards-${newCardNum}`;
currentCardLabel.id = `LORCard-label${newCardNum}`;
currentCard.id = `LORCard${newCardNum}`;
}
amtcards--;
const curState = currentState();
undoStack.push(curState);
document.getElementById("cardCount").innerHTML = amtcards;
}
//drawCard on button press
function drawCard(amtCards) {
if(amtCards < 10)
{
amtCards += 1;
const imgCardDiv = document.createElement('div');
imgCardDiv.className = 'img-cards';
imgCardDiv.id = `img-cards-${amtCards}`;
const img = document.createElement('img');
img.src = "Card-Back-Images/Summoner's-Rift.png";
img.alt = "LOR Card";
img.style.width = "100%";
img.style.height = "100%";
img.id = `LORCard${amtCards}`;
img.className = 'LORCardClass';
img.onclick = () => cardSelected(`img-cards-${amtCards}`);
const label = document.createElement('p');
label.className = 'card-labels';
label.id = `LORCard-label${amtCards}`;
label.innerHTML = `Card ${amtCards}<br>Turn: ${turnCounter}`;
imgCardDiv.appendChild(label);
imgCardDiv.appendChild(img);
cardContainer.appendChild(imgCardDiv);
//create right click functionality
// Add contextmenu event listener for right-click
img.addEventListener('contextmenu', discardCard);
}
return amtCards;
}
//sets the mana label to be the first label, then sorts the rest alphabetically
function sortLabel(cardlabel)
{
const label = document.getElementById(cardlabel);
const labelArray = label.innerHTML.split("<br>");
label.innerHTML = labelArray[0] + "<br>";
for(var i = 0; i < labelArray.length; i++) {
if(labelArray[i].indexOf("mana") === 1) {
label.innerHTML += labelArray[i] + "<br>";
}
}
for(var i = 1; i < labelArray.length; i++) {
if(labelArray[i].indexOf("mana") === -1) {
label.innerHTML += labelArray[i] + "<br>";
}
}
label.innerHTML = label.innerHTML.substring(0, label.innerHTML.lastIndexOf("<br>"));
}
//Custom Labels
function createInputArea(cardnumber, reducedCost) {
if(isInputArea == false) {
// Remove the inputContainer (which contains the input and submit button)
const inputContainer = document.getElementById('inputContainer');
const userInput = document.getElementById('userInput');
userInput.removeEventListener('keyup', getUserInput);
inputContainer.remove();
}
else {
isInputArea = false;
}
ignoreEventListener = true;
// Create the inputContainer and other elements
const inputContainer = document.createElement('div');
inputContainer.id = 'inputContainer';
const userInput = document.createElement('input');
userInput.type = 'text';
userInput.id = 'userInput';
userInput.placeholder = 'Enter text';
userInput.addEventListener('keyup', function(event) {
if (event.key === 'Enter') {
getUserInput(cardnumber, reducedCost); // Pass the cardnumber when Enter is pressed
ignoreEventListener = false;
}
});
const submitButton = document.createElement('button');
submitButton.textContent = 'Submit';
submitButton.onclick = function() {
getUserInput(cardnumber, reducedCost); // Pass the cardnumber when the button is clicked
ignoreEventListener = false;
};
inputContainer.appendChild(userInput);
inputContainer.appendChild(submitButton);
// Get the flex-card-image-container and append the inputContainer
const flexCardImageContainer = document.getElementById(`img-cards-${cardnumber}`);
flexCardImageContainer.appendChild(inputContainer);
// Set focus on the input field when displayed
userInput.focus();
}
//helper function for createInputArea
function getUserInput(cardnumber, reducedCost) {
const userInputIntoLabel = document.getElementById('userInput').value;
if (userInputIntoLabel.trim() !== '') {
// Do something with the user's input
const currentCardLabel = document.getElementById(`LORCard-label${cardnumber}`);
currentCardLabel.innerHTML += '<br>' + reducedCost + userInputIntoLabel;
}
// Remove the inputContainer (which contains the input and submit button)
const inputContainer = document.getElementById('inputContainer');
inputContainer.parentNode.removeChild(inputContainer);
const curState = currentState();
undoStack.push(curState);
isInputArea = true;
}
//returns current cards
function currentState() {
const cardLabelsElements = document.querySelectorAll('.card-labels');
// Initialize an array to store the labels
const labelsArray = [];
// Iterate through each element and store its content in the array
cardLabelsElements.forEach(element => {
labelsArray.push(element.innerHTML);
});
// Log the array to the console
console.log(undoStack);
return labelsArray;
}
//main function for buttons
function ButtonSelected(buttonID)
{
//remove all other active button classes - set them to inactive
var buttons = document.getElementsByClassName("button active");
for(var i = 0; i < buttons.length; i++){
if(buttons[i].classList.contains("active")){
buttons[i].classList.add("inactive");
buttons[i].classList.remove("active");
}
}
//if statement for non-toggle buttons - if they are non-toggle class,
//go into a switch case to do the correct thing-
switch(buttonID) {
case "next":
//deleteFleeting();
updateDisplay();
break;
case "draw":
amtcards = drawCard(amtcards);
break;
case "restart":
location.reload();
break;
case "start-game":
document.getElementById('mulligan-phase').style.display = 'none';
document.getElementById('game-phase').style.display = 'flex';
handleStartButton();
break;
case "undo": //must be last case
undoUpdateCards();
return;
case "help":
alert('Left click to add a label\nRight click to delete a card');
return;
default: throw "Issue with button selection occured";
}
const curState = currentState();
undoStack.push(curState);
document.getElementById("cardCount").innerHTML = amtcards;
}
//main function for cards
function cardSelected(wrapperID) {
const cardnumber = parseInt(wrapperID.split("-")[2]);
if(mulliganPhase) {
handleReplaceButton(cardnumber);
}
else {
createInputArea(cardnumber, "");
}
}