From 1c9b97172e84f55d037016e57793cb3e990ae855 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Mon, 5 Feb 2024 16:25:00 -0500 Subject: [PATCH] created stats api --- app.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/app.py b/app.py index efd3556..f4d05bf 100644 --- a/app.py +++ b/app.py @@ -44,6 +44,7 @@ def serve_favicon(): #API Calls +# List All Families @app.route("/api/families") def json_families(): cursor = g.db.cursor() @@ -51,6 +52,7 @@ def json_families(): families = [row[0] for row in cursor.fetchall()] return jsonify(families) +# List All Monsters @app.route("/api/monsters") def json_monsters(): selected_family = request.args.get("family") @@ -70,6 +72,57 @@ def json_monsters(): monsters = [row[0] for row in cursor.fetchall()] 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 @app.route("/monster/")