Spaces:
Running
Running
Update script.js
Browse files
script.js
CHANGED
|
@@ -1,12 +1,4 @@
|
|
| 1 |
document.addEventListener("DOMContentLoaded", () => {
|
| 2 |
-
const currentUser = localStorage.getItem("currentUser");
|
| 3 |
-
|
| 4 |
-
if (!currentUser) {
|
| 5 |
-
alert("User not logged in.");
|
| 6 |
-
window.location.href = "index.html";
|
| 7 |
-
return;
|
| 8 |
-
}
|
| 9 |
-
|
| 10 |
const balanceDisplay = document.getElementById("balance");
|
| 11 |
const stakeAmountInput = document.getElementById("stake-amount");
|
| 12 |
const stakeBtn = document.getElementById("stake-btn");
|
|
@@ -17,30 +9,13 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 17 |
const skyVideo = document.querySelector(".sky-video");
|
| 18 |
const audio = new Audio("sound.mp3"); // Replace with your sound file path
|
| 19 |
|
| 20 |
-
let balance =
|
| 21 |
let gameInterval;
|
| 22 |
let multiplier = 1.0;
|
| 23 |
let crashPoint = 0;
|
| 24 |
let stakeAmount = 0;
|
| 25 |
let gameId = null;
|
| 26 |
|
| 27 |
-
// Fetch and display user's balance
|
| 28 |
-
const fetchBalance = async () => {
|
| 29 |
-
try {
|
| 30 |
-
const response = await fetch(`/balance?username=${currentUser}`);
|
| 31 |
-
const data = await response.json();
|
| 32 |
-
if (data.success) {
|
| 33 |
-
balance = data.balance;
|
| 34 |
-
balanceDisplay.textContent = balance.toFixed(2);
|
| 35 |
-
} else {
|
| 36 |
-
alert(data.message);
|
| 37 |
-
}
|
| 38 |
-
} catch (error) {
|
| 39 |
-
console.error("Error fetching balance:", error);
|
| 40 |
-
alert("Failed to fetch balance.");
|
| 41 |
-
}
|
| 42 |
-
};
|
| 43 |
-
|
| 44 |
// Play sky video and sound
|
| 45 |
const playMedia = () => {
|
| 46 |
if (skyVideo) skyVideo.play();
|
|
@@ -65,81 +40,46 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 65 |
return;
|
| 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 |
-
// Move the plane across the screen
|
| 95 |
-
plane.style.transform = `translateX(${multiplier * 10}px)`;
|
| 96 |
-
|
| 97 |
-
if (multiplier >= crashPoint) {
|
| 98 |
-
clearInterval(gameInterval);
|
| 99 |
-
alert("Plane crashed! You lost your stake.");
|
| 100 |
-
stopMedia(); // Stop video and sound
|
| 101 |
-
resetGame();
|
| 102 |
-
}
|
| 103 |
-
}, 100);
|
| 104 |
-
} else {
|
| 105 |
-
alert(data.message);
|
| 106 |
}
|
| 107 |
-
}
|
| 108 |
-
console.error("Error starting game:", error);
|
| 109 |
-
alert("Failed to start game.");
|
| 110 |
-
}
|
| 111 |
};
|
| 112 |
|
| 113 |
// Cash out before crash
|
| 114 |
-
const cashout =
|
| 115 |
clearInterval(gameInterval);
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
const data = await response.json();
|
| 125 |
-
if (data.success) {
|
| 126 |
-
balance = data.newBalance;
|
| 127 |
-
balanceDisplay.textContent = balance.toFixed(2);
|
| 128 |
-
|
| 129 |
-
alert(
|
| 130 |
-
`You cashed out at ${multiplier.toFixed(2)}x. Winnings: ${data.winnings.toFixed(
|
| 131 |
-
2
|
| 132 |
-
)}`
|
| 133 |
-
);
|
| 134 |
-
stopMedia(); // Stop video and sound
|
| 135 |
-
resetGame();
|
| 136 |
-
} else {
|
| 137 |
-
alert(data.message);
|
| 138 |
-
}
|
| 139 |
-
} catch (error) {
|
| 140 |
-
console.error("Error during cashout:", error);
|
| 141 |
-
alert("Failed to cash out.");
|
| 142 |
-
}
|
| 143 |
};
|
| 144 |
|
| 145 |
// Reset game state
|
|
@@ -148,7 +88,6 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 148 |
plane.style.transform = "translateX(0px)";
|
| 149 |
cashoutBtn.disabled = true;
|
| 150 |
stakeBtn.disabled = false;
|
| 151 |
-
fetchBalance();
|
| 152 |
};
|
| 153 |
|
| 154 |
// Event listeners
|
|
@@ -158,6 +97,6 @@ document.addEventListener("DOMContentLoaded", () => {
|
|
| 158 |
window.location.href = "home";
|
| 159 |
});
|
| 160 |
|
| 161 |
-
//
|
| 162 |
-
|
| 163 |
});
|
|
|
|
| 1 |
document.addEventListener("DOMContentLoaded", () => {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
const balanceDisplay = document.getElementById("balance");
|
| 3 |
const stakeAmountInput = document.getElementById("stake-amount");
|
| 4 |
const stakeBtn = document.getElementById("stake-btn");
|
|
|
|
| 9 |
const skyVideo = document.querySelector(".sky-video");
|
| 10 |
const audio = new Audio("sound.mp3"); // Replace with your sound file path
|
| 11 |
|
| 12 |
+
let balance = 100; // Default balance if no backend interaction
|
| 13 |
let gameInterval;
|
| 14 |
let multiplier = 1.0;
|
| 15 |
let crashPoint = 0;
|
| 16 |
let stakeAmount = 0;
|
| 17 |
let gameId = null;
|
| 18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
// Play sky video and sound
|
| 20 |
const playMedia = () => {
|
| 21 |
if (skyVideo) skyVideo.play();
|
|
|
|
| 40 |
return;
|
| 41 |
}
|
| 42 |
|
| 43 |
+
crashPoint = Math.random() * 5 + 1; // Random crash point for simulation
|
| 44 |
+
multiplier = 1.0;
|
| 45 |
+
|
| 46 |
+
balance -= stakeAmount;
|
| 47 |
+
balanceDisplay.textContent = balance.toFixed(2);
|
| 48 |
+
|
| 49 |
+
cashoutBtn.disabled = false;
|
| 50 |
+
stakeBtn.disabled = true;
|
| 51 |
+
|
| 52 |
+
// Start video and sound
|
| 53 |
+
playMedia();
|
| 54 |
+
|
| 55 |
+
// Start multiplier animation and plane movement
|
| 56 |
+
gameInterval = setInterval(() => {
|
| 57 |
+
multiplier += 0.1;
|
| 58 |
+
multiplierDisplay.textContent = multiplier.toFixed(2) + "x";
|
| 59 |
+
|
| 60 |
+
// Move the plane across the screen
|
| 61 |
+
plane.style.transform = `translateX(${multiplier * 10}px)`;
|
| 62 |
+
|
| 63 |
+
if (multiplier >= crashPoint) {
|
| 64 |
+
clearInterval(gameInterval);
|
| 65 |
+
alert("Plane crashed! You lost your stake.");
|
| 66 |
+
stopMedia(); // Stop video and sound
|
| 67 |
+
resetGame();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
}
|
| 69 |
+
}, 100);
|
|
|
|
|
|
|
|
|
|
| 70 |
};
|
| 71 |
|
| 72 |
// Cash out before crash
|
| 73 |
+
const cashout = () => {
|
| 74 |
clearInterval(gameInterval);
|
| 75 |
|
| 76 |
+
const winnings = stakeAmount * multiplier;
|
| 77 |
+
balance += winnings;
|
| 78 |
+
balanceDisplay.textContent = balance.toFixed(2);
|
| 79 |
+
|
| 80 |
+
alert(`You cashed out at ${multiplier.toFixed(2)}x. Winnings: ${winnings.toFixed(2)}`);
|
| 81 |
+
stopMedia(); // Stop video and sound
|
| 82 |
+
resetGame();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
};
|
| 84 |
|
| 85 |
// Reset game state
|
|
|
|
| 88 |
plane.style.transform = "translateX(0px)";
|
| 89 |
cashoutBtn.disabled = true;
|
| 90 |
stakeBtn.disabled = false;
|
|
|
|
| 91 |
};
|
| 92 |
|
| 93 |
// Event listeners
|
|
|
|
| 97 |
window.location.href = "home";
|
| 98 |
});
|
| 99 |
|
| 100 |
+
// Initialize balance
|
| 101 |
+
balanceDisplay.textContent = balance.toFixed(2);
|
| 102 |
});
|