Skip to content

Commit

Permalink
refactor games.py
Browse files Browse the repository at this point in the history
  • Loading branch information
MufaddalHakim committed Oct 6, 2021
1 parent 184eb28 commit 0f8f990
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ log.txt
# PyCharm
.idea

# VSCode
.vscode

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
Expand Down
7 changes: 4 additions & 3 deletions bot/cogs/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self, bot):

async def evaluate_results(self, game):
participants = game.participants
dealer_card_value = game.calculate_card_value()
dealer_card_value = game.dealer.calculate_card_value()
for participant in list(participants):
player = participants[participant]
me = self.bot.get_user(player.user_id)
Expand All @@ -40,9 +40,9 @@ async def evaluate_results(self, game):

async def dealers_play(self, game):
while True:
card_value = game.calculate_card_value(dealer=True)
card_value = game.dealer.calculate_card_value()
if card_value < 17:
game.cards.append(game.deck.get_random_card())
game.dealer.cards.append(game.deck.get_random_card())
else:
break
await self.evaluate_results(game)
Expand Down Expand Up @@ -105,6 +105,7 @@ async def init_blackjack(self, ctx, bet_amount):
game = self.live_games[ctx.channel.id]
else:
game = Game(ctx.channel.id)
game.deck.give_random_card(game.dealer, 2)
self.live_games[ctx.channel.id] = game

if not len(game.participants) == blackjack_player_limit:
Expand Down
2 changes: 1 addition & 1 deletion bot/utils/embed_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def black_jack_embed(user: User, player, outcome: str = None, hidden: bool = Tru
:return: discord.Embed
"""
embed = black_jack_template(user, player, "", Color.gold())
embed.add_field(name="Dealer hand", value=player.game.get_emote_string(hidden=hidden))
embed.add_field(name="Dealer hand", value=player.game.dealer.get_emote_string(hidden=hidden))
if outcome == "win":
embed.colour = Color.dark_green()
embed.description = "**Outcome:** You won!"
Expand Down
12 changes: 6 additions & 6 deletions bot/utils/gambling_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@


class Player:
def __init__(self, user_id: int, bet_amount, game, message: discord.Message = None):
def __init__(self, user_id: int, bet_amount, game, message: discord.Message = None, is_dealer=False):
self.user_id = user_id
self.bet_amount = int(bet_amount)
self.card_value = 0
self.cards = []
self.game = game
self.message = message
self.stay = False
self.is_dealer = is_dealer

def calculate_card_value(self, dealer=False) -> int:
def calculate_card_value(self) -> int:
value = 0
a_count = 0
for card in self.cards:
Expand All @@ -26,7 +27,7 @@ def calculate_card_value(self, dealer=False) -> int:
else:
value += int(card.name)

if not dealer:
if not self.is_dealer:
if a_count != 0:
for _ in range(a_count):
if value + 11 > 21:
Expand All @@ -52,13 +53,12 @@ def get_emote_string(self, hidden=True) -> str:
return f"{self.cards[0].emote} + {blank_card_emoji}\nvalue: ?"


class Game(Player):
class Game():
def __init__(self, channel: discord.TextChannel): # noqa
self.channel = channel
self.participants = {}
self.deck = Deck()
self.cards = self.deck.get_random_cards(2)
self.card_value = self.calculate_card_value()
self.dealer = Player(0000, 0, game=self, is_dealer=True)


class Card(object):
Expand Down

0 comments on commit 0f8f990

Please sign in to comment.