breed page refactored and definitions split from the app routes

This commit is contained in:
Chris kerr 2024-02-06 18:32:35 -05:00
parent 4e90e13548
commit 9c9872f351
5 changed files with 205 additions and 100 deletions

200
app.py
View File

@ -1,6 +1,8 @@
from flask import Flask, render_template, g, abort, request, jsonify, send_from_directory from flask import Flask, render_template, g, abort, request, jsonify, send_from_directory
import sqlite3, os import sqlite3, os
from src.python.breed import get_breed_id, get_breeding_pairs, get_used_in_breeds
app = Flask(__name__) app = Flask(__name__)
DATABASE = "src/database.db" DATABASE = "src/database.db"
@ -193,7 +195,7 @@ def monster_stats(monster_name):
# Add this route for fetching breeding combinations # Add this route for fetching breeding combinations
@app.route("/breeds") @app.route("/breed")
def get_breeding_combinations(): def get_breeding_combinations():
selected_monster = request.args.get("monster") selected_monster = request.args.get("monster")
if not selected_monster: if not selected_monster:
@ -211,7 +213,7 @@ def get_breeding_combinations():
used_in_breeds = get_used_in_breeds(selected_monster) used_in_breeds = get_used_in_breeds(selected_monster)
return render_template( return render_template(
"breeds.html", "breed.html",
selected_monster={ selected_monster={
"name": selected_monster, "name": selected_monster,
"base_pair": base_pair, "base_pair": base_pair,
@ -220,103 +222,103 @@ def get_breeding_combinations():
used_in_breeds=used_in_breeds, used_in_breeds=used_in_breeds,
) )
def get_used_in_breeds(target_monster): #def get_used_in_breeds(target_monster):
cursor = g.db.cursor() # cursor = g.db.cursor()
#
# Fetch breed IDs where the selected monster is used as a base # # Fetch breed IDs where the selected monster is used as a base
cursor.execute( # cursor.execute(
""" # """
SELECT breed_id # SELECT breed_id
FROM breed_requirements # FROM breed_requirements
WHERE requirement_type = 'base' # WHERE requirement_type = 'base'
AND requirement_value = ? # AND requirement_value = ?
""", # """,
(target_monster,), # (target_monster,),
) # )
#
base_breed_ids = [row[0] for row in cursor.fetchall()] # base_breed_ids = [row[0] for row in cursor.fetchall()]
#
# Fetch breed IDs where the selected monster is used as a mate # # Fetch breed IDs where the selected monster is used as a mate
cursor.execute( # cursor.execute(
""" # """
SELECT breed_id # SELECT breed_id
FROM breed_requirements # FROM breed_requirements
WHERE requirement_type = 'mate' # WHERE requirement_type = 'mate'
AND requirement_value = ? # AND requirement_value = ?
""", # """,
(target_monster,), # (target_monster,),
) # )
#
mate_breed_ids = [row[0] for row in cursor.fetchall()] # mate_breed_ids = [row[0] for row in cursor.fetchall()]
#
# Combine the results from both queries # # Combine the results from both queries
used_in_breed_ids = base_breed_ids + mate_breed_ids # used_in_breed_ids = base_breed_ids + mate_breed_ids
#
# Fetch the target monsters for the obtained breed IDs # # Fetch the target monsters for the obtained breed IDs
used_in_breeds = [] # used_in_breeds = []
for breed_id in used_in_breed_ids: # for breed_id in used_in_breed_ids:
cursor.execute( # cursor.execute(
""" # """
SELECT target # SELECT target
FROM breeds # FROM breeds
WHERE id = ? # WHERE id = ?
""", # """,
(breed_id,), # (breed_id,),
) # )
target_monster = cursor.fetchone() # target_monster = cursor.fetchone()
if target_monster: # if target_monster:
used_in_breeds.append(target_monster[0]) # used_in_breeds.append(target_monster[0])
#
return used_in_breeds # return used_in_breeds
#
def get_breed_id(target_monster): #def get_breed_id(target_monster):
cursor = g.db.cursor() # cursor = g.db.cursor()
#
# Fetch breed ID based on the selected monster as a target # # Fetch breed ID based on the selected monster as a target
cursor.execute( # cursor.execute(
""" # """
SELECT breeds.id # SELECT breeds.id
FROM breeds # FROM breeds
WHERE breeds.target = ? # WHERE breeds.target = ?
""", # """,
(target_monster,), # (target_monster,),
) # )
#
breed_id = cursor.fetchone() # breed_id = cursor.fetchone()
#
if breed_id: # if breed_id:
return breed_id[0] # return breed_id[0]
else: # else:
return None # return None
#
#
def get_breeding_pairs(breed_id): #def get_breeding_pairs(breed_id):
cursor = g.db.cursor() # cursor = g.db.cursor()
#
# Fetch base and mate breeding combinations based on the breed ID # # Fetch base and mate breeding combinations based on the breed ID
cursor.execute( # cursor.execute(
""" # """
SELECT requirement_type, requirement_value # SELECT requirement_type, requirement_value
FROM breed_requirements # FROM breed_requirements
WHERE breed_id = ? # WHERE breed_id = ?
""", # """,
(breed_id,), # (breed_id,),
) # )
#
breeding_info = cursor.fetchall() # breeding_info = cursor.fetchall()
#
base_pair = [ # base_pair = [
value # value
for (requirement_type, value) in breeding_info # for (requirement_type, value) in breeding_info
if requirement_type == "base" # if requirement_type == "base"
] # ]
mate_pair = [ # mate_pair = [
value # value
for (requirement_type, value) in breeding_info # for (requirement_type, value) in breeding_info
if requirement_type == "mate" # if requirement_type == "mate"
] # ]
#
return base_pair, mate_pair # return base_pair, mate_pair
if __name__ == "__main__": if __name__ == "__main__":

4
src/python/__innit__.py Normal file
View File

@ -0,0 +1,4 @@
from .breed import get_breeding_pairs, get_breed_id, get_used_in_breeds
__all__ = ["breed",]

99
src/python/breed.py Normal file
View File

@ -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

View File

@ -13,7 +13,7 @@ function updateIframes() {
// Update breedingIframe src based on the selected monster // Update breedingIframe src based on the selected monster
const breedingIframeSrc = selectedMonster const breedingIframeSrc = selectedMonster
? `/breeds?monster=${selectedMonster}` ? `/breed?monster=${selectedMonster}`
: "about:blank"; : "about:blank";
breedingIframe.src = breedingIframeSrc; breedingIframe.src = breedingIframeSrc;