244 lines
10 KiB
Python
244 lines
10 KiB
Python
|
|
# -*- coding: utf-8 -*-
|
||
|
|
# games/blackjack/views/blackjack_turn_driver.py
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from games.blackjack.models.bot_seat import BlackjackBotSeat
|
||
|
|
from games.blackjack.models.hand_evaluator import HandEvaluator
|
||
|
|
from games.blackjack.views.blackjack_showdown_presenter import BlackjackShowdownPresenter
|
||
|
|
from models.database import Database
|
||
|
|
from resources.code_res.game_constants import BJ_BOT_TURN_DELAY_MS
|
||
|
|
from resources.code_res.i18n import t
|
||
|
|
from view.widgets.image_button import ImageButton
|
||
|
|
|
||
|
|
STEP_MITGEHEN = "MITGEHEN"
|
||
|
|
STEP_ZIEHEN = "ZIEHEN"
|
||
|
|
STEP_ERHOEHEN = "ERHOEHEN"
|
||
|
|
|
||
|
|
|
||
|
|
class BlackjackTurnDriver:
|
||
|
|
"""Steuert die Aktions-Buttons des Spielers und die KI-Zugschleife der Bots am Blackjack-Tisch."""
|
||
|
|
|
||
|
|
@staticmethod
|
||
|
|
def render_tree_step(view) -> None:
|
||
|
|
view.bj_account = Database.get_blackjack_account(view.bj_account["id"])
|
||
|
|
wallet = view.bj_account["gold"]
|
||
|
|
hand = view.engine.player_hands[view.active_hand_idx]
|
||
|
|
|
||
|
|
btn_norm = view.ctx.assets.raw_btn_default
|
||
|
|
btn_purp = view.ctx.assets.raw_btn_purple
|
||
|
|
btn_fire = view.ctx.assets.raw_btn_fire or btn_purp
|
||
|
|
primary_btns: list[ImageButton] = []
|
||
|
|
overflow_btns: list[ImageButton] = []
|
||
|
|
|
||
|
|
if view.turn_step == STEP_MITGEHEN:
|
||
|
|
to_call = min(wallet, max(0, view.engine.highest_bet - hand.bet_in_pot))
|
||
|
|
primary_btns.append(
|
||
|
|
ImageButton(
|
||
|
|
view.bottom_bar, t("bj_tree_btn_call_yes", to_call),
|
||
|
|
btn_fire, btn_fire, lambda: view._on_step1_call_yes(to_call),
|
||
|
|
sound_mgr=view.ctx.sounds, width=162, height=30, style="fire",
|
||
|
|
hover_text=lambda: t("bj_tip_call_yes"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
overflow_btns.append(
|
||
|
|
ImageButton(
|
||
|
|
view.bottom_bar, t("bj_tree_btn_call_no_lose"),
|
||
|
|
btn_norm, btn_purp, view._on_fold_hand,
|
||
|
|
sound_mgr=view.ctx.sounds, width=162, height=30, style="amethyst",
|
||
|
|
hover_text=lambda: t("bj_tip_fold"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
view._set_bottom_buttons(primary_btns, overflow_buttons=overflow_btns)
|
||
|
|
return
|
||
|
|
|
||
|
|
if view.turn_step == STEP_ZIEHEN:
|
||
|
|
primary_btns.append(
|
||
|
|
ImageButton(
|
||
|
|
view.bottom_bar, t("bj_tree_btn_draw_yes"),
|
||
|
|
btn_fire, btn_fire, view._on_step2_draw_yes,
|
||
|
|
sound_mgr=view.ctx.sounds, width=138, height=30, style="fire",
|
||
|
|
hover_text=lambda: t("bj_tip_draw_yes"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
if view.engine.can_double_down(view.active_hand_idx, wallet):
|
||
|
|
dd_cost = view.engine.get_double_down_cost(view.active_hand_idx)
|
||
|
|
primary_btns.append(
|
||
|
|
ImageButton(
|
||
|
|
view.bottom_bar, t("bj_double_btn", dd_cost),
|
||
|
|
btn_norm, btn_purp, view._on_step2_double_down,
|
||
|
|
sound_mgr=view.ctx.sounds, width=132, height=30,
|
||
|
|
style="amethyst", hover_text=lambda: t("bj_tip_double"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
if view.engine.can_split_hand(view.active_hand_idx, wallet):
|
||
|
|
primary_btns.append(
|
||
|
|
ImageButton(
|
||
|
|
view.bottom_bar, t("bj_split_btn", view.engine.highest_bet),
|
||
|
|
btn_norm, btn_purp, view._on_step2_split,
|
||
|
|
sound_mgr=view.ctx.sounds, width=124, height=30,
|
||
|
|
style="amethyst", hover_text=lambda: t("bj_tip_split"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
overflow_btns.append(
|
||
|
|
ImageButton(
|
||
|
|
view.bottom_bar, t("bj_tree_btn_draw_no"),
|
||
|
|
btn_norm, btn_purp, view._on_step2_draw_no,
|
||
|
|
sound_mgr=None, width=134, height=30, style="amethyst",
|
||
|
|
hover_text=lambda: t("bj_tip_draw_no"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
overflow_btns.append(
|
||
|
|
ImageButton(
|
||
|
|
view.bottom_bar, t("bj_fold_btn"),
|
||
|
|
btn_norm, btn_purp, view._on_fold_hand,
|
||
|
|
sound_mgr=view.ctx.sounds, width=110, height=30, style="amethyst",
|
||
|
|
hover_text=lambda: t("bj_tip_fold"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
view._set_bottom_buttons(primary_btns, overflow_buttons=overflow_btns)
|
||
|
|
return
|
||
|
|
|
||
|
|
if view.turn_step == STEP_ERHOEHEN:
|
||
|
|
for chip in [c for c in view._get_current_level_chips() if wallet >= c]:
|
||
|
|
primary_btns.append(
|
||
|
|
ImageButton(
|
||
|
|
view.bottom_bar, f"+{chip}g",
|
||
|
|
btn_fire, btn_fire, lambda c=chip: view._on_step3_raise_yes(c),
|
||
|
|
sound_mgr=view.ctx.sounds, width=76, height=30, style="fire",
|
||
|
|
hover_text=lambda: t("bj_tip_raise_chip"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
overflow_btns.append(
|
||
|
|
ImageButton(
|
||
|
|
view.bottom_bar, t("bj_tree_btn_raise_no"),
|
||
|
|
btn_norm, btn_purp, view._finish_current_hand_turn,
|
||
|
|
sound_mgr=view.ctx.sounds, width=162, height=30, style="amethyst",
|
||
|
|
hover_text=lambda: t("bj_tip_raise_no"),
|
||
|
|
)
|
||
|
|
)
|
||
|
|
view._set_bottom_buttons(primary_btns, overflow_buttons=overflow_btns)
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def start_bots_cycle(cls, view) -> None:
|
||
|
|
view._set_bottom_buttons([])
|
||
|
|
if not view.bots or not view.engine.any_contender_alive(view.bots):
|
||
|
|
view._on_table_pass_completed()
|
||
|
|
return
|
||
|
|
view.active_bot_idx = 0
|
||
|
|
view._persist_state()
|
||
|
|
cls.step_next_bot(view)
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def step_next_bot(cls, view) -> None:
|
||
|
|
if not view._canvas_alive():
|
||
|
|
return
|
||
|
|
while view.active_bot_idx < len(view.bots):
|
||
|
|
bot = view.bots[view.active_bot_idx]
|
||
|
|
if bot.is_active_in_pot and (
|
||
|
|
not view.settle_only_mode
|
||
|
|
or (bot.gold > 0 and bot.bet_in_pot < view.engine.highest_bet)
|
||
|
|
):
|
||
|
|
break
|
||
|
|
view.active_bot_idx += 1
|
||
|
|
|
||
|
|
if view.active_bot_idx >= len(view.bots):
|
||
|
|
view.active_bot_idx = -1
|
||
|
|
view._persist_state()
|
||
|
|
view._on_table_pass_completed()
|
||
|
|
return
|
||
|
|
|
||
|
|
bot = view.bots[view.active_bot_idx]
|
||
|
|
view._persist_state()
|
||
|
|
view._render_table(False, False)
|
||
|
|
view._turn_job = view.ctx.root.after(
|
||
|
|
BJ_BOT_TURN_DELAY_MS, lambda b=bot: cls.execute_bot_action(view, b)
|
||
|
|
)
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def execute_bot_action(cls, view, bot: BlackjackBotSeat) -> None:
|
||
|
|
if not view._canvas_alive():
|
||
|
|
return
|
||
|
|
upcard = view.engine.dealer_hand[0]
|
||
|
|
dyn_chips = view._get_current_level_chips()
|
||
|
|
rem_cards = view.engine.deck.remaining()
|
||
|
|
to_call = min(bot.gold, max(0, view.engine.highest_bet - bot.bet_in_pot))
|
||
|
|
|
||
|
|
if to_call > 0:
|
||
|
|
if not view.bot_ai.decide_step1_mitgehen(
|
||
|
|
bot, view.engine.highest_bet, upcard, rem_cards
|
||
|
|
):
|
||
|
|
bot.folded = bot.cards_locked = True
|
||
|
|
bot.status_text = t("bj_bot_action_fold")
|
||
|
|
cls._finish_bot_step(view)
|
||
|
|
return
|
||
|
|
view.ctx.sounds.play("click")
|
||
|
|
view.engine.bot_add_gold_to_pot(bot, to_call)
|
||
|
|
if view.settle_only_mode:
|
||
|
|
bot.status_text = t("bj_bot_action_call", to_call)
|
||
|
|
cls._finish_bot_step(view)
|
||
|
|
return
|
||
|
|
|
||
|
|
drew_card = False
|
||
|
|
if not bot.cards_locked and not bot.doubled:
|
||
|
|
if view.bot_ai.wants_double_down(
|
||
|
|
bot, upcard, view.engine.highest_bet, rem_cards
|
||
|
|
):
|
||
|
|
dd_cost = (
|
||
|
|
max(view.engine.highest_bet, bot.bet_in_pot) * 2 - bot.bet_in_pot
|
||
|
|
)
|
||
|
|
paid = view.engine.bot_add_gold_to_pot(bot, dd_cost)
|
||
|
|
view.ctx.sounds.play("click")
|
||
|
|
view.engine.bot_draw_card(bot)
|
||
|
|
bot.doubled = bot.cards_locked = True
|
||
|
|
if HandEvaluator.is_triple_seven(bot.hand):
|
||
|
|
view._conclude_with_payload(
|
||
|
|
BlackjackShowdownPresenter.trigger_instant_777(view, False, bot)
|
||
|
|
)
|
||
|
|
return
|
||
|
|
b_val, _ = HandEvaluator.evaluate(bot.hand)
|
||
|
|
bot.status_text = (
|
||
|
|
t("bj_bot_action_bust", b_val)
|
||
|
|
if bot.busted
|
||
|
|
else t("bj_bot_action_double", paid)
|
||
|
|
)
|
||
|
|
cls._finish_bot_step(view)
|
||
|
|
return
|
||
|
|
|
||
|
|
if view.bot_ai.decide_step2_ziehen(bot, upcard, rem_cards):
|
||
|
|
view.ctx.sounds.play("click")
|
||
|
|
view.engine.bot_draw_card(bot)
|
||
|
|
drew_card = True
|
||
|
|
if HandEvaluator.is_triple_seven(bot.hand):
|
||
|
|
view._conclude_with_payload(
|
||
|
|
BlackjackShowdownPresenter.trigger_instant_777(view, False, bot)
|
||
|
|
)
|
||
|
|
return
|
||
|
|
if bot.busted:
|
||
|
|
b_val, _ = HandEvaluator.evaluate(bot.hand)
|
||
|
|
bot.status_text = t("bj_bot_action_bust", b_val)
|
||
|
|
cls._finish_bot_step(view)
|
||
|
|
return
|
||
|
|
|
||
|
|
raise_amt = view.bot_ai.decide_step3_erhoehen(
|
||
|
|
bot, dyn_chips, rem_cards, upcard
|
||
|
|
)
|
||
|
|
if raise_amt > 0:
|
||
|
|
view.ctx.sounds.play("click")
|
||
|
|
view.engine.bot_add_gold_to_pot(bot, raise_amt)
|
||
|
|
bot.status_text = t("bj_bot_action_bet", raise_amt)
|
||
|
|
elif drew_card:
|
||
|
|
bot.status_text = t("bj_bot_action_hit", len(bot.hand))
|
||
|
|
elif to_call > 0:
|
||
|
|
bot.status_text = t("bj_bot_action_call", to_call)
|
||
|
|
else:
|
||
|
|
bot.status_text = t("bj_bot_action_ready", len(bot.hand))
|
||
|
|
cls._finish_bot_step(view)
|
||
|
|
|
||
|
|
@classmethod
|
||
|
|
def _finish_bot_step(cls, view) -> None:
|
||
|
|
view.active_bot_idx += 1
|
||
|
|
view._persist_state()
|
||
|
|
view._render_table(False, False)
|
||
|
|
view._turn_job = view.ctx.root.after(
|
||
|
|
BJ_BOT_TURN_DELAY_MS, lambda: cls.step_next_bot(view)
|
||
|
|
)
|