2024-07-01 10:26:54 +00:00
|
|
|
from flask_restx import Resource, Namespace, reqparse
|
2024-07-01 00:56:11 +00:00
|
|
|
from app.functions.roll_dices import roll_dices
|
|
|
|
|
2024-07-01 13:24:27 +00:00
|
|
|
|
|
|
|
def str_to_bool(val):
|
|
|
|
return str(val).lower() == 'true'
|
|
|
|
|
|
|
|
|
2024-07-01 00:56:11 +00:00
|
|
|
namespace = Namespace('dice', description='Dice Operations')
|
2024-07-01 10:26:54 +00:00
|
|
|
|
|
|
|
# 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')
|
2024-07-01 13:24:27 +00:00
|
|
|
parser.add_argument('discard_lowest', type=str_to_bool, required=True, help='Whether to discard lowest roll')
|
2024-07-01 00:56:11 +00:00
|
|
|
|
|
|
|
|
2024-07-03 23:32:28 +00:00
|
|
|
@namespace.route('') # resolves to: /dice
|
2024-07-01 00:56:11 +00:00
|
|
|
class RollDice(Resource):
|
2024-07-01 10:26:54 +00:00
|
|
|
@namespace.expect(parser)
|
|
|
|
def get(self):
|
|
|
|
args = parser.parse_args()
|
|
|
|
quantity = args['quantity']
|
|
|
|
geometry = args['geometry']
|
|
|
|
discard_lowest = args['discard_lowest']
|
|
|
|
if quantity < 1 or quantity > 100:
|
|
|
|
return {'message': 'Quantity must be between 1 and 100'}, 400
|
|
|
|
if geometry < 3 or geometry > 100:
|
|
|
|
return {'message': 'Geometry must be between 3 and 100'}, 400
|
2024-07-01 00:56:11 +00:00
|
|
|
return roll_dices(quantity, geometry, discard_lowest), 200
|