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_cors import CORS
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 schemas import DiceSchema, CharacterSchema, EncounterSchema, MentalAttackSchema
from encounters import EncounterTable
from mentattack import MentalAttackMatrix
from mutations import Mutations
import random
app = Flask(__name__)
@ -24,72 +25,25 @@ ma = api.namespace('ma', description='Mental Attack operations')
character = api.namespace('character', description='Character operations')
encounter = api.namespace('encounter', description='Encounter operations')
dice_model = dice.model('Dice', dice_model)
ability_model = ability.model('Ability', ability_model)
# TODO: Add ability schema
hp_model = hp.model('HP', hp_model)
ma_model = ma.model('MA', ma_model)
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')
# TODO: Add hitpoint schema
dice_model = dice.model('Dice', dice_model)
dice_schema = DiceSchema()
ma_model = ma.model('MA', ma_model)
ma_schema = MentalAttackSchema()
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'
)
character_model = character.model('Character', character_model)
character_schema = CharacterSchema()
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_model = encounter.model('Encounter', encounter_model)
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'])
class RollDice(Resource):
@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'
)