split app and models; improve roll_dices; add requirements.txt

This commit is contained in:
Greg Gauthier 2024-06-22 11:29:05 +01:00
parent d9237b2891
commit 366a340b1d
3 changed files with 58 additions and 14 deletions

46
app.py
View File

@ -1,5 +1,7 @@
from flask import Flask from flask import Flask
from flask_restx import Api, Resource, fields from flask_restx import Api, Resource, fields
from marshmallow import Schema, fields, validate
from models import dice_model, ability_model, hp_model
import random import random
app = Flask(__name__) app = Flask(__name__)
@ -11,20 +13,18 @@ dice = api.namespace('dice', description='Dice operations')
ability = api.namespace('ability', description='Ability operations') ability = api.namespace('ability', description='Ability operations')
hp = api.namespace('hp', description='HP operations') hp = api.namespace('hp', description='HP operations')
dice_model = dice.model('Dice', { dice_model = dice.model('Dice', dice_model)
'quantity': fields.Integer(required=True, description='The number of dice to roll'), ability_model = ability.model('Ability', ability_model)
'geometry': fields.Integer(required=True, description='The number of sides on each die'), hp_model = hp.model('HP', hp_model)
'discard_lowest': fields.Boolean(required=False, default=False, description='Drop the lowest score')
})
ability_model = ability.model('Ability', {
'chartype': fields.String(required=False, default="human", description='Character type')
})
hp_model = hp.model('HP', { class DiceSchema(Schema):
'chartype': fields.String(required=False, default="human", description='Character type'), quantity = fields.Int(required=True, validate=validate.Range(min=1), description='The number of dice to roll')
'conscore': fields.Integer(required=True, description='Conscore') geometry = fields.Int(required=True, validate=validate.Range(min=2), description='The number of sides on each die')
}) discard_lowest = fields.Bool(default=False, description='Drop the lowest score')
dice_schema = DiceSchema()
@api.route('/roll/dice', methods=['POST']) @api.route('/roll/dice', methods=['POST'])
@ -32,6 +32,9 @@ class RollDice(Resource):
@dice.expect(dice_model) @dice.expect(dice_model)
def post(self): def post(self):
data = api.payload data = api.payload
errors = dice_schema.validate(data)
if errors:
return errors, 400
quantity = data.get('quantity') quantity = data.get('quantity')
geometry = data.get('geometry') geometry = data.get('geometry')
discard_lowest = data.get('discard_lowest') discard_lowest = data.get('discard_lowest')
@ -83,16 +86,31 @@ class RollToHit(Resource):
def roll_dices(dice_count, dice_sides, discard_lowest): def roll_dices(dice_count, dice_sides, discard_lowest):
roll_results = [] roll_results = []
discarded = []
for _ in range(dice_count): for _ in range(dice_count):
roll_results.append(random.randint(1, dice_sides)) roll_results.append(random.randint(1, dice_sides))
if discard_lowest and len(roll_results) > 0: if discard_lowest and len(roll_results) > 0:
discarded.append(min(roll_results))
roll_results.remove(min(roll_results)) roll_results.remove(min(roll_results))
if discarded:
net_dice_count = dice_count -1
mnemonic = str(dice_count)+"(-1)d"+str(dice_sides)
else:
mnemonic = str(dice_count)+'d'+str(dice_sides)
net_dice_count = dice_count
result = { result = {
'die-set': {'quantity': dice_count, 'geometry': dice_sides}, 'dice-set': {
'mnemonic': mnemonic,
'min-roll': net_dice_count,
'max-roll': dice_sides*net_dice_count
},
'rolls': roll_results, 'rolls': roll_results,
'sum': sum(roll_results) 'result': sum(roll_results)
} }
if discarded:
result['discarded'] = discarded
return result return result

23
models.py Normal file
View File

@ -0,0 +1,23 @@
from flask_restx import fields
# Common fields
chartype_field = fields.String(required=False, default="human", description='Character type. Allowed values: "human", '
'"mutant", "android", "robot"')
# Dice model
dice_model = {
'quantity': fields.Integer(required=True, description='The number of dice to roll'),
'geometry': fields.Integer(required=True, description='The number of sides on each die'),
'discard_lowest': fields.Boolean(required=False, default=False, description='Drop the lowest score')
}
# Ability model
ability_model = {
'chartype': chartype_field
}
# Hp model
hp_model = {
'chartype': chartype_field,
'conscore': fields.Integer(required=True, description='Conscore')
}

3
requirements.txt Normal file
View File

@ -0,0 +1,3 @@
Flask~=3.0.3
flask-restx~=1.3.0
marshmallow~=3.21.3