From 4952b407ab15e05e569b9fb6720e27d1811c51c8 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Fri, 21 Jun 2024 22:49:11 +0100 Subject: [PATCH] dieroller app initial setup --- .gitignore | 2 ++ app.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 .gitignore create mode 100644 app.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2483976 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +__pycache__/ diff --git a/app.py b/app.py new file mode 100644 index 0000000..d17eac9 --- /dev/null +++ b/app.py @@ -0,0 +1,82 @@ +from flask import Flask, request, jsonify +import random + +app = Flask(__name__) + + +@app.route('/') +def hello_world(): # put application's code here + return 'Hello World!' + + +@app.route('/roll/chance', methods=['GET']) +def roll_chance(): + result = roll_dices(1, 100, False) + return jsonify(result["sum"]) + + +@app.route('/roll/tohit', methods=['GET']) +def roll_tohit(): + result = roll_dices(1, 20, False) + return jsonify(result["sum"]) + + +@app.route('/roll/ability', methods=['POST', 'GET']) +def roll_ability(): + if request.json.get('chartype') is None: + chartype = 'human' + else: + chartype = request.json.get('chartype') + + if chartype == 'human': + geometry = 8 + else: + geometry = 6 + results = roll_dices(4, geometry, True) + + return jsonify(results) + + +@app.route('/roll/hp', methods=['POST', 'GET']) +def roll_sum(): + chartype = request.json.get('quantity') + if chartype is None: + chartype = 'human' + conscore = request.json.get('conscore') + if conscore is None: + return jsonify({"ERROR": "NO CONSTITUTION SCORE SUPPLIED"}) + if chartype == 'human': + geometry = 8 + else: + geometry = 6 + + return jsonify(roll_dices(conscore, geometry, False)) + + +@app.route('/roll/dice', methods=['POST']) +def rolldie(): + quantity = request.json.get('quantity') + geometry = request.json.get('geometry') + discard_lowest = request.json.get('discard_lowest', False) + + return jsonify(roll_dices(quantity, geometry, discard_lowest)) + + +def roll_dices(dice_count, dice_sides, discard_lowest): + roll_results = [] + for _ in range(dice_count): + roll_results.append(random.randint(1, dice_sides)) + if discard_lowest and len(roll_results) > 0: + roll_results.remove(min(roll_results)) + + result = { + 'die-set': {'quantity': dice_count, 'geometry': dice_sides}, + 'rolls': roll_results, + 'sum': sum(roll_results) + } + + return result + + +if __name__ == '__main__': + app.run()