the_goth_cardgame_collection/games/poker/views/poker_arena_renderer.py

324 lines
14 KiB
Python

# -*- coding: utf-8 -*-
# games/poker/views/poker_arena_renderer.py
from __future__ import annotations
import tkinter as tk
from resources.code_res.game_constants import (
ACCENT_GOLD,
ACCENT_GOTH,
ERROR_FG,
FG_COLOR,
FONT_COLOR_TITLE_MAIN,
FONT_SIZE_BODY,
MUTED_FG,
PLAYING_CARD_SIZE,
SUBTLE_BORDER,
)
from resources.code_res.i18n import t
from games.poker.models.poker_evaluator import PokerEvaluator
from models.database import Database
from models.progression import Progression
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 auf das Arena-Canvas."""
@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] + 24
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)
if show_bot_cards:
c_w, c_h = PK_BOT_SHOWDOWN_CARD
step_x = 22
fan_left, fan_top = bx - box_w // 2 + 16, 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=PK_BOT_SHOWDOWN_CARD)
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 + 4
view._place_goth_photo_or_fallback(txt_cx, by - 24, 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 + 24, st_ph, st_txt, sub_sz, st_col)
else:
view._place_goth_photo_or_fallback(bx, by - 23, title_ph, title_txt, name_sz, title_col)
view._place_goth_photo_or_fallback(bx, by, stack_ph, stack_txt, sub_sz, ACCENT_GOLD)
view._place_goth_photo_or_fallback(bx, by + 23, st_ph, st_txt, sub_sz, st_col)
return top_center_bottom
@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
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(148, felt_bot - felt_top)
base_cw, base_ch = PK_OVAL_BOARD_CARD
scale = max(84, min(base_ch, avail_h - 58)) / max(1, base_ch)
card_w, card_h = max(58, int(round(base_cw * scale))), int(round(base_ch * scale))
card_sz = (card_w, card_h)
gap = max(8, int(card_w * 0.13))
start_x = t_cx - (5 * card_w + 4 * gap) // 2
board_cy = felt_top + card_h // 2 + max(4, (avail_h - card_h - 52) // 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",
)
pot_txt = t("pk_pot_banner", view.engine.total_pot).split("|")[0].strip()
pot_cy = min(felt_bot - 20, max(board_cy + card_h // 2 + 28, (board_cy + card_h // 2 + felt_bot) // 2))
pot_sz = max(15, FONT_SIZE_BODY)
pot_ph = view._make_goth_photo(pot_txt, pot_sz, FONT_COLOR_TITLE_MAIN)
if pot_ph is not None:
bw, bh = max(230, pot_ph.width() + 64), max(38, 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, 240, 38, 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
avail_h = max(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(88, min(base_h, avail_h - 52)) / max(1, base_h)
card_w, card_h = max(60, int(round(base_w * scale))), int(round(base_h * scale))
card_sz = (card_w, card_h)
p_photos = []
for card in human.hole_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)
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(14, FONT_SIZE_BODY - 1)
lbl_ph = view._make_goth_photo(lbl_txt, lbl_sz, lbl_col)
cards_gap = 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 250
box_w = max(300, min(540, max(cards_span_w + 96, txt_w + 72)))
box_h = min(avail_h, card_h + 52)
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 - 11) - 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 - 20, lbl_ph, lbl_txt, lbl_sz, lbl_col)
ref_btn = view._card_ref_btn
if ref_btn is not None and ref_btn.winfo_exists():
if view._result_dialog is not None or view._fx_overlay is not None or view.engine.street == "SHOWDOWN":
ref_btn.place_forget()
else:
ref_bw, ref_bh = ref_btn.pixel_width, ref_btn.pixel_height
ref_x = box_cx + box_w // 2 + 16
if ref_x + ref_bw > cw - pad_x - 6:
ref_x = max(pad_x + 6, box_cx - box_w // 2 - ref_bw - 16)
ref_y = zone_cy + box_h // 2 - ref_bh - 6
ref_btn.place(x=ref_x, y=ref_y, width=ref_bw, height=ref_bh)
tk.Widget.tkraise(ref_btn)
@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,
}