From 9c9872f351ce263d44bb479ea4ab3b89e211893b Mon Sep 17 00:00:00 2001 From: Chris kerr Date: Tue, 6 Feb 2024 18:32:35 -0500 Subject: [PATCH] breed page refactored and definitions split from the app routes --- app.py | 200 +++++++++++++------------- src/python/__innit__.py | 4 + src/python/breed.py | 99 +++++++++++++ static/js/updateIFrames.js | 2 +- templates/{breeds.html => breed.html} | 0 5 files changed, 205 insertions(+), 100 deletions(-) create mode 100644 src/python/__innit__.py create mode 100644 src/python/breed.py rename templates/{breeds.html => breed.html} (100%) diff --git a/app.py b/app.py index d00bbfc..7b0c722 100644 --- a/app.py +++ b/app.py @@ -1,6 +1,8 @@ from flask import Flask, render_template, g, abort, request, jsonify, send_from_directory import sqlite3, os +from src.python.breed import get_breed_id, get_breeding_pairs, get_used_in_breeds + app = Flask(__name__) DATABASE = "src/database.db" @@ -193,7 +195,7 @@ def monster_stats(monster_name): # Add this route for fetching breeding combinations -@app.route("/breeds") +@app.route("/breed") def get_breeding_combinations(): selected_monster = request.args.get("monster") if not selected_monster: @@ -211,7 +213,7 @@ def get_breeding_combinations(): used_in_breeds = get_used_in_breeds(selected_monster) return render_template( - "breeds.html", + "breed.html", selected_monster={ "name": selected_monster, "base_pair": base_pair, @@ -220,103 +222,103 @@ def get_breeding_combinations(): 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 +#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 if __name__ == "__main__": diff --git a/src/python/__innit__.py b/src/python/__innit__.py new file mode 100644 index 0000000..b95b022 --- /dev/null +++ b/src/python/__innit__.py @@ -0,0 +1,4 @@ +from .breed import get_breeding_pairs, get_breed_id, get_used_in_breeds + + +__all__ = ["breed",] diff --git a/src/python/breed.py b/src/python/breed.py new file mode 100644 index 0000000..891b159 --- /dev/null +++ b/src/python/breed.py @@ -0,0 +1,99 @@ +from flask import g + +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/static/js/updateIFrames.js b/static/js/updateIFrames.js index fceb433..e5533d0 100644 --- a/static/js/updateIFrames.js +++ b/static/js/updateIFrames.js @@ -13,7 +13,7 @@ function updateIframes() { // Update breedingIframe src based on the selected monster const breedingIframeSrc = selectedMonster - ? `/breeds?monster=${selectedMonster}` + ? `/breed?monster=${selectedMonster}` : "about:blank"; breedingIframe.src = breedingIframeSrc; diff --git a/templates/breeds.html b/templates/breed.html similarity index 100% rename from templates/breeds.html rename to templates/breed.html