gammatools/app/routes/physical_attack.py

36 lines
1.5 KiB
Python

from flask import request, jsonify
from flask_restx import Resource, Namespace, reqparse
from app.functions.roll_physical_attack import roll_physical_attack
namespace = Namespace('gameplay', description='Gamma World Rules')
parser = reqparse.RequestParser()
parser.add_argument('dac', type=int, required=True, help='REQUIRED: Defender Armour Class. Needed for both attacks')
parser.add_argument('awc', type=int, help='OPTIONAL(*): Attacker Weapon Class. Only needed for weapon attacks')
parser.add_argument('ahd', type=int, help='OPTIONAL(*): Attacker Hit Dice. Only needed for non-weapon attacks')
parser.add_argument('modifier', type=int, help='OPTIONAL: Roll Modifier')
@namespace.route('/attack/physical') # resolves to: /gameplay/attack/physical
class PhysicalAttack(Resource):
@namespace.expect(parser)
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
try:
result = roll_physical_attack(dac, modifier, awc, ahd)
return result, 200
except ValueError as e:
return jsonify({"error": str(e)}), 400