Update static/js/game.js
Browse files- static/js/game.js +92 -64
static/js/game.js
CHANGED
@@ -1,32 +1,28 @@
|
|
1 |
let balance = 10;
|
2 |
let flipsLeft = 1000;
|
3 |
-
let currentCoin =
|
4 |
-
let coins =
|
5 |
|
6 |
function updateStats() {
|
7 |
document.getElementById('balance').textContent = `$${balance.toFixed(2)}`;
|
8 |
-
document.getElementById('flips').textContent = flipsLeft;
|
9 |
}
|
10 |
|
11 |
function updateShop() {
|
12 |
const shop = document.getElementById('shop');
|
13 |
shop.innerHTML = '';
|
14 |
|
15 |
-
|
16 |
const coinElement = document.createElement('div');
|
17 |
-
coinElement.className =
|
18 |
-
coinElement.
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
container.appendChild(priceElement);
|
27 |
-
|
28 |
-
shop.appendChild(container);
|
29 |
-
}
|
30 |
|
31 |
const mintButton = document.createElement('button');
|
32 |
mintButton.id = 'mint-button';
|
@@ -35,36 +31,43 @@ function updateShop() {
|
|
35 |
shop.appendChild(mintButton);
|
36 |
}
|
37 |
|
38 |
-
function selectCoin(
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
updateShop();
|
43 |
-
} else {
|
44 |
-
alert("You can't afford this coin!");
|
45 |
}
|
46 |
}
|
47 |
|
48 |
async function flipCoin() {
|
49 |
if (flipsLeft > 0) {
|
50 |
flipsLeft--;
|
51 |
-
const response = await fetch('/flip', {
|
52 |
method: 'POST',
|
53 |
headers: {
|
54 |
'Content-Type': 'application/json',
|
55 |
},
|
56 |
-
body: JSON.stringify({
|
57 |
});
|
58 |
-
const
|
59 |
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
65 |
|
66 |
if (flipsLeft === 0) {
|
67 |
-
gameOver
|
68 |
}
|
69 |
}
|
70 |
}
|
@@ -72,59 +75,84 @@ async function flipCoin() {
|
|
72 |
async function mintCoin() {
|
73 |
if (balance >= 4) {
|
74 |
balance -= 4;
|
75 |
-
const response = await fetch('/mint', { method: 'POST' });
|
76 |
const newCoin = await response.json();
|
77 |
-
coins
|
78 |
-
updateShop();
|
79 |
updateStats();
|
80 |
-
|
81 |
-
alert("You don't have enough money to mint a new coin!");
|
82 |
}
|
83 |
}
|
84 |
|
85 |
async function gameOver() {
|
86 |
-
document.
|
87 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
88 |
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
const leaderboardBody = document.getElementById('leaderboard-body');
|
93 |
-
leaderboardBody.innerHTML = '';
|
94 |
|
95 |
-
|
96 |
-
const row = leaderboardBody.insertRow();
|
97 |
-
row.insertCell(0).textContent = index + 1;
|
98 |
-
row.insertCell(1).textContent = entry.initials;
|
99 |
-
row.insertCell(2).textContent = entry.score;
|
100 |
-
});
|
101 |
}
|
102 |
|
103 |
-
async function submitScore() {
|
104 |
-
|
|
|
105 |
if (initials) {
|
106 |
-
await fetch('/leaderboard', {
|
107 |
method: 'POST',
|
108 |
headers: {
|
109 |
'Content-Type': 'application/json',
|
110 |
},
|
111 |
body: JSON.stringify({ initials, score: balance }),
|
112 |
});
|
113 |
-
|
114 |
-
} else {
|
115 |
-
alert('Please enter your initials!');
|
116 |
}
|
117 |
}
|
118 |
|
119 |
-
function
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
}
|
122 |
|
123 |
-
|
124 |
-
const response = await fetch('/');
|
125 |
-
|
126 |
-
coins = JSON.parse(data.match(/coins = (.+?);/)[1]);
|
127 |
updateStats();
|
128 |
updateShop();
|
129 |
-
document.getElementById('coin').
|
130 |
-
}
|
|
|
|
|
|
1 |
let balance = 10;
|
2 |
let flipsLeft = 1000;
|
3 |
+
let currentCoin = 1;
|
4 |
+
let coins = [];
|
5 |
|
6 |
function updateStats() {
|
7 |
document.getElementById('balance').textContent = `$${balance.toFixed(2)}`;
|
8 |
+
document.getElementById('flips-left').textContent = flipsLeft;
|
9 |
}
|
10 |
|
11 |
function updateShop() {
|
12 |
const shop = document.getElementById('shop');
|
13 |
shop.innerHTML = '';
|
14 |
|
15 |
+
coins.forEach(coin => {
|
16 |
const coinElement = document.createElement('div');
|
17 |
+
coinElement.className = 'shop-item';
|
18 |
+
coinElement.innerHTML = `
|
19 |
+
<div class="coin-icon ${coin.id === currentCoin ? 'selected' : ''}"
|
20 |
+
style="background-color: ${coin.color};"
|
21 |
+
onclick="selectCoin(${coin.id})"></div>
|
22 |
+
<div>$${coin.price}</div>
|
23 |
+
`;
|
24 |
+
shop.appendChild(coinElement);
|
25 |
+
});
|
|
|
|
|
|
|
|
|
26 |
|
27 |
const mintButton = document.createElement('button');
|
28 |
mintButton.id = 'mint-button';
|
|
|
31 |
shop.appendChild(mintButton);
|
32 |
}
|
33 |
|
34 |
+
function selectCoin(id) {
|
35 |
+
const coin = coins.find(c => c.id === id);
|
36 |
+
if (coin && balance >= coin.price) {
|
37 |
+
balance -= coin.price;
|
38 |
+
currentCoin = id;
|
39 |
+
updateStats();
|
40 |
updateShop();
|
|
|
|
|
41 |
}
|
42 |
}
|
43 |
|
44 |
async function flipCoin() {
|
45 |
if (flipsLeft > 0) {
|
46 |
flipsLeft--;
|
47 |
+
const response = await fetch('/api/flip', {
|
48 |
method: 'POST',
|
49 |
headers: {
|
50 |
'Content-Type': 'application/json',
|
51 |
},
|
52 |
+
body: JSON.stringify({ coinId: currentCoin }),
|
53 |
});
|
54 |
+
const result = await response.json();
|
55 |
|
56 |
+
const coin = document.getElementById('coin');
|
57 |
+
coin.style.transform = 'rotateY(720deg)';
|
58 |
+
setTimeout(() => {
|
59 |
+
coin.style.transform = 'rotateY(0deg)';
|
60 |
+
if (result.result === 'heads') {
|
61 |
+
balance += result.value;
|
62 |
+
coin.textContent = 'H';
|
63 |
+
} else {
|
64 |
+
coin.textContent = 'T';
|
65 |
+
}
|
66 |
+
updateStats();
|
67 |
+
}, 500);
|
68 |
|
69 |
if (flipsLeft === 0) {
|
70 |
+
setTimeout(gameOver, 1000);
|
71 |
}
|
72 |
}
|
73 |
}
|
|
|
75 |
async function mintCoin() {
|
76 |
if (balance >= 4) {
|
77 |
balance -= 4;
|
78 |
+
const response = await fetch('/api/mint', { method: 'POST' });
|
79 |
const newCoin = await response.json();
|
80 |
+
coins.push(newCoin);
|
|
|
81 |
updateStats();
|
82 |
+
updateShop();
|
|
|
83 |
}
|
84 |
}
|
85 |
|
86 |
async function gameOver() {
|
87 |
+
const gameOverScreen = document.createElement('div');
|
88 |
+
gameOverScreen.id = 'game-over';
|
89 |
+
gameOverScreen.innerHTML = `
|
90 |
+
<h2>Game Over</h2>
|
91 |
+
<p>Your final balance: $${balance.toFixed(2)}</p>
|
92 |
+
<div id="leaderboard"></div>
|
93 |
+
<form id="initials-form">
|
94 |
+
<input type="text" id="initials-input" maxlength="3" placeholder="Enter your initials">
|
95 |
+
<button type="submit" id="submit-score">Submit Score</button>
|
96 |
+
</form>
|
97 |
+
<button id="play-again">Play Again</button>
|
98 |
+
`;
|
99 |
+
document.body.appendChild(gameOverScreen);
|
100 |
|
101 |
+
document.getElementById('initials-form').onsubmit = submitScore;
|
102 |
+
document.getElementById('play-again').onclick = resetGame;
|
|
|
|
|
|
|
103 |
|
104 |
+
await updateLeaderboard();
|
|
|
|
|
|
|
|
|
|
|
105 |
}
|
106 |
|
107 |
+
async function submitScore(event) {
|
108 |
+
event.preventDefault();
|
109 |
+
const initials = document.getElementById('initials-input').value.toUpperCase();
|
110 |
if (initials) {
|
111 |
+
await fetch('/api/leaderboard', {
|
112 |
method: 'POST',
|
113 |
headers: {
|
114 |
'Content-Type': 'application/json',
|
115 |
},
|
116 |
body: JSON.stringify({ initials, score: balance }),
|
117 |
});
|
118 |
+
await updateLeaderboard();
|
|
|
|
|
119 |
}
|
120 |
}
|
121 |
|
122 |
+
async function updateLeaderboard() {
|
123 |
+
const response = await fetch('/api/leaderboard');
|
124 |
+
const leaderboard = await response.json();
|
125 |
+
const leaderboardElement = document.getElementById('leaderboard');
|
126 |
+
leaderboardElement.innerHTML = `
|
127 |
+
<h3>Leaderboard</h3>
|
128 |
+
<table>
|
129 |
+
<tr><th>Rank</th><th>Initials</th><th>Score</th></tr>
|
130 |
+
${leaderboard.map((entry, index) => `
|
131 |
+
<tr>
|
132 |
+
<td>${index + 1}</td>
|
133 |
+
<td>${entry.initials}</td>
|
134 |
+
<td>$${entry.score.toFixed(2)}</td>
|
135 |
+
</tr>
|
136 |
+
`).join('')}
|
137 |
+
</table>
|
138 |
+
`;
|
139 |
+
}
|
140 |
+
|
141 |
+
function resetGame() {
|
142 |
+
balance = 10;
|
143 |
+
flipsLeft = 1000;
|
144 |
+
currentCoin = 1;
|
145 |
+
updateStats();
|
146 |
+
updateShop();
|
147 |
+
document.getElementById('game-over').remove();
|
148 |
}
|
149 |
|
150 |
+
async function initGame() {
|
151 |
+
const response = await fetch('/api/coins');
|
152 |
+
coins = await response.json();
|
|
|
153 |
updateStats();
|
154 |
updateShop();
|
155 |
+
document.getElementById('coin').onclick = flipCoin;
|
156 |
+
}
|
157 |
+
|
158 |
+
initGame();
|