# -*- coding: utf-8 -*- # games/blackjack/views/blackjack_arena_renderer.py from __future__ import annotations from games.blackjack.models.hand_evaluator import HandEvaluator from games.blackjack.models.player_hand import PlayerHand from resources.code_res.game_constants import ( ACCENT_GOLD, ACCENT_GOTH, BJ_BOT_CARD_SIZE, BJ_SPLIT_CARD_SIZE, ERROR_FG, FG_COLOR, FONT_COLOR_TITLE_MAIN, FONT_SIZE_BODY, PLAYING_CARD_SIZE, ) from resources.code_res.i18n import t BJ_DEALER_CARD_SIZE = (86, 125) BJ_SEAT_LAYOUT_BY_COUNT = { 1: (2,), 2: (1, 2), 3: (1, 2, 3), 4: (0, 1, 2, 3), } class BlackjackArenaRenderer: """Zeichnet Dealer, Pot, Kultisten-Ecken und Spielerhände (inkl. Split) auf das Arena-Canvas.""" @staticmethod def hand_state_suffix(hand: PlayerHand) -> str: if hand.busted: return t("bj_state_busted") if hand.folded: return t("bj_state_folded") if hand.doubled or hand.cards_locked: return t("bj_state_locked") return "" @staticmethod def draw_dealer_and_pot( view, table_rect: tuple[int, int, int, int], player_top_y: int, reveal_dealer: bool, ) -> None: tx1, ty1, tx2, ty2 = table_rect t_cx = (tx1 + tx2) // 2 t_h = max(260, ty2 - ty1) felt_top = ty1 + max(36, int(t_h * 0.15)) felt_bot = min(ty2 - max(28, int(t_h * 0.12)), player_top_y - 12) _dc_w, dc_h = BJ_DEALER_CARD_SIZE avail_felt_h = max(170, felt_bot - felt_top) d_cards_cy = felt_top + min(dc_h // 2 + 12, int(avail_felt_h * 0.34)) d_photos = [] for c_idx, card in enumerate(view.engine.dealer_hand): photo = ( view.ctx.assets.get_card_back_photo(color=view.cfg["back_color"], size=BJ_DEALER_CARD_SIZE) if c_idx == 1 and not reveal_dealer else view.ctx.assets.get_card_photo(card.image_filename, size=BJ_DEALER_CARD_SIZE) ) if photo: view._photo_refs.append(photo) d_photos.append(photo) if d_photos: view._draw_fanned_cards_on_canvas( t_cx, d_cards_cy, d_photos, BJ_DEALER_CARD_SIZE, visible_step=38, max_width=340 ) pot_txt = t("bj_pot_banner", view.engine.pot).split("|")[0].strip() pot_cy = min( felt_bot - 22, max(d_cards_cy + dc_h // 2 + 34, (d_cards_cy + dc_h // 2 + felt_bot) // 2), ) pot_size = max(16, FONT_SIZE_BODY + 1) pot_ph = view._make_goth_photo(pot_txt, size_px=pot_size, color=FONT_COLOR_TITLE_MAIN) if pot_ph is not None: bw, bh = max(230, pot_ph.width() + 64), max(40, pot_ph.height() + 18) 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, 240, 40, frame_style="gold") view._place_goth_photo_or_fallback(t_cx, pot_cy, None, pot_txt, pot_size, ACCENT_GOLD) @staticmethod def draw_bots_at_corners( view, cw: int, ch: int, pad_x: int, pad_top: int, pad_bot: int, table_rect: tuple[int, int, int, int], reveal_opponents: bool, ) -> None: if not view.bots: return tx1, ty1, tx2, ty2 = table_rect bc_w, bc_h = BJ_BOT_CARD_SIZE card_scale = 0.90 if ch < 560 else 1.0 cur_card_sz = (max(52, int(bc_w * card_scale)), max(76, int(bc_h * card_scale))) base_box_w = max(198, cur_card_sz[0] + 114) box_h = cur_card_sz[1] + 90 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 + 14: mid_y = (top_cy + bot_cy) // 2 top_cy, bot_cy = mid_y - (box_h + 14) // 2, mid_y + (box_h + 14) // 2 slot_indices = BJ_SEAT_LAYOUT_BY_COUNT.get(len(view.bots), (0, 1, 2, 3)) name_sz, sub_sz = max(14, FONT_SIZE_BODY - 1), max(13, FONT_SIZE_BODY - 2) for bot_i, bot in enumerate(view.bots): coord_idx = slot_indices[bot_i] is_left_side = coord_idx in (0, 1) by = bot_cy if coord_idx in (0, 3) else top_cy is_turn = (bot_i == view.active_bot_idx) is_out = (bot.folded or bot.busted) frame_style = "gold" if is_turn else "purple" status_border = ERROR_FG if is_out else None disp_name = t("bj_bot_rebuy", bot.name) if bot.rebought else bot.name name_col = ACCENT_GOLD if is_turn else ACCENT_GOTH gold_txt = t("bj_bot_gold_lbl", bot.gold) if reveal_opponents and bot.hand: b_val, _ = HandEvaluator.evaluate(bot.hand) if bot.folded: info_txt = t("bj_rank_folded", b_val) elif bot.busted: info_txt = t("bj_rank_busted", b_val) else: info_txt = t("bj_bot_showdown_val", b_val, bot.bet_in_pot) elif bot.status_text: info_txt = bot.status_text elif bot.hand: info_txt = t("bj_bot_cards_hidden", len(bot.hand), bot.bet_in_pot) else: info_txt = "" info_col = ERROR_FG if is_out else FG_COLOR name_ph = view._make_goth_photo(disp_name, name_sz, name_col, max_width=220) gold_ph = view._make_goth_photo(gold_txt, sub_sz, ACCENT_GOLD) info_ph = view._make_goth_photo(info_txt, sub_sz, info_col, max_width=220) widest_txt = max((ph.width() for ph in (name_ph, gold_ph, info_ph) if ph), default=140) box_w = max(base_box_w, min(268, widest_txt + 54)) bx = ( max(pad_x + box_w // 2, tx1 + int(box_w * 0.36)) if is_left_side else min(cw - pad_x - box_w // 2, tx2 - int(box_w * 0.36)) ) view._draw_framed_panel(bx, by, box_w, box_h, frame_style=frame_style, status_border=status_border) top_y, bot_y = by - box_h // 2, by + box_h // 2 view._place_goth_photo_or_fallback(bx, top_y + 23, name_ph, disp_name, name_sz, name_col) view._place_goth_photo_or_fallback(bx, top_y + 42, gold_ph, gold_txt, sub_sz, ACCENT_GOLD) show_faces = reveal_opponents or bot.busted bot_photos = [] for card in bot.hand: photo = ( view.ctx.assets.get_card_photo(card.image_filename, size=cur_card_sz) if show_faces else view.ctx.assets.get_card_back_photo(color=view.cfg["back_color"], size=cur_card_sz) ) if photo: view._photo_refs.append(photo) bot_photos.append(photo) if bot_photos: view._draw_fanned_cards_on_canvas( bx, by + 6, bot_photos, cur_card_sz, visible_step=25, max_width=box_w - 36 ) view._place_goth_photo_or_fallback(bx, bot_y - 21, info_ph, info_txt, sub_sz, info_col) @classmethod def draw_player_hands(cls, view, cw: int, player_zone_top: int, player_zone_bot: int) -> None: hands = view.engine.player_hands if not hands: return avail_h = max(140, player_zone_bot - player_zone_top) zone_cy = (player_zone_top + player_zone_bot) // 2 if len(hands) == 1: h = hands[0] base_w, base_h = PLAYING_CARD_SIZE scale = max(88, min(base_h, avail_h - 52)) / max(1, base_h) card_sz = (max(60, int(round(base_w * scale))), int(round(base_h * scale))) p_photos = [] for card in h.cards: photo = view.ctx.assets.get_card_photo(card.image_filename, size=card_sz) if photo: view._photo_refs.append(photo) p_photos.append(photo) p_val, p_soft = HandEvaluator.evaluate(h.cards) soft_hint = t("bj_soft_hint", p_val) if p_soft and p_val < 21 else "" lbl_txt = t("bj_player_total", p_val, soft_hint, h.bet_in_pot, cls.hand_state_suffix(h)) is_player_turn = (view.engine.state == "ACTION_LOOP" and view.active_bot_idx == -1) is_out = (h.busted or h.folded) lbl_col = ERROR_FG if is_out else FONT_COLOR_TITLE_MAIN lbl_sz = max(14, FONT_SIZE_BODY - 1) lbl_ph = view._make_goth_photo(lbl_txt, lbl_sz, lbl_col) step = 36 fan_w = card_sz[0] + max(0, len(p_photos) - 1) * step txt_w = lbl_ph.width() if lbl_ph is not None else 240 box_w = max(300, min(560, max(fan_w + 96, txt_w + 72))) box_h = min(avail_h, card_sz[1] + 52) view._draw_framed_panel( cw // 2, zone_cy, box_w, box_h, frame_style="gold" if is_player_turn and not is_out else "purple", status_border=ERROR_FG if is_out else None, ) if p_photos: view._draw_fanned_cards_on_canvas( cw // 2, zone_cy - 11, p_photos, card_sz, visible_step=step, max_width=box_w - 44 ) view._place_goth_photo_or_fallback(cw // 2, zone_cy + box_h // 2 - 20, lbl_ph, lbl_txt, lbl_sz, lbl_col) return # Split-Hände nebeneinander base_w, base_h = BJ_SPLIT_CARD_SIZE scale = max(80, min(base_h, avail_h - 48)) / max(1, base_h) card_sz = (max(54, int(round(base_w * scale))), int(round(base_h * scale))) box_h = min(avail_h, card_sz[1] + 48) lbl_sz = max(13, FONT_SIZE_BODY - 2) prepared_splits = [] max_box_w = 228 for idx, h in enumerate(hands): is_active = ( view.engine.state == "ACTION_LOOP" and view.active_bot_idx == -1 and idx == view.active_hand_idx ) is_out = (h.busted or h.folded) p_val, p_soft = HandEvaluator.evaluate(h.cards) soft_hint = t("bj_soft_hint", p_val) if p_soft and p_val < 21 else "" lbl_txt = t("bj_hand_box_title", idx + 1, p_val, soft_hint, h.bet_in_pot, cls.hand_state_suffix(h)) lbl_col = ACCENT_GOLD if is_active else (ERROR_FG if is_out else FG_COLOR) lbl_ph = view._make_goth_photo(lbl_txt, lbl_sz, lbl_col) txt_w = lbl_ph.width() if lbl_ph is not None else 180 sp_fan_w = card_sz[0] + max(0, len(h.cards) - 1) * 28 max_box_w = max(max_box_w, max(228, min(320, max(sp_fan_w + 64, txt_w + 60)))) prepared_splits.append((h, is_active, is_out, lbl_txt, lbl_col, lbl_ph)) n_hands = len(hands) slot_gap = max(max_box_w + 14, min(310, (cw - 360) // max(1, n_hands))) start_cx = cw // 2 - ((n_hands - 1) * slot_gap) // 2 for idx, (h, is_active, is_out, lbl_txt, lbl_col, lbl_ph) in enumerate(prepared_splits): h_cx = start_cx + idx * slot_gap view._draw_framed_panel( h_cx, zone_cy, max_box_w, box_h, frame_style="gold" if is_active else "purple", status_border=ERROR_FG if is_out else None, ) sp_photos = [] for card in h.cards: photo = view.ctx.assets.get_card_photo(card.image_filename, size=card_sz) if photo: view._photo_refs.append(photo) sp_photos.append(photo) if sp_photos: view._draw_fanned_cards_on_canvas( h_cx, zone_cy - 10, sp_photos, card_sz, visible_step=28, max_width=max_box_w - 36 ) view._place_goth_photo_or_fallback(h_cx, zone_cy + box_h // 2 - 18, lbl_ph, lbl_txt, lbl_sz, lbl_col)