diff --git a/cogs/GameCogs.py b/cogs/GameCogs.py index d5aaf05..1fd1e30 100644 --- a/cogs/GameCogs.py +++ b/cogs/GameCogs.py @@ -44,8 +44,7 @@ class BlackjackCog(commands.Cog): @cog_ext.cog_subcommand(**params["blackjackBet"]) async def blackjackBet(self, ctx, bet): - await ctx.defer() - await self.bot.games.blackjack.parseBlackjack(f"bet {bet}", ctx) + await self.bot.games.blackjack.playerDrawHand(ctx, bet) @cog_ext.cog_subcommand(**params["blackjackStand"]) async def blackjackStand(self, ctx, hand = ""): @@ -53,9 +52,8 @@ class BlackjackCog(commands.Cog): await self.bot.games.blackjack.parseBlackjack(f"stand {hand}", ctx) @cog_ext.cog_subcommand(**params["blackjackHit"]) - async def blackjackHit(self, ctx, hand = ""): - await ctx.defer() - await self.bot.games.blackjack.parseBlackjack(f"hit {hand}", ctx) + async def blackjackHit(self, ctx, hand = 0): + await self.bot.games.blackjack.hit(ctx, hand) class ConnectFourCog(commands.Cog): diff --git a/funcs/games/blackjack.py b/funcs/games/blackjack.py index eb7ce62..9b5d001 100644 --- a/funcs/games/blackjack.py +++ b/funcs/games/blackjack.py @@ -171,57 +171,71 @@ class Blackjack(): return hand, allStanding, preAllStanding # When players try to hit - def blackjackHit(self,channel,user,handNumber = 0): + async def hit(self, ctx, handNumber = 0): + try: + ctx.defer() + except: + self.bot.log("Defer failed") + channel = str(ctx.channel_id) + user = f"#{ctx.author.id}" + roundDone = False + game = self.bot.database["blackjack games"].find_one({"_id":channel}) if user in game["user hands"]: - hand, handNumber = self.getHandNumber(game["user hands"][user],handNumber) + userHands = game["user hands"][user] + hand, handNumber = self.getHandNumber(userHands, handNumber) - if hand != None: - if game["round"] > 0: - if hand["hit"] == False: - if hand["standing"] == False: - hand["hand"].append(self.drawCard(channel)) - hand["hit"] = True + if hand == None: + logMessage = "They didn't specify a hand" + sendMessage = "You need to specify a hand" + elif game["round"] <= 0: + logMessage = "They tried to hit on the 0th round" + sendMessage = "You can't hit before you see your cards" + elif hand["hit"]: + logMessage = "They've already hit this round" + sendMessage = "You've already hit this round" + elif hand["standing"]: + logMessage = "They're already standing" + sendMessage = "You can't hit when you're standing" + else + hand["hand"].append(self.drawCard(channel)) + hand["hit"] = True - handValue = self.calcHandValue(hand["hand"]) + handValue = self.calcHandValue(hand["hand"]) - if handValue > 21: - hand["busted"] = True + if handValue > 21: + hand["busted"] = True - if handNumber == 2: - self.bot.database["blackjack games"].update_one({"_id":channel}, - {"$set":{"user hands."+user+".other hand":hand}}) - elif handNumber == 3: - self.bot.database["blackjack games"].update_one({"_id":channel}, - {"$set":{"user hands."+user+".third hand":hand}}) - elif handNumber == 4: - self.bot.database["blackjack games"].update_one({"_id":channel}, - {"$set":{"user hands."+user+".fourth hand":hand}}) - else: - self.bot.database["blackjack games"].update_one({"_id":channel}, - {"$set":{"user hands."+user:hand}}) - - response = "accept" - roundDone = self.isRoundDone(self.bot.database["blackjack games"].find_one({"_id":channel})) - - return response + str(roundDone)[0] + str(game["round"]) - else: - self.bot.log(user+" is already standing") - return "You can't hit when you're standing" - else: - self.bot.log(user+" has already hit this round") - return "You've already hit this round" + if handNumber == 2: + self.bot.database["blackjack games"].update_one({"_id":channel}, + {"$set":{"user hands."+user+".other hand":hand}}) + elif handNumber == 3: + self.bot.database["blackjack games"].update_one({"_id":channel}, + {"$set":{"user hands."+user+".third hand":hand}}) + elif handNumber == 4: + self.bot.database["blackjack games"].update_one({"_id":channel}, + {"$set":{"user hands."+user+".fourth hand":hand}}) else: - self.bot.log(user+" tried to hit on the 0th round") - return "You can't hit before you see your cards" - else: - self.bot.log(user+" didn't specify a hand") - return "You need to specify a hand" + self.bot.database["blackjack games"].update_one({"_id":channel}, + {"$set":{"user hands."+user:hand}}) + + roundDone = self.isRoundDone(self.bot.database["blackjack games"].find_one({"_id":channel})) + + sendMessage = f"{ctx.author.display_name} hit" + logMessage = "They succeeded" else: - self.bot.log(user+" tried to hit without being in the game") - return "You have to enter the game before you can hit" + logMessage = "They tried to hit without being in the game" + sendMessage = "You have to enter the game before you can hit" + + await ctx.send(sendMessage) + self.bot.log(logMessage) + + if roundDone: + gameID = game["gameID"] + self.bot.log("Hit calling self.blackjackLoop()", channel) + await self.blackjackLoop(ctx.channel, game["round"]+1, gameID) # When players try to double down def blackjackDouble(self,channel,user,handNumber = 0): @@ -452,56 +466,66 @@ class Blackjack(): return "You can only split 3 times","" # Player enters the game and draws a hand - def blackjackPlayerDrawHand(self,channel,user,bet): - game = self.bot.database["blackjack games"].find_one({"_id":channel}) + async def playerDrawHand(self, ctx, bet : int): + try: + ctx.defer() + except: + self.bot.log("Defer failed") + channel = str(ctx.channel_id) + user = f"#{ctx.author.id}" + collection = self.bot.database["blackjack games"] + game = collection.find_one({"_id":channel}) + userName = self.bot.databaseFuncs.getName(user) - self.bot.log(self.bot.databaseFuncs.getName(user)+" is trying to join the game in "+channel) + self.bot.log(f"{userName} is trying to join the Blackjack game") - if game != None: - if user not in game["user hands"]: - if len(game["user hands"]) < 5: - if game["round"] == 0: - if bet >= 0: - if self.bot.money.checkBalance(user) >= bet: - self.bot.money.addMoney(user,-1 * bet) - playerHand = [self.drawCard(channel),self.drawCard(channel)] - - handValue = self.calcHandValue(playerHand) - - if handValue == 21: - blackjackHand = True - else: - blackjackHand = False - - newHand = {"hand":playerHand, - "bet":bet,"standing":False,"busted":False,"blackjack":blackjackHand,"hit":True, - "doubled":False,"split":0,"other hand":{},"third hand":{},"fourth hand":{}} - self.bot.database["blackjack games"].update_one({"_id":channel}, - {"$set":{"user hands."+user:newHand}}) - - self.bot.log(f"{self.bot.databaseFuncs.getName(user)} entered the game with a bet of {bet}") - return f"{self.bot.databaseFuncs.getName(user)} entered the game with a bet of {bet}" - else: - self.bot.log(user+" doesn't have enough GwendoBucks") - return "You don't have enough GwendoBucks to place that bet" - else: - self.bot.log(user+" tried to bet a negative amount") - return "You can't bet a negative amount" - else: - self.bot.log("The table is no longer open for bets") - return "The table is no longer open for bets" - else: - self.bot.log("There are already 5 players in the game.") - return "There's already a maximum of players at the table." - else: - self.bot.log(user+" is already in the game") - return "You've already entered this game" + if game == None: + sendMessage = "There is no game going on in this channel" + logMessage = sendMessage + elif user in game["user hands"]: + sendMessage = "You're already in the game!" + logMessage = "They're already in the game" + elif len(game["user hands"]) >= 5: + sendMessage = "There can't be more than 5 players in a game" + logMessage = "There were already 5 players in the game" + elif game["round"] != 0: + sendMessage = "The table is no longer taking bets" + logMessage = "They tried to join after the game begun" + elif bet < 0: + sendMessage = "You can't bet a negative amount" + logMessage = "They tried to bet a negative amount" + elif self.bot.money.checkBalance(user) < bet: + sendMessage = "You don't have enough GwendoBucks" + logMessage = "They didn't have enough GwendoBucks" else: - self.bot.log("There is no game going on in "+channel) - return "There is no game going on in this channel" + self.bot.money.addMoney(user,-1 * bet) + playerHand = [self.drawCard(channel) for _ in range(2)] + + handValue = self.calcHandValue(playerHand) + + if handValue == 21: + blackjackHand = True + else: + blackjackHand = False + + newHand = {"hand":playerHand, "bet":bet, "standing":False, + "busted":False, "blackjack":blackjackHand, "hit":True, + "doubled":False, "split":0, "other hand":{}, + "third hand":{}, "fourth hand":{}} + + function = {"$set":{f"user hands.{user}":newHand}} + collection.update_one({"_id":channel}, function) + + enterGameText = "entered the game with a bet of" + betText = f"{bet} GwendoBucks" + sendMessage = f"{userName} {enterGameText} {betText}" + logMessage = sendMessage + + self.bot.log(sendMessage) + await ctx.send(logMessage) # Starts a game of blackjack - def blackjackStart(self,channel:str): + def blackjackStart(self, channel:str): game = self.bot.database["blackjack games"].find_one({"_id":channel}) self.bot.log("Trying to start a blackjack game in "+channel) @@ -751,38 +775,6 @@ class Blackjack(): else: await ctx.channel.send(new_message) - # Entering game and placing bet - elif content.startswith("bet"): - commands = content.split(" ") - amount = int(commands[1]) - response = self.blackjackPlayerDrawHand(str(channel),"#"+str(ctx.author.id),amount) - await ctx.send(response) - - # Hitting - elif content.startswith("hit"): - if content == "hit": - response = self.blackjackHit(str(channel),"#"+str(ctx.author.id)) - else: - commands = content.split(" ") - try: - handNumber = int(commands[1]) - except: - handNumber = 0 - response = self.blackjackHit(str(channel),"#"+str(ctx.author.id),handNumber) - - if response.startswith("accept"): - await ctx.send(f"{ctx.author.display_name} hit") - #try: - if response[6] == "T": - gameID = self.bot.database["blackjack games"].find_one({"_id":str(channel)})["gameID"] - self.bot.log("Hit calling self.blackjackLoop()",str(channel)) - await self.blackjackLoop(ctx.channel,int(response[7:])+1,gameID) - #except: - # self.bot.log("Something fucked up (error code 1320)",str(channel)) - else: - await ctx.send(response) - - # Standing elif content.startswith("stand"): if content == "hit":