Sergidev commited on
Commit
e9abc3c
1 Parent(s): 39af35a

Update static/script.js

Browse files
Files changed (1) hide show
  1. 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.is_heads ? "H" : "T";
23
- if (data.is_heads) {
24
- balance += data.value;
25
- }
26
- flipsLeft--;
27
  updateInfo();
 
 
 
 
 
28
  });
29
  }
30
  }
31
 
32
  function buyCoin(index) {
33
- fetch("/buy", {
34
- method: "POST",
35
- headers: {
36
- "Content-Type": "application/json",
37
- },
38
- body: JSON.stringify({ coin_index: index, balance: balance }),
39
- })
40
- .then((response) => response.json())
41
- .then((data) => {
42
- if (data.success) {
43
- balance -= coins[index].price;
44
- currentCoin = index;
45
- document.getElementById("coin").style.backgroundColor =
46
- coins[index].color;
47
- updateInfo();
48
- } else {
49
- alert("Not enough money to buy this coin!");
50
- }
51
- });
 
52
  }
53
 
54
  function generateCoin() {
55
- fetch("/generate_coin", {
56
- method: "POST",
57
- })
58
- .then((response) => response.json())
59
- .then((data) => {
60
- if (data.success) {
61
- coins.push(data.coin);
62
- const shop = document.getElementById("shop");
63
- const newCoin = document.createElement("div");
64
- newCoin.className = "shop-coin";
65
- newCoin.style.backgroundColor = data.coin.color;
66
- newCoin.innerHTML = `
67
- <div>P: ${data.coin.winrate.toFixed(2)}</div>
68
- <div>V: $${data.coin.value.toFixed(2)}</div>
69
- <div>C: $${data.coin.price.toFixed(2)}</div>
70
- `;
71
- newCoin.onclick = () => buyCoin(coins.length - 1);
72
- shop.appendChild(newCoin);
73
- } else {
74
- console.error("Failed to generate new coin:", data.error);
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
+ });