gammatools/app.py

83 lines
2.0 KiB
Python

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()