# -*- coding: utf-8 -*- # games/solitaire/base/solitaire_session_guard.py from __future__ import annotations import tkinter as tk from games.solitaire.base.solitaire_canvas_input import SolitaireCanvasInput from models.database import Database from resources.code_res.i18n import t class SolitaireSessionGuard(SolitaireCanvasInput): """ Verwaltet für alle Solitaire-Tische Fokus-Pause, 500ms-Uhr-Tick, Savegame-Checkpoints, Root-Keybindings, Sprachwechsel und sauberes Timer-Cleanup. """ CHECKPOINT_TICKS: int = 30 def _is_ui_locked(self) -> bool: return ( self.engine.won or self._game_concluded or self._auto_running or (self._modal_overlay is not None) or (self._fx_overlay is not None) ) def _canvas_alive(self) -> bool: if not self._alive: return False try: return bool(self.canvas.winfo_exists()) except Exception: return False def _can_show_hover_zoom(self) -> bool: return ( self._canvas_alive() and not self._is_ui_locked() and not self._dragging and self._press_info is None and self._window_active and (self.animator is None or not self.animator.is_active) ) def _play_table_music(self): for method_name in ("play_ingame_music", "play_table_music"): fn = getattr(self.ctx.sounds, method_name, None) if callable(fn): fn() return def _bind_root(self, seq: str, callback): if seq and seq.startswith("<") and seq.endswith(">"): try: func_id = self.ctx.root.bind(seq, callback, add="+") if func_id: self._bind_ids.append((seq, func_id)) except tk.TclError: pass def _on_canvas_destroy(self, event=None): if event is not None and event.widget is not self.canvas: return if not self._alive: return try: self.engine.pause_timer() if not self._game_concluded: self._save_progress() except Exception: pass self._cleanup_timers() def _cleanup_timers(self): self._alive = False self.engine.pause_timer() if self._fx_overlay is not None: self._fx_overlay.cancel() self._fx_overlay = None if self.hover_zoom is not None: self.hover_zoom.cancel() if self.animator is not None: self.animator.cancel() for job in ( self._tick_job, self._focus_job, self._hint_job, self._auto_job, self._resize_job, ): if job is not None: try: self.ctx.root.after_cancel(job) except Exception: pass self._tick_job = self._focus_job = self._hint_job = None self._auto_job = self._resize_job = None for seq, func_id in self._bind_ids: try: self.ctx.root.unbind(seq, func_id) except Exception: pass self._bind_ids.clear() def _schedule_focus_check(self, _event=None): if not self._canvas_alive(): return if self._focus_job is not None: try: self.ctx.root.after_cancel(self._focus_job) except Exception: pass try: self._focus_job = self.ctx.root.after(60, self._check_window_focus) except Exception: self._focus_job = None def _check_window_focus(self): self._focus_job = None if not self._canvas_alive(): return try: is_iconic = self.ctx.root.state() == "iconic" has_focus = self.ctx.root.focus_displayof() is not None and not is_iconic except Exception: return should_run = ( has_focus and (self._modal_overlay is None) and (self._fx_overlay is None) and (not self._game_concluded) and (not self.engine.won) ) prev_paused = self.engine.is_timer_paused self._window_active = has_focus if should_run: self.engine.resume_timer() elif not prev_paused: self.engine.pause_timer() self._save_progress() if prev_paused != self.engine.is_timer_paused: self._update_hud_text() if ( self.animator is None or not self.animator.is_active ) and self._fx_overlay is None: self.redraw() def _close_active_modal(self): if self._modal_overlay is not None: try: self._modal_overlay.destroy() except Exception: pass self._modal_overlay = None self._check_window_focus() self._sync_buttons() def _refresh_lang(self): if self.animator: self.animator.flush() self.engine.pause_timer() self._save_progress() state_json = self.engine.to_json() self._cleanup_timers() type(self)( self.ctx, variant_key=self.variant_key, difficulty=self.difficulty, on_back_to_menu=self.on_back_to_menu, resume_state_json=state_json, ) def _save_progress(self): if self._game_concluded: return pid = self.ctx.profile["id"] if self.engine.won: Database.delete_game_state(pid, self.GAME_FOLDER_ID) return Database.save_game_state( pid, self.GAME_FOLDER_ID, self.variant_key, self.engine.to_json() ) def _start_clock(self): if not self._canvas_alive(): return self._check_window_focus() self._update_hud_text() if ( self.engine.has_timer_started and not self.engine.is_timer_paused and not self.engine.won and not self._game_concluded ): self._checkpoint_ticks += 1 if self._checkpoint_ticks >= self.CHECKPOINT_TICKS: self._checkpoint_ticks = 0 self._save_progress() try: self._tick_job = self.ctx.root.after(500, self._start_clock) except Exception: self._tick_job = None def _extra_hud_suffix(self) -> str: return "" def _update_hud_text(self): if not self._alive: return try: elapsed_str = Database.format_seconds(self.engine.get_elapsed_seconds()) paused_tag = ( t("sol_hud_paused_tag") if ( self.engine.is_timer_paused and not self.engine.won and not self._game_concluded ) else "" ) diff_label = t(f"sol_diff_short_{self.difficulty}") new_hud = ( t( "sol_hud_stats", self.engine.score, self.engine.moves, elapsed_str, paused_tag, diff_label, ) + self._extra_hud_suffix() ) if self.hud_var.get() != new_hud: self.hud_var.set(new_hud) except Exception: pass