Compare commits

...

3 Commits

12 changed files with 136 additions and 138 deletions

131
app.py
View File

@ -1,16 +1,11 @@
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, send_from_directory, url_for
import sqlite3, os, csv
from src.python.breed import get_breed_id, get_breeding_pairs, get_used_in_breeds import os
from src.util.utils import *
app = Flask(__name__) app = Flask(__name__)
DATABASE = "src/database.db"
def connect_db():
return sqlite3.connect(DATABASE)
@app.before_request @app.before_request
def before_request(): def before_request():
@ -44,19 +39,15 @@ def serve_monster_sprite(selected_monster):
def serve_favicon(): def serve_favicon():
return send_from_directory( '','static/img/favicon.ico') return send_from_directory( '','static/img/favicon.ico')
#API Calls #TO-DO: Split the rest of these API Calls
# List All Families #API Calls
@app.route("/api/families") from src.api.get_families import *
def json_families(): app.register_blueprint(get_families_bp)
cursor = g.db.cursor()
cursor.execute("SELECT DISTINCT name FROM families")
families = [row[0] for row in cursor.fetchall()]
return jsonify(families)
# List All Monsters # List All Monsters
@app.route("/api/monsters") @app.route("/api/monsters")
def json_monsters(): def get_monsters():
selected_family = request.args.get("family") selected_family = request.args.get("family")
cursor = g.db.cursor() cursor = g.db.cursor()
@ -84,7 +75,7 @@ def json_monsters():
@app.route("/api/monsters/stats") @app.route("/api/monsters/stats")
def json_monsters_stats(): def get_monster_stats():
cursor = g.db.cursor() cursor = g.db.cursor()
# Check if 'monster' argument is provided # Check if 'monster' argument is provided
@ -221,14 +212,104 @@ def get_breeding_combinations():
}, },
used_in_breeds=used_in_breeds, used_in_breeds=used_in_breeds,
) )
def get_used_in_breeds(target_monster):
cursor = g.db.cursor()
def read_csv(file_path): # Fetch breed IDs where the selected monster is used as a base
data = [] cursor.execute(
with open(file_path, 'r', newline='', encoding='utf-8') as csvfile: """
reader = csv.DictReader(csvfile) SELECT breed_id
for row in reader: FROM breed_requirements
data.append(row) WHERE requirement_type = 'base'
return data 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') @app.route('/skills')
def skills(): def skills():

0
src/api/__innit__.py Normal file
View File

11
src/api/get_families.py Normal file
View File

@ -0,0 +1,11 @@
from flask import Blueprint, g, jsonify
get_families_bp = Blueprint('families', __name__)
# List All Families
@get_families_bp.route("/api/families")
def get_families():
cursor = g.db.cursor()
cursor.execute("SELECT DISTINCT name FROM families")
families = [row[0] for row in cursor.fetchall()]
return jsonify(families)

View File

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

View File

@ -1,99 +0,0 @@
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

0
src/routes/__innit__.py Normal file
View File

0
src/util/__innit__.py Normal file
View File

14
src/util/utils.py Normal file
View File

@ -0,0 +1,14 @@
import sqlite3, csv
DATABASE = "src/data/database.db"
def connect_db():
return sqlite3.connect(DATABASE)
def read_csv(file_path):
data = []
with open(file_path, 'r', newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
data.append(row)
return data

View File

@ -655,6 +655,11 @@ video {
border-color: rgb(148 163 184 / var(--tw-border-opacity)); border-color: rgb(148 163 184 / var(--tw-border-opacity));
} }
.bg-gray-900 {
--tw-bg-opacity: 1;
background-color: rgb(17 24 39 / var(--tw-bg-opacity));
}
.bg-neutral-800 { .bg-neutral-800 {
--tw-bg-opacity: 1; --tw-bg-opacity: 1;
background-color: rgb(38 38 38 / var(--tw-bg-opacity)); background-color: rgb(38 38 38 / var(--tw-bg-opacity));
@ -665,11 +670,6 @@ video {
background-color: rgb(15 23 42 / var(--tw-bg-opacity)); background-color: rgb(15 23 42 / var(--tw-bg-opacity));
} }
.bg-gray-900 {
--tw-bg-opacity: 1;
background-color: rgb(17 24 39 / var(--tw-bg-opacity));
}
.p-2 { .p-2 {
padding: 0.5rem; padding: 0.5rem;
} }
@ -736,11 +736,6 @@ video {
line-height: 1.75rem; line-height: 1.75rem;
} }
.text-blue-500 {
--tw-text-opacity: 1;
color: rgb(59 130 246 / var(--tw-text-opacity));
}
.text-purple-400 { .text-purple-400 {
--tw-text-opacity: 1; --tw-text-opacity: 1;
color: rgb(192 132 252 / var(--tw-text-opacity)); color: rgb(192 132 252 / var(--tw-text-opacity));