Create js/game.js
Browse files- static/js/game.js +130 -0
static/js/game.js
ADDED
@@ -0,0 +1,130 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
let balance = 10;
|
2 |
+
let flipsLeft = 1000;
|
3 |
+
let currentCoin = 'bronze';
|
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 |
+
for (const [name, coin] of Object.entries(coins)) {
|
16 |
+
const coinElement = document.createElement('div');
|
17 |
+
coinElement.className = `shop-coin ${name === currentCoin ? 'selected' : ''}`;
|
18 |
+
coinElement.style.backgroundColor = coin.color;
|
19 |
+
coinElement.onclick = () => selectCoin(name);
|
20 |
+
|
21 |
+
const priceElement = document.createElement('div');
|
22 |
+
priceElement.textContent = `$${coin.price}`;
|
23 |
+
|
24 |
+
const container = document.createElement('div');
|
25 |
+
container.appendChild(coinElement);
|
26 |
+
container.appendChild(priceElement);
|
27 |
+
|
28 |
+
shop.appendChild(container);
|
29 |
+
}
|
30 |
+
|
31 |
+
const mintButton = document.createElement('button');
|
32 |
+
mintButton.id = 'mint-button';
|
33 |
+
mintButton.textContent = '🎲 Mint ($4)';
|
34 |
+
mintButton.onclick = mintCoin;
|
35 |
+
shop.appendChild(mintButton);
|
36 |
+
}
|
37 |
+
|
38 |
+
function selectCoin(name) {
|
39 |
+
if (coins[name].price <= balance) {
|
40 |
+
currentCoin = name;
|
41 |
+
document.getElementById('coin').style.backgroundColor = coins[name].color;
|
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({ coin: currentCoin }),
|
57 |
+
});
|
58 |
+
const data = await response.json();
|
59 |
+
|
60 |
+
if (data.result === 'heads') {
|
61 |
+
balance += data.value;
|
62 |
+
}
|
63 |
+
|
64 |
+
updateStats();
|
65 |
+
|
66 |
+
if (flipsLeft === 0) {
|
67 |
+
gameOver();
|
68 |
+
}
|
69 |
+
}
|
70 |
+
}
|
71 |
+
|
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[newCoin.name] = newCoin;
|
78 |
+
updateShop();
|
79 |
+
updateStats();
|
80 |
+
} else {
|
81 |
+
alert("You don't have enough money to mint a new coin!");
|
82 |
+
}
|
83 |
+
}
|
84 |
+
|
85 |
+
async function gameOver() {
|
86 |
+
document.getElementById('game-area').style.display = 'none';
|
87 |
+
document.getElementById('game-over').style.display = 'flex';
|
88 |
+
|
89 |
+
const response = await fetch('/leaderboard');
|
90 |
+
const leaderboard = await response.json();
|
91 |
+
|
92 |
+
const leaderboardBody = document.getElementById('leaderboard-body');
|
93 |
+
leaderboardBody.innerHTML = '';
|
94 |
+
|
95 |
+
leaderboard.forEach((entry, index) => {
|
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 |
+
const initials = document.getElementById('initials').value;
|
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 |
+
location.reload();
|
114 |
+
} else {
|
115 |
+
alert('Please enter your initials!');
|
116 |
+
}
|
117 |
+
}
|
118 |
+
|
119 |
+
function playAgain() {
|
120 |
+
location.reload();
|
121 |
+
}
|
122 |
+
|
123 |
+
window.onload = async function() {
|
124 |
+
const response = await fetch('/');
|
125 |
+
const data = await response.text();
|
126 |
+
coins = JSON.parse(data.match(/coins = (.+?);/)[1]);
|
127 |
+
updateStats();
|
128 |
+
updateShop();
|
129 |
+
document.getElementById('coin').style.backgroundColor = coins[currentCoin].color;
|
130 |
+
};
|