2024-07-03 23:32:28 +00:00
|
|
|
from flask import request, jsonify
|
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_physical_attack import roll_physical_attack
|
|
|
|
|
|
|
|
namespace = Namespace('gameplay', description='Gamma World Rules')
|
|
|
|
|
2024-07-01 10:26:54 +00:00
|
|
|
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')
|
|
|
|
|
2024-07-01 00:56:11 +00:00
|
|
|
|
|
|
|
@namespace.route('/attack/physical') # resolves to: /gameplay/attack/physical
|
|
|
|
class PhysicalAttack(Resource):
|
2024-07-01 10:26:54 +00:00
|
|
|
@namespace.expect(parser)
|
2024-07-01 00:56:11 +00:00
|
|
|
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
|
2024-07-03 23:32:28 +00:00
|
|
|
|
2024-07-01 00:56:11 +00:00
|
|
|
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
|
|
|
|
|
2024-07-03 23:32:28 +00:00
|
|
|
try:
|
|
|
|
result = roll_physical_attack(dac, modifier, awc, ahd)
|
|
|
|
return result, 200
|
|
|
|
except ValueError as e:
|
|
|
|
return jsonify({"error": str(e)}), 400
|