add check roll

This commit is contained in:
Greg Gauthier 2024-06-24 21:00:40 +01:00
parent 5dc73d949c
commit e765b4723c
3 changed files with 60 additions and 2 deletions

29
app.py
View File

@ -2,9 +2,10 @@ from flask import Flask
from flask_cors import CORS from flask_cors import CORS
from flask_restx import Api, Resource from flask_restx import Api, Resource
from models import dice_model, ability_model, hp_model, character_model, encounter_model, ma_model, mutation_model from models import dice_model, ability_model, hp_model, character_model, encounter_model, ma_model, mutation_model, \
check_model
from schemas import DiceSchema, CharacterSchema, EncounterSchema, MentalAttackSchema, AbilitySchema, HPSchema, \ from schemas import DiceSchema, CharacterSchema, EncounterSchema, MentalAttackSchema, AbilitySchema, HPSchema, \
MutationSchema MutationSchema, CheckSchema
from encounters import EncounterTable from encounters import EncounterTable
from mentattack import MentalAttackMatrix from mentattack import MentalAttackMatrix
@ -27,6 +28,10 @@ ma = api.namespace('ma', description='Mental Attack operations')
mut = api.namespace('mut', description='Mutation operations') mut = api.namespace('mut', description='Mutation operations')
character = api.namespace('character', description='Character operations') character = api.namespace('character', description='Character operations')
encounter = api.namespace('encounter', description='Encounter operations') encounter = api.namespace('encounter', description='Encounter operations')
check = api.namespace('check', description='Check operations')
check_model = check.model('Check', check_model )
check_schema = CheckSchema()
ability_model = ability.model('Ability', ability_model) ability_model = ability.model('Ability', ability_model)
ability_schema = AbilitySchema() ability_schema = AbilitySchema()
@ -164,6 +169,26 @@ class RollMentalAttack(Resource):
return result, 200 return result, 200
@api.route('/roll/check', methods=['POST'])
class RollCheck(Resource):
@check.expect(check_model)
def post(self):
data = api.payload
errors = check_schema.validate(data)
if errors:
return errors, 400
ability_score = data.get('ability_score')
multiplier = data.get('multiplier')
threshold = ability_score * multiplier
rolled = roll_dices(1, 100, False).get('result')
if rolled < threshold:
return {'threshold': threshold, 'rolled': rolled, 'success': True}, 200
else:
return {'threshold': threshold, 'rolled': rolled, 'success': False}, 200
@api.route('/roll/tohit', methods=['GET']) @api.route('/roll/tohit', methods=['GET'])
class RollToHit(Resource): class RollToHit(Resource):
@staticmethod @staticmethod

View File

@ -15,6 +15,23 @@ conscore_field = fields.Integer(
description='The characters constitution score' description='The characters constitution score'
) )
check_model = {
'ability_score': fields.Integer(
required=True,
default=10,
min=3,
max=21,
description='The score of the ability to check against'
),
'multiplier': fields.Integer(
required=True,
default=4,
min=2, max=8,
description='Sets the threshold for the check. In general, the higher the multiplier, the higher the '
'likelihood of success. Range: 2 - 7'
)
}
# Mutations Model # Mutations Model
mutation_model = { mutation_model = {
'conscore': conscore_field, 'conscore': conscore_field,

View File

@ -59,6 +59,22 @@ class AbilitySchema(Schema):
) )
class CheckSchema(Schema):
ability_score = fields.Integer(
required=True,
default=10,
validate=validate.Range(min=3, max=21),
description='The score of the ability to be checked against'
)
multiplier = fields.Integer(
required=True,
default=4,
validate=validate.Range(min=2, max=8),
description='Sets the threshold for the check. In general, the higher the multiplier, the higher the '
'likelihood of success. Range: 2 - 7'
)
class EncounterSchema(Schema): class EncounterSchema(Schema):
terrain = fields.String( terrain = fields.String(
required=True, required=True,