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
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.casino import distribute_wager_badges
|
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"
|
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": [],
|
|
|
|
"player_value": 0,
|
|
|
|
"dealer": [],
|
|
|
|
"dealer_value": 0,
|
|
|
|
"player_bought_insurance": False,
|
|
|
|
"player_doubled_down": False,
|
|
|
|
"status": BlackjackStatus.PLAYING,
|
2023-02-24 08:53:03 +00:00
|
|
|
"actions": [],
|
2022-10-28 23:39:31 +00:00
|
|
|
"wager": {
|
|
|
|
"amount": 0,
|
|
|
|
"currency": "coins"
|
|
|
|
},
|
|
|
|
"payout": 0
|
|
|
|
}
|
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-03-16 06:27:58 +00:00
|
|
|
return g.db.query(CasinoGame).filter(
|
2022-12-14 19:30:05 +00:00
|
|
|
CasinoGame.active == True,
|
|
|
|
CasinoGame.kind == 'blackjack',
|
|
|
|
CasinoGame.user_id == gambler.id).first()
|
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):
|
2022-10-28 23:39:31 +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
|
|
|
|
|
|
|
|
|
|
|
def handle_blackjack_deal(state):
|
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
|
|
|
|
|
|
|
|
|
|
|
def handle_blackjack_hit(state):
|
2022-10-28 23:39:31 +00:00
|
|
|
deck = build_deck(state)
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
def handle_blackjack_stay(state):
|
2022-10-28 23:39:31 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
def handle_blackjack_double_down(state):
|
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
|
|
|
|
|
|
|
|
|
|
|
def handle_blackjack_buy_insurance(state):
|
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
|
|
|
|
|
|
|
|
|
|
|
def check_for_completion(state):
|
2022-10-28 23:39:31 +00:00
|
|
|
after_initial_deal = len(
|
|
|
|
state['player']) == 2 and len(state['dealer']) == 2
|
|
|
|
player_hand_value = get_value_of_hand(state['player'])
|
|
|
|
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.
|
|
|
|
if player_hand_value == -1:
|
|
|
|
state['status'] = BlackjackStatus.LOST
|
|
|
|
return True, state
|
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.
|
|
|
|
if state['status'] == BlackjackStatus.STAYED:
|
|
|
|
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
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
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
|
2022-09-10 21:01:34 +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
|
|
|
|
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
|
|
|
|
return 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
|
|
|
|
return 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
|
|
|
|
return player_hand_value in (10, 11) and player_never_hit
|
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']
|
|
|
|
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.")
|
|
|
|
|
|
|
|
gambler.pay_account(game.currency, payout)
|
2023-01-01 11:36:20 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
if game.currency == 'coins':
|
2022-12-23 22:22:41 +00:00
|
|
|
if status in {BlackjackStatus.BLACKJACK, BlackjackStatus.WON}:
|
2022-10-28 23:39:31 +00:00
|
|
|
distribute_wager_badges(gambler, game.wager, won=True)
|
|
|
|
elif status == BlackjackStatus.LOST:
|
|
|
|
distribute_wager_badges(gambler, game.wager, won=False)
|
|
|
|
|
|
|
|
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,
|
2022-09-10 21:01:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def dispatch_action(gambler, action):
|
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
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
new_state = handler(state)
|
|
|
|
new_state['player_value'] = get_value_of_hand(new_state['player'])
|
|
|
|
new_state['dealer_value'] = get_value_of_hand(new_state['dealer'])
|
|
|
|
new_state['actions'] = get_available_actions(new_state)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
game.game_state = json.dumps(new_state)
|
2023-03-16 06:27:58 +00:00
|
|
|
g.db.add(game)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
game_over, final_state = check_for_completion(new_state)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
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()
|
|
|
|
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
|
|
|
|
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)
|
2022-09-10 21:01:34 +00:00
|
|
|
|
2022-10-28 23:39:31 +00:00
|
|
|
return actions
|