fix discarded die roll thing

This commit is contained in:
Greg Gauthier 2024-06-28 19:10:35 +01:00
parent 0d5050a004
commit 81b651fe52
2 changed files with 49 additions and 47 deletions

View File

@ -4,53 +4,7 @@
<meta charset="UTF-8">
<title>Gamma World Die Roller</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script>
window.onload = function() {
document.getElementById('diceForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevent the form from submitting the normal way
const quantity = parseInt(document.getElementById('quantity').value, 10);
const geometry = parseInt(document.getElementById('geometry').value, 10);
const discard = document.getElementById('discard').checked;
fetch('https://gammaworld.gmgauthier.com/roll/dice', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
quantity: quantity,
geometry: geometry,
discard_lowest: discard
}),
})
.then(response => response.json())
.then(data => {
const resultsDiv = document.getElementById('results');
let html = '';
if (data['dice-set']) {
let diceSet = data['dice-set'];
html += '<p><h3>Dice Set:</h3></p>';
html += `<ul>
<li>Mnemonic: ${diceSet['mnemonic']}</li>
<li>Min Roll: ${diceSet['min-roll']}</li>
<li>Max Roll: ${diceSet['max-roll']}</li>
</ul>`;
}
if (data['rolls'] && quantity > 1) {
html += `<p><h3>Rolls: </h3>${data['rolls'].join(', ')}</p>`;
}
if (data['result']) {
html += `<p><h3>Result: </h3><h2>${data['result']}</h2></p>`;
}
resultsDiv.innerHTML = html;
})
.catch(error => {
console.error('Error:', error);
});
});
}
</script>
<script src="rolldice.js" defer></script>
</head>
<body>

48
web/rolldice.js Normal file
View File

@ -0,0 +1,48 @@
window.onload = function () {
document.getElementById('diceForm').addEventListener('submit', function (event) {
event.preventDefault(); // Prevent the form from submitting the normal way
const quantity = parseInt(document.getElementById('quantity').value, 10);
const geometry = parseInt(document.getElementById('geometry').value, 10);
const discard = document.getElementById('discard').checked;
fetch('https://gammaworld.gmgauthier.com/roll/dice', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
quantity: quantity,
geometry: geometry,
discard_lowest: discard
}),
})
.then(response => response.json())
.then(data => {
const resultsDiv = document.getElementById('results');
let html = '';
if (data['dice-set']) {
let diceSet = data['dice-set'];
html += '<p><h3>Dice Set:</h3></p>';
html += `<ul>
<li>Mnemonic: ${diceSet['mnemonic']}</li>
<li>Min Roll: ${diceSet['min-roll']}</li>
<li>Max Roll: ${diceSet['max-roll']}</li>
</ul>`;
}
if (data['rolls'] && quantity > 1) {
html += `<p><h3>Rolls: </h3>${data['rolls'].join(', ')}</p>`;
}
if (data['discarded']) {
html += `<p><h3>Discarded:</h3>${data['discarded']}</p>`;
}
if (data['result']) {
html += `<p><h3>Result: </h3><h2>${data['result']}</h2></p>`;
}
resultsDiv.innerHTML = html;
})
.catch(error => {
console.error('Error:', error);
});
});
}