🧹 Cleaning up blackjack commands

This commit is contained in:
Nikolaj Danger
2021-04-02 18:23:58 +02:00
parent aff29a8d91
commit 33eedf8981
2 changed files with 113 additions and 123 deletions

View File

@@ -44,8 +44,7 @@ class BlackjackCog(commands.Cog):
@cog_ext.cog_subcommand(**params["blackjackBet"]) @cog_ext.cog_subcommand(**params["blackjackBet"])
async def blackjackBet(self, ctx, bet): async def blackjackBet(self, ctx, bet):
await ctx.defer() await self.bot.games.blackjack.playerDrawHand(ctx, bet)
await self.bot.games.blackjack.parseBlackjack(f"bet {bet}", ctx)
@cog_ext.cog_subcommand(**params["blackjackStand"]) @cog_ext.cog_subcommand(**params["blackjackStand"])
async def blackjackStand(self, ctx, hand = ""): async def blackjackStand(self, ctx, hand = ""):
@@ -53,9 +52,8 @@ class BlackjackCog(commands.Cog):
await self.bot.games.blackjack.parseBlackjack(f"stand {hand}", ctx) await self.bot.games.blackjack.parseBlackjack(f"stand {hand}", ctx)
@cog_ext.cog_subcommand(**params["blackjackHit"]) @cog_ext.cog_subcommand(**params["blackjackHit"])
async def blackjackHit(self, ctx, hand = ""): async def blackjackHit(self, ctx, hand = 0):
await ctx.defer() await self.bot.games.blackjack.hit(ctx, hand)
await self.bot.games.blackjack.parseBlackjack(f"hit {hand}", ctx)
class ConnectFourCog(commands.Cog): class ConnectFourCog(commands.Cog):

View File

@@ -171,17 +171,35 @@ class Blackjack():
return hand, allStanding, preAllStanding return hand, allStanding, preAllStanding
# When players try to hit # 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}) game = self.bot.database["blackjack games"].find_one({"_id":channel})
if user in game["user hands"]: 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 hand == None:
if game["round"] > 0: logMessage = "They didn't specify a hand"
if hand["hit"] == False: sendMessage = "You need to specify a hand"
if hand["standing"] == False: 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["hand"].append(self.drawCard(channel))
hand["hit"] = True hand["hit"] = True
@@ -203,25 +221,21 @@ class Blackjack():
self.bot.database["blackjack games"].update_one({"_id":channel}, self.bot.database["blackjack games"].update_one({"_id":channel},
{"$set":{"user hands."+user:hand}}) {"$set":{"user hands."+user:hand}})
response = "accept"
roundDone = self.isRoundDone(self.bot.database["blackjack games"].find_one({"_id":channel})) roundDone = self.isRoundDone(self.bot.database["blackjack games"].find_one({"_id":channel}))
return response + str(roundDone)[0] + str(game["round"]) sendMessage = f"{ctx.author.display_name} hit"
logMessage = "They succeeded"
else: else:
self.bot.log(user+" is already standing") logMessage = "They tried to hit without being in the game"
return "You can't hit when you're standing" sendMessage = "You have to enter the game before you can hit"
else:
self.bot.log(user+" has already hit this round") await ctx.send(sendMessage)
return "You've already hit this round" self.bot.log(logMessage)
else:
self.bot.log(user+" tried to hit on the 0th round") if roundDone:
return "You can't hit before you see your cards" gameID = game["gameID"]
else: self.bot.log("Hit calling self.blackjackLoop()", channel)
self.bot.log(user+" didn't specify a hand") await self.blackjackLoop(ctx.channel, game["round"]+1, gameID)
return "You need to specify a hand"
else:
self.bot.log(user+" tried to hit without being in the game")
return "You have to enter the game before you can hit"
# When players try to double down # When players try to double down
def blackjackDouble(self,channel,user,handNumber = 0): def blackjackDouble(self,channel,user,handNumber = 0):
@@ -452,19 +466,40 @@ class Blackjack():
return "You can only split 3 times","" return "You can only split 3 times",""
# Player enters the game and draws a hand # Player enters the game and draws a hand
def blackjackPlayerDrawHand(self,channel,user,bet): async def playerDrawHand(self, ctx, bet : int):
game = self.bot.database["blackjack games"].find_one({"_id":channel}) 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 game == None:
if user not in game["user hands"]: sendMessage = "There is no game going on in this channel"
if len(game["user hands"]) < 5: logMessage = sendMessage
if game["round"] == 0: elif user in game["user hands"]:
if bet >= 0: sendMessage = "You're already in the game!"
if self.bot.money.checkBalance(user) >= bet: 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.money.addMoney(user,-1 * bet) self.bot.money.addMoney(user,-1 * bet)
playerHand = [self.drawCard(channel),self.drawCard(channel)] playerHand = [self.drawCard(channel) for _ in range(2)]
handValue = self.calcHandValue(playerHand) handValue = self.calcHandValue(playerHand)
@@ -473,35 +508,24 @@ class Blackjack():
else: else:
blackjackHand = False blackjackHand = False
newHand = {"hand":playerHand, newHand = {"hand":playerHand, "bet":bet, "standing":False,
"bet":bet,"standing":False,"busted":False,"blackjack":blackjackHand,"hit":True, "busted":False, "blackjack":blackjackHand, "hit":True,
"doubled":False,"split":0,"other hand":{},"third hand":{},"fourth hand":{}} "doubled":False, "split":0, "other hand":{},
self.bot.database["blackjack games"].update_one({"_id":channel}, "third hand":{}, "fourth hand":{}}
{"$set":{"user hands."+user:newHand}})
self.bot.log(f"{self.bot.databaseFuncs.getName(user)} entered the game with a bet of {bet}") function = {"$set":{f"user hands.{user}":newHand}}
return f"{self.bot.databaseFuncs.getName(user)} entered the game with a bet of {bet}" collection.update_one({"_id":channel}, function)
else:
self.bot.log(user+" doesn't have enough GwendoBucks") enterGameText = "entered the game with a bet of"
return "You don't have enough GwendoBucks to place that bet" betText = f"{bet} GwendoBucks"
else: sendMessage = f"{userName} {enterGameText} {betText}"
self.bot.log(user+" tried to bet a negative amount") logMessage = sendMessage
return "You can't bet a negative amount"
else: self.bot.log(sendMessage)
self.bot.log("The table is no longer open for bets") await ctx.send(logMessage)
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"
else:
self.bot.log("There is no game going on in "+channel)
return "There is no game going on in this channel"
# Starts a game of blackjack # 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}) game = self.bot.database["blackjack games"].find_one({"_id":channel})
self.bot.log("Trying to start a blackjack game in "+channel) self.bot.log("Trying to start a blackjack game in "+channel)
@@ -751,38 +775,6 @@ class Blackjack():
else: else:
await ctx.channel.send(new_message) 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 # Standing
elif content.startswith("stand"): elif content.startswith("stand"):
if content == "hit": if content == "hit":