dwm-app/src/api/get_monster_stats.py

68 lines
2.5 KiB
Python
Raw Normal View History

from flask import Blueprint, g, jsonify, request
get_monster_stats_bp = Blueprint('monster_stats', __name__)
# List All Monsters
@get_monster_stats_bp.route("/api/monster/stats")
def get_monster_stats():
cursor = g.db.cursor()
# Check if 'monster' argument is provided
selected_monster = request.args.get("monster")
if selected_monster:
2024-02-21 15:43:08 +00:00
# Fetch specific stats for the monster
query = """
SELECT
monsters.name,
monsters.agl AS agility,
monsters.int AS intelligence,
monsters.maxlvl AS max_level,
monsters.exp AS experience,
monsters.hp AS health_points,
monsters.atk AS attack,
monsters.def AS defense,
families.name AS family,
2024-02-21 15:43:08 +00:00
spawn_locations.map || ' - ' || spawn_locations.description AS location,
GROUP_CONCAT(skills.skill) AS skills
FROM monsters
LEFT JOIN families ON monsters.family_id = families.id
LEFT JOIN spawn_locations ON monsters.id = spawn_locations.monster_id
2024-02-21 15:43:08 +00:00
LEFT JOIN skills ON monsters.id = skills.monster_id
WHERE LOWER(monsters.name) = LOWER(?)
2024-02-21 15:43:08 +00:00
GROUP BY monsters.id
"""
cursor.execute(query, (selected_monster.lower(),))
# Fetch the result and convert it to a dictionary
monster_stats = cursor.fetchone()
print("Result:", monster_stats)
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",
"family": "Family",
2024-02-21 15:43:08 +00:00
"location": "Location",
"skills": "Skills"
}
# Create a new dictionary with descriptive stat names
formatted_stats = {
"name": monster_stats[0],
2024-02-21 15:43:08 +00:00
**{stat_labels[key]: monster_stats[i + 1] for i, key in enumerate(["agility", "intelligence", "max_level", "experience", "health_points", "attack", "defense", "family", "location", "skills"])}
}
return jsonify(formatted_stats)
else:
return jsonify({"error": "Monster not found"}), 404
else:
return jsonify({"error": "Monster name not provided"}), 400