From 3b66bf0eb37adc09eafb02680562c583350a1aa9 Mon Sep 17 00:00:00 2001 From: 0ceanSlim Date: Thu, 15 Feb 2024 17:01:53 -0500 Subject: [PATCH] all views moved into src.views from app.py --- app.py | 202 ++----------------------------------- src/views/breed_info.py | 129 +++++++++++++++++++++++ src/views/monster_stats.py | 61 +++++++++++ src/views/skills.py | 12 +++ 4 files changed, 209 insertions(+), 195 deletions(-) create mode 100644 src/views/breed_info.py create mode 100644 src/views/monster_stats.py create mode 100644 src/views/skills.py diff --git a/app.py b/app.py index 7c7438c..d18097f 100644 --- a/app.py +++ b/app.py @@ -1,4 +1,4 @@ -from flask import Flask, render_template, g, abort, request, jsonify +from flask import Flask, render_template from src.util.utils import * @@ -7,6 +7,9 @@ from src.api.get_families import * from src.api.get_monster_stats import * from src.views.serve_content import * +from src.views.monster_stats import * +from src.views.breed_info import * +from src.views.skills import * app = Flask(__name__) @@ -24,205 +27,14 @@ app.register_blueprint(serve_favicon_bp) app.register_blueprint(serve_monster_sprite_bp) # Register Other Views Blurprints (HTML Render Templates) - +app.register_blueprint(monster_stats_bp) +app.register_blueprint(breed_info_bp) +app.register_blueprint(skills_bp) @app.route("/") def show_app(): js_files = get_js_files() return render_template("app.html", js_files=js_files) - -# Render HTML Templates - -@app.route("/monster/") -def monster_stats(monster_name): - cursor = g.db.cursor() - - # Retrieve monster stats from the database based on name - cursor.execute( - """ - SELECT - monsters.id, monsters.name, families.name AS family, monsters.in_story, - monsters.agl, monsters.int, monsters.maxlvl, monsters.atk, monsters.mp, - monsters.exp, monsters.hp, monsters.def - FROM - monsters - JOIN families ON monsters.family_id = families.id - WHERE - monsters.name = ? - """, - (monster_name,), - ) - - monster_stats = cursor.fetchone() - - if monster_stats is None: - abort(404) - - # Retrieve skills for the monster - cursor.execute("SELECT skill FROM skills WHERE monster_id = ?", (monster_stats[0],)) - skills = [row[0] for row in cursor.fetchall()] - - # Retrieve spawn locations for the monster - cursor.execute( - "SELECT map, description FROM spawn_locations WHERE monster_id = ?", - (monster_stats[0],), - ) - spawn_locations = [ - {"map": row[0], "description": row[1]} for row in cursor.fetchall() - ] - - return render_template( - "stats.html", - monster={ - "id": monster_stats[0], - "name": monster_stats[1], - "family": monster_stats[2], - "in_story": "Yes" if monster_stats[3] else "No", - "agl": monster_stats[4], - "int": monster_stats[5], - "maxlvl": monster_stats[6], - "atk": monster_stats[7], - "mp": monster_stats[8], - "exp": monster_stats[9], - "hp": monster_stats[10], - "def": monster_stats[11], - "skills": skills, - "spawn_locations": spawn_locations, - }, - ) - - -# Add this route for fetching breeding combinations -@app.route("/breed") -def get_breeding_combinations(): - selected_monster = request.args.get("monster") - if not selected_monster: - return jsonify({"error": "Invalid input"}) - - # Fetch breed ID based on the selected monster as a target - breed_id = get_breed_id(selected_monster) - - if breed_id is None: - return jsonify({"error": f"No breed information found for {selected_monster}"}) - - base_pair, mate_pair = get_breeding_pairs(breed_id) - - # Fetch breeds in which the selected monster is used - used_in_breeds = get_used_in_breeds(selected_monster) - - return render_template( - "breed.html", - selected_monster={ - "name": selected_monster, - "base_pair": base_pair, - "mate_pair": mate_pair, - }, - used_in_breeds=used_in_breeds, - ) - -def get_used_in_breeds(target_monster): - cursor = g.db.cursor() - - # Fetch breed IDs where the selected monster is used as a base - cursor.execute( - """ - SELECT breed_id - FROM breed_requirements - WHERE requirement_type = 'base' - AND requirement_value = ? - """, - (target_monster,), - ) - - base_breed_ids = [row[0] for row in cursor.fetchall()] - - # Fetch breed IDs where the selected monster is used as a mate - cursor.execute( - """ - SELECT breed_id - FROM breed_requirements - WHERE requirement_type = 'mate' - AND requirement_value = ? - """, - (target_monster,), - ) - - mate_breed_ids = [row[0] for row in cursor.fetchall()] - - # Combine the results from both queries - used_in_breed_ids = base_breed_ids + mate_breed_ids - - # Fetch the target monsters for the obtained breed IDs - used_in_breeds = [] - for breed_id in used_in_breed_ids: - cursor.execute( - """ - SELECT target - FROM breeds - WHERE id = ? - """, - (breed_id,), - ) - target_monster = cursor.fetchone() - if target_monster: - used_in_breeds.append(target_monster[0]) - - return used_in_breeds - -def get_breed_id(target_monster): - cursor = g.db.cursor() - - # Fetch breed ID based on the selected monster as a target - cursor.execute( - """ - SELECT breeds.id - FROM breeds - WHERE breeds.target = ? - """, - (target_monster,), - ) - - breed_id = cursor.fetchone() - - if breed_id: - return breed_id[0] - else: - return None - - -def get_breeding_pairs(breed_id): - cursor = g.db.cursor() - - # Fetch base and mate breeding combinations based on the breed ID - cursor.execute( - """ - SELECT requirement_type, requirement_value - FROM breed_requirements - WHERE breed_id = ? - """, - (breed_id,), - ) - - breeding_info = cursor.fetchall() - - base_pair = [ - value - for (requirement_type, value) in breeding_info - if requirement_type == "base" - ] - mate_pair = [ - value - for (requirement_type, value) in breeding_info - if requirement_type == "mate" - ] - - return base_pair, mate_pair - -@app.route('/skills') -def skills(): - csv_data = read_csv('src/skills_data.csv') - return render_template('skills.html', csv_data=csv_data) - if __name__ == "__main__": app.run(debug=True) diff --git a/src/views/breed_info.py b/src/views/breed_info.py new file mode 100644 index 0000000..a1e7775 --- /dev/null +++ b/src/views/breed_info.py @@ -0,0 +1,129 @@ +from flask import Blueprint, render_template, g, request +import jsonify + +breed_info_bp = Blueprint('view_breed_info', __name__) + +@breed_info_bp.route("/breed") +def get_breeding_combinations(): + selected_monster = request.args.get("monster") + if not selected_monster: + return jsonify({"error": "Invalid input"}) + + # Fetch breed ID based on the selected monster as a target + breed_id = get_breed_id(selected_monster) + + if breed_id is None: + return jsonify({"error": f"No breed information found for {selected_monster}"}) + + base_pair, mate_pair = get_breeding_pairs(breed_id) + + # Fetch breeds in which the selected monster is used + used_in_breeds = get_used_in_breeds(selected_monster) + + return render_template( + "breed.html", + selected_monster={ + "name": selected_monster, + "base_pair": base_pair, + "mate_pair": mate_pair, + }, + used_in_breeds=used_in_breeds, + ) + +def get_used_in_breeds(target_monster): + cursor = g.db.cursor() + + # Fetch breed IDs where the selected monster is used as a base + cursor.execute( + """ + SELECT breed_id + FROM breed_requirements + WHERE requirement_type = 'base' + AND requirement_value = ? + """, + (target_monster,), + ) + + base_breed_ids = [row[0] for row in cursor.fetchall()] + + # Fetch breed IDs where the selected monster is used as a mate + cursor.execute( + """ + SELECT breed_id + FROM breed_requirements + WHERE requirement_type = 'mate' + AND requirement_value = ? + """, + (target_monster,), + ) + + mate_breed_ids = [row[0] for row in cursor.fetchall()] + + # Combine the results from both queries + used_in_breed_ids = base_breed_ids + mate_breed_ids + + # Fetch the target monsters for the obtained breed IDs + used_in_breeds = [] + for breed_id in used_in_breed_ids: + cursor.execute( + """ + SELECT target + FROM breeds + WHERE id = ? + """, + (breed_id,), + ) + target_monster = cursor.fetchone() + if target_monster: + used_in_breeds.append(target_monster[0]) + + return used_in_breeds + +def get_breed_id(target_monster): + cursor = g.db.cursor() + + # Fetch breed ID based on the selected monster as a target + cursor.execute( + """ + SELECT breeds.id + FROM breeds + WHERE breeds.target = ? + """, + (target_monster,), + ) + + breed_id = cursor.fetchone() + + if breed_id: + return breed_id[0] + else: + return None + + +def get_breeding_pairs(breed_id): + cursor = g.db.cursor() + + # Fetch base and mate breeding combinations based on the breed ID + cursor.execute( + """ + SELECT requirement_type, requirement_value + FROM breed_requirements + WHERE breed_id = ? + """, + (breed_id,), + ) + + breeding_info = cursor.fetchall() + + base_pair = [ + value + for (requirement_type, value) in breeding_info + if requirement_type == "base" + ] + mate_pair = [ + value + for (requirement_type, value) in breeding_info + if requirement_type == "mate" + ] + + return base_pair, mate_pair \ No newline at end of file diff --git a/src/views/monster_stats.py b/src/views/monster_stats.py new file mode 100644 index 0000000..660b0c2 --- /dev/null +++ b/src/views/monster_stats.py @@ -0,0 +1,61 @@ +from flask import Blueprint, render_template, abort, g + +monster_stats_bp = Blueprint('view_monster_stats', __name__) + +@monster_stats_bp.route("/monster/") +def monster_stats(monster_name): + cursor = g.db.cursor() + + # Retrieve monster stats from the database based on name + cursor.execute( + """ + SELECT + monsters.id, monsters.name, families.name AS family, monsters.in_story, + monsters.agl, monsters.int, monsters.maxlvl, monsters.atk, monsters.mp, + monsters.exp, monsters.hp, monsters.def + FROM + monsters + JOIN families ON monsters.family_id = families.id + WHERE + monsters.name = ? + """, + (monster_name,), + ) + + monster_stats = cursor.fetchone() + + if monster_stats is None: + abort(404) + + # Retrieve skills for the monster + cursor.execute("SELECT skill FROM skills WHERE monster_id = ?", (monster_stats[0],)) + skills = [row[0] for row in cursor.fetchall()] + + # Retrieve spawn locations for the monster + cursor.execute( + "SELECT map, description FROM spawn_locations WHERE monster_id = ?", + (monster_stats[0],), + ) + spawn_locations = [ + {"map": row[0], "description": row[1]} for row in cursor.fetchall() + ] + + return render_template( + "stats.html", + monster={ + "id": monster_stats[0], + "name": monster_stats[1], + "family": monster_stats[2], + "in_story": "Yes" if monster_stats[3] else "No", + "agl": monster_stats[4], + "int": monster_stats[5], + "maxlvl": monster_stats[6], + "atk": monster_stats[7], + "mp": monster_stats[8], + "exp": monster_stats[9], + "hp": monster_stats[10], + "def": monster_stats[11], + "skills": skills, + "spawn_locations": spawn_locations, + }, + ) diff --git a/src/views/skills.py b/src/views/skills.py new file mode 100644 index 0000000..8f8fd7d --- /dev/null +++ b/src/views/skills.py @@ -0,0 +1,12 @@ +from flask import Blueprint, render_template + +from ..util.utils import * + +skills_bp = Blueprint('skills', __name__) +serve_monster_sprite_bp = Blueprint('monster_sprite', __name__) + +#Serve Monster Sprites +@skills_bp.route('/skills') +def skills(): + csv_data = read_csv('src/skills_data.csv') + return render_template('skills.html', csv_data=csv_data) \ No newline at end of file