Sergidev commited on
Commit
3c7fe9f
1 Parent(s): 697e0da

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +162 -76
static/script.js CHANGED
@@ -4,95 +4,181 @@ let currentCoin = 0;
4
  let isGenerating = false;
5
 
6
  function updateInfo() {
7
- document.getElementById("balance").textContent = balance.toFixed(2);
8
- document.getElementById("flips-left").textContent = flipsLeft;
9
  }
10
 
11
  function flipCoin() {
12
- if (flipsLeft > 0 && !isGenerating) {
13
- fetch("/flip", {
14
- method: "POST",
15
- headers: {
16
- "Content-Type": "application/json",
17
- },
18
- body: JSON.stringify({ coin_index: currentCoin }),
19
- })
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
  });
 
4
  let isGenerating = false;
5
 
6
  function updateInfo() {
7
+ document.getElementById("balance").textContent = balance.toFixed(2);
8
+ document.getElementById("flips-left").textContent = flipsLeft;
9
  }
10
 
11
  function flipCoin() {
12
+ if (flipsLeft > 0 && !isGenerating) {
13
+ fetch("/flip", {
14
+ method: "POST",
15
+ headers: {
16
+ "Content-Type": "application/json",
17
+ },
18
+ body: JSON.stringify({ coin_index: currentCoin }),
19
+ })
20
+ .then((response) => response.json())
21
+ .then((data) => {
22
+ if (data.error) {
23
+ alert(data.error);
24
+ } else {
25
+ const coin = document.getElementById("coin");
26
+ coin.textContent = data.result;
27
+ balance = data.balance;
28
+ flipsLeft = data.flips_left;
29
+ updateInfo();
30
+
31
+ // Reset coin appearance after a short delay
32
+ setTimeout(() => {
33
+ coin.textContent = "";
34
+ }, 500);
35
+
36
+ if (flipsLeft === 0) {
37
+ showGameOver();
38
+ }
39
+ }
40
+ });
41
+ }
42
  }
43
 
44
  function buyCoin(index) {
45
+ if (!isGenerating && index !== currentCoin) {
46
+ fetch("/buy", {
47
+ method: "POST",
48
+ headers: {
49
+ "Content-Type": "application/json",
50
+ },
51
+ body: JSON.stringify({ index: index }),
52
+ })
53
+ .then((response) => response.json())
54
+ .then((data) => {
55
+ if (data.success) {
56
+ balance = data.balance;
57
+ currentCoin = index;
58
+ document.getElementById("coin").style.backgroundColor = coins[index].color;
59
+ updateInfo();
60
+ } else {
61
+ alert("Not enough money to buy this coin or you already own it!");
62
+ }
63
+ });
64
+ }
65
  }
66
 
67
  function generateCoin() {
68
+ if (!isGenerating && balance >= 4) {
69
+ isGenerating = true;
70
+ document.getElementById("loading-overlay").style.display = "flex";
71
+ fetch("/generate_coin", {
72
+ method: "POST",
73
+ })
74
+ .then((response) => response.json())
75
+ .then((data) => {
76
+ if (data.success) {
77
+ coins.push(data.coin);
78
+ const shop = document.getElementById("shop");
79
+ const newCoin = createShopItem(data.coin, coins.length - 1);
80
+ shop.appendChild(newCoin);
81
+ balance = data.balance;
82
+ updateInfo();
83
+ } else {
84
+ alert("Failed to generate new coin: " + data.error);
85
+ }
86
+ })
87
+ .finally(() => {
88
+ isGenerating = false;
89
+ document.getElementById("loading-overlay").style.display = "none";
90
+ });
91
+ } else if (balance < 4) {
92
+ alert("Not enough balance to generate a coin");
93
+ }
94
+ }
95
+
96
+ function createShopItem(coin, index) {
97
+ const shopItem = document.createElement("div");
98
+ shopItem.className = "shop-item";
99
+ shopItem.dataset.index = index;
100
+
101
+ const shopCoin = document.createElement("div");
102
+ shopCoin.className = "shop-coin";
103
+ shopCoin.style.backgroundColor = coin.color;
104
+
105
+ const coinPrice = document.createElement("span");
106
+ coinPrice.className = "coin-price";
107
+ coinPrice.textContent = `$${coin.price.toFixed(2)}`;
108
+
109
+ const coinName = document.createElement("div");
110
+ coinName.className = "coin-name";
111
+ coinName.textContent = coin.name;
112
+
113
+ const coinTooltip = document.createElement("div");
114
+ coinTooltip.className = "coin-tooltip";
115
+ coinTooltip.innerHTML = `
116
+ <strong>${coin.name}</strong><br>
117
+ Cost: $${coin.price.toFixed(2)}<br>
118
+ Value: $${coin.value.toFixed(2)}<br>
119
+ Win rate: ${coin.winrate.toFixed(2)}<br>
120
+ ${coin.ability ? `Ability: ${coin.ability}` : ''}
121
+ `;
122
+
123
+ shopCoin.appendChild(coinPrice);
124
+ shopItem.appendChild(shopCoin);
125
+ shopItem.appendChild(coinName);
126
+ shopItem.appendChild(coinTooltip);
127
+
128
+ shopItem.onclick = () => buyCoin(index);
129
+
130
+ return shopItem;
131
+ }
132
+
133
+ function showGameOver() {
134
+ const modal = document.getElementById("game-over");
135
+ const finalScore = document.getElementById("final-score");
136
+ finalScore.textContent = balance.toFixed(2);
137
+ modal.style.display = "block";
138
+ }
139
+
140
+ function submitScore() {
141
+ const initials = document.getElementById("initials").value.toUpperCase();
142
+ if (initials.length > 0) {
143
+ fetch("/game_over", {
144
+ method: "POST",
145
+ headers: {
146
+ "Content-Type": "application/json",
147
+ },
148
+ body: JSON.stringify({ initials: initials }),
149
+ })
150
+ .then((response) => response.json())
151
+ .then((data) => {
152
+ if (data.success) {
153
+ location.reload(); // Reload the page to start a new game and show updated leaderboard
154
+ }
155
+ });
156
+ } else {
157
+ alert("Please enter your initials");
158
+ }
159
+ }
160
+
161
+ function resetGame() {
162
+ fetch("/reset_game", {
163
+ method: "POST",
164
  })
165
+ .then((response) => response.json())
166
+ .then((data) => {
167
  if (data.success) {
168
+ balance = 0;
169
+ flipsLeft = 1000;
170
+ currentCoin = 0;
171
+ updateInfo();
172
+ document.getElementById("coin").style.backgroundColor = coins[0].color;
173
+ document.getElementById("game-over").style.display = "none";
 
 
 
 
 
 
 
 
 
 
174
  }
175
+ });
 
 
 
 
 
176
  }
177
 
178
  document.addEventListener("DOMContentLoaded", () => {
179
+ updateInfo();
180
+ document.getElementById("coin").onclick = flipCoin;
181
+ document.getElementById("generate-coin").onclick = generateCoin;
182
+ document.getElementById("submit-score").onclick = submitScore;
183
+ document.getElementById("play-again").onclick = resetGame;
184
  });