Roll dice

By jennybo on Jun 17, 2024

<!DOCTYPE html>
<html>
<head>
    <title>Crypto Casino</title>
    <script>
        async function placeBet() {
            const betAmount = parseFloat(document.getElementById('betAmount').value);
            const response = await fetch('/api/rollDice', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ betAmount })
            });
            const result = await response.json();
            document.getElementById('result').innerText = result.message;
        }
    </script>
</head>
<body>
    <h1>Crypto Casino</h1>
    <label for="betAmount">Bet Amount (BTC):</label>
    <input type="number" id="betAmount" step="0.01">
    <button onclick="placeBet()">Roll Dice</button>
    <p id="result"></p>
</body>
</html>
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const PORT = 3000;

app.use(bodyParser.json());

app.post('/api/rollDice', (req, res) => {
    const { betAmount } = req.body;
    const diceResult = Math.floor(Math.random() * 6) + 1;
    let message;
    if (diceResult >= 4) {
        message = `You win! You rolled a ${diceResult}. You won ${(betAmount * 2).toFixed(2)} BTC.`;
    } else {
        message = `You lose! You rolled a ${diceResult}. Better luck next time!`;
    }
    res.json({ message });
});

app.listen(PORT, () => {
    console.log(`Server is running on http://localhost:${PORT}`);
});

Comments

Sign in to comment.
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.