breeding database is wrong, todo:fix

This commit is contained in:
Chris kerr 2024-01-30 18:21:26 -05:00
parent c41789e3a4
commit 9a246dd6b7
3 changed files with 53 additions and 123 deletions

114
app.py
View File

@ -3,53 +3,64 @@ import sqlite3
app = Flask(__name__) app = Flask(__name__)
DATABASE = 'static/data/monsters.db' DATABASE = "static/data/monsters.db"
def connect_db(): def connect_db():
return sqlite3.connect(DATABASE) return sqlite3.connect(DATABASE)
@app.before_request @app.before_request
def before_request(): def before_request():
g.db = connect_db() g.db = connect_db()
@app.teardown_request @app.teardown_request
def teardown_request(exception): def teardown_request(exception):
if hasattr(g, 'db'): if hasattr(g, "db"):
g.db.close() g.db.close()
@app.route("/") @app.route("/")
def show_index(): def show_index():
return render_template("index.html") return render_template("index.html")
@app.route('/get_families')
@app.route("/get_families")
def get_families(): def get_families():
cursor = g.db.cursor() cursor = g.db.cursor()
cursor.execute('SELECT DISTINCT name FROM families') cursor.execute("SELECT DISTINCT name FROM families")
families = [row[0] for row in cursor.fetchall()] families = [row[0] for row in cursor.fetchall()]
return jsonify(families) return jsonify(families)
@app.route('/get_monsters')
@app.route("/get_monsters")
def get_monsters(): def get_monsters():
selected_family = request.args.get('family') selected_family = request.args.get("family")
cursor = g.db.cursor() cursor = g.db.cursor()
if selected_family: if selected_family:
cursor.execute(''' cursor.execute(
"""
SELECT name FROM monsters SELECT name FROM monsters
WHERE family_id = (SELECT id FROM families WHERE name = ?) WHERE family_id = (SELECT id FROM families WHERE name = ?)
''', (selected_family,)) """,
(selected_family,),
)
else: else:
cursor.execute('SELECT DISTINCT name FROM monsters') cursor.execute("SELECT DISTINCT name FROM monsters")
monsters = [row[0] for row in cursor.fetchall()] monsters = [row[0] for row in cursor.fetchall()]
return jsonify(monsters) return jsonify(monsters)
@app.route('/monster/<monster_name>')
@app.route("/monster/<monster_name>")
def monster_info(monster_name): def monster_info(monster_name):
cursor = g.db.cursor() cursor = g.db.cursor()
# Retrieve monster information from the database based on name # Retrieve monster information from the database based on name
cursor.execute(''' cursor.execute(
"""
SELECT SELECT
monsters.id, monsters.name, families.name AS family, monsters.in_story, monsters.id, monsters.name, families.name AS family, monsters.in_story,
monsters.agl, monsters.int, monsters.maxlvl, monsters.atk, monsters.mp, monsters.agl, monsters.int, monsters.maxlvl, monsters.atk, monsters.mp,
@ -59,7 +70,9 @@ def monster_info(monster_name):
JOIN families ON monsters.family_id = families.id JOIN families ON monsters.family_id = families.id
WHERE WHERE
monsters.name = ? monsters.name = ?
''', (monster_name,)) """,
(monster_name,),
)
monster_info = cursor.fetchone() monster_info = cursor.fetchone()
@ -67,59 +80,38 @@ def monster_info(monster_name):
abort(404) abort(404)
# Retrieve skills for the monster # Retrieve skills for the monster
cursor.execute('SELECT skill FROM skills WHERE monster_id = ?', (monster_info[0],)) cursor.execute("SELECT skill FROM skills WHERE monster_id = ?", (monster_info[0],))
skills = [row[0] for row in cursor.fetchall()] skills = [row[0] for row in cursor.fetchall()]
# Retrieve spawn locations for the monster # Retrieve spawn locations for the monster
cursor.execute('SELECT map, description FROM spawn_locations WHERE monster_id = ?', (monster_info[0],)) cursor.execute(
spawn_locations = [{'map': row[0], 'description': row[1]} for row in cursor.fetchall()] "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={ return render_template(
'id': monster_info[0], "monsters.html",
'name': monster_info[1], monster={
'family': monster_info[2], "id": monster_info[0],
'in_story': 'Yes' if monster_info[3] else 'No', "name": monster_info[1],
'agl': monster_info[4], "family": monster_info[2],
'int': monster_info[5], "in_story": "Yes" if monster_info[3] else "No",
'maxlvl': monster_info[6], "agl": monster_info[4],
'atk': monster_info[7], "int": monster_info[5],
'mp': monster_info[8], "maxlvl": monster_info[6],
'exp': monster_info[9], "atk": monster_info[7],
'hp': monster_info[10], "mp": monster_info[8],
'def': monster_info[11], "exp": monster_info[9],
'skills': skills, "hp": monster_info[10],
'spawn_locations': spawn_locations "def": monster_info[11],
}) "skills": skills,
"spawn_locations": spawn_locations,
# Add a route for the breeding page },
@app.route('/breeding') )
def breeding():
# Pass any necessary data to the breeding template (e.g., breeding_data)
return render_template('breeding.html')
# Add this function to handle breeding logic
def breed_monsters(parent1, parent2):
# Implement your breeding logic here
# For now, let's assume the result is a string
result = f"{parent1} x {parent2} => Offspring Monster"
return result
# Add this route for breeding if __name__ == "__main__":
@app.route('/breed', methods=['GET'])
def breed():
# Get parent monsters from query parameters
parent1 = request.args.get('parent1')
parent2 = request.args.get('parent2')
# Validate input (you may want to add more validation)
if not parent1 or not parent2:
return jsonify({'error': 'Invalid input'})
# Call the breeding function
result = breed_monsters(parent1, parent2)
# Return the breeding result as JSON
return jsonify({'result': result})
if __name__ == '__main__':
app.run(debug=True) app.run(debug=True)

View File

@ -1,19 +0,0 @@
// static/js/breeding.js
function breed() {
var parent1 = document.getElementById("parent1").value;
var parent2 = document.getElementById("parent2").value;
// Make an AJAX request to your Flask route for breeding logic
// You can use fetch or another library for AJAX requests
// For demonstration purposes, we'll assume you have a Flask route named /breed
fetch(`/breed?parent1=${parent1}&parent2=${parent2}`)
.then(response => response.json())
.then(data => {
// Display the breeding result in the breedingResult div
var breedingResultDiv = document.getElementById("breedingResult");
breedingResultDiv.innerHTML = `<p>Breeding Result: ${data.result}</p>`;
})
.catch(error => console.error('Error:', error));
}

View File

@ -1,43 +0,0 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Dragon Warrior Monsters 2 - Breeding</title>
<link rel="stylesheet" href="../static/style/output.css">
</head>
<body class="p-8">
<h1 class="font-bold text-blue-500">Breeding Guide</h1>
<div class="mt-8">
<h2 class="font-bold text-blue-500">Breeding Options</h2>
<div>
<label for="parent1">Select Parent 1:</label>
<select id="parent1" class="p-2 border border-gray-400">
<!-- Add options dynamically based on breeding data -->
{% for breed in breeding_data %}
<option value="{{ breed.target }}">{{ breed.target }}</option>
{% endfor %}
</select>
</div>
<div class="mt-4">
<label for="parent2">Select Parent 2:</label>
<select id="parent2" class="p-2 border border-gray-400">
<!-- Add options dynamically based on breeding data -->
{% for breed in breeding_data %}
<option value="{{ breed.target }}">{{ breed.target }}</option>
{% endfor %}
</select>
</div>
<button onclick="breed()">Breed!</button>
<!-- Display breeding result -->
<div id="breedingResult" class="mt-4"></div>
</div>
<script src="{{ url_for('static', filename='js/breeding.js') }}"></script>
</body>
</html>