982 lines
32 KiB
Python
982 lines
32 KiB
Python
"""
|
|
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 SlashContext # Used for typehints
|
|
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.
|
|
"""
|
|
round_done = True
|
|
|
|
for user_hands in game["user hands"].values():
|
|
for hand in user_hands:
|
|
if (not hand["hit"]) and (not hand["standing"]):
|
|
round_done = False
|
|
break
|
|
|
|
return round_done
|
|
|
|
class Blackjack(CardGame):
|
|
"""
|
|
Deals with blackjack commands and gameplay logic.
|
|
|
|
*Methods*
|
|
---------
|
|
hit(ctx: SlashContext, hand_number: int = 0)
|
|
double(ctx: SlashContext, hand_number: int = 0)
|
|
stand(ctx: SlashContext, hand_number: int = 0)
|
|
split(ctx: SlashContext, hand_number: int = 0)
|
|
enter_game(ctx: SlashContext, bet: int)
|
|
start(ctx: SlashContext)
|
|
hilo(ctx: SlashContext)
|
|
shuffle(ctx: SlashContext)
|
|
cards(ctx: SlashContext)
|
|
"""
|
|
|
|
def __init__(self, bot):
|
|
"""Initialize the class."""
|
|
super().__init__(bot)
|
|
self.decks_used = 4
|
|
self.game_name = "blackjack"
|
|
self.draw = DrawBlackjack(bot, self)
|
|
|
|
async def _test_valid_command(self, game: dict, user: str,
|
|
hand_number: int, ctx: SlashContext):
|
|
valid_command = False
|
|
|
|
if game is None:
|
|
self.bot.log("There's no game going on")
|
|
await ctx.send("There's no game going on")
|
|
elif user not in game["user hands"]:
|
|
self.bot.log("They tried something without being in the game")
|
|
await ctx.send("You have to enter the game before you can hit")
|
|
|
|
elif len(game["user hands"][user]) > 1 and hand_number == 0:
|
|
self.bot.log("They didn't specify a hand")
|
|
await ctx.send("You need to specify a hand")
|
|
elif len(game["user hands"][user]) < hand_number:
|
|
self.bot.log("They tried something with a hand they don't have")
|
|
await ctx.send("You don't have that many hands")
|
|
elif game["round"] <= 0:
|
|
self.bot.log("They tried to do something on the 0th round")
|
|
await ctx.send("You haven't seen your cards yet!")
|
|
elif game["user hands"][user][max(hand_number-1,0)]["hit"]:
|
|
self.bot.log("They've already hit this round")
|
|
await ctx.send("You've already hit this round")
|
|
elif game["user hands"][user][max(hand_number-1,0)]["standing"]:
|
|
self.bot.log("They're already standing")
|
|
await ctx.send("You're already standing")
|
|
else:
|
|
valid_command = 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)
|
|
value = self._calc_hand_value([drawn_card])
|
|
|
|
if value <= 6:
|
|
self._update_document(channel, {"$inc": {"hilo": 1}}, "hilo")
|
|
elif value >= 10:
|
|
self._update_document(channel, {"$inc": {"hilo": -1}}, "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)
|
|
|
|
done = False
|
|
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)
|
|
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)
|
|
|
|
done = False
|
|
|
|
self._update_document(channel, {"$inc": {"round": 1}})
|
|
|
|
all_standing = True
|
|
pre_all_standing = True
|
|
message = self.long_strings["Blackjack all players standing"]
|
|
|
|
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))
|
|
new_hands, all_standing, pre_all_standing = standing_test
|
|
hand_updater = {"$set": {"user hands."+user: new_hands}}
|
|
self._update_document(channel, hand_updater)
|
|
|
|
if all_standing:
|
|
self._update_document(channel, {"$set": {"all standing": True}})
|
|
|
|
if all_standing:
|
|
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
|
|
|
|
def _blackjack_finish(self, channel: str):
|
|
"""
|
|
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(channel)
|
|
|
|
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 += "{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"
|
|
|
|
winnings_text += f" {reason}\n"
|
|
|
|
self.bot.money.addMoney(user, net_winnings)
|
|
|
|
self._delete_document(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:
|
|
bet = hand["bet"]
|
|
winnings += -1 * bet
|
|
|
|
if hand["blackjack"] and not dealer_blackjack:
|
|
reason += "\n(blackjack)"
|
|
net_winnings += math.floor(2.5 * bet)
|
|
elif dealer_blackjack:
|
|
reason += "\n(dealer blackjack)"
|
|
elif hand["busted"]:
|
|
reason += "\n(busted)"
|
|
else:
|
|
hand_value = self._calc_hand_value(hand["hand"])
|
|
if dealer_busted:
|
|
reason += "\n(dealer busted)"
|
|
winnings += 2 * bet
|
|
elif hand_value > dealer_value:
|
|
winnings += 2 * bet
|
|
reason += "\n(highest value)"
|
|
elif hand_value == dealer_value:
|
|
reason += "\n(pushed)"
|
|
winnings += bet
|
|
else:
|
|
reason += "\n(highest value)"
|
|
|
|
winnings += net_winnings
|
|
return winnings, net_winnings, reason[1:]
|
|
|
|
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)
|
|
await self._send_image(channel)
|
|
|
|
if all_standing:
|
|
await asyncio.sleep(5)
|
|
else:
|
|
await asyncio.sleep(120)
|
|
|
|
game = self.access_document(str(channel.id))
|
|
|
|
if game is None:
|
|
self.bot.log(f"Ending loop on round {game_round}", str(channel.id))
|
|
return
|
|
|
|
real_round = game["round"] or -1
|
|
real_id = game["game_id"] or -1
|
|
if game_round != real_round or game_id != real_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:
|
|
new_message = self._blackjack_finish(str(channel.id))
|
|
await channel.send(new_message)
|
|
|
|
|
|
async def hit(self, ctx: SlashContext, hand_number: int = 0):
|
|
"""
|
|
Hit on a hand.
|
|
|
|
*Parameters*
|
|
------------
|
|
ctx: SlashContext
|
|
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)
|
|
|
|
valid_command = self._test_valid_command(game, user, hand_number, ctx)
|
|
|
|
if not valid_command:
|
|
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: SlashContext, hand_number: int = 0):
|
|
"""
|
|
Double a hand.
|
|
|
|
*Parameters*
|
|
------------
|
|
ctx: SlashContext
|
|
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)
|
|
|
|
valid_command = self._test_valid_command(game, user, hand_number, ctx)
|
|
|
|
if not valid_command:
|
|
return
|
|
|
|
if len(game["user hands"][user][max(hand_number-1,0)]["hand"]) != 2:
|
|
await ctx.send("They tried to double after round 1")
|
|
self.bot.log("You can only double on the first round")
|
|
elif balance < game["user hands"][user][max(hand_number-1,0)]["bet"]:
|
|
await ctx.send("They tried to double without having enough money")
|
|
self.bot.log("You can't double when you don't have 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: SlashContext, hand_number: int = 0):
|
|
"""
|
|
Stand on a hand.
|
|
|
|
*Parameters*
|
|
------------
|
|
ctx: SlashContext
|
|
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)
|
|
|
|
valid_command = self._test_valid_command(game, user, hand_number, ctx)
|
|
|
|
if not valid_command:
|
|
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: SlashContext, hand_number: int = 0):
|
|
"""
|
|
Split a hand.
|
|
|
|
*Parameters*
|
|
------------
|
|
ctx: SlashContext
|
|
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)
|
|
|
|
|
|
valid_command = self._test_valid_command(game, user, hand_number, ctx)
|
|
|
|
if not valid_command:
|
|
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: SlashContext, bet: int):
|
|
"""
|
|
Enter the blackjack game.
|
|
|
|
*Parameters*
|
|
------------
|
|
ctx: SlashContext
|
|
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 game is None:
|
|
send_message = "There is no game going on in this channel"
|
|
log_message = send_message
|
|
elif user in game["user hands"]:
|
|
send_message = "You're already in the game!"
|
|
log_message = "They're already in the game"
|
|
elif len(game["user hands"]) >= 5:
|
|
send_message = "There can't be more than 5 players in a game"
|
|
log_message = "There were already 5 players in the game"
|
|
elif game["round"] != 0:
|
|
send_message = "The table is no longer taking bets"
|
|
log_message = "They tried to join after the game begun"
|
|
elif bet < 0:
|
|
send_message = "You can't bet a negative amount"
|
|
log_message = "They tried to bet a negative amount"
|
|
elif self.bot.money.checkBalance(user) < bet:
|
|
send_message = "You don't have enough GwendoBucks"
|
|
log_message = "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"
|
|
send_message = send_message.format(user_name, bet)
|
|
log_message = send_message
|
|
|
|
self.bot.log(log_message)
|
|
await ctx.send(send_message)
|
|
|
|
async def start(self, ctx: SlashContext):
|
|
"""
|
|
Start a blackjack game.
|
|
|
|
*Parameters*
|
|
------------
|
|
ctx: SlashContext
|
|
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
|
|
cards = self.access_document(channel, "cards")
|
|
if cards is not None:
|
|
cards_left = len(cards["cards"])
|
|
|
|
# Shuffles if not enough cards
|
|
if cards_left < blackjack_min_cards:
|
|
self._shuffle_cards(channel)
|
|
self.bot.log("Shuffling the blackjack deck...", channel)
|
|
await ctx.channel.send("Shuffling the deck...")
|
|
|
|
game = self.access_document(channel)
|
|
|
|
self.bot.log(f"Trying to start a blackjack game in {channel}")
|
|
|
|
if game is not None:
|
|
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 = {
|
|
"_id": channel, "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"])
|
|
await self._start_new(ctx.channel, new_game)
|
|
|
|
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(self._blackjack_finish(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: SlashContext):
|
|
"""
|
|
Get the hilo of the blackjack game.
|
|
|
|
*Parameters*
|
|
------------
|
|
ctx: SlashContext
|
|
The context of the command.
|
|
"""
|
|
data = self.access_document(str(ctx.channel_id), "hilo")
|
|
hilo = data["hilo"] if data else 0
|
|
|
|
await ctx.send(f"Hi-lo value: {hilo}", hidden=True)
|
|
|
|
async def shuffle(self, ctx: SlashContext):
|
|
"""
|
|
Shuffle the cards used for blackjack.
|
|
|
|
*Parameters*
|
|
------------
|
|
ctx: SlashContext
|
|
The context of the command.
|
|
"""
|
|
self._shuffle_cards(str(ctx.channel_id))
|
|
self.bot.log("Shuffling the blackjack deck...", str(ctx.channel_id))
|
|
await ctx.send("Shuffling the deck...")
|
|
|
|
async def cards(self, ctx: SlashContext):
|
|
"""
|
|
Get how many cards are left for blackjack.
|
|
|
|
*Parameters*
|
|
------------
|
|
ctx: SlashContext
|
|
The context of the command.
|
|
"""
|
|
cards = self.access_document(str(ctx.channel_id), "cards")
|
|
cards_left = len(cards["cards"]) if cards else 0
|
|
decks_left = round(cards_left/52, 1)
|
|
|
|
send_message = f"Cards left:\n{cards_left} cards, {decks_left} decks"
|
|
await ctx.send(send_message, hidden=True)
|
|
|
|
|
|
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"],
|
|
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)
|
|
background.paste(text_image, text_position, text_image)
|
|
|
|
width = background.size[0]
|
|
text_width = self._get_font(font_size).getsize(text)[0]
|
|
text_position = (int(width/2)-int(text_width/2), 85)
|
|
|
|
return background
|