Skip to content

Commit

Permalink
fix:Refactor database module to support multiple characters and track…
Browse files Browse the repository at this point in the history
… added Nikkes
  • Loading branch information
valentin-marquez committed Oct 15, 2024
1 parent e61e891 commit 02de65f
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 15 deletions.
46 changes: 36 additions & 10 deletions src/data/database.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,44 @@
import datetime
import json
import os
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, List, Tuple

from src.config import Config


class NikkeDatabase:

def __init__(self) -> None:
self.config: Config = Config()
self.data_folder: str = str(self.config.USER_DATA_DIR)
self.current_file: str = self._generate_new_filename()
self.data: Dict[str, Any] = {}
self.data: List[Dict[str, Any]] = []
self.added_nikkes: int = 0

def _generate_new_filename(self) -> str:
timestamp = datetime.datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
return str(self.config.get_user_data_file(timestamp))

def load_data(self) -> Dict[str, Any]:
def load_data(self) -> List[Dict[str, Any]]:
if os.path.exists(self.current_file):
with open(self.current_file, "r") as f:
return json.load(f)
return {}
return []

def save_data(self) -> None:
os.makedirs(self.data_folder, exist_ok=True)
with open(self.current_file, "w") as f:
json.dump(self.data, f, indent=4)

def add_or_update_character(self, name: str, nikke_info: Dict[str, Any]) -> bool:
def add_or_update_character(
self, name: str, nikke_info: Dict[str, Any]
) -> Tuple[bool, bool]:
# Validate combat_power
try:
combat_power = int(nikke_info.get("combat_power", "0"))
except ValueError:
combat_power = 0

simplified_info = {
"name": name,
"manufacturer": nikke_info.get("manufacturer"),
Expand All @@ -38,18 +48,34 @@ def add_or_update_character(self, name: str, nikke_info: Dict[str, Any]) -> bool
"rarity": nikke_info.get("rarity"),
"weapon": nikke_info.get("weapon"),
"element": nikke_info.get("element"),
"combat_power": nikke_info.get("combat_power"),
"combat_power": str(combat_power),
"last_updated": str(datetime.datetime.now()),
}

self.data[name] = simplified_info
# Check if character already exists
for i, character in enumerate(self.data):
if character["name"] == name:
# Update existing character
self.data[i] = simplified_info
self.save_data()
return True, False # Updated, but not added

# If character doesn't exist, add new one
self.data.append(simplified_info)
self.added_nikkes += 1
self.save_data()
return True
return True, True # Added new character

def get_character(self, name: str) -> Optional[Dict[str, Any]]:
return self.data.get(name)
for character in self.data:
if character["name"] == name:
return character
return None

def get_added_nikkes_count(self) -> int:
return self.added_nikkes

def get_all_characters(self) -> Dict[str, Any]:
def get_all_characters(self) -> List[Dict[str, Any]]:
return self.data

def close(self) -> None:
Expand Down
13 changes: 8 additions & 5 deletions src/gui/ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def stop(self):


class NikkeOCRUI(QMainWindow):

def __init__(self) -> None:
super().__init__()
self.config: Config = Config()
Expand All @@ -66,7 +67,6 @@ def __init__(self) -> None:

self.automation_active: bool = False
self.first_nikke_name: Optional[str] = None
self.processed_nikkes: int = 0
self.current_step: int = 0
self.selected_rarities: List[str] = ["SSR", "SR", "R"]

Expand Down Expand Up @@ -219,7 +219,6 @@ def _start_automation(self) -> None:
self.automation_active = True
self.current_step = 0
self.first_nikke_name = None
self.processed_nikkes = 0
self.status_label.setText(_("Status: Running (Press F1 to stop)"))
self.log(_("Automation started. Performing click sequence..."))
self._perform_automation()
Expand All @@ -230,7 +229,7 @@ def _stop_automation(self) -> None:
self.current_step = 0
self.status_label.setText(_("Status: Idle (Press F1 to start)"))
self.log(_("Automation stopped and reset"))
self.log(f"Total Nikkes processed: {self.processed_nikkes}")
self.log(f"Total new Nikkes added: {self.database.get_added_nikkes_count()}")

def _move_to_next_character(self) -> None:
self.log(_("Clicking to move to next character."))
Expand Down Expand Up @@ -462,8 +461,12 @@ def _handle_character(self, nikke_info: Dict[str, Any]) -> None:
name: str = nikke_info["name"]
self.log(f"Identified Nikke: {name}")

if self.database.add_or_update_character(name, nikke_info):
self.log(f"Updated database for {name}")
success, added = self.database.add_or_update_character(name, nikke_info)
if success:
if added:
self.log(f"Added new Nikke to database: {name}")
else:
self.log(f"Updated existing Nikke in database: {name}")
else:
self.log(f"Failed to update database for {name}")

Expand Down

0 comments on commit 02de65f

Please sign in to comment.