From 963d4cc475621a1f0cd7475df40874b5e78a2aaf Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Mon, 1 Jul 2024 14:24:27 +0100 Subject: [PATCH] fix problem with boolean on the flask app;setup the front end to use query parameters;fail trying to get environment variables to work --- app/functions/roll_dices.py | 3 ++- app/routes/roll_dice.py | 7 ++++++- web/config.js | 10 ++++++++++ web/config.json | 8 ++++++++ web/rolldice.html | 2 +- web/rolldice.js | 28 ++++++++++++---------------- 6 files changed, 39 insertions(+), 19 deletions(-) create mode 100644 web/config.js create mode 100644 web/config.json diff --git a/app/functions/roll_dices.py b/app/functions/roll_dices.py index e769dcf..a52d6ea 100644 --- a/app/functions/roll_dices.py +++ b/app/functions/roll_dices.py @@ -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)) diff --git a/app/routes/roll_dice.py b/app/routes/roll_dice.py index 7cfb653..89faced 100644 --- a/app/routes/roll_dice.py +++ b/app/routes/roll_dice.py @@ -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 diff --git a/web/config.js b/web/config.js new file mode 100644 index 0000000..41413b5 --- /dev/null +++ b/web/config.js @@ -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)); diff --git a/web/config.json b/web/config.json new file mode 100644 index 0000000..e046d4d --- /dev/null +++ b/web/config.json @@ -0,0 +1,8 @@ +{ + "dev": { + "BASE_URL": "http://127.0.0.1:5000" + }, + "prod": { + "BASE_URL": "https://gammaworld.gmgauthier.com" + } +} \ No newline at end of file diff --git a/web/rolldice.html b/web/rolldice.html index d5e8c41..47b60dd 100644 --- a/web/rolldice.html +++ b/web/rolldice.html @@ -4,7 +4,7 @@ Gamma World Die Roller - + diff --git a/web/rolldice.js b/web/rolldice.js index 3d398dd..9a5c0dc 100644 --- a/web/rolldice.js +++ b/web/rolldice.js @@ -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({ - quantity: quantity, - geometry: geometry, - discard_lowest: discard - }), - }) + 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'); @@ -25,10 +21,10 @@ window.onload = function () { let diceSet = data['dice-set']; html += '

Dice Set:

'; html += ``; +
  • Mnemonic: ${diceSet['mnemonic']}
  • +
  • Min Roll: ${diceSet['min-roll']}
  • +
  • Max Roll: ${diceSet['max-roll']}
  • + `; } if (data['rolls'] && quantity > 1) { html += `

    Rolls:

    ${data['rolls'].join(', ')}

    `;