You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

122 lines
4.2 KiB

import sqlite3
from flask import Flask, render_template, jsonify
from collections import defaultdict
import os
app = Flask(__name__)
def load_pokemon_data():
pokemon_list = []
conn = sqlite3.connect('pokemon_database.db')
cursor = conn.cursor()
cursor.execute('''
SELECT p.national_dex_number, p.name, pf.form_name, pf.image_path, pf.is_default, p.introduced_in_gen,
g.name AS earliest_game, m.icon_path AS mark_icon
FROM pokemon p
JOIN pokemon_forms pf ON p.national_dex_number = pf.pokemon_id
LEFT JOIN form_encounters fe ON pf.id = fe.form_id
LEFT JOIN games g ON fe.game_id = g.id
LEFT JOIN mark_game_associations mga ON g.id = mga.game_id
LEFT JOIN marks m ON mga.mark_id = m.id
GROUP BY p.national_dex_number, pf.id
ORDER BY p.national_dex_number, pf.is_default DESC, pf.form_name
''')
current_group = []
current_generation = None
current_dex_number = None
pokemon_forms = []
for row in cursor.fetchall():
national_dex_number, name, form_name, image_path, is_default, introduced_in_gen, earliest_game, mark_icon = row
pokemon = {
'ID': national_dex_number,
'Name': name,
'Form': form_name,
'Image': image_path,
'IsDefault': is_default,
'Generation': introduced_in_gen,
'EarliestGame': earliest_game,
'MarkIcon': mark_icon
}
if national_dex_number != current_dex_number:
if pokemon_forms:
for form in pokemon_forms:
current_group.append(form)
if len(current_group) == 30:
pokemon_list.append(current_group)
current_group = []
pokemon_forms = []
current_dex_number = national_dex_number
if is_default:
if current_generation is None or introduced_in_gen != current_generation:
if current_group:
while len(current_group) < 30:
current_group.append(None) # Add empty slots
pokemon_list.append(current_group)
current_group = []
current_generation = introduced_in_gen
pokemon_forms.append(pokemon)
# Add the last set of forms
for form in pokemon_forms:
current_group.append(form)
if len(current_group) == 30:
pokemon_list.append(current_group)
current_group = []
# Add any remaining Pokémon
if current_group:
while len(current_group) < 30:
current_group.append(None) # Add empty slots to the last group
pokemon_list.append(current_group)
conn.close()
return pokemon_list
@app.route('/')
def index():
pokemon_list = load_pokemon_data()
return render_template('index.html', grouped_pokemon=pokemon_list)
@app.route('/pokemon/<int:dex_number>')
def pokemon_details(dex_number):
conn = sqlite3.connect('pokemon_database.db')
cursor = conn.cursor()
cursor.execute('''
SELECT pf.form_name, g.name, l.name, em.name
FROM form_encounters fe
JOIN games g ON fe.game_id = g.id
JOIN locations l ON fe.location_id = l.id
JOIN encounter_methods em ON fe.encounter_method_id = em.id
JOIN pokemon_forms pf ON fe.form_id = pf.id
WHERE pf.pokemon_id = ?
ORDER BY pf.is_default DESC, pf.form_name, g.generation, g.name, l.name
''', (dex_number,))
encounters = [
{'form': form, 'game': game, 'location': location, 'method': method}
for form, game, location, method in cursor.fetchall()
]
conn.close()
return jsonify(encounters)
if __name__ == '__main__':
extra_files = ['.']
extra_dirs = ['./templates/', './static/']
for extra_dir in extra_dirs:
for dirname, dirs, files in os.walk(extra_dir):
for filename in files:
filename = os.path.join(dirname, filename)
if os.path.isfile(filename):
extra_files.append(filename)
app.run(debug=True, extra_files=extra_files)