Fully converted to slash commands

This commit is contained in:
NikolajDanger
2021-03-31 00:38:51 +02:00
parent a8a7e5eabd
commit b345720468
50 changed files with 1102 additions and 1111 deletions

View File

@ -6,7 +6,7 @@ import discord
from shutil import copyfile
from funcs import logThis, replaceMultiple
from utils import replaceMultiple
from .blackjackDraw import DrawBlackjack
class Blackjack():
@ -16,7 +16,7 @@ class Blackjack():
# Shuffles the blackjack cards
def blackjackShuffle(self, decks, channel):
logThis("Shuffling the blackjack deck")
self.bot.log("Shuffling the blackjack deck")
with open("resources/games/deckofCards.txt","r") as f:
deck = f.read()
@ -27,7 +27,7 @@ class Blackjack():
self.bot.database["blackjack cards"].update_one({"_id":channel},{"$set":{"_id":channel,"cards":allDecks}},upsert=True)
# Creates hilo file
logThis("creating hilo doc for "+channel)
self.bot.log("creating hilo doc for "+channel)
data = 0
self.bot.database["hilo"].update_one({"_id":channel},{"$set":{"_id":channel,"hilo":data}},upsert=True)
@ -35,7 +35,7 @@ class Blackjack():
# Calculates the value of a blackjack hand
def calcHandValue(self, hand : list):
logThis("Calculating hand value")
self.bot.log("Calculating hand value", level = 10)
values = []
values.append(0)
@ -58,13 +58,13 @@ class Blackjack():
if value <= 21:
handValue = value
logThis("Calculated "+str(hand)+" to be "+str(handValue))
self.bot.log("Calculated "+str(hand)+" to be "+str(handValue), level = 10)
return handValue
# Draws a card from the deck
def drawCard(self, channel):
logThis("drawing a card")
self.bot.log("drawing a card", level = 10)
drawnCard = self.bot.database["blackjack cards"].find_one({"_id":channel})["cards"][0]
self.bot.database["blackjack cards"].update_one({"_id":channel},{"$pop":{"cards":-1}})
@ -97,7 +97,7 @@ class Blackjack():
# Goes to the next round and calculates some stuff
def blackjackContinue(self, channel):
logThis("Continuing blackjack game")
self.bot.log("Continuing blackjack game", level = 10)
game = self.bot.database["blackjack games"].find_one({"_id":channel})
done = False
@ -109,20 +109,20 @@ class Blackjack():
message = "All players are standing. The dealer now shows his cards and draws."
if game["all standing"]:
logThis("All are standing")
self.bot.log("All are standing")
done = self.dealerDraw(channel)
message = "The dealer draws a card."
game = self.bot.database["blackjack games"].find_one({"_id":channel})
logThis("Testing if all are standing")
self.bot.log("Testing if all are standing", level = 10)
for user in game["user hands"]:
try:
newUser, allStanding, preAllStanding = self.testIfStanding(game["user hands"][user],allStanding,preAllStanding,True)
self.bot.database["blackjack games"].update_one({"_id":channel},{"$set":{"user hands."+user:newUser}})
except:
logThis("Error in testing if all are standing (error code 1331)")
self.bot.log("Error in testing if all are standing (error code 1331)")
if allStanding:
self.bot.database["blackjack games"].update_one({"_id":channel},{"$set":{"all standing":True}})
@ -130,7 +130,7 @@ class Blackjack():
try:
self.draw.drawImage(channel)
except:
logThis("Error drawing blackjack table (error code 1340)")
self.bot.log("Error drawing blackjack table (error code 1340)")
if allStanding:
if done == False:
@ -209,19 +209,19 @@ class Blackjack():
return response + str(roundDone)[0] + str(game["round"])
else:
logThis(user+" is already standing")
self.bot.log(user+" is already standing")
return "You can't hit when you're standing"
else:
logThis(user+" has already hit this round")
self.bot.log(user+" has already hit this round")
return "You've already hit this round"
else:
logThis(user+" tried to hit on the 0th round")
self.bot.log(user+" tried to hit on the 0th round")
return "You can't hit before you see your cards"
else:
logThis(user+" didn't specify a hand")
self.bot.log(user+" didn't specify a hand")
return "You need to specify a hand"
else:
logThis(user+" tried to hit without being in the game")
self.bot.log(user+" tried to hit without being in the game")
return "You have to enter the game before you can hit"
@ -267,27 +267,27 @@ class Blackjack():
roundDone = self.isRoundDone(self.bot.database["blackjack games"].find_one({"_id":channel}))
return "Adding another "+str(bet)+" GwendoBucks to "+self.bot.funcs.getName(user)+"'s bet and drawing another card.",str(roundDone)[0] + str(game["round"])
return "Adding another "+str(bet)+" GwendoBucks to "+self.bot.databaseFuncs.getName(user)+"'s bet and drawing another card.",str(roundDone)[0] + str(game["round"])
else:
logThis(user+" doesn't have enough GwendoBucks")
self.bot.log(user+" doesn't have enough GwendoBucks")
return "You don't have enough GwendoBucks",""
else:
logThis(user+" tried to double on round "+str(game["round"]))
self.bot.log(user+" tried to double on round "+str(game["round"]))
return "You can only double down on the first round",""
else:
logThis(user+" is already standing")
self.bot.log(user+" is already standing")
return "You can't double when you're standing",""
else:
logThis(user+" has already hit this round")
self.bot.log(user+" has already hit this round")
return "You've already hit this round",""
else:
logThis(user+" tried to double on the 0th round")
self.bot.log(user+" tried to double on the 0th round")
return "You can't double down before you see your cards",""
else:
logThis(user+" didn't specify a hand")
self.bot.log(user+" didn't specify a hand")
return "You need to specify which hand"
else:
logThis(user+" tried to double without being in the game")
self.bot.log(user+" tried to double without being in the game")
return "You can't double when you're not in the game",""
# When players try to stand
@ -322,19 +322,19 @@ class Blackjack():
return response + str(roundDone)[0] + str(game["round"])
else:
logThis(user+" is already standing")
self.bot.log(user+" is already standing")
return "You're already standing"
else:
logThis(user+" has already hit this round")
self.bot.log(user+" has already hit this round")
return "You've already hit this round"
else:
logThis(user+" tried to stand on the first round")
self.bot.log(user+" tried to stand on the first round")
return "You can't stand before you see your cards"
else:
logThis(user+" didn't specify a hand")
self.bot.log(user+" didn't specify a hand")
return "You need to specify which hand"
else:
logThis(user+" tried to stand without being in the game")
self.bot.log(user+" tried to stand without being in the game")
return "You have to enter the game before you can stand"
# When players try to split
@ -355,7 +355,7 @@ class Blackjack():
elif handNumber == 3:
hand = game["user hands"][user]["third hand"]
else:
logThis(user+" tried to hit without specifying which hand")
self.bot.log(user+" tried to hit without specifying which hand")
return "You have to specify the hand you're hitting with."
if game["user hands"][user]["split"] == 1:
@ -365,7 +365,7 @@ class Blackjack():
newHand = game["user hands"][user]["fourth hand"]
otherHand = 4
else:
logThis(user+" tried to split without specifying which hand")
self.bot.log(user+" tried to split without specifying which hand")
return "You have to specify the hand you're splitting.",""
if game["user hands"][user]["split"] < 3:
@ -430,34 +430,34 @@ class Blackjack():
roundDone = self.isRoundDone(self.bot.database["blackjack games"].find_one({"_id":channel}))
return "Splitting "+self.bot.funcs.getName(user)+"'s hand into 2. Adding their original bet to the second hand. You can use \"!Blackjack hit/stand/double 1\" and \"!Blackjack hit/stand/double 2\" to play the different hands.",str(roundDone)[0] + str(game["round"])
return "Splitting "+self.bot.databaseFuncs.getName(user)+"'s hand into 2. Adding their original bet to the second hand. You can use \"!Blackjack hit/stand/double 1\" and \"!Blackjack hit/stand/double 2\" to play the different hands.",str(roundDone)[0] + str(game["round"])
else:
logThis(user+" doesn't have enough GwendoBucks")
self.bot.log(user+" doesn't have enough GwendoBucks")
return "You don't have enough GwendoBucks",""
else:
logThis(user+" tried to split 2 different cards")
self.bot.log(user+" tried to split 2 different cards")
return "Your cards need to have the same value to split",""
else:
logThis(user+" tried to split later than they could")
self.bot.log(user+" tried to split later than they could")
return "You can only split on the first round",""
else:
logThis(user+" is already standing")
self.bot.log(user+" is already standing")
return "You can't split when you're standing",""
else:
logThis(user+" has already hit this round")
self.bot.log(user+" has already hit this round")
return "You've already hit this round",""
else:
logThis(user+" tried to split on the 0th round")
self.bot.log(user+" tried to split on the 0th round")
return "You can't split before you see your cards",""
else:
logThis(user+" tried to split more than three times")
self.bot.log(user+" tried to split more than three times")
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})
logThis(self.bot.funcs.getName(user)+" is trying to join the game in "+channel)
self.bot.log(self.bot.databaseFuncs.getName(user)+" is trying to join the game in "+channel)
if game != None:
if user not in game["user hands"]:
@ -481,32 +481,32 @@ class Blackjack():
self.bot.database["blackjack games"].update_one({"_id":channel},
{"$set":{"user hands."+user:newHand}})
logThis(f"{self.bot.funcs.getName(user)} entered the game with a bet of {bet}")
return f"{self.bot.funcs.getName(user)} entered the game with a bet of {bet}"
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:
logThis(user+" doesn't have enough GwendoBucks")
self.bot.log(user+" doesn't have enough GwendoBucks")
return "You don't have enough GwendoBucks to place that bet"
else:
logThis(user+" tried to bet a negative amount")
self.bot.log(user+" tried to bet a negative amount")
return "You can't bet a negative amount"
else:
logThis("The table is no longer open for bets")
self.bot.log("The table is no longer open for bets")
return "The table is no longer open for bets"
else:
logThis("There are already 5 players in the game.")
self.bot.log("There are already 5 players in the game.")
return "There's already a maximum of players at the table."
else:
logThis(user+" is already in the game")
self.bot.log(user+" is already in the game")
return "You've already entered this game"
else:
logThis("There is no game going on in "+channel)
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
def blackjackStart(self,channel:str):
game = self.bot.database["blackjack games"].find_one({"_id":channel})
logThis("Trying to start a blackjack game in "+channel)
self.bot.log("Trying to start a blackjack game in "+channel)
if game == None:
@ -524,7 +524,7 @@ class Blackjack():
return "started"
else:
logThis("There is already a blackjack game going on in "+channel)
self.bot.log("There is already a blackjack game going on in "+channel)
return "There's already a blackjack game going on. Try again in a few minutes."
# Ends the game and calculates winnings
@ -540,26 +540,23 @@ class Blackjack():
try:
for user in game["user hands"]:
try:
winnings, netWinnings, reason = self.calcWinnings(game["user hands"][user],dealerValue,True,dealerBlackjack,dealerBusted)
except:
logThis("Error calculating winnings for "+str(user)+" (error code 1312)")
winnings, netWinnings, reason = self.calcWinnings(game["user hands"][user],dealerValue,True,dealerBlackjack,dealerBusted)
if winnings < 0:
if winnings == -1:
finalWinnings += self.bot.funcs.getName(user)+" lost "+str(-1 * winnings)+" GwendoBuck "+reason+"\n"
finalWinnings += self.bot.databaseFuncs.getName(user)+" lost "+str(-1 * winnings)+" GwendoBuck "+reason+"\n"
else:
finalWinnings += self.bot.funcs.getName(user)+" lost "+str(-1 * winnings)+" GwendoBucks "+reason+"\n"
finalWinnings += self.bot.databaseFuncs.getName(user)+" lost "+str(-1 * winnings)+" GwendoBucks "+reason+"\n"
else:
if winnings == 1:
finalWinnings += self.bot.funcs.getName(user)+" won "+str(winnings)+" GwendoBuck "+reason+"\n"
finalWinnings += self.bot.databaseFuncs.getName(user)+" won "+str(winnings)+" GwendoBuck "+reason+"\n"
else:
finalWinnings += self.bot.funcs.getName(user)+" won "+str(winnings)+" GwendoBucks "+reason+"\n"
finalWinnings += self.bot.databaseFuncs.getName(user)+" won "+str(winnings)+" GwendoBucks "+reason+"\n"
self.bot.money.addMoney(user,netWinnings)
except:
logThis("Error calculating winnings (error code 1311)")
self.bot.log("Error calculating winnings (error code 1311)")
self.bot.database["blackjack games"].delete_one({"_id":channel})
@ -567,7 +564,7 @@ class Blackjack():
def calcWinnings(self,hand, dealerValue, topLevel, dealerBlackjack, dealerBusted):
logThis("Calculating winnings")
self.bot.log("Calculating winnings", level = 10)
reason = ""
bet = hand["bet"]
winnings = -1 * bet
@ -637,7 +634,7 @@ class Blackjack():
return hand, handNumber
except:
logThis("Problem with getHandNumber() (error code 1322)")
self.bot.log("Problem with getHandNumber() (error code 1322)")
def isRoundDone(self,game):
roundDone = True
@ -662,14 +659,14 @@ class Blackjack():
# Loop of blackjack game rounds
async def blackjackLoop(self,channel,gameRound,gameID):
logThis("Loop "+str(gameRound),str(channel.id))
self.bot.log("Loop "+str(gameRound),str(channel.id), level = 10)
with open("resources/games/oldImages/blackjack"+str(channel.id), "r") as f:
oldImage = await channel.fetch_message(int(f.read()))
new_message, allStanding, gamedone = self.blackjackContinue(str(channel.id))
if new_message != "":
logThis(new_message,str(channel.id))
self.bot.log(new_message,str(channel.id), level = 10)
await channel.send(new_message)
if gamedone == False:
await oldImage.delete()
@ -683,7 +680,7 @@ class Blackjack():
else:
await asyncio.sleep(120)
except:
logThis("Loop "+str(gameRound)+" interrupted (error code 1321)")
self.bot.log("Loop "+str(gameRound)+" interrupted (error code 1321)")
game = self.bot.database["blackjack games"].find_one({"_id":str(channel.id)})
@ -693,18 +690,18 @@ class Blackjack():
if gameRound == realRound and realGameID == gameID:
if gamedone == False:
logThis("Loop "+str(gameRound)+" calling self.blackjackLoop()",str(channel.id))
self.bot.log("Loop "+str(gameRound)+" calling self.blackjackLoop()",str(channel.id))
await self.blackjackLoop(channel,gameRound+1,gameID)
else:
try:
new_message = self.blackjackFinish(str(channel.id))
except:
logThis("Something fucked up (error code 1310)")
self.bot.log("Something fucked up (error code 1310)")
await channel.send(new_message)
else:
logThis("Ending loop on round "+str(gameRound),str(channel.id))
self.bot.log("Ending loop on round "+str(gameRound),str(channel.id), level = 10)
else:
logThis("Ending loop on round "+str(gameRound),str(channel.id))
self.bot.log("Ending loop on round "+str(gameRound),str(channel.id), level = 10)
async def parseBlackjack(self,content, ctx):
# Blackjack shuffle variables
@ -714,7 +711,7 @@ class Blackjack():
channel = ctx.channel_id
# Starts the game
if content == "":
await ctx.send("Staring a new game of blackjack")
await ctx.send("Starting a new game of blackjack")
cardsLeft = 0
cards = self.bot.database["blackjack cards"].find_one({"_id":str(channel)})
if cards != None:
@ -723,7 +720,7 @@ class Blackjack():
# Shuffles if not enough cards
if cardsLeft < blackjackMinCards:
self.blackjackShuffle(blackjackDecks,str(channel))
logThis("Shuffling the blackjack deck...",str(channel))
self.bot.log("Shuffling the blackjack deck...",str(channel))
await ctx.channel.send("Shuffling the deck...")
new_message = self.blackjackStart(str(channel))
@ -749,7 +746,7 @@ class Blackjack():
# Loop of game rounds
if gamedone == False:
logThis("!blackjack calling self.blackjackLoop()",str(channel))
self.bot.log("!blackjack calling self.blackjackLoop()",str(channel))
await self.blackjackLoop(ctx.channel,1,gameID)
else:
new_message = self.blackjackFinish(str(channel))
@ -781,10 +778,10 @@ class Blackjack():
#try:
if response[6] == "T":
gameID = self.bot.database["blackjack games"].find_one({"_id":str(channel)})["gameID"]
logThis("Hit calling self.blackjackLoop()",str(channel))
self.bot.log("Hit calling self.blackjackLoop()",str(channel))
await self.blackjackLoop(ctx.channel,int(response[7:])+1,gameID)
#except:
# logThis("Something fucked up (error code 1320)",str(channel))
# self.bot.log("Something fucked up (error code 1320)",str(channel))
else:
await ctx.send(response)
@ -806,10 +803,10 @@ class Blackjack():
#try:
if response[6] == "T":
gameID = self.bot.database["blackjack games"].find_one({"_id":str(channel)})["gameID"]
logThis("Stand calling self.blackjackLoop()",str(channel))
self.bot.log("Stand calling self.blackjackLoop()",str(channel))
await self.blackjackLoop(ctx.channel,int(response[7:])+1,gameID)
#except:
# logThis("Something fucked up (error code 1320)",str(channel))
# self.bot.log("Something fucked up (error code 1320)",str(channel))
else:
await ctx.send(response)
@ -827,10 +824,10 @@ class Blackjack():
try:
if roundDone[0] == "T":
gameID = self.bot.database["blackjack games"].find_one({"_id":str(channel)})["gameID"]
logThis("Double calling self.blackjackLoop()",str(channel))
self.bot.log("Double calling self.blackjackLoop()",str(channel))
await self.blackjackLoop(ctx.channel,int(roundDone[1:])+1,gameID)
except:
logThis("Something fucked up (error code 1320)",str(channel))
self.bot.log("Something fucked up (error code 1320)",str(channel))
# Splitting hand
elif content.startswith("split"):
@ -846,10 +843,10 @@ class Blackjack():
try:
if roundDone[0] == "T":
gameID = self.bot.database["blackjack games"].find_one({"_id":str(channel)})["gameID"]
logThis("Split calling self.blackjackLoop()",str(channel))
self.bot.log("Split calling self.blackjackLoop()",str(channel))
await self.blackjackLoop(ctx.channel,int(roundDone[1:])+1,gameID)
except:
logThis("Something fucked up (error code 1320)")
self.bot.log("Something fucked up (error code 1320)")
# Returning current hi-lo value
elif content.startswith("hilo"):
@ -863,7 +860,7 @@ class Blackjack():
# Shuffles the blackjack deck
elif content.startswith("shuffle"):
self.blackjackShuffle(blackjackDecks,str(channel))
logThis("Shuffling the blackjack deck...",str(channel))
self.bot.log("Shuffling the blackjack deck...",str(channel))
await ctx.send("Shuffling the deck...")
@ -878,6 +875,6 @@ class Blackjack():
await ctx.send(str(cardsLeft)+" cards, "+str(decksLeft)+" decks", hidden=True)
else:
logThis("Not a command (error code 1301)")
self.bot.log("Not a command (error code 1301)")
await ctx.send("I didn't quite understand that (error code 1301)")