dwm-app/app.py

263 lines
6.8 KiB
Python
Raw Normal View History

2024-01-30 16:06:00 +00:00
from flask import Flask, render_template, g, abort, request, jsonify
import sqlite3, os
2024-01-30 16:06:00 +00:00
app = Flask(__name__)
2024-01-30 23:21:26 +00:00
DATABASE = "static/data/monsters.db"
2024-01-30 16:06:00 +00:00
def connect_db():
return sqlite3.connect(DATABASE)
2024-01-30 23:21:26 +00:00
2024-01-30 16:06:00 +00:00
@app.before_request
def before_request():
g.db = connect_db()
2024-01-30 23:21:26 +00:00
2024-01-30 16:06:00 +00:00
@app.teardown_request
def teardown_request(exception):
2024-01-30 23:21:26 +00:00
if hasattr(g, "db"):
2024-01-30 16:06:00 +00:00
g.db.close()
2024-02-02 02:52:59 +00:00
def get_js_files():
2024-02-02 02:52:59 +00:00
js_folder = os.path.join(app.static_folder, "js")
js_files = [f for f in os.listdir(js_folder) if f.endswith(".js")]
return js_files
2024-01-30 23:21:26 +00:00
2024-02-02 02:52:59 +00:00
@app.route("/")
2024-01-30 16:06:00 +00:00
def show_index():
js_files = get_js_files()
2024-02-02 02:52:59 +00:00
return render_template("index.html", js_files=js_files)
2024-01-30 16:06:00 +00:00
2024-01-30 23:21:26 +00:00
@app.route("/get_families")
2024-01-30 16:06:00 +00:00
def get_families():
cursor = g.db.cursor()
2024-01-30 23:21:26 +00:00
cursor.execute("SELECT DISTINCT name FROM families")
2024-01-30 16:06:00 +00:00
families = [row[0] for row in cursor.fetchall()]
return jsonify(families)
2024-01-30 23:21:26 +00:00
@app.route("/get_monsters")
2024-01-30 16:06:00 +00:00
def get_monsters():
2024-01-30 23:21:26 +00:00
selected_family = request.args.get("family")
2024-01-30 16:06:00 +00:00
cursor = g.db.cursor()
if selected_family:
2024-01-30 23:21:26 +00:00
cursor.execute(
"""
2024-01-30 16:06:00 +00:00
SELECT name FROM monsters
WHERE family_id = (SELECT id FROM families WHERE name = ?)
2024-01-30 23:21:26 +00:00
""",
(selected_family,),
)
2024-01-30 16:06:00 +00:00
else:
2024-01-30 23:21:26 +00:00
cursor.execute("SELECT DISTINCT name FROM monsters")
2024-01-30 16:06:00 +00:00
monsters = [row[0] for row in cursor.fetchall()]
return jsonify(monsters)
2024-01-30 23:21:26 +00:00
@app.route("/monster/<monster_name>")
2024-01-30 16:06:00 +00:00
def monster_info(monster_name):
cursor = g.db.cursor()
# Retrieve monster information from the database based on name
2024-01-30 23:21:26 +00:00
cursor.execute(
"""
2024-01-30 16:06:00 +00:00
SELECT
monsters.id, monsters.name, families.name AS family, monsters.in_story,
monsters.agl, monsters.int, monsters.maxlvl, monsters.atk, monsters.mp,
monsters.exp, monsters.hp, monsters.def
FROM
monsters
JOIN families ON monsters.family_id = families.id
WHERE
monsters.name = ?
2024-01-30 23:21:26 +00:00
""",
(monster_name,),
)
2024-01-30 16:06:00 +00:00
monster_info = cursor.fetchone()
if monster_info is None:
abort(404)
# Retrieve skills for the monster
2024-01-30 23:21:26 +00:00
cursor.execute("SELECT skill FROM skills WHERE monster_id = ?", (monster_info[0],))
2024-01-30 16:06:00 +00:00
skills = [row[0] for row in cursor.fetchall()]
# Retrieve spawn locations for the monster
2024-01-30 23:21:26 +00:00
cursor.execute(
"SELECT map, description FROM spawn_locations WHERE monster_id = ?",
(monster_info[0],),
)
spawn_locations = [
{"map": row[0], "description": row[1]} for row in cursor.fetchall()
]
return render_template(
"monsters.html",
monster={
"id": monster_info[0],
"name": monster_info[1],
"family": monster_info[2],
"in_story": "Yes" if monster_info[3] else "No",
"agl": monster_info[4],
"int": monster_info[5],
"maxlvl": monster_info[6],
"atk": monster_info[7],
"mp": monster_info[8],
"exp": monster_info[9],
"hp": monster_info[10],
"def": monster_info[11],
"skills": skills,
"spawn_locations": spawn_locations,
},
)
2024-02-02 02:52:59 +00:00
@app.route("/monster_info_json/<monster_name>")
def monster_info_json(monster_name):
cursor = g.db.cursor()
# Retrieve monster information from the database based on name
cursor.execute(
"""
SELECT
monsters.id, monsters.name, families.name AS family, monsters.in_story,
monsters.agl, monsters.int, monsters.maxlvl, monsters.atk, monsters.mp,
monsters.exp, monsters.hp, monsters.def
FROM
monsters
JOIN families ON monsters.family_id = families.id
WHERE
monsters.name = ?
""",
(monster_name,),
)
monster_info = cursor.fetchone()
if monster_info is None:
abort(404)
# Retrieve skills for the monster
cursor.execute("SELECT skill FROM skills WHERE monster_id = ?", (monster_info[0],))
skills = [row[0] for row in cursor.fetchall()]
# Retrieve spawn locations for the monster
cursor.execute(
"SELECT map, description FROM spawn_locations WHERE monster_id = ?",
(monster_info[0],),
)
spawn_locations = [
{"map": row[0], "description": row[1]} for row in cursor.fetchall()
]
return jsonify(
monster={
"id": monster_info[0],
"name": monster_info[1],
"family": monster_info[2],
"in_story": "Yes" if monster_info[3] else "No",
"agl": monster_info[4],
"int": monster_info[5],
"maxlvl": monster_info[6],
"atk": monster_info[7],
"mp": monster_info[8],
"exp": monster_info[9],
"hp": monster_info[10],
"def": monster_info[11],
"skills": skills,
"spawn_locations": spawn_locations,
}
)
2024-01-30 23:21:26 +00:00
2024-01-31 01:17:15 +00:00
# Add this route for fetching breeding combinations
@app.route("/get_breeding_combinations")
def get_breeding_combinations():
selected_monster = request.args.get("monster")
if not selected_monster:
return jsonify({"error": "Invalid input"})
# Fetch breed ID based on the selected monster as a target
breed_id = get_breed_id(selected_monster)
2024-01-31 01:17:15 +00:00
if breed_id is None:
return jsonify({"error": f"No breed information found for {selected_monster}"})
base_combinations, mate_combinations = get_breeding_info(breed_id)
return render_template(
"breeding.html",
selected_monster={
"name": selected_monster,
"base_combinations": base_combinations,
"mate_combinations": mate_combinations,
},
2024-01-31 01:17:15 +00:00
)
def get_breed_id(target_monster):
2024-01-31 01:17:15 +00:00
cursor = g.db.cursor()
# Fetch breed ID based on the selected monster as a target
2024-01-31 01:17:15 +00:00
cursor.execute(
"""
SELECT breeds.id
2024-01-31 01:17:15 +00:00
FROM breeds
WHERE breeds.target = ?
""",
(target_monster,),
2024-01-31 01:17:15 +00:00
)
breed_id = cursor.fetchone()
2024-01-31 01:17:15 +00:00
if breed_id:
return breed_id[0]
else:
return None
2024-01-31 01:17:15 +00:00
def get_breeding_info(breed_id):
2024-01-31 01:17:15 +00:00
cursor = g.db.cursor()
# Fetch base and mate breeding combinations based on the breed ID
2024-01-31 01:17:15 +00:00
cursor.execute(
"""
SELECT requirement_type, requirement_value
FROM breed_requirements
WHERE breed_id = ?
""",
(breed_id,),
2024-01-31 01:17:15 +00:00
)
breeding_info = cursor.fetchall()
base_combinations = [
value
for (requirement_type, value) in breeding_info
if requirement_type == "base"
]
mate_combinations = [
value
for (requirement_type, value) in breeding_info
if requirement_type == "mate"
]
2024-01-31 01:17:15 +00:00
return base_combinations, mate_combinations
2024-01-31 01:17:15 +00:00
2024-02-02 02:52:59 +00:00
@app.route("/footer")
def show_footer():
return render_template("footer.html")
2024-01-31 01:17:15 +00:00
2024-02-02 02:52:59 +00:00
2024-01-30 23:21:26 +00:00
if __name__ == "__main__":
2024-01-30 16:06:00 +00:00
app.run(debug=True)