src dir setup, some utils moved

This commit is contained in:
0ceanSlim 2024-02-14 15:55:41 -05:00
parent 68baef221f
commit 4bcb98f2c1
11 changed files with 119 additions and 128 deletions

115
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():
@ -222,13 +217,103 @@ def get_breeding_combinations():
used_in_breeds=used_in_breeds, used_in_breeds=used_in_breeds,
) )
def read_csv(file_path): def get_used_in_breeds(target_monster):
data = [] cursor = g.db.cursor()
with open(file_path, 'r', newline='', encoding='utf-8') as csvfile:
reader = csv.DictReader(csvfile) # Fetch breed IDs where the selected monster is used as a base
for row in reader: cursor.execute(
data.append(row) """
return data 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') @app.route('/skills')
def skills(): def skills():

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

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));