# -*- coding: utf-8 -*- # games/poker/views/poker_menu_view.py from __future__ import annotations import sqlite3 import tkinter as tk from games.poker.views.poker_table_view import PokerTableView from view.widgets.menu_body_canvas import MenuBodyCanvas from models.database import Database from models.progression import Progression from resources.code_res.game_constants import ( DEVILS_DEAL_GOLD_REWARD, FONT_COLOR_BODY_DEFAULT, FONT_COLOR_BODY_MUTED, FONT_COLOR_DISABLED, 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, GOLD_MAX_ACCOUNTS, PK_MAX_BOTS, PK_STARTING_GOLD, PK_VARIANTS, XP_TO_GOLD_AMOUNT, ) from resources.code_res.i18n import t from view.widgets.bot_setup_widget import BotSetupWidget from view.widgets.goth_dropdown import GothDropdown from view.widgets.goth_entry import GothEntry 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 self._photo_refs: list = [] 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) self._photo_refs.clear() 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") ) state_msg = {"text": status_msg, "ok": status_is_ok} 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, ) cv = MenuBodyCanvas(self.ctx) 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 table_buttons: list[tuple[str, ImageButton]] = [] def on_account_switched(_val=None): cur = get_current_acc() if cur is not None: fresh = Database.get_poker_account(cur["id"]) Database.save_setting(setting_key, str(fresh["id"])) 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"]) state_msg["text"] = "" if cv.winfo_exists() and cv.winfo_width() >= 200: draw_layout(cv.winfo_width(), cv.winfo_height()) acc_dd = GothDropdown( cv, self.ctx.assets, values=acc_names if has_accounts else [t("bj_no_account_item")], textvariable=selected_name_var, on_change=on_account_switched, sound_mgr=self.ctx.sounds, width=215, height=36, enabled=has_accounts, ) 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) ) exchange_btn = ImageButton( cv, ex_btn_txt, btn_norm, btn_purp, exchange_xp_action, sound_mgr=self.ctx.sounds, width=180, height=36, enabled=can_exchange, style="amethyst", ) def delete_selected_account(): cur = get_current_acc() if cur is not None: Database.delete_poker_account(cur["id"]) self.show() delete_btn = ImageButton( cv, t("bj_delete_acc_btn"), btn_norm, btn_purp, delete_selected_account, sound_mgr=self.ctx.sounds, width=136, height=32, enabled=has_accounts, style="amethyst", ) can_create_more = len(accounts) < GOLD_MAX_ACCOUNTS 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 = GothEntry( cv, self.ctx.assets, width=215, height=36, textvariable=new_acc_var, enabled=can_create_more, ) def create_new_account(): if not can_create_more: return name = new_entry.get().strip() if not name: state_msg["text"] = t("bj_err_empty_name") state_msg["ok"] = False draw_layout(cv.winfo_width(), cv.winfo_height()) 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: state_msg["text"] = t("pk_err_duplicate_name") if isinstance(exc, sqlite3.IntegrityError) else str(exc) state_msg["ok"] = False draw_layout(cv.winfo_width(), cv.winfo_height()) if can_create_more: new_entry.bind_return(lambda _e: create_new_account()) create_btn = ImageButton( cv, t("bj_create_acc_btn", PK_STARTING_GOLD), btn_fire if not has_accounts else btn_norm, btn_fire if not has_accounts else btn_purp, create_new_account, sound_mgr=self.ctx.sounds, width=180, height=36, enabled=can_create_more, style="fire" if not has_accounts else "amethyst", ) sac_dd = None devil_btn = None min_sac_lvl = Progression.get_devils_deal_min_level() classic_cfg = PK_VARIANTS["poker_holdem"] sac_candidates = Database.get_sacrificeable_profiles(pid, GAME_FOLDER_ID) if (devils_deal_open and has_accounts) else [] if sac_candidates: 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()))) sac_dd = GothDropdown( cv, self.ctx.assets, values=list(sac_map.keys()), textvariable=sac_var, sound_mgr=self.ctx.sounds, width=250, height=34, ) 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: state_msg["text"] = t("pk_err_need_one_bot") state_msg["ok"] = False draw_layout(cv.winfo_width(), cv.winfo_height()) 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), ) devil_btn = ImageButton( cv, t("bj_devils_deal_btn", DEVILS_DEAL_GOLD_REWARD), btn_fire, btn_fire, seal_devils_deal, sound_mgr=None, width=180, height=34, style="fire", ) saved_bots = Database.load_bot_configs(pid, GAME_FOLDER_ID) if not saved_bots: saved_bots = { 0: (t("bot_default_name", 1), 1), 1: (t("bot_default_name", 2), 2), } self.bot_widget = BotSetupWidget(cv, saved_bots, max_bots=PK_MAX_BOTS, enabled=has_accounts) def enter_table(v_key: str): 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: state_msg["text"] = t("pk_err_need_one_bot") state_msg["ok"] = False draw_layout(cv.winfo_width(), cv.winfo_height()) return min_req = PK_VARIANTS[v_key]["min_entry"] cur_pk_prog = self.ctx.refresh_game_progress() if acc["gold"] < min_req: state_msg["text"] = t( "bj_err_not_enough_gold_entry", acc["account_name"], min_req, acc["gold"], cur_pk_prog["xp_exchange_cost"], XP_TO_GOLD_AMOUNT, ) state_msg["ok"] = False draw_layout(cv.winfo_width(), cv.winfo_height()) 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), ) variant_items = list(PK_VARIANTS.items()) for v_key, cfg in variant_items: can_play_v = bool(active_acc and active_acc["gold"] >= cfg["min_entry"]) t_btn = ImageButton( cv, t("bj_enter_table_btn"), btn_fire, btn_fire, lambda k=v_key: enter_table(k), sound_mgr=self.ctx.sounds, width=168, height=38, enabled=can_play_v, style="fire", ) table_buttons.append((v_key, t_btn)) def draw_layout(w: int, h: int): cv.sync_bg(w, h) cv.delete("fg") self._photo_refs.clear() compact = (h < 710) pad_x = max(44, int(w * 0.045)) usable_w = min(1240, w - 2 * pad_x) x0 = (w - usable_w) // 2 col_gap = 20 col_w = (usable_w - col_gap) // 2 left_x = x0 right_x = x0 + col_w + col_gap top_sec_y = 10 if compact else 18 upper_h = max(296, PK_MAX_BOTS * BotSetupWidget.ROW_HEIGHT + 44) s1_ph = self.ctx.assets.get_goth_text_photo( t("pk_sec1_title", len(accounts), GOLD_MAX_ACCOUNTS, PK_STARTING_GOLD), FONT_SIZE_HEADING - 3, FONT_COLOR_TITLE_MAIN, max_width=col_w - 10, align="left", ) s2_ph = self.ctx.assets.get_goth_text_photo( t("pk_bots_sec_title"), FONT_SIZE_HEADING - 3, FONT_COLOR_HEADING_SUB if has_accounts else FONT_COLOR_DISABLED, max_width=col_w - 10, align="left", ) self._photo_refs.extend([s1_ph, s2_ph]) cv.create_image(left_x + 8, top_sec_y + 12, image=s1_ph, anchor="w", tags="fg") cv.create_image(right_x + 8, top_sec_y + 12, image=s2_ph, anchor="w", tags="fg") box_top = top_sec_y + 32 box_cy = box_top + upper_h // 2 l_frame = self.ctx.assets.get_box_photo("box_big", (col_w, upper_h)) r_frame = self.ctx.assets.get_box_photo("box_purple", (col_w, upper_h)) if l_frame: self._photo_refs.append(l_frame) cv.create_image(left_x + col_w // 2, box_cy, image=l_frame, anchor="center", tags="fg") if r_frame: self._photo_refs.append(r_frame) cv.create_image(right_x + col_w // 2, box_cy, image=r_frame, anchor="center", tags="fg") in_pad = 22 lbl_col_w = 118 btn_w = 180 mid_field_w = max(140, col_w - 2 * in_pad - lbl_col_w - btn_w - 16) if acc_dd._width != mid_field_w: acc_dd._width = mid_field_w acc_dd.configure(width=mid_field_w) acc_dd._render_closed() r1_y = box_top + 40 l_act = self.ctx.assets.get_goth_text_photo( t("bj_active_acc_lbl"), FONT_SIZE_BODY, FONT_COLOR_BODY_DEFAULT if has_accounts else FONT_COLOR_DISABLED, align="left", ) self._photo_refs.append(l_act) cv.create_image(left_x + in_pad, r1_y, image=l_act, anchor="w", tags="fg") cv.create_window(left_x + in_pad + lbl_col_w, r1_y, window=acc_dd, anchor="w", tags="fg") cv.create_window(left_x + col_w - in_pad, r1_y, window=exchange_btn, anchor="e", tags="fg") gb_y = r1_y + 54 gb_w = col_w - 2 * in_pad gb_frame = self.ctx.assets.get_box_photo("box_gold_double", (gb_w, 50)) if gb_frame: self._photo_refs.append(gb_frame) cv.create_image(left_x + col_w // 2, gb_y, image=gb_frame, anchor="center", tags="fg") cur_acc_obj = get_current_acc() fresh_gold = Database.get_poker_account(cur_acc_obj["id"])["gold"] if cur_acc_obj else 0 gold_str = t("pk_gold_display_lbl", fresh_gold) if cur_acc_obj else t("bj_gold_display_none") g_ph = self.ctx.assets.get_goth_text_photo( gold_str, FONT_SIZE_HEADING - 3, FONT_COLOR_TITLE_MAIN if cur_acc_obj else FONT_COLOR_BODY_MUTED, align="left", ) self._photo_refs.append(g_ph) cv.create_image(left_x + in_pad + 18, gb_y, image=g_ph, anchor="w", tags="fg") cv.create_window(left_x + col_w - in_pad - 14, gb_y, window=delete_btn, anchor="e", tags="fg") r3_y = gb_y + 54 l_new = self.ctx.assets.get_goth_text_photo( t("bj_new_acc_lbl"), FONT_SIZE_BODY, FONT_COLOR_TITLE_MAIN if not has_accounts else (FONT_COLOR_BODY_DEFAULT if can_create_more else FONT_COLOR_DISABLED), align="left", ) self._photo_refs.append(l_new) cv.create_image(left_x + in_pad, r3_y, image=l_new, anchor="w", tags="fg") cv.create_window(left_x + in_pad + lbl_col_w, r3_y, window=new_entry, anchor="w", tags="fg") cv.create_window(left_x + col_w - in_pad, r3_y, window=create_btn, anchor="e", tags="fg") info_y = r3_y + 42 if devils_deal_open and has_accounts: warn_ph = self.ctx.assets.get_goth_text_photo( t("bj_devils_deal_warn", min_sac_lvl, DEVILS_DEAL_GOLD_REWARD), FONT_SIZE_SMALL_MIN, FONT_COLOR_STATUS_LOSS, max_width=gb_w, align="left", ) self._photo_refs.append(warn_ph) cv.create_image(left_x + in_pad, info_y, image=warn_ph, anchor="nw", tags="fg") if sac_dd is not None and devil_btn is not None: dy = box_top + upper_h - 30 cv.create_window(left_x + in_pad, dy, window=sac_dd, anchor="w", tags="fg") cv.create_window(left_x + col_w - in_pad, dy, window=devil_btn, anchor="e", tags="fg") else: none_ph = self.ctx.assets.get_goth_text_photo( t("bj_devils_deal_none", min_sac_lvl), FONT_SIZE_SMALL_MIN, FONT_COLOR_BODY_MUTED, align="left" ) self._photo_refs.append(none_ph) cv.create_image(left_x + in_pad, box_top + upper_h - 28, image=none_ph, anchor="w", tags="fg") else: info_txt = ( 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) ) inf_ph = self.ctx.assets.get_goth_text_photo( info_txt, FONT_SIZE_SMALL_MIN, FONT_COLOR_STATUS_LOSS if is_divine else FONT_COLOR_BODY_MUTED, max_width=gb_w, align="left", ) self._photo_refs.append(inf_ph) cv.create_image(left_x + in_pad, info_y + 4, image=inf_ph, anchor="nw", tags="fg") if self.bot_widget is not None: bw_w = col_w - 40 bw_h = PK_MAX_BOTS * BotSetupWidget.ROW_HEIGHT + 8 cv.create_window( right_x + col_w // 2, box_cy, window=self.bot_widget, width=bw_w, height=bw_h, anchor="center", tags="fg" ) sec3_hdr_y = box_top + upper_h + (18 if compact else 26) if state_msg["text"]: fb_ph = self.ctx.assets.get_goth_text_photo( state_msg["text"], FONT_SIZE_BODY, FONT_COLOR_STATUS_WIN if state_msg["ok"] else FONT_COLOR_STATUS_LOSS, max_width=usable_w // 2, align="right", ) self._photo_refs.append(fb_ph) cv.create_image(x0 + usable_w - 6, sec3_hdr_y, image=fb_ph, anchor="e", tags="fg") s3_ph = self.ctx.assets.get_goth_text_photo( t("pk_sec3_title"), FONT_SIZE_HEADING - 3, FONT_COLOR_HEADING_SUB if has_accounts else FONT_COLOR_DISABLED, align="left", ) self._photo_refs.append(s3_ph) cv.create_image(left_x + 8, sec3_hdr_y, image=s3_ph, anchor="w", tags="fg") var_top = sec3_hdr_y + 20 var_h = max(118, min(148, h - var_top - 18)) var_cy = var_top + var_h // 2 v_frame = self.ctx.assets.get_box_photo("box_gold_small", (col_w, var_h)) if v_frame: self._photo_refs.append(v_frame) for idx, (v_key, cfg) in enumerate(variant_items): vx = left_x if idx == 0 else right_x if v_frame: cv.create_image(vx + col_w // 2, var_cy, image=v_frame, anchor="center", tags="fg") vt_ph = self.ctx.assets.get_goth_text_photo( t(f"variant_{v_key}"), FONT_SIZE_HEADING - 2, FONT_COLOR_TITLE_MAIN if has_accounts else FONT_COLOR_DISABLED, align="left", ) vd_ph = self.ctx.assets.get_goth_text_photo( t("pk_variant_desc", cfg["sb"], cfg["bb"], cfg["min_entry"]), FONT_SIZE_SMALL_MIN, FONT_COLOR_BODY_MUTED, max_width=col_w - 56, align="left", ) self._photo_refs.extend([vt_ph, vd_ph]) cv.create_image(vx + 28, var_top + 30, image=vt_ph, anchor="w", tags="fg") cv.create_image(vx + 28, var_top + 60, image=vd_ph, anchor="w", tags="fg") cv.create_window( vx + col_w - 28, var_top + var_h - 30, window=table_buttons[idx][1], anchor="e", tags="fg" ) cv.bind("", lambda e: draw_layout(e.width, e.height) if e.width >= 200 else None) self.ctx.root.update_idletasks() draw_layout(max(900, cv.winfo_width()), max(620, cv.winfo_height()))