-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame.js
420 lines (362 loc) · 12.2 KB
/
game.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
// Select elements
const playButton = document.getElementById("playButton");
const cashoutButton = document.getElementById("cashoutButton");
const betAmountInput = document.getElementById("betAmount");
const autoCashoutInput = document.getElementById("autoCashout");
const multiplierText = document.getElementById("multiplierText");
const gameResult = document.getElementById("gameResult");
const countdownText = document.getElementById("countdownText");
const countdownOverlay = document.getElementById("countdownOverlay");
const historyContainer = document.getElementById("historyContainer");
// Constants for states
const GAME_STATE = {
INACTIVE: "inactive",
COUNTDOWN: "countdown",
ACTIVE: "active",
CRASHED: "crashed",
};
// Variables
let multiplier = 1.0;
let gameInterval = null;
let countdown = 10; // Countdown before game starts
let gameState = GAME_STATE.INACTIVE;
let autoCashoutValue = null;
let cashoutTriggered = false;
let gameHistory = [];
let inactivityTimer = null;
// Enhanced Error Handling
const ErrorHandler = {
log: (error, context = 'General') => {
console.error(`[${context}] Error:`, error);
// Future: Could send to error tracking service
},
trackError: (errorType, details) => {
gameAnalytics.errors = gameAnalytics.errors || {};
gameAnalytics.errors[errorType] =
(gameAnalytics.errors[errorType] || 0) + 1;
}
};
// Robust Sound Management
class SoundManager {
constructor() {
this.sounds = {
cashout: this.createAudio('sounds/cashout.mp3'),
crash: this.createAudio('sounds/crash.mp3')
};
this.enabled = true;
}
createAudio(src) {
const audio = new Audio(src);
audio.onerror = (e) => {
ErrorHandler.log(e, 'Audio Loading');
ErrorHandler.trackError('audioLoadFail', { src });
};
return audio;
}
play(soundName) {
if (!this.enabled) return;
try {
const sound = this.sounds[soundName];
if (sound) {
sound.currentTime = 0;
sound.play().catch(e => {
ErrorHandler.log(e, 'Audio Playback');
});
}
} catch (error) {
ErrorHandler.log(error, 'Sound Playback');
}
}
toggle() {
this.enabled = !this.enabled;
return this.enabled;
}
}
// Robust Settings Management
class GameSettingsManager {
constructor() {
this.defaultSettings = {
soundEnabled: true,
darkMode: false,
autoCashoutDefault: 2.0
};
this.settings = this.load();
}
load() {
try {
const saved = localStorage.getItem('crashGameSettings');
return saved ? JSON.parse(saved) : {...this.defaultSettings};
} catch (error) {
ErrorHandler.log(error, 'Settings Load');
return {...this.defaultSettings};
}
}
save() {
try {
localStorage.setItem('crashGameSettings',
JSON.stringify(this.settings));
} catch (error) {
ErrorHandler.log(error, 'Settings Save');
}
}
get(key) {
return this.settings[key] ?? this.defaultSettings[key];
}
set(key, value) {
this.settings[key] = value;
this.save();
}
reset() {
this.settings = {...this.defaultSettings};
this.save();
}
}
// Robust Analytics Manager
class GameAnalyticsManager {
constructor() {
this.defaultAnalytics = {
totalGames: 0,
totalWinnings: 0,
totalLosses: 0,
highestMultiplier: 1.0,
winStreak: 0,
lossStreak: 0,
errors: {}
};
this.analytics = this.load();
}
load() {
try {
const saved = localStorage.getItem('crashGameAnalytics');
return saved ? JSON.parse(saved) : {...this.defaultAnalytics};
} catch (error) {
ErrorHandler.log(error, 'Analytics Load');
return {...this.defaultAnalytics};
}
}
save() {
try {
localStorage.setItem('crashGameAnalytics',
JSON.stringify(this.analytics));
} catch (error) {
ErrorHandler.log(error, 'Analytics Save');
}
}
update(won, multiplier) {
this.analytics.totalGames++;
if (won) {
this.analytics.totalWinnings += multiplier;
this.analytics.winStreak++;
this.analytics.lossStreak = 0;
} else {
this.analytics.totalLosses++;
this.analytics.lossStreak++;
this.analytics.winStreak = 0;
}
this.analytics.highestMultiplier =
Math.max(this.analytics.highestMultiplier, multiplier);
this.save();
}
reset() {
this.analytics = {...this.defaultAnalytics};
this.save();
}
}
// Global Instances
const soundManager = new SoundManager();
const settingsManager = new GameSettingsManager();
const analyticsManager = new GameAnalyticsManager();
// Function to reset the game state
function resetGame() {
multiplier = 1.0;
multiplierText.textContent = "1.00x";
multiplierText.style.color = "#fff"; // Reset color
playButton.disabled = false;
cashoutButton.disabled = true;
gameResult.textContent = "";
cashoutTriggered = false;
gameState = GAME_STATE.INACTIVE;
autoCashoutValue = null;
countdown = 10; // Reset countdown
clearInterval(gameInterval);
resetGameUI();
}
// Function to reset UI
function resetGameUI() {
playButton.disabled = true;
cashoutButton.disabled = false;
countdownOverlay.classList.add("hidden");
countdownText.textContent = countdown;
}
// Handle inactivity
function startInactivityTimer() {
clearTimeout(inactivityTimer);
inactivityTimer = setTimeout(() => {
alert("Game inactive for too long. Restarting!");
resetGame();
}, 30000); // 30 seconds inactivity
}
// Countdown Timer
function startCountdown() {
resetGame(); // Ensure fresh start
gameState = GAME_STATE.COUNTDOWN;
countdownOverlay.classList.remove("hidden");
countdownText.textContent = countdown;
const countdownInterval = setInterval(() => {
countdown -= 1;
countdownText.textContent = countdown;
startInactivityTimer();
if (countdown <= 0) {
clearInterval(countdownInterval);
countdownOverlay.classList.add("hidden");
startGame();
}
}, 1000);
}
// Start Game Logic
function startGame() {
resetGameUI();
gameState = GAME_STATE.ACTIVE;
playButton.disabled = true;
cashoutButton.disabled = false;
autoCashoutValue = parseFloat(autoCashoutInput.value) || null;
if (autoCashoutValue <= 0) autoCashoutValue = null;
gameInterval = setInterval(() => {
try {
// Increase multiplier
multiplier += 0.01 + Math.random() * 0.05; // Random growth
multiplierText.textContent = `${multiplier.toFixed(2)}x`;
// Change multiplier text color dynamically
multiplierText.style.color = "#0f0"; // Green while increasing
// Check for auto cashout
if (autoCashoutValue && multiplier >= autoCashoutValue && !cashoutTriggered) {
handleCashout(true);
}
// Simulate crash
if (Math.random() < 0.01 + multiplier / 100) { // Higher chance to crash at higher multipliers
endGame();
}
} catch (error) {
ErrorHandler.log(error, 'Game Progression');
endGame();
}
}, 100);
}
// Handle Manual or Auto Cashout
function handleCashout(auto = false) {
playButton.disabled = false;
cashoutButton.disabled = true;
if (gameState !== GAME_STATE.ACTIVE || cashoutTriggered) return;
cashoutTriggered = true;
clearInterval(gameInterval);
gameState = GAME_STATE.INACTIVE;
const betAmount = parseFloat(betAmountInput.value) || 0;
if (betAmount <= 0) {
alert("Invalid bet amount!");
resetGame();
return;
}
const winnings = (betAmount * multiplier).toFixed(2);
gameResult.textContent = auto
? `Auto Cashout! You won $${winnings}`
: `You cashed out! You won $${winnings}`;
gameResult.style.color = "#0f0"; // Green for winning result
updateHistory(multiplier.toFixed(2), "green");
resetGameUI();
soundManager.play('cashout');
analyticsManager.update(true, multiplier);
}
// End Game (Crash)
function endGame() {
if (gameState !== GAME_STATE.ACTIVE) return;
clearInterval(gameInterval);
gameState = GAME_STATE.CRASHED;
gameResult.textContent = "Game Crashed!";
gameResult.style.color = "#f00"; // Red for losing result
multiplierText.style.color = "#f00"; // Red for crash multiplier
updateHistory(multiplier.toFixed(2), "red");
// Reset the game and enable the play button after a short delay
setTimeout(() => {
resetGame();
playButton.disabled = false; // Ensure play button is re-enabled
}, 2000);
soundManager.play('crash');
analyticsManager.update(false, multiplier);
}
// Update Game History
function updateHistory(value, color) {
gameHistory.unshift({ value, color });
if (gameHistory.length > 5) gameHistory.pop(); // Keep last 5 games
const fragment = document.createDocumentFragment();
gameHistory.forEach((entry) => {
const historyItem = document.createElement("div");
historyItem.className = "p-2 rounded text-center";
historyItem.style.backgroundColor = entry.color === "green" ? "#0f0" : "#f00";
historyItem.style.color = "#000"; // Text color
historyItem.textContent = `${entry.value}x`;
fragment.appendChild(historyItem);
});
historyContainer.innerHTML = ""; // Clear previous history
historyContainer.appendChild(fragment);
}
// Event Listeners
playButton.addEventListener("click", () => {
const betAmount = parseFloat(betAmountInput.value);
if (!betAmount || betAmount <= 0) {
alert("Please enter a valid bet amount!");
return;
}
countdown = 10; // Reset countdown
startCountdown();
startInactivityTimer();
});
cashoutButton.addEventListener("click", () => handleCashout(false));
// Inactivity handler
window.addEventListener("mousemove", startInactivityTimer);
window.addEventListener("keydown", startInactivityTimer);
// Enhanced Settings Toggle
function createSettingsToggle() {
const settingsContainer = document.createElement('div');
settingsContainer.innerHTML = `
<div class="settings-container">
<button id="soundToggle">
🔊 Sound: ${settingsManager.get('soundEnabled') ? 'ON' : 'OFF'}
</button>
<button id="darkModeToggle">
🌓 Dark Mode: ${settingsManager.get('darkMode') ? 'ON' : 'OFF'}
</button>
<button id="resetAnalytics">
🔄 Reset Analytics
</button>
</div>
`;
document.body.appendChild(settingsContainer);
const soundToggle = document.getElementById('soundToggle');
const darkModeToggle = document.getElementById('darkModeToggle');
const resetAnalyticsBtn = document.getElementById('resetAnalytics');
soundToggle.addEventListener('click', () => {
const newState = soundManager.toggle();
settingsManager.set('soundEnabled', newState);
soundToggle.textContent = `🔊 Sound: ${newState ? 'ON' : 'OFF'}`;
});
darkModeToggle.addEventListener('click', () => {
const currentMode = settingsManager.get('darkMode');
const newMode = !currentMode;
settingsManager.set('darkMode', newMode);
document.body.classList.toggle('dark-mode', newMode);
darkModeToggle.textContent = `🌓 Dark Mode: ${newMode ? 'ON' : 'OFF'}`;
});
resetAnalyticsBtn.addEventListener('click', () => {
analyticsManager.reset();
alert('Game analytics have been reset!');
});
}
// Initialize on load
window.addEventListener('load', () => {
// Apply saved dark mode
document.body.classList.toggle(
'dark-mode',
settingsManager.get('darkMode')
);
createSettingsToggle();
});