File size: 3,835 Bytes
512b774
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
let balance = 10;
let flipsLeft = 1000;
let currentCoin = 'bronze';
let coins = {};

function updateStats() {
    document.getElementById('balance').textContent = `$${balance.toFixed(2)}`;
    document.getElementById('flips').textContent = flipsLeft;
}

function updateShop() {
    const shop = document.getElementById('shop');
    shop.innerHTML = '';
    
    for (const [name, coin] of Object.entries(coins)) {
        const coinElement = document.createElement('div');
        coinElement.className = `shop-coin ${name === currentCoin ? 'selected' : ''}`;
        coinElement.style.backgroundColor = coin.color;
        coinElement.onclick = () => selectCoin(name);
        
        const priceElement = document.createElement('div');
        priceElement.textContent = `$${coin.price}`;
        
        const container = document.createElement('div');
        container.appendChild(coinElement);
        container.appendChild(priceElement);
        
        shop.appendChild(container);
    }
    
    const mintButton = document.createElement('button');
    mintButton.id = 'mint-button';
    mintButton.textContent = '🎲 Mint ($4)';
    mintButton.onclick = mintCoin;
    shop.appendChild(mintButton);
}

function selectCoin(name) {
    if (coins[name].price <= balance) {
        currentCoin = name;
        document.getElementById('coin').style.backgroundColor = coins[name].color;
        updateShop();
    } else {
        alert("You can't afford this coin!");
    }
}

async function flipCoin() {
    if (flipsLeft > 0) {
        flipsLeft--;
        const response = await fetch('/flip', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ coin: currentCoin }),
        });
        const data = await response.json();
        
        if (data.result === 'heads') {
            balance += data.value;
        }
        
        updateStats();
        
        if (flipsLeft === 0) {
            gameOver();
        }
    }
}

async function mintCoin() {
    if (balance >= 4) {
        balance -= 4;
        const response = await fetch('/mint', { method: 'POST' });
        const newCoin = await response.json();
        coins[newCoin.name] = newCoin;
        updateShop();
        updateStats();
    } else {
        alert("You don't have enough money to mint a new coin!");
    }
}

async function gameOver() {
    document.getElementById('game-area').style.display = 'none';
    document.getElementById('game-over').style.display = 'flex';
    
    const response = await fetch('/leaderboard');
    const leaderboard = await response.json();
    
    const leaderboardBody = document.getElementById('leaderboard-body');
    leaderboardBody.innerHTML = '';
    
    leaderboard.forEach((entry, index) => {
        const row = leaderboardBody.insertRow();
        row.insertCell(0).textContent = index + 1;
        row.insertCell(1).textContent = entry.initials;
        row.insertCell(2).textContent = entry.score;
    });
}

async function submitScore() {
    const initials = document.getElementById('initials').value;
    if (initials) {
        await fetch('/leaderboard', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
            },
            body: JSON.stringify({ initials, score: balance }),
        });
        location.reload();
    } else {
        alert('Please enter your initials!');
    }
}

function playAgain() {
    location.reload();
}

window.onload = async function() {
    const response = await fetch('/');
    const data = await response.text();
    coins = JSON.parse(data.match(/coins = (.+?);/)[1]);
    updateStats();
    updateShop();
    document.getElementById('coin').style.backgroundColor = coins[currentCoin].color;
};