created stats api
This commit is contained in:
parent
2d2a47ea83
commit
1c9b97172e
53
app.py
53
app.py
@ -44,6 +44,7 @@ def serve_favicon():
|
|||||||
|
|
||||||
#API Calls
|
#API Calls
|
||||||
|
|
||||||
|
# List All Families
|
||||||
@app.route("/api/families")
|
@app.route("/api/families")
|
||||||
def json_families():
|
def json_families():
|
||||||
cursor = g.db.cursor()
|
cursor = g.db.cursor()
|
||||||
@ -51,6 +52,7 @@ def json_families():
|
|||||||
families = [row[0] for row in cursor.fetchall()]
|
families = [row[0] for row in cursor.fetchall()]
|
||||||
return jsonify(families)
|
return jsonify(families)
|
||||||
|
|
||||||
|
# List All Monsters
|
||||||
@app.route("/api/monsters")
|
@app.route("/api/monsters")
|
||||||
def json_monsters():
|
def json_monsters():
|
||||||
selected_family = request.args.get("family")
|
selected_family = request.args.get("family")
|
||||||
@ -70,6 +72,57 @@ def json_monsters():
|
|||||||
monsters = [row[0] for row in cursor.fetchall()]
|
monsters = [row[0] for row in cursor.fetchall()]
|
||||||
return jsonify(monsters)
|
return jsonify(monsters)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/api/monsters/stats")
|
||||||
|
def json_monsters_stats():
|
||||||
|
cursor = g.db.cursor()
|
||||||
|
|
||||||
|
# Check if 'monster' argument is provided
|
||||||
|
selected_monster = request.args.get("monster")
|
||||||
|
|
||||||
|
if selected_monster:
|
||||||
|
# Fetch specific stats for the monster
|
||||||
|
cursor.execute("""
|
||||||
|
SELECT
|
||||||
|
name,
|
||||||
|
agl AS agility,
|
||||||
|
int AS intelligence,
|
||||||
|
maxlvl AS max_level,
|
||||||
|
exp AS experience,
|
||||||
|
hp AS health_points,
|
||||||
|
atk AS attack,
|
||||||
|
def AS defense
|
||||||
|
FROM monsters
|
||||||
|
WHERE LOWER(name) = LOWER(?)
|
||||||
|
""", (selected_monster.lower(),))
|
||||||
|
|
||||||
|
# Fetch the result and convert it to a dictionary
|
||||||
|
monster_stats = cursor.fetchone()
|
||||||
|
|
||||||
|
if monster_stats:
|
||||||
|
# Map stat names to descriptive labels
|
||||||
|
stat_labels = {
|
||||||
|
"max_level": "Max Level",
|
||||||
|
"experience": "Experience",
|
||||||
|
"health_points": "Health Points",
|
||||||
|
"attack": "Attack",
|
||||||
|
"defense": "Defense",
|
||||||
|
"agility": "Agility",
|
||||||
|
"intelligence": "Intelligence"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Create a new dictionary with descriptive stat names
|
||||||
|
formatted_stats = {
|
||||||
|
"name": monster_stats[0],
|
||||||
|
**{stat_labels[key]: monster_stats[i + 1] for i, key in enumerate(["agility", "intelligence", "max_level", "experience", "health_points", "attack", "defense"])}
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonify(formatted_stats)
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Monster not found"}), 404
|
||||||
|
else:
|
||||||
|
return jsonify({"error": "Monster name not provided"}), 400
|
||||||
|
|
||||||
# Render HTML Templates
|
# Render HTML Templates
|
||||||
|
|
||||||
@app.route("/monster/<monster_name>")
|
@app.route("/monster/<monster_name>")
|
||||||
|
Loading…
Reference in New Issue
Block a user