106 lines
3.9 KiB
Python
106 lines
3.9 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# controller/app_controller.py
|
||
|
|
from pathlib import Path
|
||
|
|
from controller.app_context import AppContext
|
||
|
|
from controller.game_loader import GameLoader
|
||
|
|
from models.database import Database
|
||
|
|
from models.sound_manager import SoundManager
|
||
|
|
import resources.code_res.i18n as i18n
|
||
|
|
from resources.code_res.i18n import t
|
||
|
|
|
||
|
|
|
||
|
|
class AppController:
|
||
|
|
def __init__(self, root_dir: Path):
|
||
|
|
self.root_dir = root_dir
|
||
|
|
Database.init_db()
|
||
|
|
i18n.reload_translations()
|
||
|
|
i18n.set_lang(Database.load_setting("language", "DE"))
|
||
|
|
self.loader = GameLoader(root_dir)
|
||
|
|
self.sounds = SoundManager(root_dir / "resources" / "sounds")
|
||
|
|
self.active_profile = None
|
||
|
|
self.view = None
|
||
|
|
|
||
|
|
def bind_view(self, view):
|
||
|
|
self.view = view
|
||
|
|
|
||
|
|
def toggle_language(self, custom_refresh=None):
|
||
|
|
new_lang = i18n.next_lang()
|
||
|
|
Database.save_setting("language", new_lang)
|
||
|
|
self.view.title(t("app_title"))
|
||
|
|
if custom_refresh is not None:
|
||
|
|
custom_refresh()
|
||
|
|
elif self.active_profile:
|
||
|
|
self.show_main_menu()
|
||
|
|
else:
|
||
|
|
self.start()
|
||
|
|
|
||
|
|
def start(self):
|
||
|
|
self.sounds.play_menu_music()
|
||
|
|
profiles = Database.get_profiles()
|
||
|
|
last_prof = Database.load_setting("last_profile_name", "")
|
||
|
|
self.view.show_login(profiles, preselect_name=last_prof)
|
||
|
|
|
||
|
|
def do_login(self, name: str):
|
||
|
|
if not name:
|
||
|
|
return
|
||
|
|
Database.save_setting("last_profile_name", name)
|
||
|
|
self.active_profile = Database.get_or_create_profile(name)
|
||
|
|
self.show_main_menu()
|
||
|
|
|
||
|
|
def do_create_profile(self, name: str):
|
||
|
|
if not name:
|
||
|
|
return
|
||
|
|
Database.save_setting("last_profile_name", name)
|
||
|
|
self.active_profile = Database.get_or_create_profile(name)
|
||
|
|
self.show_main_menu()
|
||
|
|
|
||
|
|
def logout_to_login(self):
|
||
|
|
self.sounds.play_menu_music()
|
||
|
|
last_prof = self.active_profile["name"] if self.active_profile else Database.load_setting("last_profile_name", "")
|
||
|
|
self.active_profile = None
|
||
|
|
self.view.show_login(Database.get_profiles(), preselect_name=last_prof)
|
||
|
|
|
||
|
|
def has_active_devils_lock(self) -> bool:
|
||
|
|
"""Nur aktiv, wenn wirklich das DB-Flag devils_deal_active == 1 gesetzt wurde!"""
|
||
|
|
if not self.active_profile:
|
||
|
|
return False
|
||
|
|
pid = self.active_profile["id"]
|
||
|
|
devils_acc = Database.get_active_devils_bj_account(pid)
|
||
|
|
if devils_acc is None:
|
||
|
|
# Verwaiste Blackjack-Savegames ohne aktives Pakt-Flag sofort wegräumen
|
||
|
|
Database.delete_game_state(pid, "blackjack")
|
||
|
|
return False
|
||
|
|
return True
|
||
|
|
|
||
|
|
def resume_devils_deal(self):
|
||
|
|
games = self.loader.discover_games()
|
||
|
|
for g in games:
|
||
|
|
if g["folder"] == "blackjack":
|
||
|
|
self.launch_game(g)
|
||
|
|
return
|
||
|
|
|
||
|
|
def show_main_menu(self):
|
||
|
|
self.sounds.play_menu_music()
|
||
|
|
if self.active_profile:
|
||
|
|
pid = self.active_profile["id"]
|
||
|
|
self.active_profile = Database.get_profile_by_id(pid)
|
||
|
|
if self.has_active_devils_lock():
|
||
|
|
self.view.show_main_menu(self.active_profile, [], devils_resume_only=True)
|
||
|
|
else:
|
||
|
|
games = self.loader.discover_games()
|
||
|
|
for g in games:
|
||
|
|
g["progress"] = Database.get_game_progress(pid, g["folder"])
|
||
|
|
self.view.show_main_menu(self.active_profile, games, devils_resume_only=False)
|
||
|
|
|
||
|
|
def show_statistics(self):
|
||
|
|
if self.has_active_devils_lock():
|
||
|
|
return
|
||
|
|
self.sounds.play_menu_music()
|
||
|
|
if self.active_profile:
|
||
|
|
self.active_profile = Database.get_profile_by_id(self.active_profile["id"])
|
||
|
|
self.view.show_statistics(self.active_profile)
|
||
|
|
|
||
|
|
def launch_game(self, game_entry: dict):
|
||
|
|
self.view.clear_window(apply_global_bg=True)
|
||
|
|
ctx = AppContext(self, game_folder=game_entry["folder"])
|
||
|
|
game_entry["start"](ctx)
|