fix problem with boolean on the flask app;setup the front end to use query parameters;fail trying to get environment variables to work

This commit is contained in:
Greg Gauthier 2024-07-01 14:24:27 +01:00
parent 77aa5f8b69
commit 963d4cc475
6 changed files with 39 additions and 19 deletions

View File

@ -6,7 +6,8 @@ def roll_dices(dice_count, dice_sides, discard_lowest):
discarded = []
for _ in range(dice_count):
roll_results.append(random.randint(1, dice_sides))
if discard_lowest and len(roll_results) > 0:
if discard_lowest and len(roll_results) > 1:
discarded.append(min(roll_results))
roll_results.remove(min(roll_results))

View File

@ -1,13 +1,18 @@
from flask_restx import Resource, Namespace, reqparse
from app.functions.roll_dices import roll_dices
def str_to_bool(val):
return str(val).lower() == 'true'
namespace = Namespace('dice', description='Dice Operations')
# Define the parser and request args:
parser = reqparse.RequestParser()
parser.add_argument('quantity', type=int, required=True, help='Quantity of dice to roll')
parser.add_argument('geometry', type=int, required=True, help='Number of faces on the dice')
parser.add_argument('discard_lowest', type=bool, required=True, help='Whether to discard lowest roll')
parser.add_argument('discard_lowest', type=str_to_bool, required=True, help='Whether to discard lowest roll')
@namespace.route('/') # resolves to: /dice

10
web/config.js Normal file
View File

@ -0,0 +1,10 @@
// config.js
let environment = window.location.hostname === 'parmenides' ? 'dev' : 'prod';
fetch('./config.json')
.then(response => response.json())
.then(config => {
window.BASE_URL = config[environment].BASE_URL;
console.log("BASE URL:", window.BASE_URL);
})
.catch(error => console.error('Error:', error));

8
web/config.json Normal file
View File

@ -0,0 +1,8 @@
{
"dev": {
"BASE_URL": "http://127.0.0.1:5000"
},
"prod": {
"BASE_URL": "https://gammaworld.gmgauthier.com"
}
}

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8">
<title>Gamma World Die Roller</title>
<link rel="stylesheet" type="text/css" href="styles.css">
<script src="rolldice.js" defer></script>
<script type="module" src="rolldice.js" defer></script>
</head>
<body>

View File

@ -1,22 +1,18 @@
window.onload = function () {
document.getElementById('diceForm').addEventListener('submit', function (event) {
event.preventDefault(); // Prevent the form from submitting the normal way
event.preventDefault(); // Prevent the form from submitting via the default form action
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({
const queryParams = new URLSearchParams({
quantity: quantity,
geometry: geometry,
discard_lowest: discard
}),
})
});
fetch(`http://127.0.0.1:5000/dice?${queryParams}`)
.then(response => response.json())
.then(data => {
const resultsDiv = document.getElementById('results');