diff --git a/database/db_controller.py b/database/db_controller.py index 4542c95..3b42f20 100644 --- a/database/db_controller.py +++ b/database/db_controller.py @@ -437,6 +437,16 @@ class DBController: results = self.cursor.fetchall() return [dict(row) for row in results] + def get_game_by_id(self, id): + self.cursor.execute(''' + SELECT * FROM games + WHERE id = ? + ''', (id)) + + # Fetch and print the results + result = self.cursor.fetchone() + return dict(result) + def get_games_by_generation(self, generation): self.cursor.execute(''' SELECT id, name FROM games @@ -462,4 +472,14 @@ class DBController: ''', (pfic, game_id, type, json.dumps(data))) self.conn.commit() print(f"Added: {pfic}") - pass \ No newline at end of file + pass + + def get_encounters(self, pfic): + self.cursor.execute(''' + SELECT * FROM encounters + WHERE PFIC = ? + ''', (pfic)) + + # Fetch and print the results + results = self.cursor.fetchall() + return [dict(row) for row in results] \ No newline at end of file diff --git a/ui/main_window_controller.py b/ui/main_window_controller.py index 379510a..f2a20af 100644 --- a/ui/main_window_controller.py +++ b/ui/main_window_controller.py @@ -3,6 +3,7 @@ from PyQt6.QtWidgets import QMenu from PyQt6.QtGui import QAction import os +from ui.workers.calculate_origin_mark_worker import CalculateOriginMarkWorker from ui.workers.gather_encounter_locations import GatherEncountersWorker from ui.workers.gather_home_storage_status_worker import GatherHomeStorageStatus from ui.workers.gather_pokemon_forms_worker import GatherPokemonFormsWorker @@ -153,7 +154,12 @@ class MainWindowController: db.update_encounter_locations(data) def gather_marks_info(self): - pass + worker = CalculateOriginMarkWorker() + worker.signals.finished.connect(self.on_marks_calculated) + self.thread_pool.start(worker) + + def on_marks_calculated(self, data): + print("Works Done!") def load_shiftable_forms(self): pass diff --git a/ui/workers/calculate_origin_mark_worker.py b/ui/workers/calculate_origin_mark_worker.py new file mode 100644 index 0000000..4cdaf40 --- /dev/null +++ b/ui/workers/calculate_origin_mark_worker.py @@ -0,0 +1,101 @@ +from PyQt6.QtCore import QObject, pyqtSignal, QRunnable + +from cache import cache +from db import db + +from utility.functions import get_display_name, get_shiftable_forms + +class CalculateOriginMarkWorkerSignals(QObject): + finished = pyqtSignal(list) + +class CalculateOriginMarkWorker(QRunnable): + def __init__(self): + super().__init__() + self.signals = CalculateOriginMarkWorkerSignals() + self.marks = {} + + def run(self): + try: + gathered_data = self.calculate_marks() + self.signals.finished.emit(gathered_data) + except Exception as e: + print(f"Error gathering Pokémon home storage status: {e}") + + def calculate_marks(self): + all_pokemon_forms = db.get_list_of_pokemon_forms() + for form_entry in all_pokemon_forms: + if form_entry["storable_in_home"] == False: + continue + + print(f"Determining mark for {get_display_name(form_entry)}") + + target_generation = form_entry["generation"] + pfic = form_entry["pfic"] + + #Rule 1 + # 1. If a pokemon form has a previous evolution from within the same generation, + # use the mark of the previous evolution. This should be recursive within the same generation. + chain = db.get_full_evolution_paths(pfic) + if chain: + base_form_in_generation = None + last_pfic = pfic + current_pfic = pfic + while True: + current_pfic, _ = db.get_previous_evolution(current_pfic) + if current_pfic == None: + base_form_in_generation = last_pfic + break + chain_pokemon_data = db.get_pokemon_details(current_pfic) + #chain_pokemon_data = event_system.call_sync('get_pokemon_form_by_pfic', current_pfic[0]) + if chain_pokemon_data["generation"] == target_generation: + base_form_in_generation = current_pfic + else: + base_form_in_generation = last_pfic + break + last_pfic = current_pfic + + if base_form_in_generation and base_form_in_generation != pfic: + print(f"Base form in generation for {get_display_name(form_entry)} is {base_form_in_generation}") + mark_id = self.determine_origin_mark(base_form_in_generation, target_generation) + if mark_id != None: + #event_system.emit_sync('assign_mark_to_form', (pfic, mark_id)) + self.marks[pfic] = mark_id + continue + elif base_form_in_generation == pfic: + mark_id = self.determine_origin_mark(pfic, target_generation) + if mark_id != None: + #event_system.emit_sync('assign_mark_to_form', (pfic, mark_id)) + self.marks[pfic] = mark_id + continue; + + pass + + def determine_origin_mark(self, pfic, target_generation): + shiftable_forms = get_shiftable_forms(pfic) + if len(shiftable_forms) > 0: + for shiftable_form in shiftable_forms: + mark_id = self.determine_origin_mark(shiftable_form[2], target_generation) + return mark_id + encounters = db.get_encounters(pfic) + if encounters: + generation_encounters = [] + for encounter in encounters: + game_info = db.get_game_by_id(encounter["game_id"]) + game_generation = game_info["generation"] + game_id = game_info["id"] + encounter = encounter + (game_generation, game_id) + if game_generation == target_generation: + generation_encounters.append(encounter) + if len(generation_encounters) > 0: + generation_encounters = sorted(generation_encounters, key=lambda x: x[12]) + form_info = db.get_pokemon_details(pfic) + game_info = db.get_game_by_id(encounter["game_id"]) + mark_id = 1 # TODO: Work this bit out. + mark_id = event_system.call_sync('get_mark_for_game_name', generation_encounters[0][0]) + if mark_id == None: + self.logger.info(f"No mark found for {form_info[0]} {form_info[1]}") + else: + mark_details = event_system.call_sync('get_mark_details', mark_id) + self.logger.info(f"Mark for {form_info[0]} {form_info[1]} is {mark_details[0]} - {mark_details[1]}") + return mark_id + return None diff --git a/utility/data.py b/utility/data.py index 186c386..ed88ba4 100644 --- a/utility/data.py +++ b/utility/data.py @@ -322,4 +322,16 @@ POKEMON_PROPER_NOUNS = POKEMON_PROPER_NOUNS | set(regional_descriptors) days = ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"] times = ["Morning", "Day", "Night"] -rods = ["Old Rod", "Good Rod", "Super Rod"] \ No newline at end of file +rods = ["Old Rod", "Good Rod", "Super Rod"] + +shiftable_forms = [ + {"from_pfic":"0412-04-002-0", "to_pfic":"0412-04-001-0"}, + {"from_pfic":"0412-04-003-0", "to_pfic":"0412-04-001-0"}, + {"from_pfic":"0641-05-002-0", "to_pfic":"0641-05-001-0"}, + {"from_pfic":"0642-05-002-0", "to_pfic":"0642-05-001-0"}, + {"from_pfic":"0645-05-002-0", "to_pfic":"0645-05-001-0"}, + {"from_pfic":"0647-05-002-0", "to_pfic":"0647-05-001-0"}, + {"from_pfic":"0720-06-002-0", "to_pfic":"0720-06-001-0"}, + {"from_pfic":"0905-08-002-0", "to_pfic":"0905-08-001-0"}, + {"from_pfic":"0492-04-002-0", "to_pfic":"0492-04-001-0"} +] \ No newline at end of file diff --git a/utility/functions.py b/utility/functions.py index 32f3c8b..da2c89a 100644 --- a/utility/functions.py +++ b/utility/functions.py @@ -1,4 +1,4 @@ -from .data import pokemon_generations, main_line_games +from .data import pokemon_generations, main_line_games, shiftable_forms import unicodedata import re @@ -129,4 +129,11 @@ def extract_bracketed_text(string): for unmatched_start in stack: results.append(string[unmatched_start + 1:]) - return results \ No newline at end of file + return results + +def get_shiftable_forms(pfic): + forms = [] + for form_pair in shiftable_forms: + if pfic == form_pair["from_pfic"]: + forms.append(form_pair) + return forms