179 lines
6.0 KiB
Python
179 lines
6.0 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# games/solitaire/base/solitaire_stats_view.py
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import tkinter as tk
|
||
|
|
|
||
|
|
from models.database import Database
|
||
|
|
from resources.code_res.game_constants import (
|
||
|
|
ACCENT_GOLD,
|
||
|
|
ACCENT_GOTH,
|
||
|
|
BG_COLOR,
|
||
|
|
BORDER_COLOR,
|
||
|
|
CELL_BG,
|
||
|
|
FG_COLOR,
|
||
|
|
MUTED_FG,
|
||
|
|
PANEL_BG,
|
||
|
|
STAT_FIELD_KEYS,
|
||
|
|
SUBTLE_BORDER,
|
||
|
|
)
|
||
|
|
from resources.code_res.i18n import t
|
||
|
|
from view.widgets.image_button import ImageButton
|
||
|
|
|
||
|
|
# Kurze, prägnante Button-Labels für Varianten mit mehreren Modi
|
||
|
|
SHORT_VARIANT_TAB_KEYS: dict[str, str] = {
|
||
|
|
"klondike_1": "kl_draw_toggle_1",
|
||
|
|
"klondike_3": "kl_draw_toggle_3",
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
class SolitaireStatsView:
|
||
|
|
"""Kompakte Statistik-Ansicht mit direktem Varianten-Umschalter im selben Block."""
|
||
|
|
|
||
|
|
def __init__(self, context, game_folder: str, game_title_key: str, on_back):
|
||
|
|
self.ctx = context
|
||
|
|
self.game_folder = game_folder
|
||
|
|
self.game_title_key = game_title_key
|
||
|
|
self.on_back = on_back
|
||
|
|
self._active_variant_id: int | None = None
|
||
|
|
|
||
|
|
def show(self, preselect_variant_id: int | None = None):
|
||
|
|
pid = self.ctx.profile["id"]
|
||
|
|
self._active_variant_id = preselect_variant_id
|
||
|
|
|
||
|
|
self.ctx.root.clear_window(apply_global_bg=True)
|
||
|
|
prog = Database.get_game_progress(pid, self.game_folder)
|
||
|
|
game_name = t(self.game_title_key)
|
||
|
|
|
||
|
|
self.ctx.build_game_topbar(
|
||
|
|
self.ctx.root,
|
||
|
|
t("stats_view_topbar", game_name),
|
||
|
|
on_game_menu=self.on_back,
|
||
|
|
on_stats=None,
|
||
|
|
stats_enabled=False,
|
||
|
|
on_lang_refresh=lambda: self.show(preselect_variant_id=self._active_variant_id),
|
||
|
|
)
|
||
|
|
|
||
|
|
container = tk.Frame(self.ctx.root, bg=BG_COLOR, padx=24, pady=16)
|
||
|
|
container.place(relx=0.5, rely=0.53, anchor="center", relwidth=0.86, relheight=0.80)
|
||
|
|
|
||
|
|
card = tk.Frame(
|
||
|
|
container,
|
||
|
|
bg=PANEL_BG,
|
||
|
|
padx=24,
|
||
|
|
pady=20,
|
||
|
|
highlightthickness=1,
|
||
|
|
highlightbackground=BORDER_COLOR,
|
||
|
|
)
|
||
|
|
card.pack(fill="both", expand=True)
|
||
|
|
|
||
|
|
# Kopfzeile: Links Rang/XP, rechts kompakter Umschalter (z. B. Gesamt | 1 Karte | 3 Karten)
|
||
|
|
header_row = tk.Frame(card, bg=PANEL_BG)
|
||
|
|
header_row.pack(fill="x", pady=(0, 14))
|
||
|
|
|
||
|
|
tk.Label(
|
||
|
|
header_row,
|
||
|
|
text=t(
|
||
|
|
"stats_game_rank_info",
|
||
|
|
game_name,
|
||
|
|
prog["title"],
|
||
|
|
prog["level"],
|
||
|
|
prog["current_xp"],
|
||
|
|
prog["needed_xp"],
|
||
|
|
),
|
||
|
|
font=("Georgia", 11, "bold"),
|
||
|
|
fg=ACCENT_GOLD,
|
||
|
|
bg=PANEL_BG,
|
||
|
|
).pack(side="left", anchor="w")
|
||
|
|
|
||
|
|
variants = Database.get_variants_for_filter(self.game_folder)
|
||
|
|
if len(variants) > 1:
|
||
|
|
toggle_bar = tk.Frame(header_row, bg=PANEL_BG)
|
||
|
|
toggle_bar.pack(side="right")
|
||
|
|
|
||
|
|
tabs: list[tuple[int | None, str]] = [(None, t("sol_stats_tab_all"))]
|
||
|
|
for v in variants:
|
||
|
|
v_key = v["variant_key"]
|
||
|
|
short_lbl = (
|
||
|
|
t(SHORT_VARIANT_TAB_KEYS[v_key])
|
||
|
|
if v_key in SHORT_VARIANT_TAB_KEYS
|
||
|
|
else v["translated_name"]
|
||
|
|
)
|
||
|
|
tabs.append((v["id"], short_lbl))
|
||
|
|
|
||
|
|
for vid, tab_text in tabs:
|
||
|
|
is_selected = self._active_variant_id == vid
|
||
|
|
raw_norm = self.ctx.assets.raw_btn_active if is_selected else self.ctx.assets.raw_sel
|
||
|
|
raw_act = self.ctx.assets.raw_btn_active if is_selected else self.ctx.assets.raw_sel_active
|
||
|
|
btn_w = 110 if vid is None else 150
|
||
|
|
|
||
|
|
ImageButton(
|
||
|
|
toggle_bar,
|
||
|
|
tab_text,
|
||
|
|
raw_norm,
|
||
|
|
raw_act,
|
||
|
|
lambda target_vid=vid: self._switch_tab(target_vid),
|
||
|
|
sound_mgr=self.ctx.sounds,
|
||
|
|
width=btn_w,
|
||
|
|
height=32,
|
||
|
|
font=("Helvetica", 9, "bold"),
|
||
|
|
).pack(side="left", padx=(6, 0))
|
||
|
|
|
||
|
|
# Aktive Unterüberschrift des Blocks
|
||
|
|
active_title = t("stats_all_variants_of_game", game_name)
|
||
|
|
for v in variants:
|
||
|
|
if v["id"] == self._active_variant_id:
|
||
|
|
active_title = v["translated_name"]
|
||
|
|
break
|
||
|
|
|
||
|
|
tk.Label(
|
||
|
|
card,
|
||
|
|
text=t("stats_game_board_title", active_title),
|
||
|
|
font=("Georgia", 12, "bold"),
|
||
|
|
fg=ACCENT_GOTH,
|
||
|
|
bg=PANEL_BG,
|
||
|
|
).pack(anchor="w", pady=(0, 10))
|
||
|
|
|
||
|
|
grid_frame = tk.Frame(card, bg=PANEL_BG)
|
||
|
|
grid_frame.pack(fill="x", pady=(4, 0))
|
||
|
|
grid_frame.columnconfigure(0, weight=1, uniform="statcol")
|
||
|
|
grid_frame.columnconfigure(1, weight=1, uniform="statcol")
|
||
|
|
|
||
|
|
stats = Database.get_stats_overview(
|
||
|
|
pid,
|
||
|
|
variant_id=self._active_variant_id,
|
||
|
|
folder_path=self.game_folder if self._active_variant_id is None else None,
|
||
|
|
)
|
||
|
|
|
||
|
|
for idx, (field_key, label_i18n_key) in enumerate(STAT_FIELD_KEYS):
|
||
|
|
r, c = divmod(idx, 2)
|
||
|
|
cell = tk.Frame(
|
||
|
|
grid_frame,
|
||
|
|
bg=CELL_BG,
|
||
|
|
padx=16,
|
||
|
|
pady=12,
|
||
|
|
highlightthickness=1,
|
||
|
|
highlightbackground=SUBTLE_BORDER,
|
||
|
|
)
|
||
|
|
cell.grid(row=r, column=c, sticky="ew", padx=(0, 8) if c == 0 else (8, 0), pady=6)
|
||
|
|
|
||
|
|
tk.Label(
|
||
|
|
cell,
|
||
|
|
text=t(label_i18n_key),
|
||
|
|
font=("Helvetica", 10),
|
||
|
|
fg=MUTED_FG,
|
||
|
|
bg=CELL_BG,
|
||
|
|
).pack(side="left")
|
||
|
|
|
||
|
|
tk.Label(
|
||
|
|
cell,
|
||
|
|
text=str(stats.get(field_key, "--")),
|
||
|
|
font=("Georgia", 12, "bold"),
|
||
|
|
fg=ACCENT_GOTH if "streak" in field_key else FG_COLOR,
|
||
|
|
bg=CELL_BG,
|
||
|
|
).pack(side="right")
|
||
|
|
|
||
|
|
def _switch_tab(self, variant_id: int | None):
|
||
|
|
if variant_id == self._active_variant_id:
|
||
|
|
return
|
||
|
|
self.show(preselect_variant_id=variant_id)
|