551 lines
20 KiB
Python
551 lines
20 KiB
Python
# -*- coding: utf-8 -*-
|
|
# view/unified_stats_view.py
|
|
from __future__ import annotations
|
|
|
|
import tkinter as tk
|
|
from PIL import Image, ImageTk
|
|
|
|
from models.database import Database
|
|
from resources.code_res.game_constants import (
|
|
BG_COLOR,
|
|
FONT_COLOR_BODY_DEFAULT,
|
|
FONT_COLOR_BODY_MUTED,
|
|
FONT_COLOR_HEADING_SUB,
|
|
FONT_COLOR_STATUS_LOSS,
|
|
FONT_COLOR_STATUS_WIN,
|
|
FONT_COLOR_TITLE_MAIN,
|
|
FONT_SIZE_BODY,
|
|
FONT_SIZE_HEADING,
|
|
FONT_SIZE_SMALL_MIN,
|
|
STAT_FIELD_KEYS,
|
|
STAT_GAME_GROUPS,
|
|
)
|
|
from resources.code_res.i18n import t
|
|
from view.widgets.goth_dropdown import GothDropdown
|
|
from view.widgets.image_button import ImageButton
|
|
|
|
SHORT_VARIANT_TAB_KEYS: dict[str, str] = {
|
|
"klondike_1": "kl_draw_toggle_1",
|
|
"klondike_3": "kl_draw_toggle_3",
|
|
"spider_1suit": "sp_suit_toggle_1",
|
|
"spider_2suit": "sp_suit_toggle_2",
|
|
"spider_4suit": "sp_suit_toggle_4",
|
|
}
|
|
|
|
ENDPOINT_ALIASES: dict[str, str | None] = {
|
|
"": None,
|
|
"all": None,
|
|
"gesamt": None,
|
|
"blackjack": "blackjack",
|
|
"poker": "poker",
|
|
"klondike": "solitaire/klondike",
|
|
"solitaire/klondike": "solitaire/klondike",
|
|
"freecell": "solitaire/freecell",
|
|
"solitaire/freecell": "solitaire/freecell",
|
|
"spider": "solitaire/spider_solitaire",
|
|
"spider_solitaire": "solitaire/spider_solitaire",
|
|
"solitaire/spider_solitaire": "solitaire/spider_solitaire",
|
|
}
|
|
|
|
|
|
def resolve_stats_endpoint(endpoint: str | None) -> str | None:
|
|
"""Löst Kurznamen wie 'klondike', 'spider' oder 'blackjack' auf den DB-Ordnerpfad auf."""
|
|
if endpoint is None:
|
|
return None
|
|
clean = str(endpoint).strip().lower()
|
|
if clean in ENDPOINT_ALIASES:
|
|
return ENDPOINT_ALIASES[clean]
|
|
|
|
valid_folders = [folder for folder, _tkey in STAT_GAME_GROUPS]
|
|
if clean in valid_folders:
|
|
return clean
|
|
for f in valid_folders:
|
|
if f.endswith(f"/{clean}") or clean in f:
|
|
return f
|
|
return None
|
|
|
|
|
|
class UnifiedStatsView(tk.Canvas):
|
|
"""
|
|
Zentrale Vollbild-Statistik-Ansicht für alle Spiele (ohne Topbar & ohne Bottombar):
|
|
- Parameter 1 (`game_endpoint`): Spiel-Endpunkt ('blackjack', 'poker', 'klondike', 'freecell', 'spider' oder None)
|
|
- Parameter 2 (`on_back`): Optionale Rücksprung-Funktion; falls None, schließt sich das Overlay
|
|
einfach und gibt die exakt darunterliegende Spielansicht sofort wieder frei!
|
|
"""
|
|
|
|
@classmethod
|
|
def open(
|
|
cls,
|
|
context,
|
|
game_endpoint: str | None = None,
|
|
on_back=None,
|
|
) -> "UnifiedStatsView":
|
|
GothDropdown.close_any_open_popup()
|
|
root = context.root
|
|
existing = getattr(root, "_unified_stats_overlay", None)
|
|
if existing is not None and existing.winfo_exists():
|
|
existing.destroy()
|
|
|
|
view = cls(context, game_endpoint=game_endpoint, on_back=on_back)
|
|
setattr(root, "_unified_stats_overlay", view)
|
|
return view
|
|
|
|
def __init__(
|
|
self,
|
|
context,
|
|
game_endpoint: str | None = None,
|
|
on_back=None,
|
|
):
|
|
super().__init__(
|
|
context.root,
|
|
bg=BG_COLOR,
|
|
bd=0,
|
|
highlightthickness=0,
|
|
)
|
|
self.ctx = context
|
|
self.on_back = on_back
|
|
|
|
self._active_folder: str | None = resolve_stats_endpoint(game_endpoint)
|
|
self._active_variant_id: int | None = None
|
|
self._active_account_id: int | None = None
|
|
|
|
self._bg_photo: ImageTk.PhotoImage | None = None
|
|
self._photo_refs: list = []
|
|
self._child_buttons: list[ImageButton] = []
|
|
self._esc_bind_id: str | None = None
|
|
|
|
self.place(relx=0.0, rely=0.0, relwidth=1.0, relheight=1.0)
|
|
tk.Widget.tkraise(self)
|
|
|
|
self.bind("<Configure>", self._on_configure)
|
|
self.bind("<Destroy>", self._on_destroy)
|
|
try:
|
|
self._esc_bind_id = self.ctx.root.bind("<Escape>", self._handle_back, add="+")
|
|
except tk.TclError:
|
|
self._esc_bind_id = None
|
|
|
|
self._rebuild_view()
|
|
|
|
def show(
|
|
self,
|
|
game_folder: str | None = None,
|
|
variant_id: int | None = None,
|
|
account_id: int | None = None,
|
|
):
|
|
self._active_folder = resolve_stats_endpoint(game_folder)
|
|
self._active_variant_id = variant_id
|
|
self._active_account_id = account_id
|
|
self._rebuild_view()
|
|
|
|
def _on_destroy(self, event=None):
|
|
if event is not None and event.widget is not self:
|
|
return
|
|
if self._esc_bind_id is not None:
|
|
try:
|
|
self.ctx.root.unbind("<Escape>", self._esc_bind_id)
|
|
except Exception:
|
|
pass
|
|
self._esc_bind_id = None
|
|
if getattr(self.ctx.root, "_unified_stats_overlay", None) is self:
|
|
setattr(self.ctx.root, "_unified_stats_overlay", None)
|
|
|
|
def _handle_back(self, _event=None):
|
|
back_cb = self.on_back
|
|
if self.winfo_exists():
|
|
self.destroy()
|
|
if callable(back_cb):
|
|
back_cb()
|
|
return "break"
|
|
|
|
def _switch_filter(
|
|
self,
|
|
folder: str | None,
|
|
variant_id: int | None = None,
|
|
account_id: int | None = None,
|
|
):
|
|
self._active_folder = folder
|
|
self._active_variant_id = variant_id
|
|
self._active_account_id = account_id
|
|
self._rebuild_view()
|
|
|
|
def _clear_buttons(self):
|
|
for b in self._child_buttons:
|
|
try:
|
|
if b.winfo_exists():
|
|
b.destroy()
|
|
except Exception:
|
|
pass
|
|
self._child_buttons.clear()
|
|
|
|
def _render_full_bg(self, w: int, h: int):
|
|
if hasattr(self.ctx.assets, "get_bg_patch_photo"):
|
|
self._bg_photo = self.ctx.assets.get_bg_patch_photo(w, h, 0, 0, w, h)
|
|
else:
|
|
src = (
|
|
self.ctx.assets.raw_bg_vertical
|
|
if (h > w and self.ctx.assets.raw_bg_vertical is not None)
|
|
else (self.ctx.assets.raw_bg_horizontal or self.ctx.assets.raw_bg)
|
|
)
|
|
if src is not None:
|
|
self._bg_photo = ImageTk.PhotoImage(
|
|
src.resize((w, h), Image.Resampling.BILINEAR)
|
|
)
|
|
|
|
if self._bg_photo is not None:
|
|
self.delete("stats_bg")
|
|
self.create_image(0, 0, image=self._bg_photo, anchor="nw", tags="stats_bg")
|
|
self.tag_lower("stats_bg")
|
|
|
|
def _on_configure(self, event):
|
|
if event.widget is not self or event.width < 200 or event.height < 200:
|
|
return
|
|
self._draw_canvas_layout(event.width, event.height)
|
|
|
|
def _rebuild_view(self):
|
|
if not self.winfo_exists():
|
|
return
|
|
self._clear_buttons()
|
|
|
|
pid = self.ctx.profile["id"]
|
|
|
|
btn_norm = self.ctx.assets.raw_btn_default
|
|
btn_purp = self.ctx.assets.raw_btn_purple
|
|
btn_fire = self.ctx.assets.raw_btn_fire or btn_purp
|
|
|
|
self._back_btn = ImageButton(
|
|
self,
|
|
t("btn_back"),
|
|
btn_fire,
|
|
btn_fire,
|
|
self._handle_back,
|
|
sound_mgr=self.ctx.sounds,
|
|
width=118,
|
|
height=34,
|
|
style="fire",
|
|
)
|
|
self._child_buttons.append(self._back_btn)
|
|
|
|
# 1. Spiel-Umschalter (Alle Spiele + registrierte Spiele)
|
|
all_lbl = t("filter_all_games")
|
|
game_tabs: list[tuple[str | None, str]] = [(None, all_lbl)]
|
|
for folder_key, title_i18n_key in STAT_GAME_GROUPS:
|
|
game_tabs.append((folder_key, t(title_i18n_key)))
|
|
|
|
self._game_btns: list[tuple[ImageButton, int]] = []
|
|
for f_key, g_lbl in game_tabs:
|
|
is_sel = (self._active_folder == f_key)
|
|
bw = max(108, min(162, len(g_lbl) * 9 + 34))
|
|
b = ImageButton(
|
|
self,
|
|
g_lbl,
|
|
btn_fire if is_sel else btn_norm,
|
|
btn_fire if is_sel else btn_purp,
|
|
lambda target_f=f_key: self._switch_filter(target_f, None, None),
|
|
sound_mgr=self.ctx.sounds,
|
|
width=bw,
|
|
height=34,
|
|
style="fire" if is_sel else "amethyst",
|
|
)
|
|
self._child_buttons.append(b)
|
|
self._game_btns.append((b, b.pixel_width))
|
|
|
|
# 2. Goldbeutel-Umschalter (nur bei Blackjack & Poker)
|
|
self._acc_btns: list[tuple[ImageButton, int]] = []
|
|
accounts: list[dict] = []
|
|
active_acc_obj: dict | None = None
|
|
if self._active_folder == "blackjack":
|
|
accounts = Database.get_blackjack_accounts(pid)
|
|
elif self._active_folder == "poker":
|
|
accounts = Database.get_poker_accounts(pid)
|
|
|
|
if accounts:
|
|
for a in accounts:
|
|
if a["id"] == self._active_account_id:
|
|
active_acc_obj = a
|
|
break
|
|
if self._active_account_id is not None and active_acc_obj is None:
|
|
self._active_account_id = None
|
|
|
|
prof_all_lbl = t("stats_profile_total_tab")
|
|
is_prof_sel = (active_acc_obj is None)
|
|
b_prof = ImageButton(
|
|
self,
|
|
prof_all_lbl,
|
|
btn_fire if is_prof_sel else btn_norm,
|
|
btn_fire if is_prof_sel else btn_purp,
|
|
lambda: self._switch_filter(
|
|
self._active_folder, self._active_variant_id, None
|
|
),
|
|
sound_mgr=self.ctx.sounds,
|
|
width=142,
|
|
height=32,
|
|
style="fire" if is_prof_sel else "amethyst",
|
|
)
|
|
self._child_buttons.append(b_prof)
|
|
self._acc_btns.append((b_prof, b_prof.pixel_width))
|
|
|
|
for acc in accounts:
|
|
is_a_sel = (active_acc_obj is not None and acc["id"] == active_acc_obj["id"])
|
|
a_lbl = f"{acc['account_name']} ({acc['gold']}g)"
|
|
bw_a = max(136, min(195, len(a_lbl) * 8 + 30))
|
|
b_acc = ImageButton(
|
|
self,
|
|
a_lbl,
|
|
btn_fire if is_a_sel else btn_norm,
|
|
btn_fire if is_a_sel else btn_purp,
|
|
lambda aid=acc["id"]: self._switch_filter(
|
|
self._active_folder, self._active_variant_id, aid
|
|
),
|
|
sound_mgr=self.ctx.sounds,
|
|
width=bw_a,
|
|
height=32,
|
|
style="fire" if is_a_sel else "amethyst",
|
|
)
|
|
self._child_buttons.append(b_acc)
|
|
self._acc_btns.append((b_acc, b_acc.pixel_width))
|
|
|
|
# 3. Varianten-Umschalter (sofern das Spiel mehrere Varianten hat)
|
|
self._var_btns: list[tuple[ImageButton, int]] = []
|
|
variants = (
|
|
Database.get_variants_for_filter(self._active_folder)
|
|
if self._active_folder
|
|
else []
|
|
)
|
|
if len(variants) > 1:
|
|
v_tabs: list[tuple[int | None, str]] = [(None, t("sol_stats_tab_all"))]
|
|
for v in variants:
|
|
vk = v["variant_key"]
|
|
short_lbl = (
|
|
t(SHORT_VARIANT_TAB_KEYS[vk])
|
|
if vk in SHORT_VARIANT_TAB_KEYS
|
|
else v["translated_name"]
|
|
)
|
|
v_tabs.append((v["id"], short_lbl))
|
|
|
|
for vid, v_text in v_tabs:
|
|
is_vsel = (self._active_variant_id == vid)
|
|
bw_v = 110 if vid is None else max(136, min(190, len(v_text) * 8 + 28))
|
|
b_var = ImageButton(
|
|
self,
|
|
v_text,
|
|
btn_fire if is_vsel else btn_norm,
|
|
btn_fire if is_vsel else btn_purp,
|
|
lambda target_vid=vid: self._switch_filter(
|
|
self._active_folder, target_vid, self._active_account_id
|
|
),
|
|
sound_mgr=self.ctx.sounds,
|
|
width=bw_v,
|
|
height=32,
|
|
style="fire" if is_vsel else "amethyst",
|
|
)
|
|
self._child_buttons.append(b_var)
|
|
self._var_btns.append((b_var, b_var.pixel_width))
|
|
|
|
# 4. Datenabfrage
|
|
if self._active_folder is None:
|
|
self._rank_line = t("stats_overall_summary", self.ctx.profile["name"])
|
|
self._board_heading = all_lbl
|
|
self._stats_data = Database.get_stats_overview(pid, variant_id=None, folder_path=None)
|
|
else:
|
|
group_map = dict(STAT_GAME_GROUPS)
|
|
game_name = (
|
|
t(group_map[self._active_folder])
|
|
if self._active_folder in group_map
|
|
else self._active_folder
|
|
)
|
|
prog = Database.get_game_progress(pid, self._active_folder)
|
|
self._rank_line = t(
|
|
"stats_game_rank_info",
|
|
game_name,
|
|
prog["title"],
|
|
prog["level"],
|
|
prog["current_xp"],
|
|
prog["needed_xp"],
|
|
)
|
|
|
|
var_label = t("stats_all_variants_of_game", game_name)
|
|
for v in variants:
|
|
if v["id"] == self._active_variant_id:
|
|
var_label = v["translated_name"]
|
|
break
|
|
|
|
if active_acc_obj is not None and self._active_folder == "blackjack":
|
|
self._board_heading = (
|
|
f"{t('bj_stats_acc_title', active_acc_obj['account_name'])} | {var_label}"
|
|
)
|
|
self._stats_data = Database.get_bj_account_stats_overview(
|
|
active_acc_obj["id"], self._active_variant_id
|
|
)
|
|
elif active_acc_obj is not None and self._active_folder == "poker":
|
|
self._board_heading = (
|
|
f"{t('bj_stats_acc_title', active_acc_obj['account_name'])} | {var_label}"
|
|
)
|
|
self._stats_data = Database.get_pk_account_stats_overview(
|
|
active_acc_obj["id"], self._active_variant_id
|
|
)
|
|
else:
|
|
self._board_heading = t("stats_game_board_title", var_label)
|
|
self._stats_data = Database.get_stats_overview(
|
|
pid,
|
|
variant_id=self._active_variant_id,
|
|
folder_path=self._active_folder if self._active_variant_id is None else None,
|
|
)
|
|
|
|
self.ctx.root.update_idletasks()
|
|
w = max(900, self.winfo_width(), self.ctx.root.winfo_width())
|
|
h = max(620, self.winfo_height(), self.ctx.root.winfo_height())
|
|
self._draw_canvas_layout(w, h)
|
|
|
|
def _draw_canvas_layout(self, w: int, h: int):
|
|
self._render_full_bg(w, h)
|
|
self.delete("fg")
|
|
self._photo_refs.clear()
|
|
|
|
pad_x = max(92, int(w * 0.088))
|
|
content_w = min(1080, w - 2 * pad_x)
|
|
left_x = (w - content_w) // 2
|
|
|
|
top_btn_cy = max(48, int(h * 0.068))
|
|
self.create_window(left_x, top_btn_cy, window=self._back_btn, anchor="w", tags="fg")
|
|
|
|
hdr_ph = self.ctx.assets.get_goth_text_photo(
|
|
t("btn_stats"),
|
|
size_px=FONT_SIZE_HEADING - 1,
|
|
color=FONT_COLOR_TITLE_MAIN,
|
|
align="right",
|
|
)
|
|
self._photo_refs.append(hdr_ph)
|
|
self.create_image(
|
|
left_x + content_w, top_btn_cy, image=hdr_ph, anchor="e", tags="fg"
|
|
)
|
|
|
|
cur_y = max(top_btn_cy + 30, int(h * 0.125))
|
|
|
|
nav_h = 58
|
|
nav_cy = cur_y + nav_h // 2
|
|
nav_box = self.ctx.assets.get_box_photo("box_gold_double", (content_w, nav_h))
|
|
if nav_box:
|
|
self._photo_refs.append(nav_box)
|
|
self.create_image(w // 2, nav_cy, image=nav_box, anchor="center", tags="fg")
|
|
|
|
total_gb_w = sum(bw for _, bw in self._game_btns) + max(0, len(self._game_btns) - 1) * 8
|
|
gx = w // 2 - total_gb_w // 2
|
|
for b_wgt, bw in self._game_btns:
|
|
self.create_window(gx, nav_cy, window=b_wgt, anchor="w", tags="fg")
|
|
gx += bw + 8
|
|
|
|
cur_y += nav_h + 14
|
|
|
|
has_acc_row = len(self._acc_btns) > 0
|
|
hdr_h = 104 if has_acc_row else 64
|
|
hdr_cy = cur_y + hdr_h // 2
|
|
hdr_box = self.ctx.assets.get_box_photo("box_purple", (content_w, hdr_h))
|
|
if hdr_box:
|
|
self._photo_refs.append(hdr_box)
|
|
self.create_image(w // 2, hdr_cy, image=hdr_box, anchor="center", tags="fg")
|
|
|
|
var_row_w = sum(bw + 6 for _, bw in self._var_btns)
|
|
top_row_y = (cur_y + 28) if has_acc_row else hdr_cy
|
|
|
|
rank_ph = self.ctx.assets.get_goth_text_photo(
|
|
self._rank_line,
|
|
size_px=FONT_SIZE_BODY + 1,
|
|
color=FONT_COLOR_TITLE_MAIN,
|
|
max_width=max(220, content_w - var_row_w - 56),
|
|
align="left",
|
|
)
|
|
self._photo_refs.append(rank_ph)
|
|
self.create_image(left_x + 24, top_row_y, image=rank_ph, anchor="w", tags="fg")
|
|
|
|
rx = left_x + content_w - 22
|
|
for b_wgt, bw in reversed(self._var_btns):
|
|
self.create_window(rx, top_row_y, window=b_wgt, anchor="e", tags="fg")
|
|
rx -= (bw + 6)
|
|
|
|
if has_acc_row:
|
|
acc_row_y = cur_y + hdr_h - 26
|
|
ax = left_x + 24
|
|
p_ph = self.ctx.assets.get_goth_text_photo(
|
|
t("stats_pouch_lbl"),
|
|
size_px=FONT_SIZE_BODY,
|
|
color=FONT_COLOR_HEADING_SUB,
|
|
align="left",
|
|
)
|
|
self._photo_refs.append(p_ph)
|
|
self.create_image(ax, acc_row_y, image=p_ph, anchor="w", tags="fg")
|
|
ax += p_ph.width() + 14
|
|
|
|
for b_wgt, bw in self._acc_btns:
|
|
self.create_window(ax, acc_row_y, window=b_wgt, anchor="w", tags="fg")
|
|
ax += bw + 8
|
|
|
|
cur_y += hdr_h + 14
|
|
|
|
bottom_safe = max(58, int(h * 0.095))
|
|
board_h = max(246, min(350, h - cur_y - bottom_safe))
|
|
board_cy = cur_y + board_h // 2
|
|
|
|
outer_box = self.ctx.assets.get_box_photo("box_big", (content_w, board_h))
|
|
if outer_box:
|
|
self._photo_refs.append(outer_box)
|
|
self.create_image(w // 2, board_cy, image=outer_box, anchor="center", tags="fg")
|
|
|
|
sub_ph = self.ctx.assets.get_goth_text_photo(
|
|
self._board_heading,
|
|
size_px=FONT_SIZE_HEADING - 3,
|
|
color=FONT_COLOR_HEADING_SUB,
|
|
max_width=content_w - 56,
|
|
align="left",
|
|
)
|
|
self._photo_refs.append(sub_ph)
|
|
self.create_image(left_x + 28, cur_y + 30, image=sub_ph, anchor="w", tags="fg")
|
|
|
|
grid_top = cur_y + 58
|
|
grid_h = board_h - 78
|
|
cols, rows = 4, 2
|
|
gap_x, gap_y = 14, 12
|
|
cell_w = (content_w - 56 - (cols - 1) * gap_x) // cols
|
|
cell_h = max(68, (grid_h - (rows - 1) * gap_y) // rows)
|
|
|
|
cell_frame = self.ctx.assets.get_box_photo("box_purple", (cell_w, cell_h))
|
|
if cell_frame:
|
|
self._photo_refs.append(cell_frame)
|
|
|
|
for idx, (field_key, label_i18n_key) in enumerate(STAT_FIELD_KEYS):
|
|
r, c = divmod(idx, cols)
|
|
cx = left_x + 28 + c * (cell_w + gap_x) + cell_w // 2
|
|
cy = grid_top + r * (cell_h + gap_y) + cell_h // 2
|
|
|
|
if cell_frame:
|
|
self.create_image(cx, cy, image=cell_frame, anchor="center", tags="fg")
|
|
|
|
lbl_ph = self.ctx.assets.get_goth_text_photo(
|
|
t(label_i18n_key),
|
|
FONT_SIZE_SMALL_MIN,
|
|
FONT_COLOR_BODY_MUTED,
|
|
max_width=cell_w - 24,
|
|
align="center",
|
|
)
|
|
if field_key == "won":
|
|
v_col = FONT_COLOR_STATUS_WIN
|
|
elif field_key == "lost":
|
|
v_col = FONT_COLOR_STATUS_LOSS
|
|
elif "streak" in field_key or field_key == "win_pct":
|
|
v_col = FONT_COLOR_HEADING_SUB
|
|
else:
|
|
v_col = FONT_COLOR_BODY_DEFAULT
|
|
|
|
val_ph = self.ctx.assets.get_goth_text_photo(
|
|
str(self._stats_data.get(field_key, "--")),
|
|
FONT_SIZE_HEADING - 2,
|
|
v_col,
|
|
max_width=cell_w - 24,
|
|
align="center",
|
|
)
|
|
self._photo_refs.extend([lbl_ph, val_ph])
|
|
self.create_image(
|
|
cx, cy - cell_h // 4 + 2, image=lbl_ph, anchor="center", tags="fg"
|
|
)
|
|
self.create_image(
|
|
cx, cy + cell_h // 5 + 2, image=val_ph, anchor="center", tags="fg"
|
|
) |