Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +63 -48
static/script.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
let balance = 0;
|
2 |
let flipsLeft = 1000;
|
3 |
let currentCoin = 0;
|
|
|
4 |
|
5 |
function updateInfo() {
|
6 |
document.getElementById("balance").textContent = balance.toFixed(2);
|
@@ -8,7 +9,7 @@ function updateInfo() {
|
|
8 |
}
|
9 |
|
10 |
function flipCoin() {
|
11 |
-
if (flipsLeft > 0) {
|
12 |
fetch("/flip", {
|
13 |
method: "POST",
|
14 |
headers: {
|
@@ -19,65 +20,79 @@ function flipCoin() {
|
|
19 |
.then((response) => response.json())
|
20 |
.then((data) => {
|
21 |
const coin = document.getElementById("coin");
|
22 |
-
coin.textContent = data.
|
23 |
-
|
24 |
-
|
25 |
-
}
|
26 |
-
flipsLeft--;
|
27 |
updateInfo();
|
|
|
|
|
|
|
|
|
|
|
28 |
});
|
29 |
}
|
30 |
}
|
31 |
|
32 |
function buyCoin(index) {
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
coins[index].color;
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
|
|
52 |
}
|
53 |
|
54 |
function generateCoin() {
|
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 |
document.addEventListener("DOMContentLoaded", () => {
|
80 |
updateInfo();
|
81 |
document.getElementById("coin").onclick = flipCoin;
|
82 |
document.getElementById("generate-coin").onclick = generateCoin;
|
83 |
-
});
|
|
|
1 |
let balance = 0;
|
2 |
let flipsLeft = 1000;
|
3 |
let currentCoin = 0;
|
4 |
+
let isGenerating = false;
|
5 |
|
6 |
function updateInfo() {
|
7 |
document.getElementById("balance").textContent = balance.toFixed(2);
|
|
|
9 |
}
|
10 |
|
11 |
function flipCoin() {
|
12 |
+
if (flipsLeft > 0 && !isGenerating) {
|
13 |
fetch("/flip", {
|
14 |
method: "POST",
|
15 |
headers: {
|
|
|
20 |
.then((response) => response.json())
|
21 |
.then((data) => {
|
22 |
const coin = document.getElementById("coin");
|
23 |
+
coin.textContent = data.result;
|
24 |
+
balance = data.balance;
|
25 |
+
flipsLeft = data.flips_left;
|
|
|
|
|
26 |
updateInfo();
|
27 |
+
|
28 |
+
// Reset coin appearance after a short delay
|
29 |
+
setTimeout(() => {
|
30 |
+
coin.textContent = "";
|
31 |
+
}, 500);
|
32 |
});
|
33 |
}
|
34 |
}
|
35 |
|
36 |
function buyCoin(index) {
|
37 |
+
if (!isGenerating) {
|
38 |
+
fetch("/buy", {
|
39 |
+
method: "POST",
|
40 |
+
headers: {
|
41 |
+
"Content-Type": "application/json",
|
42 |
+
},
|
43 |
+
body: JSON.stringify({ index: index }),
|
44 |
+
})
|
45 |
+
.then((response) => response.json())
|
46 |
+
.then((data) => {
|
47 |
+
if (data.success) {
|
48 |
+
balance = data.balance;
|
49 |
+
currentCoin = index;
|
50 |
+
document.getElementById("coin").style.backgroundColor = coins[index].color;
|
51 |
+
updateInfo();
|
52 |
+
} else {
|
53 |
+
alert("Not enough money to buy this coin!");
|
54 |
+
}
|
55 |
+
});
|
56 |
+
}
|
57 |
}
|
58 |
|
59 |
function generateCoin() {
|
60 |
+
if (!isGenerating) {
|
61 |
+
isGenerating = true;
|
62 |
+
document.getElementById("loading-overlay").style.display = "flex";
|
63 |
+
fetch("/generate_coin", {
|
64 |
+
method: "POST",
|
65 |
+
})
|
66 |
+
.then((response) => response.json())
|
67 |
+
.then((data) => {
|
68 |
+
if (data.success) {
|
69 |
+
coins.push(data.coin);
|
70 |
+
const shop = document.getElementById("shop");
|
71 |
+
const newCoin = document.createElement("div");
|
72 |
+
newCoin.className = "shop-item";
|
73 |
+
newCoin.style.backgroundColor = data.coin.color;
|
74 |
+
newCoin.innerHTML = `
|
75 |
+
<div class="shop-coin">
|
76 |
+
<div>P: ${data.coin.winrate.toFixed(2)}</div>
|
77 |
+
<div>V: $${data.coin.value.toFixed(2)}</div>
|
78 |
+
<div>C: $${data.coin.price.toFixed(2)}</div>
|
79 |
+
</div>
|
80 |
+
`;
|
81 |
+
newCoin.onclick = () => buyCoin(coins.length - 1);
|
82 |
+
shop.appendChild(newCoin);
|
83 |
+
} else {
|
84 |
+
console.error("Failed to generate new coin:", data.error);
|
85 |
+
}
|
86 |
+
})
|
87 |
+
.finally(() => {
|
88 |
+
isGenerating = false;
|
89 |
+
document.getElementById("loading-overlay").style.display = "none";
|
90 |
+
});
|
91 |
+
}
|
92 |
}
|
93 |
|
94 |
document.addEventListener("DOMContentLoaded", () => {
|
95 |
updateInfo();
|
96 |
document.getElementById("coin").onclick = flipCoin;
|
97 |
document.getElementById("generate-coin").onclick = generateCoin;
|
98 |
+
});
|