gammatools/app/routes/physical_attack.py

31 lines
1.3 KiB
Python

from flask import request
from flask_restx import Resource, Namespace
from app.functions.roll_physical_attack import roll_physical_attack
namespace = Namespace('gameplay', description='Gamma World Rules')
@namespace.route('/attack/physical') # resolves to: /gameplay/attack/physical
class PhysicalAttack(Resource):
@namespace.doc(
params={'dac': 'REQUIRED: Needed for both attacks',
'modifier': 'OPTIONAL: Roll Modifier',
'awc': 'OPTIONAL(*): Attacker Weapon Class. Only needed for weapon attacks',
'ahd': 'OPTIONAL(*): Attacker Hit Dice. Only needed for non-weapon attacks'})
def get(self):
dac = request.args.get('dac', type=int)
awc = request.args.get('awc', type=int)
ahd = request.args.get('ahd', type=int)
modifier = request.args.get('modifier', type=int)
# Request validation
if dac is None:
return {"error": "'dac' parameter is needed"}, 400
if (awc is None and ahd is None) or (awc is not None and ahd is not None):
return {"error": "Exactly one of 'awc' or 'ahd' parameters must be provided"}, 400
# Call to business logic after validation, could be placed in try-except block for handling exceptions if any
result = roll_physical_attack(dac, modifier, awc, ahd)
return result, 200