ALL API functions split from main app.py

This commit is contained in:
0ceanSlim 2024-02-15 10:09:54 -05:00
parent 133e4805fd
commit 59121c1b49
5 changed files with 116 additions and 95 deletions

112
app.py
View File

@ -1,112 +1,34 @@
from flask import Flask, render_template, g, abort, request, jsonify, send_from_directory, url_for from flask import Flask, render_template, g, abort, request, jsonify
from src.api.get_monsters import *
from src.api.get_families import *
from src.api.get_monster_stats import *
from src.util.utils import * from src.util.utils import *
from src.views.serve_content import *
app = Flask(__name__) app = Flask(__name__)
# Utils
app.before_request(before_request) app.before_request(before_request)
app.teardown_request(teardown_request) app.teardown_request(teardown_request)
# Register Serve Content Blueprints
app.register_blueprint(serve_favicon_bp)
app.register_blueprint(serve_monster_sprite_bp)
# Register API Blueprints
app.register_blueprint(get_families_bp)
app.register_blueprint(get_monsters_bp)
app.register_blueprint(get_monster_stats_bp)
@app.route("/") @app.route("/")
def show_app(): def show_app():
js_files = get_js_files() js_files = get_js_files()
return render_template("app.html", js_files=js_files) return render_template("app.html", js_files=js_files)
#Serve Monster Sprites
@app.route('/img/monster/<selected_monster>.png')
def serve_monster_sprite(selected_monster):
return send_from_directory('static/img/monster/', f'{selected_monster}.png')
#Serve Favicon
@app.route('/img/favicon.ico')
def serve_favicon():
return send_from_directory( '','static/img/favicon.ico')
#TO-DO: Split the rest of these API Calls
#API Calls
from src.api.get_families import *
app.register_blueprint(get_families_bp)
# List All Monsters
@app.route("/api/monsters")
def get_monsters():
selected_family = request.args.get("family")
cursor = g.db.cursor()
if selected_family:
cursor.execute(
"""
SELECT name
FROM monsters
WHERE family_id = (SELECT id FROM families WHERE name = ?)
ORDER BY (agl + int + atk + mp + exp + hp + def) * maxlvl ASC
""",
(selected_family,),
)
else:
cursor.execute(
"""
SELECT name
FROM monsters
ORDER BY (agl + int + atk + mp + exp + hp + def) * maxlvl ASC
"""
)
monsters = [row[0] for row in cursor.fetchall()]
return jsonify(monsters)
@app.route("/api/monsters/stats")
def get_monster_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>")

View File

@ -0,0 +1,54 @@
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:
# 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

31
src/api/get_monsters.py Normal file
View File

@ -0,0 +1,31 @@
from flask import Blueprint, g, jsonify, request
get_monsters_bp = Blueprint('monsters', __name__)
# List All Monsters
@get_monsters_bp.route("/api/monsters")
def get_monsters():
selected_family = request.args.get("family")
cursor = g.db.cursor()
if selected_family:
cursor.execute(
"""
SELECT name
FROM monsters
WHERE family_id = (SELECT id FROM families WHERE name = ?)
ORDER BY (agl + int + atk + mp + exp + hp + def) * maxlvl ASC
""",
(selected_family,),
)
else:
cursor.execute(
"""
SELECT name
FROM monsters
ORDER BY (agl + int + atk + mp + exp + hp + def) * maxlvl ASC
"""
)
monsters = [row[0] for row in cursor.fetchall()]
return jsonify(monsters)

View File

@ -0,0 +1,14 @@
from flask import send_from_directory, Blueprint
serve_favicon_bp = Blueprint('favicon', __name__)
serve_monster_sprite_bp = Blueprint('monster_sprite', __name__)
#Serve Monster Sprites
@serve_favicon_bp.route('/img/monster/<selected_monster>.png')
def serve_monster_sprite(selected_monster):
return send_from_directory('static/img/monster/', f'{selected_monster}.png')
#Serve Favicon
@serve_monster_sprite_bp.route('/img/favicon.ico')
def serve_favicon():
return send_from_directory( '','static/img/favicon.ico')