the_goth_cardgame_collection/games/blackjack/views/blackjack_arena_renderer.py

348 lines
15 KiB
Python

# -*- 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,
FONT_SIZE_SMALL_MIN,
PLAYING_CARD_SIZE,
)
from resources.code_res.i18n import t
BJ_DEALER_CARD_SIZE = (86, 125)
BJ_COMPACT_BOT_ROW_H = 112
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 auf Desktop | Top-Leiste auf Kompakt) und Spielerhände."""
@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
compact = getattr(view, "is_compact_arena", False)
if compact:
top_reserve = (BJ_COMPACT_BOT_ROW_H + 10) if view.bots else 8
felt_top = ty1 + top_reserve
felt_bot = player_top_y - 8
avail_felt_h = max(140, felt_bot - felt_top)
d_scale = min(1.0, max(0.76, (avail_felt_h - 54) / max(1, BJ_DEALER_CARD_SIZE[1])))
dc_sz = (int(round(BJ_DEALER_CARD_SIZE[0] * d_scale)), int(round(BJ_DEALER_CARD_SIZE[1] * d_scale)))
dc_h = dc_sz[1]
d_cards_cy = felt_top + dc_h // 2 + max(4, (avail_felt_h - dc_h - 48) // 3)
else:
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_sz = BJ_DEALER_CARD_SIZE
dc_h = dc_sz[1]
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=dc_sz)
if c_idx == 1 and not reveal_dealer
else view.ctx.assets.get_card_photo(card.image_filename, size=dc_sz)
)
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, dc_sz, visible_step=34 if compact else 38, max_width=320
)
pot_txt = t("bj_pot_banner", view.engine.pot).split("|")[0].strip()
pot_cy = min(
felt_bot - 20,
max(d_cards_cy + dc_h // 2 + (26 if compact else 34), (d_cards_cy + dc_h // 2 + felt_bot) // 2),
)
pot_size = max(15 if compact else 16, FONT_SIZE_BODY + (0 if compact else 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(210, pot_ph.width() + 56), max(36, pot_ph.height() + 16)
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, 38, frame_style="gold")
view._place_goth_photo_or_fallback(t_cx, pot_cy, None, pot_txt, pot_size, ACCENT_GOLD)
@classmethod
def draw_bots_at_corners(
cls,
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
if getattr(view, "is_compact_arena", False):
cls._draw_bots_top_strip(view, cw, pad_x, pad_top, reveal_opponents)
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)
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)
info_txt = cls._bot_info_text(bot, reveal_opponents)
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="gold" if is_turn else "purple",
status_border=ERROR_FG if is_out else None,
)
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 = [
ph for card in bot.hand
if (ph := (
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)
))
]
view._photo_refs.extend(bot_photos)
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_bots_top_strip(cls, view, cw: int, pad_x: int, pad_top: int, reveal_opponents: bool) -> None:
"""Zeichnet 1..4 Kultisten im Kompaktmodus nebeneinander in einer oberen Reihe."""
n = len(view.bots)
gap = 6
avail_w = max(320, cw - 2 * pad_x)
box_w = min(204, (avail_w - (n - 1) * gap) // max(1, n))
box_h = BJ_COMPACT_BOT_ROW_H
start_x = (cw - (n * box_w + (n - 1) * gap)) // 2 + box_w // 2
by = pad_top + box_h // 2 + 2
mini_sz = (36, 52)
name_sz = max(12, FONT_SIZE_SMALL_MIN)
sub_sz = max(11, FONT_SIZE_SMALL_MIN - 1)
max_tw = max(72, box_w - 18)
for bot_i, bot in enumerate(view.bots):
bx = start_x + bot_i * (box_w + gap)
is_turn = (bot_i == view.active_bot_idx)
is_out = (bot.folded or bot.busted)
view._draw_framed_panel(
bx, by, box_w, box_h,
frame_style="gold" if is_turn else "purple",
status_border=ERROR_FG if is_out else None,
)
disp_name = f"{bot.name} ({bot.gold}g)"
name_col = ACCENT_GOLD if is_turn else ACCENT_GOTH
info_txt = cls._bot_info_text(bot, reveal_opponents)
info_col = ERROR_FG if is_out else FG_COLOR
name_ph = view._make_goth_photo(disp_name, name_sz, name_col, max_width=max_tw)
info_ph = view._make_goth_photo(info_txt, sub_sz, info_col, max_width=max_tw)
view._place_goth_photo_or_fallback(bx, by - box_h // 2 + 16, name_ph, disp_name, name_sz, name_col)
show_faces = reveal_opponents or bot.busted
bot_photos = [
ph for card in bot.hand
if (ph := (
view.ctx.assets.get_card_photo(card.image_filename, size=mini_sz)
if show_faces else view.ctx.assets.get_card_back_photo(color=view.cfg["back_color"], size=mini_sz)
))
]
view._photo_refs.extend(bot_photos)
if bot_photos:
view._draw_fanned_cards_on_canvas(bx, by + 2, bot_photos, mini_sz, visible_step=16, max_width=box_w - 20)
view._place_goth_photo_or_fallback(bx, by + box_h // 2 - 14, info_ph, info_txt, sub_sz, info_col)
@staticmethod
def _bot_info_text(bot, reveal_opponents: bool) -> str:
if reveal_opponents and bot.hand:
b_val, _ = HandEvaluator.evaluate(bot.hand)
if bot.folded:
return t("bj_rank_folded", b_val)
if bot.busted:
return t("bj_rank_busted", b_val)
return t("bj_bot_showdown_val", b_val, bot.bet_in_pot)
if bot.status_text:
return bot.status_text
if bot.hand:
return t("bj_bot_cards_hidden", len(bot.hand), bot.bet_in_pot)
return ""
@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
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
if len(hands) == 1:
h = hands[0]
base_w, base_h = PLAYING_CARD_SIZE
scale = max(84, min(base_h, avail_h - 48)) / max(1, base_h)
card_sz = (max(58, int(round(base_w * scale))), int(round(base_h * scale)))
p_photos = [
ph for card in h.cards
if (ph := view.ctx.assets.get_card_photo(card.image_filename, size=card_sz))
]
view._photo_refs.extend(p_photos)
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(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)
step = 34 if compact else 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 220
box_w = max(280, min(cw - 20 if compact else 560, max(fan_w + 72, txt_w + 56)))
box_h = min(avail_h, card_sz[1] + 48)
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 - 10, p_photos, card_sz, visible_step=step, max_width=box_w - 36
)
view._place_goth_photo_or_fallback(cw // 2, zone_cy + box_h // 2 - 18, lbl_ph, lbl_txt, lbl_sz, lbl_col)
return
# Split-Hände nebeneinander (auf 658px Breite sauber eingepasst)
base_w, base_h = BJ_SPLIT_CARD_SIZE
scale = max(76, min(base_h, avail_h - 46)) / max(1, base_h)
card_sz = (max(52, int(round(base_w * scale))), int(round(base_h * scale)))
box_h = min(avail_h, card_sz[1] + 46)
lbl_sz = max(12 if compact else 13, FONT_SIZE_BODY - 2)
n_hands = len(hands)
max_allowed_box_w = (cw - 24 - (n_hands - 1) * 10) // max(1, n_hands) if compact else 320
prepared_splits = []
max_box_w = min(228, max_allowed_box_w)
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, max_width=max_allowed_box_w - 24)
txt_w = lbl_ph.width() if lbl_ph is not None else 160
sp_fan_w = card_sz[0] + max(0, len(h.cards) - 1) * 24
max_box_w = max(max_box_w, min(max_allowed_box_w, max(sp_fan_w + 48, txt_w + 40)))
prepared_splits.append((h, is_active, is_out, lbl_txt, lbl_col, lbl_ph))
slot_gap = max_box_w + (10 if compact else 14)
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 = [
ph for card in h.cards
if (ph := view.ctx.assets.get_card_photo(card.image_filename, size=card_sz))
]
view._photo_refs.extend(sp_photos)
if sp_photos:
view._draw_fanned_cards_on_canvas(
h_cx, zone_cy - 9, sp_photos, card_sz, visible_step=24, max_width=max_box_w - 28
)
view._place_goth_photo_or_fallback(h_cx, zone_cy + box_h // 2 - 17, lbl_ph, lbl_txt, lbl_sz, lbl_col)