""" Runs commands, game logic and imaging for blackjack games. *Classes* --------- Blackjack Contains the blackjack game logic. DrawBlackjack Draws images of the blackjack table. """ import math # Used for flooring decimal numbers import datetime # Used to generate the game id import asyncio # Used for sleeping from discord_slash.context import InteractionContext as IntCont # Used for # typehints from discord_slash.context import ComponentContext from discord.abc import Messageable from PIL import Image from gwendolyn.utils import replace_multiple from .game_base import CardGame, CardDrawer WHITE = (255, 255, 255) BLACK = (0, 0, 0) RED = (255, 50, 50) GOLD = (155, 123, 0) def _is_round_done(game: dict): """ Find out if the round is done. *Parameters* ------------ game: dict The game to check. *Returns* --------- round_done: bool Whether the round is done. """ return all( hand["hit"] or hand["standing"] for user_hands in game["user hands"].values() for hand in user_hands ) class Blackjack(CardGame): """ Deals with blackjack commands and gameplay logic. *Methods* --------- hit(ctx: IntCont, hand_number: int = 0) double(ctx: IntCont, hand_number: int = 0) stand(ctx: IntCont, hand_number: int = 0) split(ctx: IntCont, hand_number: int = 0) enter_game(ctx: IntCont, bet: int) start(ctx: IntCont) hilo(ctx: IntCont) shuffle(ctx: IntCont) cards(ctx: IntCont) """ def __init__(self, bot): """Initialize the class.""" super().__init__(bot, "blackjack", DrawBlackjack, 4) default_buttons = ["Hit", "Stand", "Double", "Split"] self.default_buttons = [(i, [i, "0"], 1) for i in default_buttons] async def _test_command(self, game: dict, user: str, command: str, hand_number: int, ctx: IntCont): #pylint:disable=too-many-arguments valid_command = False if user not in game["user hands"]: self.bot.log(f"They tried to {command} without being in the game") send_msg = f"You have to enter the game before you can {command}" elif len(game["user hands"][user]) < hand_number: self.bot.log(f"They tried to {command} with a hand they don't have") send_msg = "You don't have that many hands" elif game["round"] <= 0: self.bot.log(f"They tried to {command} on the 0th round") send_msg = "You haven't seen your cards yet!" elif len(game["user hands"][user]) > 1 and hand_number == 0: self._get_hand_number(ctx, command, game["user hands"][user]) return False elif game["user hands"][user][max(hand_number-1,0)]["hit"]: self.bot.log("They've already hit this round") send_msg = "You've already hit this round" elif game["user hands"][user][max(hand_number-1,0)]["standing"]: self.bot.log("They're already standing") send_msg = "You're already standing" else: valid_command = True if not valid_command: await ctx.send(send_msg, hidden=True) return valid_command def _shuffle_cards(self, channel: str): """ Shuffle an amount of decks equal to self.decks_used. The shuffled cards are placed in the database as the cards that are used by other blackjack functions. *Parameters* ------------ channel: str The id of the channel where the cards will be used. """ super()._shuffle_cards(channel) # Creates hilo file self.bot.log(f"creating hilo doc for {channel}") hilo_updater = {"$set": {"_id": channel, "hilo": 0}} self._update_document(channel, hilo_updater, "hilo") def _calc_hand_value(self, hand: list): """ Calculate the value of a blackjack hand. *Parameters* ------------ hand: list The hand to calculate the value of. Each element in the list is a card represented as a string in the format of "xy" where x is the number of the card (0, k, q, and j for 10s, kings, queens and jacks) and y is the suit (d, c, h or s). *Returns* --------- hand_value: int The blackjack value of the hand. """ values = [0] for card in hand: card_value = replace_multiple(card[0], ["0", "k", "q", "j"], "10") if card_value == "a": values = [i + 1 for i in values] + [i + 11 for i in values] else: values = [i+int(card_value) for i in values] valid_values = [i for i in values if i <= 21] hand_value = max(valid_values) if valid_values else min(values) self.bot.log(f"Calculated the value of {hand} to be {hand_value}") return hand_value def _draw_card(self, channel: str): drawn_card = super()._draw_card(channel) hilo_value = min(1,-1-((self._calc_hand_value([drawn_card])-10)//3)) self._update_document(channel, {"$inc": {"hilo": hilo_value}}, "hilo") return drawn_card def _dealer_draw(self, channel: str): """ Draw a card for the dealer. *Parameters* ------------ channel: str The id of the channel to draw a card for the dealer in. *Returns* --------- done: bool Whether the dealer is done drawing cards. """ game = self.access_document(channel) dealer_hand = game["dealer hand"] if self._calc_hand_value(dealer_hand) < 17: dealer_hand.append(self._draw_card(channel)) dealer_updater = {"$set": {"dealer hand": dealer_hand}} self._update_document(channel, dealer_updater) done = False else: done = True if self._calc_hand_value(dealer_hand) > 21: self._update_document(channel, {"$set": {"dealer busted": True}}) return done def _blackjack_continue(self, channel: str): """ Continues the blackjack game to the next round. *Parameters* ------------ channel: str The id of the channel the blackjack game is in *Returns* --------- send_message: str The message to send to the channel. all_standing: bool If all players are standing. gameDone: bool If the game has finished. """ self.bot.log("Continuing blackjack game") game = self.access_document(channel) self._update_document(channel, {"$inc": {"round": 1}}) message = self.long_strings["Blackjack all players standing"] all_standing = True pre_all_standing = True done = False if game["all standing"]: self.bot.log("All are standing") done = self._dealer_draw(channel) message = "The dealer draws a card." self.bot.log("Testing if all are standing") for user, user_hands in game["user hands"].items(): standing_test = (self._test_if_standing(user_hands)) hand_updater = {"$set": {"user hands."+user: standing_test[0]}} self._update_document(channel, hand_updater) if not standing_test[1]: all_standing = False if not standing_test[2]: pre_all_standing = False if all_standing: self._update_document(channel, {"$set": {"all standing": True}}) if done: message = "The dealer is done drawing cards" return message, True, done if pre_all_standing: return "", True, done send_message = self.long_strings["Blackjack commands"] if game["round"] == 0: send_message = send_message.format( self.long_strings["Blackjack first round"]) else: send_message = send_message.format("") return send_message, False, done def _test_if_standing(self, user_hands: list): """ Test if a player is standing on all their hands. Also resets the hand if it's not standing *Parameters* ------------ hand: dict The hands to test and reset. *Returns* --------- hand: dict The reset hand. all_standing: bool If the player is standing on all their hands. pre_all_standing: bool Is true if all_standing is True, or if a player has done something equivalent to standing but still needs to see the newly drawn card. """ # If the user has not hit, they are by definition standing. all_standing = True pre_all_standing = True for hand in user_hands: if not hand["hit"]: hand["standing"] = True if not hand["standing"]: all_standing = False if self._calc_hand_value(hand["hand"]) >= 21 or hand["doubled"]: hand["standing"] = True else: pre_all_standing = False hand["hit"] = False return user_hands, all_standing, pre_all_standing async def _blackjack_finish(self, channel: Messageable): """ Generate the winnings message after the blackjack game ends. *Parameters* ------------ channel: str The id of the channel the game is in. *Returns* --------- final_winnings: str The winnings message. """ final_winnings = "*Final Winnings:*\n" game = self.access_document(str(channel.id)) for user in game["user hands"]: winnings, net_winnings, reason = self._calc_winning( game["user hands"][user], self._calc_hand_value(game["dealer hand"]), game["dealer blackjack"], game["dealer busted"] ) user_name = self.bot.database_funcs.get_name(user) if winnings < 0: final_winnings += f"{user_name} lost" else: final_winnings += f"{user_name} won" if abs(winnings) == 1: final_winnings += " 1 GwendoBuck" else: final_winnings += f" {abs(winnings)} GwendoBucks" final_winnings += f" {reason}\n" self.bot.money.addMoney(user, net_winnings) await self._end_game(channel) return final_winnings def _calc_winning(self, user_hands: dict, dealer_value: int, dealer_blackjack: bool, dealer_busted: bool): """ Calculate how much a user has won/lost in the blackjack game. *Parameters* ------------ hand: dict The hand to calculate the winnings of. dealer_value: int The dealer's hand value. top_level: bool If the input hand is _all_ if the player's hands. If False, it's one of the hands resulting from a split. dealer_blackjack: bool If the dealer has a blackjack. dealer_busted: bool If the dealer busted. *Returns* --------- winnings: int How much the player has won/lost. net_winnings: int winnings minus the original bet. This is added to the user's account, since the bet was removed from their account when they placed the bet. reason: str The reason for why they won/lost. """ self.bot.log("Calculating winnings") reason = "" winnings = 0 net_winnings = 0 for hand in user_hands: winnings += -1 * hand["bet"] hand_value = self._calc_hand_value(hand["hand"]) states = [ (dealer_blackjack, "dealer blackjack", 0), (hand["blackjack"], "blackjack", math.floor(2.5 * hand["bet"])), (hand["busted"], "busted", 0), (dealer_busted, dealer_busted, 2*hand["bet"]), (hand_value == dealer_value, "pushed", hand["bet"]), (hand_value > dealer_value, "highest value", 2 * hand["bet"]), (True, "highest value", 0) ] for state in states: if state[0]: reason += f"({state[1]})" net_winnings += state[2] break winnings += net_winnings return winnings, net_winnings, reason async def _blackjack_loop(self, channel, game_round: int, game_id: str): """ Run blackjack logic and continue if enough time passes. *Parameters* ------------ channel: guildChannel or DMChannel The channel the game is happening in. game_round: int The round to start. game_id: str The ID of the game. """ self.bot.log(f"Loop {game_round}", str(channel.id)) new_message, all_standing, game_done = self._blackjack_continue( str(channel.id) ) if new_message != "": self.bot.log(new_message, str(channel.id)) await channel.send(new_message) if not game_done: await self._delete_old_image(channel) buttons = None if all_standing else self.default_buttons await self._send_image(channel, buttons) await asyncio.sleep([120,5][all_standing]) if not self._test_document(str(channel.id)): self.bot.log(f"Ending loop on round {game_round}", str(channel.id)) return game = self.access_document(str(channel.id)) if game_round != game["round"] or game_id != game["game_id"]: self.bot.log(f"Ending loop on round {game_round}", str(channel.id)) return if not game_done: log_message = f"Loop {game_round} starting a new blackjack loop" self.bot.log(log_message, str(channel.id)) await self._blackjack_loop(channel, game_round+1, game_id) else: await channel.send(await self._blackjack_finish(channel)) async def _get_hand_number(self, ctx: IntCont, command: str, hands_amount: int): buttons = [ (str(i+1), [command, "0", str(i+1)], 1) for i in range(hands_amount) ] await ctx.send( f"Which hand do you want to {command}?", hidden=True, components=self._get_action_rows(buttons) ) async def _hit(self, ctx: IntCont, hand_number: int = 0): """ Hit on a hand. *Parameters* ------------ ctx: IntCont The context of the command. hand_number: int = 1 The number of the hand to hit. """ await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" game = self.access_document(channel) if not await self._test_command(game, user, "hit", hand_number, ctx): return hand = game["user hands"][user][max(hand_number-1,0)] hand["hand"].append(self._draw_card(channel)) hand["hit"] = True hand_value = self._calc_hand_value(hand["hand"]) if hand_value > 21: hand["busted"] = True hand_path = f"user hands.{user}.{max(hand_number-1,0)}" self._update_document(channel, {"$set": {hand_path: hand}}) game = self.access_document(channel) await ctx.send(f"{ctx.author.display_name} hit") self.bot.log("They succeeded") if _is_round_done(game): game_id = game["game_id"] self.bot.log("Hit calling self._blackjack_loop()", channel) await self._blackjack_loop( ctx.channel, game["round"]+1, game_id ) async def _double(self, ctx: IntCont, hand_number: int = 0): """ Double a hand. *Parameters* ------------ ctx: IntCont The context of the command. hand_number: int = 0 The number of the hand to double. """ await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" game = self.access_document(channel) balance = self.bot.money.checkBalance(user) if not await self._test_command(game, user, "double", hand_number, ctx): return if len(game["user hands"][user][max(hand_number-1,0)]["hand"]) != 2: await ctx.send("You can only double on the first round") self.bot.log("They tried to double after round 1") elif balance < game["user hands"][user][max(hand_number-1,0)]["bet"]: await ctx.send("You can't double when you don't have enough money") self.bot.log("They tried to double without having enough money") else: hand = game["user hands"][user][max(hand_number-1,0)] self.bot.money.addMoney(user, -1 * hand["bet"]) hand["hand"].append(self._draw_card(channel)) hand["hit"] = True hand["doubled"] = True hand["bet"] += hand["bet"] if self._calc_hand_value(hand["hand"]) > 21: hand["busted"] = True hand_path = f"user hands.{user}.{max(hand_number-1,0)}" self._update_document(channel, {"$set": {hand_path: hand}}) game = self.access_document(channel) user_name = self.bot.database_funcs.get_name(user) send_message = self.long_strings["Blackjack double"] await ctx.send(send_message.format(hand["bet"], user_name)) self.bot.log("They succeeded") if _is_round_done(game): game_id = game["game_id"] self.bot.log("Double calling self._blackjack_loop()", channel) await self._blackjack_loop( ctx.channel, game["round"]+1, game_id ) async def _stand(self, ctx: IntCont, hand_number: int = 0): """ Stand on a hand. *Parameters* ------------ ctx: IntCont The context of the command. hand_number: int = 0 The number of the hand to stand on. """ await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" game = self.access_document(channel) if not await self._test_command(game, user, "stand", hand_number, ctx): return hand = game["user hands"][user][max(hand_number-1,0)] hand["standing"] = True hand_path = f"user hands.{user}.{max(hand_number-1,0)}" self._update_document(channel, {"$set": {hand_path: hand}}) game = self.access_document(channel) send_message = f"{ctx.author.display_name} is standing" log_message = "They succeeded" await ctx.send(send_message) self.bot.log(log_message) if _is_round_done(game): game_id = game["game_id"] self.bot.log("Stand calling self._blackjack_loop()", channel) await self._blackjack_loop(ctx.channel, game["round"]+1, game_id) async def _split(self, ctx: IntCont, hand_number: int = 0): """ Split a hand. *Parameters* ------------ ctx: IntCont The context of the command. hand_number: int = 0 The number of the hand to split. """ await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" game = self.access_document(channel) if not await self._test_command(game, user, "split", hand_number, ctx): return old_hand = game["user hands"][user][max(hand_number-1,0)] if len(game["user hands"][user]) > 3: await ctx.send("You can only split 3 times") self.bot.log("They tried to split more than three times") elif len(old_hand["hand"]) != 2: await ctx.send("You can only split on the first round") self.bot.log("They tried to split after the first round") elif len({self._calc_hand_value([i]) for i in old_hand["hand"]}) != 1: await ctx.send(self.long_strings["Blackjack different cards"]) self.bot.log("They tried to split two different cards") elif self.bot.money.checkBalance(user) < old_hand["bet"]: await ctx.send("You don't have enough GwendoBucks") self.bot.log("They didn't have enough GwendoBucks") else: self.bot.money.addMoney(user, -1 * old_hand["bet"]) old_hand["hit"] = True hands = [old_hand, { "hand": [old_hand["hand"].pop(1), self._draw_card(channel)], "bet": old_hand["bet"], "standing": False, "busted": False, "blackjack": False, "hit": True, "doubled": False }] old_hand["hand"].append(self._draw_card(channel)) for i, hand in enumerate(hands): if self._calc_hand_value(hand["hand"]) > 21: hands[i]["busted"] = True elif self._calc_hand_value(hand["hand"]) == 21: hands[i]["blackjack"] = True game["user hands"][user][max(hand_number-1,0)] = hands[0] game["user hands"][user].append(hands[1]) updater = {"$set": {f"user hands.{user}": game["user hands"][user]}} self._update_document(channel, updater) send_message = self.long_strings["Blackjack split"] user_name = self.bot.database_funcs.get_name(user) await ctx.send(send_message.format(user_name)) self.bot.log("They succeeded") game = self.access_document(channel) if _is_round_done(game): game_id = game["game_id"] self.bot.log("Split calling self._blackjack_loop()", channel) await self._blackjack_loop( ctx.channel, game["round"]+1, game_id ) async def enter_game(self, ctx: IntCont, bet: int): """ Enter the blackjack game. *Parameters* ------------ ctx: IntCont The context of the command. bet: int The bet to enter with. """ await self.bot.defer(ctx) channel = str(ctx.channel_id) user = f"#{ctx.author.id}" game = self.access_document(channel) user_name = self.bot.database_funcs.get_name(user) self.bot.log(f"{user_name} is trying to join the Blackjack game") if user in game["user hands"]: await ctx.send("You're already in the game!") self.bot.log("They're already in the game") elif len(game["user hands"]) >= 5: await ctx.send("There can't be more than 5 players in a game") self.bot.log("There were already 5 players in the game") elif game["round"] != 0: await ctx.send("The table is no longer taking bets") self.bot.log("They tried to join after the game begun") elif bet < 0: await ctx.send("You can't bet a negative amount") self.bot.log("They tried to bet a negative amount") elif self.bot.money.checkBalance(user) < bet: await ctx.send("You don't have enough GwendoBucks") self.bot.log("They didn't have enough GwendoBucks") else: self.bot.money.addMoney(user, -1 * bet) player_hand = [self._draw_card(channel) for _ in range(2)] new_hand = [{ "hand": player_hand, "bet": bet, "standing": False, "busted": False, "blackjack": self._calc_hand_value(player_hand) == 21, "hit": True, "doubled": False }] updater = {"$set": {f"user hands.{user}": new_hand}} self._update_document(channel, updater) send_message = "{} entered the game with a bet of {} GwendoBucks" await ctx.send(send_message.format(user_name, bet)) self.bot.log(send_message.format(user_name, bet)) async def start(self, ctx: IntCont): """ Start a blackjack game. *Parameters* ------------ ctx: IntCont The context of the command. """ await self.bot.defer(ctx) channel = str(ctx.channel_id) blackjack_min_cards = 50 self.bot.log("Starting blackjack game") await ctx.send("Starting a new game of blackjack") cards_left = 0 if self._test_document(channel, "cards"): cards = self.access_document(channel, "cards") cards_left = len(cards["cards"]) # Shuffles if not enough cards if cards_left < blackjack_min_cards: self._shuffle_cards(channel) await ctx.channel.send("Shuffling the deck...") self.bot.log(f"Trying to start a blackjack game in {channel}") if self._test_document(channel): await ctx.channel.send(self.long_strings["Blackjack going on"]) self.bot.log("There was already a game going on") return dealer_hand = [self._draw_card(channel), self._draw_card(channel)] game_id = datetime.datetime.now().strftime('%Y%m%d%H%M%S') new_game = { "dealer hand": dealer_hand, "dealer busted": False, "game_id": game_id, "dealer blackjack": (self._calc_hand_value(dealer_hand) == 21), "user hands": {}, "all standing": False, "round": 0 } await ctx.channel.send(self.long_strings["Blackjack started"]) labels = [0] + [10**i for i in range(4)] betting_buttons = [ (f"Bet {i}", ["bet", "0", str(i)], 1) for i in labels ] await self._start_new(ctx.channel, new_game, betting_buttons) await asyncio.sleep(30) game = self.access_document(channel) if len(game["user hands"]) == 0: await ctx.channel.send("No one entered the game. Ending the game.") await ctx.channel.send(await self._blackjack_finish(ctx.channel)) return game_id = game["game_id"] # Loop of game rounds self.bot.log("start() calling _blackjack_loop()", channel) await self._blackjack_loop(ctx.channel, 1, game_id) async def hilo(self, ctx: IntCont): """ Get the hilo of the blackjack game. *Parameters* ------------ ctx: IntCont The context of the command. """ data = self.access_document(str(ctx.channel_id), "hilo", False) hilo = data["hilo"] if data else 0 await ctx.send(f"Hi-lo value: {hilo}", hidden=True) async def shuffle(self, ctx: IntCont): """ Shuffle the cards used for blackjack. *Parameters* ------------ ctx: IntCont The context of the command. """ self._shuffle_cards(str(ctx.channel_id)) await ctx.send("Shuffling the deck...") async def cards(self, ctx: IntCont): """ Get how many cards are left for blackjack. *Parameters* ------------ ctx: IntCont The context of the command. """ cards = self.access_document(str(ctx.channel_id), "cards", False) cards_left = len(cards["cards"]) if cards else 0 decks_left = round(cards_left/52, 1) if cards else 0 send_message = f"Cards left:\n{cards_left} cards, {decks_left} decks" await ctx.send(send_message, hidden=True) async def decode_interaction(self, ctx: ComponentContext, info: list[str]): if info[0].lower() == "bet": await self.enter_game(ctx, int(info[2])) elif info[0].lower() == "hit": await self._hit(ctx, int(info[2]) if len(info) > 2 else 0) elif info[0].lower() == "stand": await self._stand(ctx, int(info[2]) if len(info) > 2 else 0) elif info[0].lower() == "double": await self._double(ctx, int(info[2]) if len(info) > 2 else 0) elif info[0].lower() == "split": await self._split(ctx, int(info[2]) if len(info) > 2 else 0) class DrawBlackjack(CardDrawer): """ Draws the blackjack image. *Methods* --------- *Attributes* ------------ bot: Gwendolyn The instance of the bot. PLACEMENT: list The order to place the user hands in. """ # pylint: disable=too-few-public-methods def __init__(self, bot, game: Blackjack): """Initialize the class.""" super().__init__(bot, game) # pylint: disable=invalid-name self.PLACEMENT = [2, 1, 3, 0, 4] # pylint: enable=invalid-name self._set_card_size(197) def _draw_dealer_hand(self, game: dict, table: Image.Image): dealer_hand = self._draw_hand( game["dealer hand"], not game["all standing"], game["dealer busted"] if game["all standing"] else False, game["dealer blackjack"] if game["all standing"] else False ) paste_position = (800-self.CARD_BORDER, 20-self.CARD_BORDER) table.paste(dealer_hand, paste_position, dealer_hand) def _draw_player_name(self, user: str, placement: int, table: Image.Image): user_name = self.bot.database_funcs.get_name(user) font, font_size = self._adjust_font(50, user_name, 360) text_width, text_height = font.getsize(user_name) text_x = 32+(384*placement)+117-(text_width//2) text_y = 960+text_height colors = [BLACK, WHITE] text_image = self._draw_shadow_text(user_name, colors, font_size) table.paste(text_image, (text_x, text_y), text_image) def _draw_player_hands(self, all_hands: dict, table: Image.Image): for i, (user, user_hands) in enumerate(all_hands.items()): hand_images = [] position_x = 32-self.CARD_BORDER+(384*self.PLACEMENT[i]) for hand in user_hands: hand_images.append(self._draw_hand( hand["hand"], False, hand["busted"], hand["blackjack"] )) for index, image in enumerate(hand_images): position_y = 840 position_y -= self.CARD_BORDER + (140 * (len(user_hands)-index)) position = (position_x, position_y) table.paste(image, position, image) self._draw_player_name(user, self.PLACEMENT[i], table) def _draw_image(self, game: dict, table: Image.Image): """ Draw the table image. *Parameters* ------------ channel: str The id of the channel the game is in. """ self._draw_dealer_hand(game, table) self._draw_player_hands(game["user hands"], table) def _draw_hand(self, hand: dict, dealer: bool, busted: bool, blackjack: bool): """ Draw a hand. *Parameters* ------------ hand: dict The hand to draw. dealer: bool If the hand belongs to the dealer who hasn't shown their second card. busted: bool If the hand is busted. blackjack: bool If the hand has a blackjack. *Returns* --------- background: Image The image of the hand. """ self.bot.log("Drawing hand {hand}, {busted}, {blackjack}") background = self._draw_cards(hand, int(dealer)) if busted or blackjack: if busted: text = "BUSTED" last_color = RED font_size = 60 elif blackjack: text = "BLACKJACK" last_color = GOLD font_size = 35 colors = [BLACK, WHITE, last_color] text_image = self._draw_shadow_text(text, colors, font_size) width = background.size[0] text_width = self._get_font(font_size).getsize(text)[0] text_position = (int(width/2)-int(text_width/2), 85) background.paste(text_image, text_position, text_image) return background