Update script.js

This commit is contained in:
Voxel 2025-04-14 17:11:11 -04:00 committed by GitHub
parent e338df7dfe
commit e7d877d7ab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -72,6 +72,22 @@ function setUsername(newUsername) {
updateUI(); updateUI();
} }
// Start the game, setting username and showing the main menu
function startGame() {
const usernameInput = document.getElementById('username-input').value.trim();
if (usernameInput === "") {
alert("Please enter a valid username.");
return;
}
// Set the username and store it in localStorage
setUsername(usernameInput);
// Show the main menu after setting the username
showScreen('main-menu');
}
// Update balance after a game // Update balance after a game
function updateStats(game, amount, xp) { function updateStats(game, amount, xp) {
let money = parseInt(localStorage.getItem('casino-money')); let money = parseInt(localStorage.getItem('casino-money'));
@ -114,273 +130,3 @@ function updateUI() {
document.getElementById(`stats-${game}-wins`).innerText = localStorage.getItem(`casino-${game}-wins`) || 0; document.getElementById(`stats-${game}-wins`).innerText = localStorage.getItem(`casino-${game}-wins`) || 0;
}); });
} }
// Blackjack Game
function playBlackjack() {
const bet = parseInt(document.getElementById('blackjack-bet').value);
let money = parseInt(localStorage.getItem('casino-money'));
if (!bet || bet <= 0 || bet > money) {
alert('Invalid bet amount.');
return;
}
money -= bet;
localStorage.setItem('casino-money', money);
// Simulate game result (basic example)
const win = Math.random() < 0.5;
let message = win ? `You won $${bet * 2}!` : `You lost $${bet}.`;
updateStats('blackjack', win ? bet * 2 : -bet, win ? 20 : 0);
document.getElementById('blackjack-message').innerText = message;
}
// Slots Game
function playSlots() {
const bet = parseInt(document.getElementById('slots-bet').value);
let money = parseInt(localStorage.getItem('casino-money'));
if (!bet || bet <= 0 || bet > money) {
alert('Invalid bet amount.');
return;
}
money -= bet;
localStorage.setItem('casino-money', money);
const symbols = ['🍒', '🍋', '🔔', '💎', '7⃣'];
const reels = document.querySelectorAll('#slots-reels .reel');
reels.forEach(reel => {
const result = symbols[Math.floor(Math.random() * symbols.length)];
const span = reel.querySelector('span');
span.style.animation = 'none';
span.offsetHeight;
span.textContent = result;
span.style.animation = '';
span.style.animation = 'spinReel 0.5s ease-out';
});
setTimeout(() => {
const results = Array.from(reels).map(reel => reel.querySelector('span').textContent);
const [a, b, c] = results;
let winnings = 0;
let xp = 0;
let message = 'You lost.';
if (a === b && b === c) {
winnings = bet * 5;
xp = 15;
message = `Jackpot! ${a} ${b} ${c} — You won $${winnings}!`;
} else if (a === b || b === c || a === c) {
winnings = bet * 2;
xp = 5;
message = `Nice! You matched two and won $${winnings}!`;
}
document.getElementById('slots-message').innerText = message;
updateStats('slots', winnings, xp);
}, 500);
}
// Roulette Game
function playRoulette() {
const bet = parseInt(document.getElementById('roulette-bet').value);
const choice = document.getElementById('roulette-choice').value;
let money = parseInt(localStorage.getItem('casino-money'));
if (!bet || bet <= 0 || bet > money) {
alert('Invalid bet amount.');
return;
}
money -= bet;
localStorage.setItem('casino-money', money);
const wheel = document.getElementById('roulette-animation');
wheel.style.animation = 'none';
wheel.offsetHeight;
wheel.style.animation = 'spinRoulette 1s ease-out';
setTimeout(() => {
const outcome = Math.floor(Math.random() * 37);
const color = outcome === 0 ? 'green' : outcome % 2 === 0 ? 'black' : 'red';
let resultText = `The ball landed on ${outcome} (${color}). You lost.`;
let winnings = 0;
let xp = 0;
if (color === choice) {
winnings = choice === 'green' ? bet * 14 : bet * 2;
resultText = `The ball landed on ${outcome} (${color}). You won $${winnings}!`;
xp = choice === 'green' ? 20 : 10;
}
document.getElementById('roulette-result').innerText = resultText;
updateStats('roulette', winnings, xp);
}, 1000);
}
// Crash Game
function playCrash() {
const bet = parseInt(document.getElementById('crash-bet').value);
let money = parseInt(localStorage.getItem('casino-money'));
if (!bet || bet <= 0 || bet > money) {
alert('Invalid bet amount.');
return;
}
money -= bet;
localStorage.setItem('casino-money', money);
const crashBar = document.getElementById('crash-animation');
crashBar.style.transform = 'scaleX(0)';
crashBar.offsetHeight;
crashBar.style.transform = 'scaleX(1)';
setTimeout(() => {
const multiplier = (Math.random() * 5 + 1).toFixed(2);
const cashout = Math.random() * 5 + 1;
let resultText = `Multiplier reached x${multiplier}. You lost.`;
let winnings = 0;
let xp = 0;
if (cashout < multiplier) {
winnings = Math.floor(bet * cashout);
resultText = `You cashed out at x${cashout.toFixed(2)} and won $${winnings}!`;
xp = 10;
}
document.getElementById('crash-result').innerText = resultText;
updateStats('crash', winnings, xp);
}, 1000);
}
// Horse Betting Game
function playHorseBetting() {
const bet = parseInt(document.getElementById('horse-bet').value);
let money = parseInt(localStorage.getItem('casino-money'));
if (!bet || bet <= 0 || bet > money) {
alert('Invalid bet amount.');
return;
}
money -= bet;
localStorage.setItem('casino-money', money);
const outcome = Math.floor(Math.random() * 4) + 1; // 4 horses
const playerHorse = document.getElementById('horse-choice').value;
let message = `You lost. Horse #${outcome} won.`;
let winnings = 0;
let xp = 0;
if (outcome == playerHorse) {
winnings = bet * 3; // Win 3x bet if correct
message = `You won $${winnings}! Horse #${outcome} won.`;
xp = 15;
}
document.getElementById('horse-message').innerText = message;
updateStats('horse', winnings, xp);
}
// Casino War Game
function playCasinoWar() {
const bet = parseInt(document.getElementById('war-bet').value);
let money = parseInt(localStorage.getItem('casino-money'));
if (!bet || bet <= 0 || bet > money) {
alert('Invalid bet amount.');
return;
}
money -= bet;
localStorage.setItem('casino-money', money);
const playerCard = Math.floor(Math.random() * 13) + 1; // 1-13 (Ace to King)
const dealerCard = Math.floor(Math.random() * 13) + 1;
let message = `You lost. Dealer's card (${dealerCard}) was higher.`;
let winnings = 0;
let xp = 0;
if (playerCard > dealerCard) {
winnings = bet * 2;
message = `You won $${winnings}! Your card (${playerCard}) was higher.`;
xp = 10;
}
document.getElementById('war-message').innerText = message;
updateStats('war', winnings, xp);
}
// Coinflip Game
function playCoinflip() {
const bet = parseInt(document.getElementById('coinflip-bet').value);
let money = parseInt(localStorage.getItem('casino-money'));
if (!bet || bet <= 0 || bet > money) {
alert('Invalid bet amount.');
return;
}
money -= bet;
localStorage.setItem('casino-money', money);
const result = Math.random() < 0.5 ? 'heads' : 'tails';
const playerChoice = document.getElementById('coinflip-choice').value;
let message = `You lost. The coin landed on ${result}.`;
let winnings = 0;
let xp = 0;
if (result === playerChoice) {
winnings = bet * 2;
message = `You won $${winnings}! The coin landed on ${result}.`;
xp = 5;
}
document.getElementById('coinflip-message').innerText = message;
updateStats('coinflip', winnings, xp);
}
// Keno Game
function playKeno() {
const bet = parseInt(document.getElementById('keno-bet').value);
let money = parseInt(localStorage.getItem('casino-money'));
if (!bet || bet <= 0 || bet > money) {
alert('Invalid bet amount.');
return;
}
money -= bet;
localStorage.setItem('casino-money', money);
const drawnNumbers = Array.from({ length: 20 }, () => Math.floor(Math.random() * 80) + 1);
const playerNumbers = Array.from(document.getElementsByClassName('keno-number')).map(num => parseInt(num.value));
const matchedNumbers = playerNumbers.filter(num => drawnNumbers.includes(num));
let message = `You lost.`;
let winnings = 0;
let xp = 0;
if (matchedNumbers.length > 0) {
winnings = bet * matchedNumbers.length;
message = `You matched ${matchedNumbers.length} numbers: ${matchedNumbers.join(', ')}. You won $${winnings}!`;
xp = 20;
}
document.getElementById('keno-message').innerText = message;
updateStats('keno', winnings, xp);
}
// Event listener to load user data when the document is loaded
document.addEventListener('DOMContentLoaded', () => {
loadUserData(); // Load user data on page load
});