# -*- coding: utf-8 -*- # games/poker/views/poker_menu_view.py import sqlite3 import tkinter as tk from tkinter import ttk from games.poker.views.poker_stats_view import PokerStatsView from games.poker.views.poker_table_view import PokerTableView from models.database import Database from models.progression import Progression from resources.code_res.game_constants import ( ACCENT_GOLD, ACCENT_GOTH, BG_COLOR, BORDER_COLOR, CELL_BG, DEVILS_DEAL_GOLD_REWARD, ERROR_FG, FG_COLOR, GOLD_MAX_ACCOUNTS, INPUT_BG, MUTED_FG, PANEL_BG, PK_MAX_BOTS, PK_STARTING_GOLD, PK_VARIANTS, SUBTLE_BORDER, XP_TO_GOLD_AMOUNT ) from resources.code_res.i18n import t from view.widgets.bot_setup_widget import BotSetupWidget from view.widgets.image_button import ImageButton GAME_FOLDER_ID = "poker" class PokerMenuView: def __init__(self, context): self.ctx = context self.bot_widget: BotSetupWidget | None = None def show(self, preselect_id: int | None = None, status_msg: str = "", status_is_ok: bool = False): pid = self.ctx.profile["id"] devils_acc = Database.get_active_devils_pk_account(pid) if devils_acc is not None: saved = Database.load_game_state(pid, GAME_FOLDER_ID) if saved is not None: PokerTableView( self.ctx, variant_key=saved["variant_key"], pk_account_id=devils_acc["id"], bot_configs=[], on_back_to_menu=lambda acc_id, msg="": self.show(preselect_id=acc_id, status_msg=msg), resume_state_json=saved["state_json"], ) return self.ctx.sounds.play_menu_music() self.ctx.root.clear_window(apply_global_bg=True) pk_prog = self.ctx.refresh_game_progress() dyn_xp_cost = pk_prog["xp_exchange_cost"] is_divine = pk_prog["is_divine_punishment"] at_floor = pk_prog["at_divine_floor"] devils_deal_open = pk_prog["devils_deal_unlocked"] setting_key = f"last_pk_account_{pid}" accounts = Database.get_poker_accounts(pid) has_accounts = len(accounts) > 0 if preselect_id is None and has_accounts: saved_id_str = Database.load_setting(setting_key, "") if saved_id_str.isdigit(): preselect_id = int(saved_id_str) acc_by_name = {a["account_name"]: a for a in accounts} acc_names = list(acc_by_name.keys()) active_acc = None if has_accounts: active_acc = accounts[0] if preselect_id is not None: for a in accounts: if a["id"] == preselect_id: active_acc = a break Database.save_setting(setting_key, str(active_acc["id"])) selected_name_var = tk.StringVar( value=active_acc["account_name"] if active_acc else t("bj_no_account_item") ) feedback_var = tk.StringVar(value=status_msg) def get_current_acc() -> dict | None: return acc_by_name.get(selected_name_var.get()) def open_pk_stats(): cur = get_current_acc() if cur is None: return if self.bot_widget is not None and has_accounts: _, to_save = self.bot_widget.collect_configs() Database.save_bot_configs(pid, GAME_FOLDER_ID, to_save) PokerStatsView( self.ctx, on_back_to_pk_menu=lambda aid: self.show(preselect_id=aid) ).show(selected_acc_id=cur["id"]) def refresh_on_lang_switch(): cur = get_current_acc() if self.bot_widget is not None and has_accounts: _, to_save = self.bot_widget.collect_configs() Database.save_bot_configs(pid, GAME_FOLDER_ID, to_save) self.show(preselect_id=cur["id"] if cur else None) topbar_gold_txt = ( t("pk_header_gold", active_acc["account_name"], active_acc["gold"]) if active_acc else "" ) topbar = self.ctx.build_game_topbar( self.ctx.root, t("pk_menu_topbar"), on_game_menu=None, on_stats=open_pk_stats, stats_enabled=has_accounts, extra_right_text=topbar_gold_txt, on_lang_refresh=refresh_on_lang_switch, ) container = tk.Frame(self.ctx.root, bg=BG_COLOR, padx=24, pady=12) container.place(relx=0.5, rely=0.53, anchor="center", relwidth=0.93, relheight=0.85) main_card = tk.Frame( container, bg=PANEL_BG, padx=24, pady=16, highlightthickness=1, highlightbackground=BORDER_COLOR ) main_card.pack(fill="x") upper_split = tk.Frame(main_card, bg=PANEL_BG) upper_split.pack(fill="x", pady=(0, 10)) upper_split.columnconfigure(0, weight=1, uniform="cols") upper_split.columnconfigure(1, weight=1, uniform="cols") # ===================================================================== # SPALTE 1: Poker-Goldkonto # ===================================================================== left_col = tk.Frame(upper_split, bg=PANEL_BG) left_col.grid(row=0, column=0, sticky="nsew", padx=(0, 10)) tk.Label( left_col, text=t("pk_sec1_title", len(accounts), GOLD_MAX_ACCOUNTS, PK_STARTING_GOLD), font=("Georgia", 12, "bold"), fg=ACCENT_GOLD, bg=PANEL_BG ).pack(anchor="w", pady=(0, 6)) acc_box = tk.Frame(left_col, bg=CELL_BG, padx=14, pady=12, highlightthickness=1, highlightbackground=SUBTLE_BORDER) acc_box.pack(fill="both", expand=True) form_grid = tk.Frame(acc_box, bg=CELL_BG) form_grid.pack(fill="x") form_grid.columnconfigure(1, weight=1) lbl_w = max(len(t("bj_active_acc_lbl")), len(t("bj_new_acc_lbl"))) tk.Label( form_grid, text=t("bj_active_acc_lbl"), width=lbl_w, anchor="w", font=("Helvetica", 10, "bold"), fg=FG_COLOR if has_accounts else MUTED_FG, bg=CELL_BG ).grid(row=0, column=0, sticky="w", padx=(0, 10), pady=4) acc_cb = ttk.Combobox( form_grid, textvariable=selected_name_var, values=acc_names if has_accounts else [t("bj_no_account_item")], state="readonly" if has_accounts else "disabled", font=("Helvetica", 10), style="Goth.TCombobox", ) acc_cb.grid(row=0, column=1, sticky="ew", padx=(0, 10), pady=4, ipady=2) def exchange_xp_action(): cur = get_current_acc() if cur is None: return if self.bot_widget is not None: _, to_save = self.bot_widget.collect_configs() Database.save_bot_configs(pid, GAME_FOLDER_ID, to_save) ok, msg = Database.exchange_xp_for_pk_gold(pid, cur["id"]) if ok and is_divine: self.ctx.sounds.play("lose") self.show(preselect_id=cur["id"], status_msg=msg, status_is_ok=ok) can_afford_xp = is_divine or (pk_prog["total_points"] >= dyn_xp_cost) can_exchange = has_accounts and (not at_floor) and can_afford_xp ex_btn_txt = ( t("bj_exchange_divine_btn", f"{dyn_xp_cost:,}".replace(",", "."), XP_TO_GOLD_AMOUNT) if is_divine else t("bj_exchange_btn", dyn_xp_cost, XP_TO_GOLD_AMOUNT) ) ImageButton( form_grid, ex_btn_txt, self.ctx.assets.raw_btn, self.ctx.assets.raw_btn_active, exchange_xp_action, sound_mgr=self.ctx.sounds, width=185, height=34, font=("Helvetica", 9, "bold"), enabled=can_exchange, ).grid(row=0, column=2, sticky="e", pady=4) # Prominente Gold-Anzeige gold_banner = tk.Frame( acc_box, bg=PANEL_BG, padx=12, pady=8, highlightthickness=1, highlightbackground=BORDER_COLOR if has_accounts else SUBTLE_BORDER ) gold_banner.pack(fill="x", pady=(8, 8)) gold_var = tk.StringVar( value=t("bj_gold_display_lbl", active_acc["gold"]) if active_acc else t("bj_gold_display_none") ) tk.Label( gold_banner, textvariable=gold_var, font=("Georgia", 13, "bold"), fg=ACCENT_GOLD if active_acc else MUTED_FG, bg=PANEL_BG, ).pack(side="left") def delete_selected_account(): cur = get_current_acc() if cur is not None: Database.delete_poker_account(cur["id"]) self.show() ImageButton( gold_banner, t("bj_delete_acc_btn"), self.ctx.assets.raw_sel, self.ctx.assets.raw_sel_active, delete_selected_account, sound_mgr=self.ctx.sounds, width=120, height=30, font=("Helvetica", 9, "bold"), enabled=has_accounts, ).pack(side="right") # Neues Konto anlegen (exakt gleiche Spaltenbreite) create_grid = tk.Frame(acc_box, bg=CELL_BG) create_grid.pack(fill="x") create_grid.columnconfigure(1, weight=1) can_create_more = len(accounts) < GOLD_MAX_ACCOUNTS tk.Label( create_grid, text=t("bj_new_acc_lbl"), width=lbl_w, anchor="w", font=("Helvetica", 10, "bold" if not has_accounts else "normal"), fg=ACCENT_GOLD if not has_accounts else (FG_COLOR if can_create_more else MUTED_FG), bg=CELL_BG, ).grid(row=0, column=0, sticky="w", padx=(0, 10), pady=4) next_idx = len(accounts) + 1 new_acc_var = tk.StringVar( value=t("pk_default_acc_name", next_idx) if can_create_more else t("bj_max_acc_reached", GOLD_MAX_ACCOUNTS) ) new_entry = tk.Entry( create_grid, textvariable=new_acc_var, font=("Helvetica", 10), bg=INPUT_BG, fg=FG_COLOR, disabledbackground="#16141c", disabledforeground="#666070", bd=0, highlightthickness=1, highlightbackground=ACCENT_GOLD if not has_accounts else BORDER_COLOR, insertbackground="white", state="normal" if can_create_more else "disabled" ) new_entry.grid(row=0, column=1, sticky="ew", padx=(0, 10), pady=4, ipady=5) def create_new_account(): if not can_create_more: return name = new_acc_var.get().strip() if not name: feedback_var.set(t("bj_err_empty_name")) return try: if self.bot_widget is not None and has_accounts: _, to_save = self.bot_widget.collect_configs() Database.save_bot_configs(pid, GAME_FOLDER_ID, to_save) created = Database.create_poker_account(pid, name) Database.save_setting(setting_key, str(created["id"])) self.show(preselect_id=created["id"]) except (sqlite3.IntegrityError, RuntimeError) as exc: feedback_var.set(t("pk_err_duplicate_name") if isinstance(exc, sqlite3.IntegrityError) else str(exc)) if can_create_more: new_entry.bind("", lambda _e: create_new_account()) ImageButton( create_grid, t("bj_create_acc_btn", PK_STARTING_GOLD), self.ctx.assets.raw_btn, self.ctx.assets.raw_btn_active, create_new_account, sound_mgr=self.ctx.sounds, width=185, height=34, font=("Helvetica", 9, "bold"), enabled=can_create_more, ).grid(row=0, column=2, sticky="e", pady=4) # Info / Divine Punishment / The Devil's Deal (Poker-autark!) min_sac_lvl = Progression.get_devils_deal_min_level() classic_cfg = PK_VARIANTS["poker_holdem"] if devils_deal_open and has_accounts: tk.Label( acc_box, text=t("bj_devils_deal_warn", min_sac_lvl, DEVILS_DEAL_GOLD_REWARD), font=("Helvetica", 8, "bold"), fg=ERROR_FG, bg=CELL_BG, wraplength=480, justify="left" ).pack(anchor="w", pady=(6, 4)) sac_candidates = Database.get_sacrificeable_profiles(pid, GAME_FOLDER_ID) if sac_candidates: devil_row = tk.Frame(acc_box, bg=CELL_BG) devil_row.pack(fill="x", pady=(2, 0)) sac_map = {f"{c['name']} ({c['title']} • Lv. {c['level']})": c["id"] for c in sac_candidates} sac_var = tk.StringVar(value=next(iter(sac_map.keys()))) ttk.Combobox( devil_row, textvariable=sac_var, values=list(sac_map.keys()), state="readonly", font=("Helvetica", 9), width=26, style="Goth.TCombobox" ).pack(side="left", fill="x", expand=True, padx=(0, 8)) def seal_devils_deal(): cur = get_current_acc() victim_id = sac_map.get(sac_var.get()) if cur is None or victim_id is None: return active_bots, to_save = self.bot_widget.collect_configs() if self.bot_widget else ([], {}) if not active_bots: feedback_var.set(t("pk_err_need_one_bot")) return Database.save_bot_configs(pid, GAME_FOLDER_ID, to_save) ok, _msg = Database.execute_devils_deal(pid, GAME_FOLDER_ID, victim_id, cur["id"]) if ok: self.ctx.sounds.play("end_battle") PokerTableView( self.ctx, variant_key="poker_holdem", pk_account_id=cur["id"], bot_configs=active_bots, on_back_to_menu=lambda acc_id, msg="": self.show(preselect_id=acc_id, status_msg=msg), ) ImageButton( devil_row, t("bj_devils_deal_btn", DEVILS_DEAL_GOLD_REWARD), self.ctx.assets.raw_btn, self.ctx.assets.raw_btn_active, seal_devils_deal, sound_mgr=None, width=185, height=32, font=("Helvetica", 9, "bold") ).pack(side="right") else: tk.Label( acc_box, text=t("bj_devils_deal_none", min_sac_lvl), font=("Helvetica", 8), fg=MUTED_FG, bg=CELL_BG ).pack(anchor="w") else: info_text = ( t("bj_divine_punishment_warn", f"{dyn_xp_cost:,}".replace(",", "."), XP_TO_GOLD_AMOUNT) if is_divine else t("pk_acc_limit_info", GOLD_MAX_ACCOUNTS, classic_cfg["sb"], classic_cfg["bb"], dyn_xp_cost, XP_TO_GOLD_AMOUNT) ) tk.Label( acc_box, text=info_text, font=("Helvetica", 8, "bold" if is_divine else "normal"), fg=ERROR_FG if is_divine else MUTED_FG, bg=CELL_BG, wraplength=480, justify="left" ).pack(anchor="w", pady=(6, 0)) # ===================================================================== # SPALTE 2: 6-Max Gegner-Setup (1 bis 5 Bots) # ===================================================================== right_col = tk.Frame(upper_split, bg=PANEL_BG) right_col.grid(row=0, column=1, sticky="nsew", padx=(10, 0)) tk.Label( right_col, text=t("pk_bots_sec_title"), font=("Georgia", 12, "bold"), fg=ACCENT_GOTH if has_accounts else MUTED_FG, bg=PANEL_BG ).pack(anchor="w", pady=(0, 6)) saved_bots = Database.load_bot_configs(pid, GAME_FOLDER_ID) if not saved_bots: # Komfort-Default beim allerersten Poker-Besuch: 2 solide Gegner voreingestellt saved_bots = { 0: (t("bot_default_name", 1), 1), 1: (t("bot_default_name", 2), 2), } self.bot_widget = BotSetupWidget(right_col, saved_bots, max_bots=PK_MAX_BOTS, enabled=has_accounts) self.bot_widget.pack(fill="both", expand=True) fb_lbl = tk.Label( main_card, textvariable=feedback_var, font=("Helvetica", 9, "bold"), fg="#7be08a" if status_is_ok else ERROR_FG, bg=PANEL_BG ) fb_lbl.pack(anchor="w", pady=(2, 4)) # ===================================================================== # SEKTION 3: Tischvariante wählen # ===================================================================== tk.Label( main_card, text=t("pk_sec3_title"), font=("Georgia", 12, "bold"), fg=ACCENT_GOTH if has_accounts else MUTED_FG, bg=PANEL_BG ).pack(anchor="w", pady=(2, 8)) def enter_table(v_key: str): fb_lbl.configure(fg=ERROR_FG) cur = get_current_acc() if not cur: return acc = Database.get_poker_account(cur["id"]) if not acc: return Database.save_setting(setting_key, str(acc["id"])) active_bots, configs_to_save = self.bot_widget.collect_configs() Database.save_bot_configs(pid, GAME_FOLDER_ID, configs_to_save) if not active_bots: feedback_var.set(t("pk_err_need_one_bot")) return min_req = PK_VARIANTS[v_key]["min_entry"] cur_pk_prog = self.ctx.refresh_game_progress() if acc["gold"] < min_req: feedback_var.set(t( "bj_err_not_enough_gold_entry", acc["account_name"], min_req, acc["gold"], cur_pk_prog["xp_exchange_cost"], XP_TO_GOLD_AMOUNT )) return PokerTableView( self.ctx, v_key, acc["id"], active_bots, on_back_to_menu=lambda acc_id, msg="": self.show(preselect_id=acc_id, status_msg=msg) ) variants_row = tk.Frame(main_card, bg=PANEL_BG) variants_row.pack(fill="x") for idx in range(len(PK_VARIANTS)): variants_row.columnconfigure(idx, weight=1) table_buttons: list[tuple[str, ImageButton]] = [] for idx, (v_key, cfg) in enumerate(PK_VARIANTS.items()): card = tk.Frame(variants_row, bg=CELL_BG, padx=16, pady=12, highlightthickness=1, highlightbackground=SUBTLE_BORDER) card.grid(row=0, column=idx, padx=(0, 8) if idx == 0 else (8, 0), sticky="nsew") tk.Label( card, text=t(f"variant_{v_key}"), font=("Georgia", 12, "bold"), fg=FG_COLOR if has_accounts else MUTED_FG, bg=CELL_BG ).pack(anchor="w") tk.Label( card, text=t("pk_variant_desc", cfg["sb"], cfg["bb"], cfg["min_entry"]), font=("Helvetica", 9), fg=MUTED_FG, bg=CELL_BG ).pack(anchor="w", pady=(4, 10)) can_play_v = bool(active_acc and active_acc["gold"] >= cfg["min_entry"]) t_btn = ImageButton( card, t("bj_enter_table_btn"), self.ctx.assets.raw_btn, self.ctx.assets.raw_btn_active, lambda k=v_key: enter_table(k), sound_mgr=self.ctx.sounds, width=160, height=36, font=("Helvetica", 10, "bold"), enabled=can_play_v, ) t_btn.pack(anchor="e") table_buttons.append((v_key, t_btn)) def on_account_switched(*_): cur = get_current_acc() if cur is not None: fresh = Database.get_poker_account(cur["id"]) Database.save_setting(setting_key, str(fresh["id"])) gold_var.set(t("bj_gold_display_lbl", fresh["gold"])) topbar.extra_label.configure(text=t("pk_header_gold", fresh["account_name"], fresh["gold"])) for v_k, tb in table_buttons: tb.set_enabled(fresh["gold"] >= PK_VARIANTS[v_k]["min_entry"]) feedback_var.set("") acc_cb.bind("<>", on_account_switched)