2022-09-10 21:01:34 +00:00
|
|
|
import json
|
|
|
|
import random
|
|
|
|
from enum import Enum
|
2022-11-15 09:19:08 +00:00
|
|
|
from math import floor
|
|
|
|
|
2022-09-10 21:01:34 +00:00
|
|
|
from flask import g
|
|
|
|
|
2022-12-14 19:30:05 +00:00
|
|
|
from files.classes.casino_game import CasinoGame
|
2024-03-02 17:12:02 +00:00
|
|
|
from files.classes.currency_logs import CurrencyLog
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.casino import distribute_wager_badges
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2023-05-03 22:12:40 +00:00
|
|
|
from sqlalchemy.orm.exc import MultipleResultsFound
|
|
|
|
|
2022-09-10 21:01:34 +00:00
|
|
|
class BlackjackStatus(str, Enum):
|
2022-10-28 23:39:31 +00:00
|
|
|
PLAYING = "PLAYING"
|
|
|
|
STAYED = "STAYED"
|
|
|
|
PUSHED = "PUSHED"
|
|
|
|
WON = "WON"
|
|
|
|
LOST = "LOST"
|
|
|
|
BLACKJACK = "BLACKJACK"
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BlackjackAction(str, Enum):
|
2022-10-28 23:39:31 +00:00
|
|
|
DEAL = "DEAL"
|
|
|
|
HIT = "HIT"
|
|
|
|
STAY = "STAY"
|
|
|
|
DOUBLE_DOWN = "DOUBLE_DOWN"
|
|
|
|
BUY_INSURANCE = "BUY_INSURANCE"
|
2023-09-18 16:31:58 +00:00
|
|
|
SPLIT = 'SPLIT'
|
|
|
|
HIT_SPLIT = 'HIT_SPLIT'
|
|
|
|
STAY_SPLIT = 'STAY_SPLIT'
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
ranks = ("2", "3", "4", "5", "6", "7", "8", "9", "X", "J", "Q", "K", "A")
|
|
|
|
suits = ("S", "H", "C", "D")
|
|
|
|
deck = [rank + suit for rank in ranks for suit in suits]
|
|
|
|
deck_count = 4
|
|
|
|
minimum_bet = 5
|
|
|
|
|
|
|
|
|
|
|
|
def get_initial_state():
|
2022-10-28 23:39:31 +00:00
|
|
|
return {
|
|
|
|
"player": [],
|
2023-09-18 16:31:58 +00:00
|
|
|
"player_split": [],
|
|
|
|
"player_split_value": 0,
|
2022-10-28 23:39:31 +00:00
|
|
|
"player_value": 0,
|
|
|
|
"dealer": [],
|
|
|
|
"dealer_value": 0,
|
2023-09-18 16:31:58 +00:00
|
|
|
"has_player_split": False,
|
2022-10-28 23:39:31 +00:00
|
|
|
"player_bought_insurance": False,
|
|
|
|
"player_doubled_down": False,
|
|
|
|
"status": BlackjackStatus.PLAYING,
|
2023-09-18 16:31:58 +00:00
|
|
|
"status_split": BlackjackStatus.PLAYING,
|
2023-02-24 08:53:03 +00:00
|
|
|
"actions": [],
|
2022-10-28 23:39:31 +00:00
|
|
|
"wager": {
|
|
|
|
"amount": 0,
|
|
|
|
"currency": "coins"
|
|
|
|
},
|
2023-09-18 16:31:58 +00:00
|
|
|
"payout": 0,
|
2022-10-28 23:39:31 +00:00
|
|
|
}
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def build_casino_game(gambler, wager, currency):
|
2022-10-28 23:39:31 +00:00
|
|
|
initial_state = get_initial_state()
|
|
|
|
initial_state['wager']['amount'] = wager
|
|
|
|
initial_state['wager']['currency'] = currency
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-12-14 19:30:05 +00:00
|
|
|
casino_game = CasinoGame()
|
2022-10-28 23:39:31 +00:00
|
|
|
casino_game.user_id = gambler.id
|
|
|
|
casino_game.currency = currency
|
|
|
|
casino_game.wager = wager
|
|
|
|
casino_game.winnings = 0
|
|
|
|
casino_game.kind = 'blackjack'
|
|
|
|
casino_game.game_state = json.dumps(initial_state)
|
|
|
|
casino_game.active = True
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(casino_game)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return casino_game
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_active_twentyone_game(gambler):
|
2023-05-03 22:12:40 +00:00
|
|
|
try:
|
|
|
|
return g.db.query(CasinoGame).filter(
|
2022-12-14 19:30:05 +00:00
|
|
|
CasinoGame.active == True,
|
|
|
|
CasinoGame.kind == 'blackjack',
|
2023-05-03 22:12:40 +00:00
|
|
|
CasinoGame.user_id == gambler.id).one_or_none()
|
|
|
|
except MultipleResultsFound:
|
|
|
|
games = g.db.query(CasinoGame).filter(
|
|
|
|
CasinoGame.active == True,
|
|
|
|
CasinoGame.kind == 'blackjack',
|
|
|
|
CasinoGame.user_id == gambler.id).all()
|
|
|
|
for game in games:
|
|
|
|
g.db.delete(game)
|
|
|
|
g.db.commit()
|
|
|
|
return None
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
def get_active_twentyone_game_state(gambler):
|
2022-10-28 23:39:31 +00:00
|
|
|
active_game = get_active_twentyone_game(gambler)
|
2022-10-30 00:32:40 +00:00
|
|
|
full_state = active_game.game_state_json
|
2022-10-28 23:39:31 +00:00
|
|
|
return remove_exploitable_information(full_state)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def charge_gambler(gambler, amount, currency):
|
2024-03-09 09:44:22 +00:00
|
|
|
charged = gambler.charge_account(currency, amount)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if not charged:
|
|
|
|
raise Exception("Gambler cannot afford charge.")
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def create_new_game(gambler, wager, currency):
|
2022-10-28 23:39:31 +00:00
|
|
|
existing_game = get_active_twentyone_game(gambler)
|
|
|
|
over_minimum_bet = wager >= minimum_bet
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if existing_game:
|
2023-02-24 09:01:57 +00:00
|
|
|
existing_game.active = False
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(existing_game)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if not over_minimum_bet:
|
|
|
|
raise Exception(f"Gambler must bet over {minimum_bet} {currency}.")
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
try:
|
|
|
|
charge_gambler(gambler, wager, currency)
|
|
|
|
new_game = build_casino_game(gambler, wager, currency)
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(new_game)
|
|
|
|
g.db.flush()
|
2022-10-28 23:39:31 +00:00
|
|
|
except:
|
|
|
|
raise Exception(f"Gambler cannot afford to bet {wager} {currency}.")
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
def handle_blackjack_deal(state, split):
|
2022-10-28 23:39:31 +00:00
|
|
|
deck = build_deck(state)
|
|
|
|
first = deck.pop()
|
|
|
|
second = deck.pop()
|
|
|
|
third = deck.pop()
|
|
|
|
fourth = deck.pop()
|
|
|
|
state['player'] = [first, third]
|
|
|
|
state['dealer'] = [second, fourth]
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
def handle_blackjack_hit(state, split = False):
|
2022-10-28 23:39:31 +00:00
|
|
|
deck = build_deck(state)
|
2023-09-18 16:31:58 +00:00
|
|
|
if(split and state['has_player_split'] and state['status_split'] != BlackjackStatus.LOST):
|
|
|
|
next_card = deck.pop()
|
|
|
|
state['player_split'].append(next_card)
|
|
|
|
elif(state['status'] != BlackjackStatus.LOST):
|
|
|
|
next_card = deck.pop()
|
|
|
|
state['player'].append(next_card)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
def handle_blackjack_stay(state, split = False):
|
|
|
|
if(split and state['has_player_split'] and state['status_split'] != BlackjackStatus.LOST):
|
|
|
|
state['status_split'] = BlackjackStatus.STAYED
|
|
|
|
elif(state['status'] != BlackjackStatus.LOST):
|
|
|
|
state['status'] = BlackjackStatus.STAYED
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
def handle_blackjack_double_down(state, split):
|
2022-10-28 23:39:31 +00:00
|
|
|
state['player_doubled_down'] = True
|
|
|
|
state = handle_blackjack_hit(state)
|
|
|
|
state = handle_blackjack_stay(state)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
def handle_blackjack_buy_insurance(state, split):
|
2022-10-28 23:39:31 +00:00
|
|
|
state['player_bought_insurance'] = True
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
def handle_split(state, split):
|
|
|
|
state['has_player_split'] = True
|
|
|
|
state['player_split'] = [state['player'].pop()]
|
|
|
|
|
|
|
|
state = handle_blackjack_hit(state)
|
|
|
|
state = handle_blackjack_hit(state, True)
|
|
|
|
|
|
|
|
return state
|
|
|
|
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
def check_for_completion(state):
|
2023-09-18 16:31:58 +00:00
|
|
|
has_split = state['has_player_split']
|
2022-10-28 23:39:31 +00:00
|
|
|
after_initial_deal = len(
|
2023-09-18 16:31:58 +00:00
|
|
|
state['player']) == 2 and len(state['dealer']) == 2 and not has_split
|
2022-10-28 23:39:31 +00:00
|
|
|
player_hand_value = get_value_of_hand(state['player'])
|
2023-09-18 16:31:58 +00:00
|
|
|
player_split_hand_value = get_value_of_hand(state['player_split'])
|
2022-10-28 23:39:31 +00:00
|
|
|
dealer_hand_value = get_value_of_hand(state['dealer'])
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
# Both player and dealer were initially dealt 21: Push.
|
|
|
|
if after_initial_deal and player_hand_value == 21 and dealer_hand_value == 21:
|
|
|
|
state['status'] = BlackjackStatus.PUSHED
|
|
|
|
return True, state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
# Player was originally dealt 21, dealer was not: Blackjack.
|
|
|
|
if after_initial_deal and player_hand_value == 21:
|
|
|
|
state['status'] = BlackjackStatus.BLACKJACK
|
|
|
|
return True, state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
# Player went bust: Lost.
|
2023-09-18 16:31:58 +00:00
|
|
|
if player_hand_value == -1 and state['status'] != BlackjackStatus.LOST:
|
2022-10-28 23:39:31 +00:00
|
|
|
state['status'] = BlackjackStatus.LOST
|
2023-09-18 16:31:58 +00:00
|
|
|
if(not has_split or state['status_split'] == BlackjackStatus.LOST):
|
|
|
|
return True, state
|
|
|
|
|
|
|
|
# Player went bust: Lost.
|
|
|
|
if player_split_hand_value == -1 and state['status_split'] != BlackjackStatus.LOST:
|
|
|
|
state['status_split'] = BlackjackStatus.LOST
|
|
|
|
if state['status'] == BlackjackStatus.LOST:
|
|
|
|
return True, state
|
|
|
|
|
|
|
|
hand_terminal_status = state['status'] in [BlackjackStatus.LOST, BlackjackStatus.STAYED]
|
|
|
|
hand_split_terminal_status = not has_split or state['status_split'] in [BlackjackStatus.LOST, BlackjackStatus.STAYED]
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
# Player chose to stay: Deal rest for dealer then determine winner.
|
2023-09-18 16:31:58 +00:00
|
|
|
if hand_split_terminal_status and hand_terminal_status:
|
2022-10-28 23:39:31 +00:00
|
|
|
deck = build_deck(state)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
while dealer_hand_value < 17 and dealer_hand_value != -1:
|
|
|
|
next_card = deck.pop()
|
|
|
|
state['dealer'].append(next_card)
|
|
|
|
dealer_hand_value = get_value_of_hand(state['dealer'])
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
if((not has_split) or state['status'] != BlackjackStatus.LOST):
|
|
|
|
if player_hand_value > dealer_hand_value or dealer_hand_value == -1:
|
|
|
|
state['status'] = BlackjackStatus.WON
|
|
|
|
elif dealer_hand_value > player_hand_value:
|
|
|
|
state['status'] = BlackjackStatus.LOST
|
|
|
|
else:
|
|
|
|
state['status'] = BlackjackStatus.PUSHED
|
2023-09-28 23:58:09 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
state['player_value'] = get_value_of_hand(state['player'])
|
|
|
|
state['dealer_value'] = get_value_of_hand(state['dealer'])
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
if has_split and state['status_split'] != BlackjackStatus.LOST:
|
|
|
|
if player_split_hand_value > dealer_hand_value or dealer_hand_value == -1:
|
|
|
|
state['status_split'] = BlackjackStatus.WON
|
|
|
|
elif dealer_hand_value > player_split_hand_value:
|
|
|
|
state['status_split'] = BlackjackStatus.LOST
|
|
|
|
else:
|
|
|
|
state['status_split'] = BlackjackStatus.PUSHED
|
|
|
|
|
|
|
|
state['player_split_value'] = get_value_of_hand(state['player_split'])
|
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return True, state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return False, state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def does_insurance_apply(state):
|
2022-10-28 23:39:31 +00:00
|
|
|
dealer = state['dealer']
|
|
|
|
dealer_hand_value = get_value_of_hand(dealer)
|
|
|
|
dealer_first_card_ace = dealer[0][0] == 'A'
|
|
|
|
dealer_never_hit = len(dealer) == 2
|
2023-09-18 16:31:58 +00:00
|
|
|
return not state['has_player_split'] and dealer_hand_value == 21 and dealer_first_card_ace and dealer_never_hit
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def can_purchase_insurance(state):
|
2022-10-28 23:39:31 +00:00
|
|
|
dealer = state['dealer']
|
|
|
|
dealer_first_card_ace = dealer[0][0] == 'A'
|
|
|
|
dealer_never_hit = len(dealer) == 2
|
2023-09-18 16:31:58 +00:00
|
|
|
return not state['has_player_split'] and dealer_first_card_ace and dealer_never_hit and not state['player_bought_insurance']
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def can_double_down(state):
|
2022-10-28 23:39:31 +00:00
|
|
|
player = state['player']
|
|
|
|
player_hand_value = get_value_of_hand(player)
|
|
|
|
player_never_hit = len(player) == 2
|
2023-09-21 13:31:02 +00:00
|
|
|
return not state['has_player_split'] and player_hand_value in (9, 10, 11) and player_never_hit
|
2023-09-18 16:31:58 +00:00
|
|
|
|
|
|
|
def can_split(state):
|
|
|
|
player = state['player']
|
|
|
|
player_never_hit = len(player) == 2
|
2023-09-26 10:00:20 +00:00
|
|
|
hand_can_split = get_value_of_card(player[0]) == get_value_of_card(player[1])
|
2023-09-18 16:31:58 +00:00
|
|
|
player_has_split = state['has_player_split']
|
|
|
|
return hand_can_split and player_never_hit and not player_has_split
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def handle_payout(gambler, state, game):
|
2022-10-28 23:39:31 +00:00
|
|
|
status = state['status']
|
2023-09-18 16:31:58 +00:00
|
|
|
split_status = state['status_split']
|
2022-10-28 23:39:31 +00:00
|
|
|
payout = 0
|
|
|
|
|
|
|
|
if status == BlackjackStatus.BLACKJACK:
|
|
|
|
game.winnings = floor(game.wager * 3/2)
|
|
|
|
payout = game.wager + game.winnings
|
|
|
|
elif status == BlackjackStatus.WON:
|
|
|
|
game.winnings = game.wager
|
|
|
|
payout = game.wager * 2
|
|
|
|
elif status == BlackjackStatus.LOST:
|
|
|
|
dealer = state['dealer']
|
|
|
|
dealer_first_card_ace = dealer[0][0] == 'A'
|
|
|
|
dealer_never_hit = len(dealer) == 2
|
|
|
|
dealer_hand_value = get_value_of_hand(dealer) == 21
|
|
|
|
insurance_applies = dealer_hand_value == 21 and dealer_first_card_ace and dealer_never_hit
|
|
|
|
|
|
|
|
if insurance_applies and state['player_bought_insurance']:
|
|
|
|
game.winnings = 0
|
|
|
|
payout = game.wager
|
|
|
|
else:
|
|
|
|
game.winnings = -game.wager
|
|
|
|
payout = 0
|
|
|
|
elif status == BlackjackStatus.PUSHED:
|
|
|
|
game.winnings = 0
|
|
|
|
payout = game.wager
|
|
|
|
else:
|
|
|
|
raise Exception("Attempted to payout a game that has not finished.")
|
2023-09-28 23:58:09 +00:00
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
if split_status == BlackjackStatus.WON:
|
|
|
|
game.winnings += game.wager
|
|
|
|
payout += game.wager * 2
|
|
|
|
elif split_status == BlackjackStatus.LOST:
|
|
|
|
game.winnings += -game.wager
|
2023-09-18 18:18:08 +00:00
|
|
|
elif split_status == BlackjackStatus.PUSHED:
|
2023-09-18 16:31:58 +00:00
|
|
|
payout += game.wager
|
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
gambler.pay_account(game.currency, payout)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2024-03-02 17:02:05 +00:00
|
|
|
if game.winnings:
|
|
|
|
currency_log = CurrencyLog(
|
|
|
|
user_id=gambler.id,
|
|
|
|
currency=game.currency,
|
2024-03-02 21:32:17 +00:00
|
|
|
amount=game.winnings,
|
2024-03-02 17:24:07 +00:00
|
|
|
reason="Blackjack bet",
|
2024-03-02 17:02:05 +00:00
|
|
|
)
|
|
|
|
g.db.add(currency_log)
|
2024-03-02 20:03:28 +00:00
|
|
|
if game.currency == 'coins':
|
2024-03-02 17:02:05 +00:00
|
|
|
currency_log.balance = gambler.coins
|
|
|
|
else:
|
|
|
|
currency_log.balance = gambler.marseybux
|
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
if status in {BlackjackStatus.BLACKJACK, BlackjackStatus.WON} or split_status in {BlackjackStatus.WON}:
|
2023-07-14 03:53:15 +00:00
|
|
|
distribute_wager_badges(gambler, game.wager, won=True)
|
2023-09-18 16:31:58 +00:00
|
|
|
elif status == BlackjackStatus.LOST or split_status == BlackjackStatus.LOST:
|
2023-07-14 03:53:15 +00:00
|
|
|
distribute_wager_badges(gambler, game.wager, won=False)
|
2022-10-28 23:39:31 +00:00
|
|
|
|
|
|
|
game.active = False
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(game)
|
2022-10-28 23:39:31 +00:00
|
|
|
|
|
|
|
return payout
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def remove_exploitable_information(state):
|
2022-10-28 23:39:31 +00:00
|
|
|
safe_state = state
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if len(safe_state['dealer']) >= 2:
|
|
|
|
safe_state['dealer'][1] = '?'
|
2022-09-16 18:51:58 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
safe_state['dealer_value'] = '?'
|
|
|
|
return safe_state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
action_handlers = {
|
2022-10-28 23:39:31 +00:00
|
|
|
BlackjackAction.DEAL: handle_blackjack_deal,
|
|
|
|
BlackjackAction.HIT: handle_blackjack_hit,
|
|
|
|
BlackjackAction.STAY: handle_blackjack_stay,
|
|
|
|
BlackjackAction.DOUBLE_DOWN: handle_blackjack_double_down,
|
|
|
|
BlackjackAction.BUY_INSURANCE: handle_blackjack_buy_insurance,
|
2023-09-18 16:31:58 +00:00
|
|
|
BlackjackAction.SPLIT: handle_split,
|
2022-09-10 21:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
def dispatch_action(gambler, action, is_split = False):
|
2022-10-28 23:39:31 +00:00
|
|
|
game = get_active_twentyone_game(gambler)
|
|
|
|
handler = action_handlers[action]
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if not game:
|
|
|
|
raise Exception(
|
|
|
|
'Gambler has no active blackjack game.')
|
|
|
|
if not handler:
|
|
|
|
raise Exception(
|
|
|
|
f'Illegal action {action} passed to Blackjack#dispatch_action.')
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-30 00:32:40 +00:00
|
|
|
state = game.game_state_json
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if action == BlackjackAction.BUY_INSURANCE:
|
|
|
|
if not can_purchase_insurance(state):
|
|
|
|
raise Exception("Insurance cannot be purchased.")
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
charge_gambler(gambler, floor(game.wager / 2), game.currency)
|
|
|
|
if action == BlackjackAction.DOUBLE_DOWN:
|
|
|
|
if not can_double_down(state):
|
|
|
|
raise Exception("Cannot double down.")
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
charge_gambler(gambler, game.wager, game.currency)
|
|
|
|
game.wager *= 2
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
if action == BlackjackAction.SPLIT:
|
|
|
|
if not can_split(state):
|
|
|
|
raise Exception("Cannot split")
|
|
|
|
|
|
|
|
charge_gambler(gambler, game.wager, game.currency)
|
|
|
|
|
|
|
|
new_state = handler(state, is_split)
|
2022-10-28 23:39:31 +00:00
|
|
|
new_state['player_value'] = get_value_of_hand(new_state['player'])
|
|
|
|
new_state['dealer_value'] = get_value_of_hand(new_state['dealer'])
|
2023-09-18 16:31:58 +00:00
|
|
|
new_state['player_split_value'] = get_value_of_hand(new_state['player_split'])
|
2023-09-15 11:30:08 +00:00
|
|
|
|
2023-09-15 11:27:00 +00:00
|
|
|
|
2023-09-15 11:30:08 +00:00
|
|
|
game_over, final_state = check_for_completion(new_state)
|
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
new_state['actions'] = get_available_actions(new_state)
|
|
|
|
game.game_state = json.dumps(new_state)
|
|
|
|
g.db.add(game)
|
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if game_over:
|
|
|
|
payout = handle_payout(gambler, final_state, game)
|
|
|
|
final_state['actions'] = [BlackjackAction.DEAL]
|
|
|
|
final_state['payout'] = payout
|
|
|
|
return final_state
|
|
|
|
else:
|
|
|
|
safe_state = remove_exploitable_information(new_state)
|
|
|
|
return safe_state
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def shuffle(collection):
|
2022-10-28 23:39:31 +00:00
|
|
|
random.shuffle(collection)
|
|
|
|
return collection
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def build_deck(state):
|
2022-10-28 23:39:31 +00:00
|
|
|
card_counts = {}
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
for card in deck:
|
|
|
|
card_counts[card] = deck_count
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
cards_already_dealt = state['player'].copy()
|
2023-09-18 16:31:58 +00:00
|
|
|
cards_already_dealt.extend(state['player_split'].copy())
|
2022-10-28 23:39:31 +00:00
|
|
|
cards_already_dealt.extend(state['dealer'].copy())
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
for card in cards_already_dealt:
|
|
|
|
card_counts[card] = card_counts[card] - 1
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
deck_without_already_dealt_cards = []
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
for card in deck:
|
|
|
|
amount = card_counts[card]
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
for _ in range(amount):
|
|
|
|
deck_without_already_dealt_cards.append(card)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return shuffle(deck_without_already_dealt_cards)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_value_of_card(card):
|
2022-10-28 23:39:31 +00:00
|
|
|
rank = card[0]
|
|
|
|
return 0 if rank == "A" else min(ranks.index(rank) + 2, 10)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_value_of_hand(hand):
|
2022-10-28 23:39:31 +00:00
|
|
|
without_aces = sum(map(get_value_of_card, hand))
|
|
|
|
ace_count = sum("A" in c for c in hand)
|
|
|
|
possibilities = []
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
for i in range(ace_count + 1):
|
|
|
|
value = without_aces + (ace_count - i) + i * 11
|
|
|
|
possibilities.append(-1 if value > 21 else value)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return max(possibilities)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-09-13 01:07:39 +00:00
|
|
|
|
2022-09-10 21:01:34 +00:00
|
|
|
def get_available_actions(state):
|
2022-10-28 23:39:31 +00:00
|
|
|
actions = []
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if state['status'] == BlackjackStatus.PLAYING:
|
|
|
|
actions.append(BlackjackAction.HIT)
|
|
|
|
actions.append(BlackjackAction.STAY)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
if state['has_player_split'] and state['status_split'] == BlackjackStatus.PLAYING:
|
|
|
|
actions.append(BlackjackAction.HIT_SPLIT)
|
|
|
|
actions.append(BlackjackAction.STAY_SPLIT)
|
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if can_double_down(state):
|
|
|
|
actions.append(BlackjackAction.DOUBLE_DOWN)
|
2022-09-13 01:07:39 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if can_purchase_insurance(state):
|
|
|
|
actions.append(BlackjackAction.BUY_INSURANCE)
|
2023-09-28 23:58:09 +00:00
|
|
|
|
2023-09-18 16:31:58 +00:00
|
|
|
if can_split(state):
|
|
|
|
actions.append(BlackjackAction.SPLIT)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return actions
|