# -*- coding: utf-8 -*- # games/poker/views/poker_arena_renderer.py from __future__ import annotations import tkinter as tk from games.poker.models.poker_evaluator import PokerEvaluator from models.db.database import Database from models.progression import Progression from resources.code_res.game_constants import ( ACCENT_GOLD, ACCENT_GOTH, ERROR_FG, FG_COLOR, FONT_COLOR_TITLE_MAIN, FONT_SIZE_BODY, FONT_SIZE_SMALL_MIN, MUTED_FG, PLAYING_CARD_SIZE, SUBTLE_BORDER, ) from resources.code_res.i18n import t PK_OVAL_BOARD_CARD = (78, 114) PK_BOT_SHOWDOWN_CARD = (42, 62) SEAT_LAYOUT_BY_COUNT = { 1: (2,), 2: (1, 3), 3: (1, 2, 3), 4: (0, 1, 3, 4), 5: (0, 1, 2, 3, 4), } class PokerArenaRenderer: """Zeichnet die 6-Max-Tischszene für Texas Hold'em (Desktop-Oval vs. tischlose Kompakt-Ansicht).""" @staticmethod def role_badge(eng, seat_idx: int) -> str: badges = [] if seat_idx == eng.dealer_pos: badges.append(t("pk_role_dealer")) if seat_idx == eng.sb_pos: badges.append(t("pk_role_sb")) if seat_idx == eng.bb_pos: badges.append(t("pk_role_bb")) return "".join(badges) @classmethod def draw_bots( cls, view, cw: int, ch: int, pad_x: int, pad_top: int, pad_bot: int, table_rect: tuple[int, int, int, int], ) -> int: bot_seats = view.engine.seats[1:] if not bot_seats: return table_rect[1] + 12 if getattr(view, "is_compact_arena", False): return cls._draw_bots_top_grid(view, bot_seats, cw, pad_x, pad_top) tx1, ty1, tx2, ty2 = table_rect is_showdown = (view.engine.street == "SHOWDOWN") box_h = 98 if is_showdown else 88 base_box_w = 216 if is_showdown else 204 top_cy = max(pad_top + box_h // 2, ty1 + box_h // 2 - 4) bot_cy = min(ch - pad_bot - box_h // 2, ty2 - box_h // 2 + 22) if bot_cy - top_cy < box_h + 16: mid_y = (top_cy + bot_cy) // 2 top_cy, bot_cy = mid_y - (box_h + 16) // 2, mid_y + (box_h + 16) // 2 top_center_cy = max(pad_top + box_h // 2, ty1 + int(box_h * 0.42)) top_center_bottom = ty1 + 24 slot_indices = SEAT_LAYOUT_BY_COUNT.get(len(bot_seats), (0, 1, 2, 3, 4)) name_sz, sub_sz = max(14, FONT_SIZE_BODY - 1), max(13, FONT_SIZE_BODY - 2) for bot_i, seat in enumerate(bot_seats): coord_idx = slot_indices[bot_i] is_turn = (not is_showdown and view.engine.turn_pos == seat.seat_id) show_bot_cards = is_showdown and bool(seat.hole_cards) and (not seat.folded) frame_style = "gold" if is_turn else "purple" status_border = ERROR_FG if seat.folded else (ACCENT_GOTH if seat.all_in else None) base_name = t("bj_bot_rebuy", seat.name) if seat.rebought else seat.name title_txt = f"{base_name}{cls.role_badge(view.engine, seat.seat_id)}" title_col = MUTED_FG if seat.folded else (ACCENT_GOLD if is_turn else ACCENT_GOTH) stack_txt = t("pk_bot_stack_lbl", seat.stack, seat.street_bet) st_txt = seat.status_text or "-" st_col = ERROR_FG if seat.folded else (ACCENT_GOLD if is_turn else FG_COLOR) max_txt_w = 176 if show_bot_cards else 230 title_ph = view._make_goth_photo(title_txt, name_sz, title_col, max_txt_w) stack_ph = view._make_goth_photo(stack_txt, sub_sz, ACCENT_GOLD, max_txt_w) st_ph = view._make_goth_photo(st_txt, sub_sz, st_col, max_txt_w) widest_txt = max((ph.width() for ph in (title_ph, stack_ph, st_ph) if ph), default=140) extra_cards_w = (PK_BOT_SHOWDOWN_CARD[0] + 38) if show_bot_cards else 0 box_w = max(base_box_w, min(296, widest_txt + extra_cards_w + 46)) if coord_idx == 2: bx, by = cw // 2, top_center_cy top_center_bottom = max(top_center_bottom, by + box_h // 2) elif coord_idx in (0, 1): bx = max(pad_x + box_w // 2, tx1 + int(box_w * 0.36)) by = bot_cy if coord_idx == 0 else top_cy else: bx = min(cw - pad_x - box_w // 2, tx2 - int(box_w * 0.36)) by = top_cy if coord_idx == 3 else bot_cy view._draw_framed_panel(bx, by, box_w, box_h, frame_style=frame_style, status_border=status_border) cls._draw_bot_box_contents( view, seat, bx, by, box_w, show_bot_cards, PK_BOT_SHOWDOWN_CARD, title_ph, title_txt, title_col, stack_ph, stack_txt, st_ph, st_txt, st_col, name_sz, sub_sz, ) return top_center_bottom @classmethod def _draw_bots_top_grid(cls, view, bot_seats: list, cw: int, pad_x: int, pad_top: int) -> int: """Zeichnet 1..5 Poker-Bots im Kompaktmodus oben nebeneinander (max. 3 pro Reihe).""" is_showdown = (view.engine.street == "SHOWDOWN") n = len(bot_seats) cols = min(3, n) rows = (n + cols - 1) // cols gap_x, gap_y = 6, 6 avail_w = max(320, cw - 2 * pad_x) box_w = min(214, (avail_w - (cols - 1) * gap_x) // max(1, cols)) box_h = 74 if rows == 2 else 82 mini_card = (34, 50) name_sz = max(12, FONT_SIZE_SMALL_MIN) sub_sz = max(11, FONT_SIZE_SMALL_MIN - 1) bottom_edge = pad_top for idx, seat in enumerate(bot_seats): r, c = divmod(idx, cols) in_row = min(cols, n - r * cols) row_start_x = (cw - (in_row * box_w + (in_row - 1) * gap_x)) // 2 + box_w // 2 bx = row_start_x + c * (box_w + gap_x) by = pad_top + r * (box_h + gap_y) + box_h // 2 bottom_edge = max(bottom_edge, by + box_h // 2) is_turn = (not is_showdown and view.engine.turn_pos == seat.seat_id) show_bot_cards = is_showdown and bool(seat.hole_cards) and (not seat.folded) status_border = ERROR_FG if seat.folded else (ACCENT_GOTH if seat.all_in else None) base_name = t("bj_bot_rebuy", seat.name) if seat.rebought else seat.name title_txt = f"{base_name}{cls.role_badge(view.engine, seat.seat_id)}" title_col = MUTED_FG if seat.folded else (ACCENT_GOLD if is_turn else ACCENT_GOTH) stack_txt = t("pk_bot_stack_lbl", seat.stack, seat.street_bet) st_txt = seat.status_text or "-" st_col = ERROR_FG if seat.folded else (ACCENT_GOLD if is_turn else FG_COLOR) max_tw = max(72, box_w - (68 if show_bot_cards else 18)) title_ph = view._make_goth_photo(title_txt, name_sz, title_col, max_tw) stack_ph = view._make_goth_photo(stack_txt, sub_sz, ACCENT_GOLD, max_tw) st_ph = view._make_goth_photo(st_txt, sub_sz, st_col, max_tw) view._draw_framed_panel( bx, by, box_w, box_h, frame_style="gold" if is_turn else "purple", status_border=status_border, ) cls._draw_bot_box_contents( view, seat, bx, by, box_w, show_bot_cards, mini_card, title_ph, title_txt, title_col, stack_ph, stack_txt, st_ph, st_txt, st_col, name_sz, sub_sz, dy=20 if rows == 2 else 22, ) return bottom_edge @staticmethod def _draw_bot_box_contents( view, seat, bx: int, by: int, box_w: int, show_cards: bool, card_sz: tuple[int, int], title_ph, title_txt: str, title_col: str, stack_ph, stack_txt: str, st_ph, st_txt: str, st_col: str, name_sz: int, sub_sz: int, dy: int = 23, ) -> None: if show_cards: c_w, c_h = card_sz step_x = max(16, c_w // 2) fan_left, fan_top = bx - box_w // 2 + 10, by - c_h // 2 for ci, c_obj in enumerate(seat.hole_cards[:2]): c_ph = view.ctx.assets.get_card_photo(c_obj.image_filename, size=card_sz) if c_ph is not None: view._photo_refs.append(c_ph) view.arena_canvas.create_image(fan_left + ci * step_x, fan_top, image=c_ph, anchor="nw", tags="fg") txt_cx = bx + (c_w + step_x) // 2 + 2 else: txt_cx = bx view._place_goth_photo_or_fallback(txt_cx, by - dy, title_ph, title_txt, name_sz, title_col) view._place_goth_photo_or_fallback(txt_cx, by, stack_ph, stack_txt, sub_sz, ACCENT_GOLD) view._place_goth_photo_or_fallback(txt_cx, by + dy, st_ph, st_txt, sub_sz, st_col) @staticmethod def draw_board_and_pot( view, table_rect: tuple[int, int, int, int], top_obstacle_y: int, player_top_y: int ) -> None: tx1, ty1, tx2, ty2 = table_rect t_cx = (tx1 + tx2) // 2 compact = getattr(view, "is_compact_arena", False) if compact: felt_top = top_obstacle_y + 10 felt_bot = player_top_y - 8 else: t_h = max(260, ty2 - ty1) felt_top = max(ty1 + max(32, int(t_h * 0.14)), top_obstacle_y + 12) felt_bot = min(ty2 - max(24, int(t_h * 0.10)), player_top_y - 10) avail_h = max(124 if compact else 148, felt_bot - felt_top) avail_w = max(320, tx2 - tx1 - 16) base_cw, base_ch = PK_OVAL_BOARD_CARD scale_h = max(72, min(base_ch, avail_h - 48)) / max(1, base_ch) scale_w = (avail_w / 5.6) / max(1, base_cw) scale = min(1.0, scale_h, scale_w) card_w, card_h = max(52, int(round(base_cw * scale))), max(76, int(round(base_ch * scale))) card_sz = (card_w, card_h) gap = max(6, int(card_w * 0.12)) start_x = t_cx - (5 * card_w + 4 * gap) // 2 board_cy = felt_top + card_h // 2 + max(2, (avail_h - card_h - 46) // 3) card_top = board_cy - card_h // 2 for slot_i in range(5): sx = start_x + slot_i * (card_w + gap) if slot_i < len(view.engine.board): photo = view.ctx.assets.get_card_photo(view.engine.board[slot_i].image_filename, size=card_sz) if photo: view._photo_refs.append(photo) view.arena_canvas.create_image(sx, card_top, image=photo, anchor="nw", tags="fg") continue view.arena_canvas.create_rectangle( sx, card_top, sx + card_w, card_top + card_h, fill="#161220", outline=SUBTLE_BORDER, width=1, tags="fg", ) # In draw_board_and_pot(...) direkt beim Pot-Text: raw_pot_banner = t("pk_pot_banner", view.engine.total_pot) pot_parts = [p.strip() for p in raw_pot_banner.split("|") if p.strip()] pot_base = pot_parts[0] if pot_parts else raw_pot_banner.strip() street_map = { "PREFLOP": "Pre-Flop", "FLOP": "Flop", "TURN": "Turn", "RIVER": "River", "SHOWDOWN": "Showdown", } street_lbl = street_map.get(getattr(view.engine, "street", ""), "") pot_txt = f"{pot_base} • {street_lbl}" if (compact and street_lbl) else pot_base pot_cy = min(felt_bot - 18, max(board_cy + card_h // 2 + 24, (board_cy + card_h // 2 + felt_bot) // 2)) pot_sz = max(14 if compact else 15, FONT_SIZE_BODY - (1 if compact else 0)) pot_ph = view._make_goth_photo(pot_txt, pot_sz, FONT_COLOR_TITLE_MAIN) if pot_ph is not None: bw, bh = max(206, pot_ph.width() + 54), max(34, pot_ph.height() + 14) view._draw_framed_panel(t_cx, pot_cy, bw, bh, frame_style="gold") view.arena_canvas.create_image(t_cx, pot_cy, image=pot_ph, anchor="center", tags="fg") else: view._draw_framed_panel(t_cx, pot_cy, 220, 36, frame_style="gold") view._place_goth_photo_or_fallback(t_cx, pot_cy, None, pot_txt, pot_sz, ACCENT_GOLD) @classmethod def draw_human_hand(cls, view, cw: int, pad_x: int, player_zone_top: int, player_zone_bot: int) -> None: human = view.engine.human_seat compact = getattr(view, "is_compact_arena", False) avail_h = max(136 if compact else 140, player_zone_bot - player_zone_top) zone_cy = (player_zone_top + player_zone_bot) // 2 base_w, base_h = PLAYING_CARD_SIZE scale = max(82, min(base_h, avail_h - 48)) / max(1, base_h) card_w, card_h = max(56, int(round(base_w * scale))), int(round(base_h * scale)) card_sz = (card_w, card_h) p_photos = [ ph for card in human.hole_cards if (ph := view.ctx.assets.get_card_photo(card.image_filename, size=card_sz)) ] view._photo_refs.extend(p_photos) state_tag = t("pk_state_folded") if human.folded else (t("pk_state_allin") if human.all_in else "") role_str = cls.role_badge(view.engine, 0) or t("pk_role_player") lbl_txt = t("pk_player_bet_only", role_str, human.street_bet, human.total_committed, state_tag) is_my_turn = (view.engine.street != "SHOWDOWN" and view.engine.turn_pos == 0 and human.can_act) lbl_col = ERROR_FG if human.folded else FONT_COLOR_TITLE_MAIN lbl_sz = max(13 if compact else 14, FONT_SIZE_BODY - 1) lbl_ph = view._make_goth_photo(lbl_txt, lbl_sz, lbl_col, max_width=cw - 56) cards_gap = 10 if compact else 12 cards_span_w = len(p_photos) * card_w + max(0, len(p_photos) - 1) * cards_gap if p_photos else card_w * 2 txt_w = lbl_ph.width() if lbl_ph is not None else 220 box_w = max(280, min(cw - 24 if compact else 540, max(cards_span_w + 76, txt_w + 56))) box_h = min(avail_h, card_h + 48) box_cx = cw // 2 status_bdr = ERROR_FG if human.folded else (ACCENT_GOTH if human.all_in else None) view._draw_framed_panel( box_cx, zone_cy, box_w, box_h, frame_style="gold" if is_my_turn else "purple", status_border=status_bdr, ) if p_photos: c_start_x = box_cx - cards_span_w // 2 c_top_y = (zone_cy - 10) - card_h // 2 for idx, ph in enumerate(p_photos): view.arena_canvas.create_image( c_start_x + idx * (card_w + cards_gap), c_top_y, image=ph, anchor="nw", tags="fg" ) view._place_goth_photo_or_fallback(box_cx, zone_cy + box_h // 2 - 18, lbl_ph, lbl_txt, lbl_sz, lbl_col) # Vor view._draw_framed_panel(...) in draw_human_hand(...): ref_btn = view._card_ref_btn show_ref = ( ref_btn is not None and ref_btn.winfo_exists() and view._result_dialog is None and view._fx_overlay is None and view.engine.street != "SHOWDOWN" ) if compact and show_ref: ref_bw = ref_btn.pixel_width max_box_for_side_btn = cw - 2 * pad_x - ref_bw - 12 box_w = max(250, min(max_box_for_side_btn, max(cards_span_w + 52, txt_w + 36))) box_cx = pad_x + box_w // 2 + 2 else: box_cx = cw // 2 @staticmethod def collect_all_seats_reveal(eng, winner_names: list[str]) -> list[dict]: win_set = set(winner_names) entries = [] for s in eng.seats: if s.hole_cards: cat_id, _ = PokerEvaluator.evaluate_best(s.hole_cards + eng.board) entries.append({ "name": s.name, "cards": [[c.suit, c.rank] for c in s.hole_cards], "folded": s.folded, "cat_id": cat_id, "is_winner": (s.name in win_set) and (not s.folded), }) return entries @staticmethod def build_localized_popup_data(view, payload: dict) -> dict: hand_desc = ( t("pk_popup_fold_win") if payload["by_fold"] else t(f"pk_hand_{payload['winning_hand_cat'] or 0}") ) human_in_win = payload.get("human_in_winners", bool(payload.get("player_won"))) if human_in_win: headline = ( t("pk_popup_split_won", hand_desc) if payload["is_split"] else t("pk_popup_player_won", hand_desc) ) else: headline = t("pk_popup_bot_won", ", ".join(payload["winner_names"]), hand_desc) other_gains = [t("pk_popup_bot_gain", b_n, b_g) for b_n, b_g in payload["bot_gains"] if b_g > 0] lvl_to = payload["leveled_to"] lvl_str = ( t("bj_popup_level_up", lvl_to, Progression.get_prestige_title(lvl_to)) if lvl_to is not None else None ) seat_showdown = [] for s_rev in payload.get("seats_reveal", []): raw_c = t(f"pk_hand_{s_rev['cat_id']}").strip() short_c = raw_c.split(" (")[0] if len(raw_c) > 20 else raw_c seat_showdown.append({ "name": s_rev["name"], "cards": s_rev["cards"], "folded": s_rev["folded"], "is_winner": s_rev["is_winner"], "combo_text": f"{t('pk_bot_act_fold')} ({short_c})" if s_rev["folded"] else short_c, }) cur_gold = Database.get_poker_account(view.pk_account["id"])["gold"] return { "headline": headline, "player_won": payload["player_won"], "player_net": payload["player_net"], "other_gains": other_gains, "xp_gained": payload["xp_gained"], "new_level_title": lvl_str, "board_cards": payload.get("board_cards", []), "seat_showdown": seat_showdown, "can_continue": cur_gold >= view.cfg["min_entry"], "can_leave_menu": not view.devils_locked, }