-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
132 lines (109 loc) · 4.23 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
let cardsData = [];
// Load cards
async function loadCards() {
try {
const response = await fetch("cards.json");
cardsData = await response.json();
populatePackSelection(); // Populate dropdown with packs after loading cards
} catch (error) {
console.error("Error loading cards:", error);
}
}
// Populate the deck selection dropdown with unique packs
function populatePackSelection() {
const packSelect = document.getElementById("pack-selection");
const uniquePacks = [...new Set(cardsData.map(card => card.Pack))];
uniquePacks.forEach(pack => {
const option = document.createElement("option");
option.value = pack;
option.textContent = pack;
packSelect.appendChild(option);
});
// Select the Base Game by default
const baseGameOption = Array.from(packSelect.options).find(option => option.value === "Base Game");
if (baseGameOption) baseGameOption.selected = true;
}
// Get selected packs from the dropdown menu
function getSelectedPacks() {
const packSelect = document.getElementById("pack-selection");
return Array.from(packSelect.selectedOptions).map(option => option.value);
}
// Function to play flip sound
const flipSound = new Audio('flip.mp3');
function playFlipSound() {
flipSound.play();
}
// Add event listeners to buttons
document.getElementById('draw-black-card').addEventListener('click', playFlipSound);
document.getElementById('draw-white-cards').addEventListener('click', playFlipSound);
// Draw a black card based on selected packs
function drawBlackCard() {
const selectedPacks = getSelectedPacks();
const promptCards = cardsData.filter(card => card.Type === "Prompt" && selectedPacks.includes(card.Pack));
if (promptCards.length === 0) return;
const randomPrompt = promptCards[Math.floor(Math.random() * promptCards.length)];
displayCards([randomPrompt]);
}
// Draw seven white cards based on selected packs
function drawWhiteCards() {
const selectedPacks = getSelectedPacks();
const responseCards = cardsData.filter(card => card.Type === "Response" && selectedPacks.includes(card.Pack));
if (responseCards.length < 7) return;
const selectedResponses = [];
for (let i = 0; i < 7; i++) {
const randomResponse = responseCards[Math.floor(Math.random() * responseCards.length)];
selectedResponses.push(randomResponse);
}
displayCards(selectedResponses);
}
// Function to change the length of "_____" lines
function reduceUnderscores(text) {
// Reduce the length of underscores by 0% without any span replacements
return text.replace(/_+/g, match => {
const reducedLength = Math.floor(match.length * 1);
return "_".repeat(reducedLength); // Keep the underscores intact
});
}
// Display the selected cards
function displayCards(cards) {
document.getElementById("main-menu").style.display = "none"; // Hide main menu
const cardsContainer = document.getElementById("cards-container");
cardsContainer.innerHTML = "";
cards.forEach(card => {
const cardElement = document.createElement("div");
cardElement.classList.add("card");
if (card.Type === "Prompt") {
cardElement.classList.add("black-card");
// Reduce underscores before rendering the card text (without replacing them with spans)
cardElement.textContent = reduceUnderscores(card.Text); // Display text as is
} else {
cardElement.textContent = card.Text;
}
cardsContainer.appendChild(cardElement);
});
document.getElementById("card-display").style.display = "block";
}
// Return to the main menu
function returnToMenu() {
document.getElementById("card-display").style.display = "none";
document.getElementById("main-menu").style.display = "block";
}
// Load cards and populate pack selection on page load
loadCards();
// Check if the user has a saved mode preference
window.addEventListener('DOMContentLoaded', () => {
const savedMode = localStorage.getItem('mode');
if (savedMode === 'dark') {
document.body.classList.add('dark-mode');
}
});
// Toggle between Light Mode and Dark Mode
function toggleMode() {
document.body.classList.toggle('dark-mode');
// Save the mode preference to localStorage
if (document.body.classList.contains('dark-mode')) {
localStorage.setItem('mode', 'dark');
} else {
localStorage.setItem('mode', 'light');
}
}