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