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
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__":

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
const breedingIframeSrc = selectedMonster
? `/breeds?monster=${selectedMonster}`
? `/breed?monster=${selectedMonster}`
: "about:blank";
breedingIframe.src = breedingIframeSrc;