🃏 Converted full blackjack functionality to slash commands
This commit is contained in:
@ -39,8 +39,7 @@ class BlackjackCog(commands.Cog):
|
|||||||
# Starts a game of blackjack
|
# Starts a game of blackjack
|
||||||
@cog_ext.cog_subcommand(**params["blackjackStart"])
|
@cog_ext.cog_subcommand(**params["blackjackStart"])
|
||||||
async def blackjackStart(self, ctx):
|
async def blackjackStart(self, ctx):
|
||||||
await ctx.defer()
|
await self.bot.games.blackjack.start(ctx)
|
||||||
await self.bot.games.blackjack.parseBlackjack("", ctx)
|
|
||||||
|
|
||||||
@cog_ext.cog_subcommand(**params["blackjackBet"])
|
@cog_ext.cog_subcommand(**params["blackjackBet"])
|
||||||
async def blackjackBet(self, ctx, bet):
|
async def blackjackBet(self, ctx, bet):
|
||||||
|
@ -504,7 +504,6 @@ class Blackjack():
|
|||||||
self.bot.log("Stand calling self.blackjackLoop()", channel)
|
self.bot.log("Stand calling self.blackjackLoop()", channel)
|
||||||
await self.blackjackLoop(ctx.channel, game["round"]+1, gameID)
|
await self.blackjackLoop(ctx.channel, game["round"]+1, gameID)
|
||||||
|
|
||||||
|
|
||||||
# Player enters the game and draws a hand
|
# Player enters the game and draws a hand
|
||||||
async def playerDrawHand(self, ctx, bet : int):
|
async def playerDrawHand(self, ctx, bet : int):
|
||||||
try:
|
try:
|
||||||
@ -565,10 +564,31 @@ class Blackjack():
|
|||||||
await ctx.send(logMessage)
|
await ctx.send(logMessage)
|
||||||
|
|
||||||
# Starts a game of blackjack
|
# Starts a game of blackjack
|
||||||
def blackjackStart(self, channel:str):
|
async def start(self, ctx):
|
||||||
|
try:
|
||||||
|
await ctx.defer()
|
||||||
|
except:
|
||||||
|
self.bot.log("Defer failed")
|
||||||
|
channel = str(ctx.channel_id)
|
||||||
|
blackjackMinCards = 50
|
||||||
|
blackjackDecks = 4
|
||||||
|
|
||||||
|
await ctx.send("Starting a new game of blackjack")
|
||||||
|
cardsLeft = 0
|
||||||
|
cards = self.bot.database["blackjack cards"].find_one({"_id":channel})
|
||||||
|
if cards != None:
|
||||||
|
cardsLeft = len(cards["cards"])
|
||||||
|
|
||||||
|
# Shuffles if not enough cards
|
||||||
|
if cardsLeft < blackjackMinCards:
|
||||||
|
self.blackjackShuffle(blackjackDecks, channel)
|
||||||
|
self.bot.log("Shuffling the blackjack deck...", channel)
|
||||||
|
await ctx.channel.send("Shuffling the deck...")
|
||||||
|
|
||||||
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)
|
||||||
|
gameStarted = False
|
||||||
|
|
||||||
if game == None:
|
if game == None:
|
||||||
|
|
||||||
@ -584,10 +604,39 @@ class Blackjack():
|
|||||||
|
|
||||||
copyfile("resources/games/blackjackTable.png","resources/games/blackjackTables/blackjackTable"+channel+".png")
|
copyfile("resources/games/blackjackTable.png","resources/games/blackjackTables/blackjackTable"+channel+".png")
|
||||||
|
|
||||||
return "started"
|
gameStarted = True
|
||||||
|
|
||||||
|
if gameStarted:
|
||||||
|
sendMessage = "Blackjack game started. Use \"/blackjack bet [amount]\" to enter the game within the next 30 seconds."
|
||||||
|
await ctx.channel.send(sendMessage)
|
||||||
|
filePath = f"resources/games/blackjackTables/blackjackTable{channel}.png"
|
||||||
|
|
||||||
|
oldImage = await ctx.channel.send(file = discord.File(filePath))
|
||||||
|
|
||||||
|
with open("resources/games/oldImages/blackjack"+channel, "w") as f:
|
||||||
|
f.write(str(oldImage.id))
|
||||||
|
|
||||||
|
await asyncio.sleep(30)
|
||||||
|
|
||||||
|
gamedone = False
|
||||||
|
|
||||||
|
game = self.bot.database["blackjack games"].find_one({"_id":str(channel)})
|
||||||
|
|
||||||
|
if len(game["user hands"]) == 0:
|
||||||
|
gamedone = True
|
||||||
|
await ctx.channel.send("No one entered the game. Ending the game.")
|
||||||
|
gameID = game["gameID"]
|
||||||
|
|
||||||
|
# Loop of game rounds
|
||||||
|
if gamedone == False:
|
||||||
|
self.bot.log("start() calling blackjackLoop()", channel)
|
||||||
|
await self.blackjackLoop(ctx.channel,1,gameID)
|
||||||
|
else:
|
||||||
|
new_message = self.blackjackFinish(channel)
|
||||||
|
await ctx.channel.send(new_message)
|
||||||
else:
|
else:
|
||||||
self.bot.log("There is already a blackjack game going on in "+channel)
|
await ctx.channel.send("There's already a blackjack game going on. Try again in a few minutes.")
|
||||||
return "There's already a blackjack game going on. Try again in a few minutes."
|
self.bot.log("There was already a game going on")
|
||||||
|
|
||||||
# Ends the game and calculates winnings
|
# Ends the game and calculates winnings
|
||||||
def blackjackFinish(self,channel):
|
def blackjackFinish(self,channel):
|
||||||
@ -764,58 +813,7 @@ class Blackjack():
|
|||||||
else:
|
else:
|
||||||
self.bot.log("Ending loop on round "+str(gameRound),str(channel.id))
|
self.bot.log("Ending loop on round "+str(gameRound),str(channel.id))
|
||||||
|
|
||||||
async def parseBlackjack(self, content, ctx):
|
# Returning current hi-lo value
|
||||||
# Blackjack shuffle variables
|
|
||||||
blackjackMinCards = 50
|
|
||||||
blackjackDecks = 4
|
|
||||||
|
|
||||||
channel = ctx.channel_id
|
|
||||||
# Starts the game
|
|
||||||
if content == "":
|
|
||||||
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:
|
|
||||||
cardsLeft = len(cards["cards"])
|
|
||||||
|
|
||||||
# Shuffles if not enough cards
|
|
||||||
if cardsLeft < blackjackMinCards:
|
|
||||||
self.blackjackShuffle(blackjackDecks,str(channel))
|
|
||||||
self.bot.log("Shuffling the blackjack deck...",str(channel))
|
|
||||||
await ctx.channel.send("Shuffling the deck...")
|
|
||||||
|
|
||||||
new_message = self.blackjackStart(str(channel))
|
|
||||||
if new_message == "started":
|
|
||||||
|
|
||||||
new_message = "Blackjack game started. Use \"/blackjack bet [amount]\" to enter the game within the next 30 seconds."
|
|
||||||
await ctx.channel.send(new_message)
|
|
||||||
oldImage = await ctx.channel.send(file = discord.File("resources/games/blackjackTables/blackjackTable"+str(channel)+".png"))
|
|
||||||
|
|
||||||
with open("resources/games/oldImages/blackjack"+str(channel), "w") as f:
|
|
||||||
f.write(str(oldImage.id))
|
|
||||||
|
|
||||||
await asyncio.sleep(30)
|
|
||||||
|
|
||||||
gamedone = False
|
|
||||||
|
|
||||||
game = self.bot.database["blackjack games"].find_one({"_id":str(channel)})
|
|
||||||
|
|
||||||
if len(game["user hands"]) == 0:
|
|
||||||
gamedone = True
|
|
||||||
await ctx.channel.send("No one entered the game. Ending the game.")
|
|
||||||
gameID = game["gameID"]
|
|
||||||
|
|
||||||
# Loop of game rounds
|
|
||||||
if gamedone == False:
|
|
||||||
self.bot.log("/blackjack calling self.blackjackLoop()",str(channel))
|
|
||||||
await self.blackjackLoop(ctx.channel,1,gameID)
|
|
||||||
else:
|
|
||||||
new_message = self.blackjackFinish(str(channel))
|
|
||||||
await ctx.channel.send(new_message)
|
|
||||||
else:
|
|
||||||
await ctx.channel.send(new_message)
|
|
||||||
|
|
||||||
# Returning current hi-lo value
|
|
||||||
async def hilo(self, ctx):
|
async def hilo(self, ctx):
|
||||||
channel = ctx.channel_id
|
channel = ctx.channel_id
|
||||||
data = self.bot.database["hilo"].find_one({"_id":str(channel)})
|
data = self.bot.database["hilo"].find_one({"_id":str(channel)})
|
||||||
|
Reference in New Issue
Block a user