From 8671ae5262db8f0d8f2fbbf712e97428c3ca7638 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Sat, 22 Jun 2024 23:56:55 +0100 Subject: [PATCH] add encounters roll --- app.py | 36 +++++++++++++++++++++++++++++++++++- encounters.py | 30 ++++++++++++++++++++++++++++++ models.py | 8 ++++++++ requirements.txt | 1 + 4 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 encounters.py diff --git a/app.py b/app.py index 6cce77a..9b4acd6 100644 --- a/app.py +++ b/app.py @@ -2,7 +2,9 @@ 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 +from models import dice_model, ability_model, hp_model, character_model, encounter_model + +from encounters import EncounterTable from mutations import Mutations import random @@ -17,11 +19,13 @@ dice = api.namespace('dice', description='Dice operations') ability = api.namespace('ability', description='Ability operations') hp = api.namespace('hp', description='HP 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) hp_model = hp.model('HP', hp_model) character_model = character.model('Character', character_model) +encounter_model = encounter.model('Encounter', encounter_model) class DiceSchema(Schema): @@ -49,6 +53,17 @@ class CharacterSchema(Schema): 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_schema = EncounterSchema() + + @api.route('/roll/dice', methods=['POST']) class RollDice(Resource): @dice.expect(dice_model) @@ -94,6 +109,25 @@ class RollHP(Resource): return roll_dices(conscore, geometry, False), 200 +@api.route('/roll/encounter', methods=['POST']) +class RollEncounter(Resource): + @encounter.expect(encounter_model) + def post(self): + data = api.payload + errors = encounter_schema.validate(data) + if errors: + return errors, 400 + + terrain = data.get('terrain').lower() + roll = roll_dices(1, 20, False).get('result') + et = EncounterTable() + creature = et.get_encounter(roll, terrain) + if creature is None: + creature = "All clear!" + + return {"encounter": creature}, 200 + + @api.route('/roll/tohit', methods=['GET']) class RollToHit(Resource): def get(self): diff --git a/encounters.py b/encounters.py new file mode 100644 index 0000000..6b005ea --- /dev/null +++ b/encounters.py @@ -0,0 +1,30 @@ +import pandas as pd +import numpy as np + + +class EncounterTable: + def __init__(self): + self.table = { + 'clear': ['zarn', 'yexil', 'android', 'badder', 'sleeth', 'arn', 'herp', 'podog', 'podog', 'jaget', + 'centisteed', 'rakox', 'rakox', 'brutorz', 'hoop', 'hawkoid', 'hopper', 'hopper', None, None], + 'mountains': ['hisser', 'blight', 'parn', 'zarn', 'manta', 'orlen', 'zeethh', 'sep', 'arn', 'yexil', 'herp', + 'wardent', 'kep plant', 'crep plant', 'cal then', 'ark', 'hawkoid', 'podog', 'carrin', None], + 'forest': ['hisser', 'sep', 'blaash', 'blackun', 'terl', 'winseen', 'pineto', 'perth', 'obb', 'kailin', + 'grens', 'badder', 'arn', 'lil', 'blood bird', 'horl choo', 'soul besh', 'dabber', 'centisteed', + None], + 'desert': ['serf', 'kamodo', 'blight', 'perth', 'parn', 'zarn', 'yexil', 'hisser', 'sep', 'calthen', + 'manta', 'kep plant', 'carrin', 'podog', None, None, None, None, None, None], + 'watery': ['winseen', 'crep plant', 'seroon loo', 'terl', 'ert telden', 'barl nep', 'ert', 'fleshin', + 'keeshin', 'narlep', 'menarl', 'herkel', 'berlep', 'crentosh', 'fen', 'gator', None, None, None, + None], + 'ruins': ['arn', 'obb', 'hoop', 'android', 'badder', 'serf', 'blaash', 'yexil', 'manta', 'ark', 'orlen', + 'dabber', 'sleeth', 'carrin', 'squeeker', 'squeeker', 'squeeker', None, None, None], + 'deathlands': ['android', 'hisser', 'blight', 'zarn', 'perth', 'blaash', 'serf', 'parn', 'squeeker', + 'squeekr', None, None, None, None, None, None, None, None, None, None] + } + self.table_dataframe = pd.DataFrame(self.table) + self.table_dataframe.index = np.arange(1, len(self.table_dataframe) + 1) + self.table_dataframe.columns = self.table_dataframe.columns.str.lower() + + def get_encounter(self, roll, environ): + return self.table_dataframe.loc[roll, environ] diff --git a/models.py b/models.py index 245e099..8977963 100644 --- a/models.py +++ b/models.py @@ -30,3 +30,11 @@ character_model = { default="random", description='The attribute emphasis of your character. Choices: "physical", "mental", "random"') } + +encounter_model = { + 'terrain': fields.String( + required=True, + default="clear", + description='The terrain being traversed by the party when the encounter roll is made' + ) +} diff --git a/requirements.txt b/requirements.txt index 3f9b19b..2004e98 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,4 @@ flask-restx~=1.3.0 marshmallow~=3.21.3 pandas~=2.2.2 flask-cors~=4.0.1 +numpy~=2.0.0 \ No newline at end of file