|
|
|
@ -13,46 +13,101 @@ def load_pokemon_data(): |
|
|
|
|
|
|
|
# First query: Get all Pokémon data |
|
|
|
cursor.execute(''' |
|
|
|
WITH EarliestGamePerPokemon AS ( |
|
|
|
SELECT e.pfic, MIN(g.generation) as min_generation, MIN(g.id) as earliest_game_id |
|
|
|
FROM encounters e |
|
|
|
JOIN games g ON e.game_id = g.id |
|
|
|
GROUP BY e.pfic |
|
|
|
) |
|
|
|
SELECT |
|
|
|
pf.national_dex, pf.name, pf.form_name, pf.PFIC, pf.generation, |
|
|
|
ps.storable_in_home, g.name as earliest_game, |
|
|
|
m.icon_path as mark_icon, m.id as mark_id, pf.is_baby_form |
|
|
|
ps.storable_in_home |
|
|
|
FROM pokemon_forms pf |
|
|
|
JOIN pokemon_storage ps ON pf.PFIC = ps.PFIC |
|
|
|
LEFT JOIN EarliestGamePerPokemon eg ON pf.PFIC = eg.pfic |
|
|
|
LEFT JOIN games g ON eg.earliest_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 |
|
|
|
WHERE ps.storable_in_home = 1 |
|
|
|
ORDER BY pf.PFIC |
|
|
|
''') |
|
|
|
|
|
|
|
pokemon_data = cursor.fetchall() |
|
|
|
|
|
|
|
# Second query: Get evolution chain with generation info |
|
|
|
cursor.execute(''' |
|
|
|
WITH RECURSIVE EvolutionChain AS ( |
|
|
|
SELECT to_pfic AS PFIC, from_pfic, pf.generation |
|
|
|
FROM evolution_chains ec |
|
|
|
JOIN pokemon_forms pf ON ec.to_pfic = pf.PFIC |
|
|
|
UNION ALL |
|
|
|
SELECT ec.to_pfic, e.from_pfic, pf.generation |
|
|
|
FROM evolution_chains ec |
|
|
|
JOIN EvolutionChain e ON ec.from_pfic = e.PFIC |
|
|
|
JOIN pokemon_forms pf ON ec.to_pfic = pf.PFIC |
|
|
|
) |
|
|
|
SELECT PFIC, MIN(from_pfic) AS base_pfic, MIN(generation) AS base_generation |
|
|
|
FROM EvolutionChain |
|
|
|
GROUP BY PFIC |
|
|
|
''') |
|
|
|
|
|
|
|
evolution_data = {row[0]: (row[1], row[2]) for row in cursor.fetchall()} |
|
|
|
# Function to get the origin mark for a Pokémon |
|
|
|
def get_origin_mark(pfic, generation): |
|
|
|
# Step 1: Check for previous evolution in the same generation |
|
|
|
cursor.execute(''' |
|
|
|
WITH RECURSIVE EvolutionChain AS ( |
|
|
|
SELECT ec.to_pfic, ec.from_pfic, pf_to.generation as to_gen, pf_from.generation as from_gen |
|
|
|
FROM evolution_chains ec |
|
|
|
JOIN pokemon_forms pf_to ON ec.to_pfic = pf_to.PFIC |
|
|
|
JOIN pokemon_forms pf_from ON ec.from_pfic = pf_from.PFIC |
|
|
|
WHERE ec.to_pfic = ? AND pf_to.generation = ? |
|
|
|
UNION ALL |
|
|
|
SELECT ec.to_pfic, ec.from_pfic, pf_to.generation, pf_from.generation |
|
|
|
FROM evolution_chains ec |
|
|
|
JOIN EvolutionChain e ON ec.to_pfic = e.from_pfic |
|
|
|
JOIN pokemon_forms pf_to ON ec.to_pfic = pf_to.PFIC |
|
|
|
JOIN pokemon_forms pf_from ON ec.from_pfic = pf_from.PFIC |
|
|
|
WHERE pf_to.generation = ? AND pf_from.generation = ? |
|
|
|
) |
|
|
|
SELECT from_pfic |
|
|
|
FROM EvolutionChain |
|
|
|
WHERE from_pfic IS NOT NULL AND from_gen = ? |
|
|
|
ORDER BY from_pfic |
|
|
|
LIMIT 1 |
|
|
|
''', (pfic, generation, generation, generation, generation)) |
|
|
|
base_pfic = cursor.fetchone() |
|
|
|
|
|
|
|
if base_pfic: |
|
|
|
pfic = base_pfic[0] |
|
|
|
|
|
|
|
# Step 2: Look for the earliest encounter for this form in the current generation |
|
|
|
cursor.execute(''' |
|
|
|
SELECT g.name, m.icon_path, m.id |
|
|
|
FROM encounters e |
|
|
|
JOIN games g ON e.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 |
|
|
|
WHERE e.pfic = ? AND g.generation = ? |
|
|
|
ORDER BY g.id |
|
|
|
LIMIT 1 |
|
|
|
''', (pfic, generation)) |
|
|
|
encounter_data = cursor.fetchone() |
|
|
|
|
|
|
|
if encounter_data: |
|
|
|
return encounter_data |
|
|
|
|
|
|
|
# Step 3: Look for encounters of previous evolutions in the current generation |
|
|
|
cursor.execute(''' |
|
|
|
WITH RECURSIVE EvolutionChain AS ( |
|
|
|
SELECT ec.to_pfic, ec.from_pfic |
|
|
|
FROM evolution_chains ec |
|
|
|
WHERE ec.to_pfic = ? |
|
|
|
UNION ALL |
|
|
|
SELECT ec.to_pfic, ec.from_pfic |
|
|
|
FROM evolution_chains ec |
|
|
|
JOIN EvolutionChain e ON ec.to_pfic = e.from_pfic |
|
|
|
) |
|
|
|
SELECT g.name, m.icon_path, m.id |
|
|
|
FROM EvolutionChain ec |
|
|
|
JOIN encounters e ON ec.from_pfic = e.pfic |
|
|
|
JOIN games g ON e.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 |
|
|
|
WHERE g.generation = ? |
|
|
|
ORDER BY g.id |
|
|
|
LIMIT 1 |
|
|
|
''', (pfic, generation)) |
|
|
|
evolution_encounter_data = cursor.fetchone() |
|
|
|
|
|
|
|
if evolution_encounter_data: |
|
|
|
return evolution_encounter_data |
|
|
|
|
|
|
|
# Step 4: Use the mark of the earliest game of the generation |
|
|
|
cursor.execute(''' |
|
|
|
SELECT g.name, m.icon_path, m.id |
|
|
|
FROM games g |
|
|
|
LEFT JOIN mark_game_associations mga ON g.id = mga.game_id |
|
|
|
LEFT JOIN marks m ON mga.mark_id = m.id |
|
|
|
WHERE g.generation = ? |
|
|
|
ORDER BY g.id |
|
|
|
LIMIT 1 |
|
|
|
''', (generation,)) |
|
|
|
generation_data = cursor.fetchone() |
|
|
|
|
|
|
|
return generation_data if generation_data else (None, None, None) |
|
|
|
|
|
|
|
# Process the data |
|
|
|
current_group = [] |
|
|
|
@ -61,33 +116,9 @@ def load_pokemon_data(): |
|
|
|
pokemon_forms = [] |
|
|
|
|
|
|
|
for row in pokemon_data: |
|
|
|
national_dex, name, form_name, pfic, generation, storable_in_home, earliest_game, mark_icon, mark_id, is_baby_form = row |
|
|
|
national_dex, name, form_name, pfic, generation, storable_in_home = row |
|
|
|
|
|
|
|
# Find the base form for the mark, considering generation |
|
|
|
base_pfic = pfic |
|
|
|
base_generation = generation |
|
|
|
while base_pfic in evolution_data: |
|
|
|
prev_pfic, prev_generation = evolution_data[base_pfic] |
|
|
|
if prev_pfic is not None and prev_generation == base_generation: |
|
|
|
base_pfic = prev_pfic |
|
|
|
else: |
|
|
|
break |
|
|
|
|
|
|
|
# If the base form is different, we need to fetch its earliest game and mark |
|
|
|
if base_pfic != pfic: |
|
|
|
cursor.execute(''' |
|
|
|
SELECT g.name, m.icon_path, m.id |
|
|
|
FROM encounters e |
|
|
|
JOIN games g ON e.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 |
|
|
|
WHERE e.pfic = ? AND g.generation = ? |
|
|
|
ORDER BY g.id |
|
|
|
LIMIT 1 |
|
|
|
''', (base_pfic, base_generation)) |
|
|
|
base_data = cursor.fetchone() |
|
|
|
if base_data: |
|
|
|
earliest_game, mark_icon, mark_id = base_data |
|
|
|
earliest_game, mark_icon, mark_id = get_origin_mark(pfic, generation) |
|
|
|
|
|
|
|
pokemon = { |
|
|
|
'pfic': pfic, |
|
|
|
@ -102,9 +133,8 @@ def load_pokemon_data(): |
|
|
|
'MarkIcon': mark_icon, |
|
|
|
'MarkID': mark_id |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Add the Pokémon to the current group |
|
|
|
if national_dex != current_dex_number: |
|
|
|
if pokemon_forms: |
|
|
|
for form in pokemon_forms: |
|
|
|
|