add simple web page to use the api

This commit is contained in:
Greg Gauthier 2024-06-25 11:05:57 +01:00
parent e765b4723c
commit dd4aada41c
2 changed files with 108 additions and 0 deletions

88
web/rolldice.html Normal file
View File

@ -0,0 +1,88 @@
<!DOCTYPE html>
<html lang="en">
<head>
<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('http://localhost:5000/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>
</head>
<body>
<h1>Gamma World Die Roller</h1>
<h2>Roll Dice</h2>
<div class="content-container">
<form id="diceForm">
<div class="form-row">
<div class="form-field">
<label for="quantity">Quantity:</label>
<input type="number" name="quantity" id="quantity" min="1" max="100" value="4">
</div>
<div class="form-field">
<label for="geometry">Geometry:</label>
<input type="number" name="geometry" id="geometry" min="2" max="100" value="6">
</div>
</div>
<div class="form-row">
<div class="form-field">
<label for="discard">Discard Lowest Roll</label><input type="checkbox" name="discard" id="discard"/>
</div>
<div class="form-field">
<button type="submit">Roll</button>
</div>
</div>
</form>
</div>
<hr/>
<div id="results"></div>
</body>
</html>

20
web/styles.css Normal file
View File

@ -0,0 +1,20 @@
.form-row {
display: flex;
justify-content: space-between;
margin-bottom: 1em;
}
.form-field {
display: flex;
flex-direction: row; /* changed this from column */
align-items: center; /* added this to vertically align elements in the row */
justify-content: space-between; /* added this to put some space between the label and the input field */
width: 200px; /* optionally set a width to keep it consistent */
}
.content-container {
max-width: 500px; /* adjust the max width as per requirement */
/* this line centers your container */
margin: 20px 0px 0 20px;
padding: 0 20px; /* bit of padding so content doesn't touch the edges */
}