move schemas to their own file; minor refactor

This commit is contained in:
Greg Gauthier 2024-06-23 12:57:26 +01:00
parent 0a546a0f78
commit 49adedbab9
2 changed files with 58 additions and 56 deletions

66
app.py
View File

@ -1,13 +1,14 @@
from flask import Flask 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 marshmallow import Schema, fields, validate
from models import dice_model, ability_model, hp_model, character_model, encounter_model, ma_model from models import dice_model, ability_model, hp_model, character_model, encounter_model, ma_model
from schemas import DiceSchema, CharacterSchema, EncounterSchema, MentalAttackSchema
from encounters import EncounterTable from encounters import EncounterTable
from mentattack import MentalAttackMatrix from mentattack import MentalAttackMatrix
from mutations import Mutations from mutations import Mutations
import random import random
app = Flask(__name__) app = Flask(__name__)
@ -24,72 +25,25 @@ ma = api.namespace('ma', description='Mental Attack 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')
dice_model = dice.model('Dice', dice_model)
ability_model = ability.model('Ability', ability_model) ability_model = ability.model('Ability', ability_model)
# TODO: Add ability schema
hp_model = hp.model('HP', hp_model) hp_model = hp.model('HP', hp_model)
ma_model = ma.model('MA', ma_model) # TODO: Add hitpoint schema
character_model = character.model('Character', character_model)
encounter_model = encounter.model('Encounter', encounter_model)
class DiceSchema(Schema):
quantity = fields.Int(required=True, validate=validate.Range(min=1), description='The number of dice to roll')
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_model = dice.model('Dice', dice_model)
dice_schema = DiceSchema() dice_schema = DiceSchema()
ma_model = ma.model('MA', ma_model)
ma_schema = MentalAttackSchema()
class CharacterSchema(Schema): character_model = character.model('Character', character_model)
chartype = fields.String(
required=True,
validate=validate.OneOf(["human", "mutant", "android", "robot"]),
description='The characters type of being'
)
emphasis = fields.String(
required=True,
validate=validate.OneOf(["physical", "mental", "random"]),
description='Valid inputs: physical, mental, random'
)
character_schema = CharacterSchema() character_schema = CharacterSchema()
encounter_model = encounter.model('Encounter', encounter_model)
class EncounterSchema(Schema):
terrain = fields.String(
required=True,
validate=validate.OneOf(["clear", "mountains", "forest", "desert", "watery", "ruins", "deathlands"]),
description='The terrain traversed at the time of the encounter roll'
)
encounter_schema = EncounterSchema() encounter_schema = EncounterSchema()
class MentalAttackSchema(Schema):
ams = fields.Integer(
required=True,
validate=validate.Range(min=3, max=18),
description='The Attackers Mental Strength'
)
dms = fields.Integer(
required=True,
validate=validate.Range(min=3, max=18),
description='The Defenders Mental Strength'
)
modifier = fields.Integer(
required=False,
default=0,
validate=validate.Range(min=-100, max=100),
description='Roll modifier for mental attack'
)
ma_schema = MentalAttackSchema()
@api.route('/roll/dice', methods=['POST']) @api.route('/roll/dice', methods=['POST'])
class RollDice(Resource): class RollDice(Resource):
@dice.expect(dice_model) @dice.expect(dice_model)

48
schemas.py Normal file
View File

@ -0,0 +1,48 @@
from marshmallow import Schema, fields, validate
class DiceSchema(Schema):
quantity = fields.Int(required=True, validate=validate.Range(min=1), description='The number of dice to roll')
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')
class CharacterSchema(Schema):
chartype = fields.String(
required=True,
validate=validate.OneOf(["human", "mutant", "android", "robot"]),
description='The characters type of being'
)
emphasis = fields.String(
required=True,
validate=validate.OneOf(["physical", "mental", "random"]),
description='Valid inputs: physical, mental, random'
)
class EncounterSchema(Schema):
terrain = fields.String(
required=True,
validate=validate.OneOf(["clear", "mountains", "forest", "desert", "watery", "ruins", "deathlands"]),
description='The terrain traversed at the time of the encounter roll'
)
class MentalAttackSchema(Schema):
ams = fields.Integer(
required=True,
validate=validate.Range(min=3, max=18),
description='The Attackers Mental Strength'
)
dms = fields.Integer(
required=True,
validate=validate.Range(min=3, max=18),
description='The Defenders Mental Strength'
)
modifier = fields.Integer(
required=False,
default=0,
validate=validate.Range(min=-100, max=100),
description='Roll modifier for mental attack'
)