2024-02-15 15:09:54 +00:00
|
|
|
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
|
2024-02-21 14:27:05 +00:00
|
|
|
query = """
|
2024-02-15 15:09:54 +00:00
|
|
|
SELECT
|
2024-02-21 14:27:05 +00:00
|
|
|
monsters.name,
|
|
|
|
monsters.agl AS agility,
|
|
|
|
monsters.int AS intelligence,
|
|
|
|
monsters.maxlvl AS max_level,
|
|
|
|
monsters.exp AS experience,
|
|
|
|
monsters.hp AS health_points,
|
2024-02-21 15:59:43 +00:00
|
|
|
monsters.mp AS mana_points,
|
2024-02-21 14:27:05 +00:00
|
|
|
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
|
2024-02-15 15:09:54 +00:00
|
|
|
FROM monsters
|
2024-02-21 14:27:05 +00:00
|
|
|
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
|
2024-02-21 14:27:05 +00:00
|
|
|
WHERE LOWER(monsters.name) = LOWER(?)
|
2024-02-21 15:43:08 +00:00
|
|
|
GROUP BY monsters.id
|
2024-02-21 14:27:05 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
cursor.execute(query, (selected_monster.lower(),))
|
2024-02-15 15:09:54 +00:00
|
|
|
|
|
|
|
# Fetch the result and convert it to a dictionary
|
|
|
|
monster_stats = cursor.fetchone()
|
2024-02-21 14:27:05 +00:00
|
|
|
print("Result:", monster_stats)
|
2024-02-15 15:09:54 +00:00
|
|
|
|
|
|
|
if monster_stats:
|
|
|
|
# Map stat names to descriptive labels
|
|
|
|
stat_labels = {
|
|
|
|
"max_level": "Max Level",
|
|
|
|
"experience": "Experience",
|
|
|
|
"health_points": "Health Points",
|
2024-02-21 15:59:43 +00:00
|
|
|
"mana_points": "Mana Points",
|
2024-02-15 15:09:54 +00:00
|
|
|
"attack": "Attack",
|
|
|
|
"defense": "Defense",
|
|
|
|
"agility": "Agility",
|
2024-02-21 14:27:05 +00:00
|
|
|
"intelligence": "Intelligence",
|
|
|
|
"family": "Family",
|
2024-02-21 15:43:08 +00:00
|
|
|
"location": "Location",
|
|
|
|
"skills": "Skills"
|
2024-02-21 14:27:05 +00:00
|
|
|
}
|
2024-02-15 15:09:54 +00:00
|
|
|
|
|
|
|
# Create a new dictionary with descriptive stat names
|
|
|
|
formatted_stats = {
|
|
|
|
"name": monster_stats[0],
|
2024-02-21 15:59:43 +00:00
|
|
|
**{stat_labels[key]: monster_stats[i + 1] for i, key in enumerate(["agility", "intelligence", "max_level", "experience", "health_points", "mana_points", "attack", "defense", "family", "location", "skills"])}
|
2024-02-15 15:09:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return jsonify(formatted_stats)
|
|
|
|
else:
|
|
|
|
return jsonify({"error": "Monster not found"}), 404
|
|
|
|
else:
|
2024-02-21 14:27:05 +00:00
|
|
|
return jsonify({"error": "Monster name not provided"}), 400
|