Spaces:
Sleeping
Sleeping
File size: 5,882 Bytes
933256c e9abc3c 933256c 3c7fe9f 933256c 3c7fe9f 933256c 3c7fe9f 933256c 3c7fe9f c844db2 3c7fe9f e9abc3c 3c7fe9f e9abc3c 3c7fe9f e9abc3c 3c7fe9f 933256c 3c7fe9f c844db2 e9abc3c |
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 |
let balance = 0;
let flipsLeft = 1000;
let currentCoin = 0;
let isGenerating = false;
function updateInfo() {
document.getElementById("balance").textContent = balance.toFixed(2);
document.getElementById("flips-left").textContent = flipsLeft;
}
function flipCoin() {
if (flipsLeft > 0 && !isGenerating) {
fetch("/flip", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ coin_index: currentCoin }),
})
.then((response) => response.json())
.then((data) => {
if (data.error) {
alert(data.error);
} else {
const coin = document.getElementById("coin");
coin.textContent = data.result;
balance = data.balance;
flipsLeft = data.flips_left;
updateInfo();
// Reset coin appearance after a short delay
setTimeout(() => {
coin.textContent = "";
}, 500);
if (flipsLeft === 0) {
showGameOver();
}
}
});
}
}
function buyCoin(index) {
if (!isGenerating && index !== currentCoin) {
fetch("/buy", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ index: index }),
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
balance = data.balance;
currentCoin = index;
document.getElementById("coin").style.backgroundColor = coins[index].color;
updateInfo();
} else {
alert("Not enough money to buy this coin or you already own it!");
}
});
}
}
function generateCoin() {
if (!isGenerating && balance >= 4) {
isGenerating = true;
document.getElementById("loading-overlay").style.display = "flex";
fetch("/generate_coin", {
method: "POST",
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
coins.push(data.coin);
const shop = document.getElementById("shop");
const newCoin = createShopItem(data.coin, coins.length - 1);
shop.appendChild(newCoin);
balance = data.balance;
updateInfo();
} else {
alert("Failed to generate new coin: " + data.error);
}
})
.finally(() => {
isGenerating = false;
document.getElementById("loading-overlay").style.display = "none";
});
} else if (balance < 4) {
alert("Not enough balance to generate a coin");
}
}
function createShopItem(coin, index) {
const shopItem = document.createElement("div");
shopItem.className = "shop-item";
shopItem.dataset.index = index;
const shopCoin = document.createElement("div");
shopCoin.className = "shop-coin";
shopCoin.style.backgroundColor = coin.color;
const coinPrice = document.createElement("span");
coinPrice.className = "coin-price";
coinPrice.textContent = `$${coin.price.toFixed(2)}`;
const coinName = document.createElement("div");
coinName.className = "coin-name";
coinName.textContent = coin.name;
const coinTooltip = document.createElement("div");
coinTooltip.className = "coin-tooltip";
coinTooltip.innerHTML = `
<strong>${coin.name}</strong><br>
Cost: $${coin.price.toFixed(2)}<br>
Value: $${coin.value.toFixed(2)}<br>
Win rate: ${coin.winrate.toFixed(2)}<br>
${coin.bonus !== 'none' ? `Bonus: ${coin.bonus}` : ''}
`;
shopCoin.appendChild(coinPrice);
shopItem.appendChild(shopCoin);
shopItem.appendChild(coinName);
shopItem.appendChild(coinTooltip);
shopItem.onclick = () => buyCoin(index);
return shopItem;
}
function showGameOver() {
const modal = document.getElementById("game-over");
const finalScore = document.getElementById("final-score");
finalScore.textContent = balance.toFixed(2);
modal.style.display = "block";
}
function submitScore() {
const initials = document.getElementById("initials").value.toUpperCase();
if (initials.length > 0) {
fetch("/game_over", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ initials: initials }),
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
location.reload(); // Reload the page to start a new game and show updated leaderboard
}
});
} else {
alert("Please enter your initials");
}
}
function resetGame() {
fetch("/reset_game", {
method: "POST",
})
.then((response) => response.json())
.then((data) => {
if (data.success) {
balance = 0;
flipsLeft = 1000;
currentCoin = 0;
updateInfo();
document.getElementById("coin").style.backgroundColor = coins[0].color;
document.getElementById("game-over").style.display = "none";
}
});
}
document.addEventListener("DOMContentLoaded", () => {
updateInfo();
document.getElementById("coin").onclick = flipCoin;
document.getElementById("generate-coin").onclick = generateCoin;
document.getElementById("submit-score").onclick = submitScore;
document.getElementById("play-again").onclick = resetGame;
document.getElementById("coin").style.backgroundColor = coins[0].color;
}); |