-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
294 lines (250 loc) · 9.32 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
// DOM Elements
const digitContainers = document.querySelectorAll(".digit-container");
const rollSound = document.getElementById("rollSound");
const tadaSound = document.getElementById("tadaSound");
const resetSound = document.getElementById("resetSound");
const backgroundAudio = document.getElementById("backgroundAudio"); // Background audio
const clappingSound = document.getElementById("clappingSound"); // Clapping sound
const speakerIcon = document.getElementById("speakerIcon");
const speakerText = document.getElementById("speakerText");
const historyList = document.getElementById("historyList");
const exportToExcelButton = document.getElementById("exportToExcelButton");
// Global Variables
let rollingIntervals = [];
let isRolling = false;
let isStopping = false;
let isAudioPlaying = false; // Track whether the audio has started
let historyData = []; // Store history data
let gameCount = 0; // Track the number of games played
const maxGames = 21; // Max games before redirection
// Event Listeners
speakerText.addEventListener("click", toggleAudio);
exportToExcelButton.addEventListener("click", exportToExcel);
document.getElementById("startButton").addEventListener("click", startRolling);
document.getElementById("stopButton").addEventListener("click", stopRolling);
document.getElementById("resetButton").addEventListener("click", resetMeter);
// Functions
// Toggle Audio Play/Pause
function toggleAudio() {
if (isAudioPlaying) {
backgroundAudio.pause();
backgroundAudio.currentTime = 0; // Restart audio
speakerText.textContent = "🔇"; // Mute icon
} else {
backgroundAudio.play();
speakerText.textContent = "🔊"; // Speaker icon
}
isAudioPlaying = !isAudioPlaying;
}
// Roll Sound: Looping on End
rollSound.addEventListener("ended", () => {
rollSound.currentTime = 0; // Restart audio
rollSound.play(); // Loop
});
// Generate Random Number
const getRandomNumberInRange = (max) => {
let randomNumber;
do {
randomNumber = Math.floor(Math.random() * (max + 1)); // Inclusive of max
// 1st place to change required for increase/decrease of digit
randomNumber = String(randomNumber).padStart(5, "0"); // 6-digit format
} while (
(randomNumber >= 4000 && randomNumber <= 5000) || // Exclude range 4000-5000
(randomNumber >= 15000 && randomNumber <= 19999) || // Exclude range 15000-19999
(randomNumber >= 25000 && randomNumber <= 29999) || // Exclude range 25000-29999
historyData.includes(randomNumber) // Ensure number is not already in history
); // Regenerate if already exists
return randomNumber;
};
// Populate Digits (0-9) for Rolling Animation
digitContainers.forEach((container) => {
const digit = container.querySelector(".digit");
for (let i = 0; i < 20; i++) {
const numDiv = document.createElement("div");
numDiv.textContent = i % 10; // Repeating digits (0-9)
digit.appendChild(numDiv);
}
});
// Button Disable Helpers
function disableStartAndReset() {
document.getElementById("startButton").disabled = true;
document.getElementById("resetButton").disabled = true;
document.getElementById("stopButton").disabled = false;
}
function disableStartAndStop() {
document.getElementById("startButton").disabled = true;
document.getElementById("stopButton").disabled = true;
document.getElementById("resetButton").disabled = false;
}
function disableResetAndStop() {
document.getElementById("resetButton").disabled = true;
document.getElementById("stopButton").disabled = true;
document.getElementById("startButton").disabled = false;
}
// Start Rolling Numbers
function startRolling() {
if (isRolling || gameCount >= maxGames) return; // Prevent multiple starts or if max games reached
isRolling = true;
isStopping = false;
disableStartAndReset();
rollSound.currentTime = 0;
rollSound.play();
digitContainers.forEach((container, index) => {
const digit = container.querySelector(".digit");
let position = 0;
rollingIntervals[index] = setInterval(() => {
position += 60; // Fast rolling speed
digit.style.transform = `translateY(-${position % 1000}px)`; // show number whether 0-5 or 0-9 while rolling
digit.style.transition = "none"; // Instant transition
}, 10); // Fast speed for rolling
});
}
// Add Final Target Number to History
function addToHistory(finalNumber) {
const timestamp = new Date().toLocaleString();
if (gameCount < 21) {
historyData.push({ Participant: `Participant ${gameCount}`, CouponNumber: finalNumber });
const participantId = `reading${gameCount + 1}`;
document.getElementById(participantId).textContent = finalNumber;
}
}
// Export History to Excel
function exportToExcel() {
if (historyData.length === 0) {
alert("No data to export!");
return;
}
const workbook = XLSX.utils.book_new();
const worksheet = XLSX.utils.json_to_sheet(historyData);
XLSX.utils.book_append_sheet(workbook, worksheet, "History");
XLSX.writeFile(workbook, "coupon_rolled_out.xlsx");
}
// Stop Rolling Numbers Gradually
function stopRolling() {
if (!isRolling || isStopping || gameCount >= maxGames) return;
isStopping = true;
isRolling = false;
rollSound.pause();
rollSound.currentTime = 0; // Reset for next play
tadaSound.currentTime = 0;
tadaSound.play();
// 2nd place to change required for increase/decrease of digit
const finalNumber = getRandomNumberInRange(50001); // 6-digit number
let finalReading = "";
digitContainers.forEach((container, index) => {
const digit = container.querySelector(".digit");
const targetDigit = parseInt(finalNumber[index]);
clearInterval(rollingIntervals[index]);
let position = parseInt(digit.style.transform.replace("translateY(-", "").replace("px)", "")) || 0;
const finalStop = targetDigit * 100;
let speed = 20;
const slowDown = setInterval(() => {
position += (finalStop - position) / 8;
digit.style.transform = `translateY(-${Math.round(position)}px)`;
digit.style.transition = `transform ${speed / 100}s ease-out`;
if (Math.abs(position - finalStop) < 1) {
digit.style.transform = `translateY(-${finalStop}px)`;
clearInterval(slowDown);
finalReading += targetDigit;
// 3rd place to change required for increase/decrease of digit
if (finalReading.length === 5) {
addToHistory(finalNumber);
disableStartAndStop();
gameCount++;
if (gameCount >= maxGames) {
showConfetti();
disableButtons();
}
}
}
speed += 2;
}, speed + index * 0.1);
});
}
// Disable Buttons After Max Games
function disableButtons() {
const buttons = document.querySelectorAll("button");
buttons.forEach(button => {
if (button.id !== "exportToExcelButton") {
button.disabled = true;
}
});
}
// Show Confetti on Max Games Reached
function showConfetti() {
launchContinuousConfetti(100000);
clappingSound.currentTime = 0;
clappingSound.play();
setTimeout(() => {
disableButtons();
}, 5000);
}
// Launch Continuous Confetti
function launchContinuousConfetti(duration = 100000) {
const confettiContainer = document.createElement("div");
confettiContainer.style.position = "fixed";
confettiContainer.style.top = 0;
confettiContainer.style.left = 0;
confettiContainer.style.width = "100%";
confettiContainer.style.height = "100%";
confettiContainer.style.pointerEvents = "none";
document.body.appendChild(confettiContainer);
const interval = 200;
const endTime = Date.now() + duration;
const createConfetti = () => {
for (let i = 0; i < 20; i++) {
const confetti = document.createElement("div");
confetti.classList.add("confetti");
confetti.style.position = "absolute";
confetti.style.width = `${Math.random() * 8 + 4}px`;
confetti.style.height = `${Math.random() * 8 + 4}px`;
confetti.style.backgroundColor = `hsl(${Math.random() * 360}, 70%, 60%)`;
confetti.style.left = `${Math.random() * 100}vw`;
confetti.style.top = `-10vh`;
confetti.style.opacity = Math.random() + 0.5;
confetti.style.transform = `rotate(${Math.random() * 360}deg)`;
confetti.style.animation = `fall ${Math.random() * 3 + 2}s linear`;
confettiContainer.appendChild(confetti);
confetti.addEventListener("animationend", () => {
confettiContainer.removeChild(confetti);
});
}
};
const intervalId = setInterval(() => {
if (Date.now() >= endTime) {
clearInterval(intervalId);
document.body.removeChild(confettiContainer);
} else {
createConfetti();
}
}, interval);
}
// CSS for Confetti Animation
const confettiStyles = `
@keyframes fall {
0% { transform: translateY(0) rotate(0deg); }
100% { transform: translateY(100vh) rotate(360deg); }
}
.confetti {
position: absolute;
border-radius: 50%;
animation: fall 3s linear infinite;
}
`;
const styleSheet = document.createElement("style");
styleSheet.type = "text/css";
styleSheet.innerText = confettiStyles;
document.head.appendChild(styleSheet);
// Reset Meter to 000000
function resetMeter() {
rollSound.pause();
rollSound.currentTime = 0;
resetSound.currentTime = 0;
resetSound.play();
digitContainers.forEach((container) => {
const digit = container.querySelector(".digit");
digit.style.transform = "translateY(0px)";
});
console.log("Odometer Reset to: 000000");
disableResetAndStop();
}