diff --git a/Gwendolyn.py b/Gwendolyn.py index 18553f0..3ebd79a 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -322,7 +322,7 @@ async def on_message(message): new_message = "Blackjack game started. Use \"!blackjack bet [amount]\" to enter the game within the next 30 seconds." await message.channel.send(new_message) - await message.channel.send(file = discord.File("resources/games/tables/blackjackTable"+str(message.channel)+".png")) + old_image = await message.channel.send(file = discord.File("resources/games/tables/blackjackTable"+str(message.channel)+".png")) await asyncio.sleep(30) @@ -336,9 +336,11 @@ async def on_message(message): while gamedone == False: new_message, allStanding, gamedone = funcs.blackjackContinue(str(message.channel)) - await message.channel.send(new_message) + if new_message != "": + await message.channel.send(new_message) if gamedone == False: - await message.channel.send(file = discord.File("resources/games/tables/blackjackTable"+str(message.channel)+".png")) + await old_image.delete() + old_image = await message.channel.send(file = discord.File("resources/games/tables/blackjackTable"+str(message.channel)+".png")) if allStanding: await asyncio.sleep(5) else: @@ -355,10 +357,11 @@ async def on_message(message): commands = message.content.lower().split(" ") try: amount = int(commands[2]) - response = funcs.blackjackPlayerDrawHand(str(message.channel),message.author.name,amount) except: funcs.logThis("I didn't understand that") response = "I didn't understand that" + else: + response = funcs.blackjackPlayerDrawHand(str(message.channel),message.author.name,amount) await message.channel.send(response) if message.content.lower().startswith("!blackjack hit"): diff --git a/funcs/games/blackjack.py b/funcs/games/blackjack.py index 2ee077f..c2b096d 100644 --- a/funcs/games/blackjack.py +++ b/funcs/games/blackjack.py @@ -68,14 +68,18 @@ def dealerDraw(channel): data = json.load(f) done = False + dealerHand = data["blackjack games"][channel]["dealer hand"] - if calcHandValue(data["blackjack games"][channel]["dealer hand"]) < 17: + if calcHandValue(dealerHand) < 17: data["blackjack games"][channel]["dealer hand"].append(drawCard()) else: done = True - if calcHandValue(data["blackjack games"][channel]["dealer hand"]) > 21: + if calcHandValue(dealerHand) > 21: data["blackjack games"][channel]["dealer busted"] = True + + if calcHandValue(dealerHand) == 21 and len(dealerHand) == 2: + data["blackjack games"][channel]["dealer blackjack"] = True with open("resources/games/games.json", "w") as f: json.dump(data,f,indent=4) @@ -83,6 +87,7 @@ def dealerDraw(channel): return done def blackjackContinue(channel): + logThis("Continuing blackjack game") with open("resources/games/games.json", "r") as f: data = json.load(f) @@ -93,6 +98,7 @@ def blackjackContinue(channel): allStanding = True + preAllStanding = True message = "All players are standing. The dealer now shows his cards and draws." if data["blackjack games"][channel]["all standing"]: @@ -111,6 +117,12 @@ def blackjackContinue(channel): if data["blackjack games"][channel]["user hands"][user]["standing"] == False: allStanding = False + + if calcHandValue(data["blackjack games"][channel]["user hands"][user]["hand"]) >= 21: + data["blackjack games"][channel]["user hands"][user]["standing"] = True + else: + preAllStanding = False + data["blackjack games"][channel]["user hands"][user]["hit"] = False if allStanding: @@ -128,6 +140,8 @@ def blackjackContinue(channel): return message, True, done else: return "The dealer is done drawing cards", True, done + elif preAllStanding: + return "", True, done else: return "You have 20 seconds to either hit or stand with \"!blackjack hit\" or \"!blackjack stand\". It's assumed you're standing if you don't make a choice.", False, done @@ -143,10 +157,8 @@ def blackjackHit(channel,user): handValue = calcHandValue(data["blackjack games"][channel]["user hands"][user]["hand"]) - if handValue == 21: - data["blackjack games"][channel]["user hands"][user]["standing"] = True - elif handValue > 21: - data["blackjack games"][channel]["user hands"][user]["standing"] = True + + if handValue > 21: data["blackjack games"][channel]["user hands"][user]["busted"] = True @@ -205,7 +217,7 @@ def blackjackPlayerDrawHand(channel,user,bet): data = json.load(f) if handValue == 21: - data["blackjack games"][channel]["user hands"][user] = {"hand":playerHand,"bet":bet,"standing":True,"busted":False,"blackjack":True,"hit":True} + data["blackjack games"][channel]["user hands"][user] = {"hand":playerHand,"bet":bet,"standing":False,"busted":False,"blackjack":True,"hit":True} else: data["blackjack games"][channel]["user hands"][user] = {"hand":playerHand,"bet":bet,"standing":False,"busted":False,"blackjack":False,"hit":True} @@ -240,7 +252,7 @@ def blackjackStart(channel:str): dealerHand = [drawCard(),drawCard()] - data["blackjack games"][channel] = {"dealer hand": dealerHand,"dealer busted":False,"user hands": {},"open for bets":True,"all standing":False} + data["blackjack games"][channel] = {"dealer hand": dealerHand,"dealer busted":False,"dealer blackjack":False,"user hands": {},"open for bets":True,"all standing":False} with open("resources/games/games.json", "w") as f: json.dump(data,f,indent=4) diff --git a/funcs/games/blackjackDraw.py b/funcs/games/blackjackDraw.py index 67f47f0..f871cd0 100644 --- a/funcs/games/blackjackDraw.py +++ b/funcs/games/blackjackDraw.py @@ -1,30 +1,45 @@ import json -from PIL import Image +from PIL import Image, ImageDraw, ImageFont def drawImage(channel): with open("resources/games/games.json", "r") as f: data = json.load(f) - - if data["blackjack games"][channel]["all standing"] == False: - dealerHand = drawHand(data["blackjack games"][channel]["dealer hand"],True) - else: - dealerHand = drawHand(data["blackjack games"][channel]["dealer hand"],False) + + fnt = ImageFont.truetype('resources/futura-bold.ttf', 50) table = Image.open("resources/games/blackjackTable.png") + placement = [2,1,3,0,4] + textImage = ImageDraw.Draw(table) + hands = data["blackjack games"][channel]["user hands"] + + dealerBusted = data["blackjack games"][channel]["dealer busted"] + dealerBlackjack = data["blackjack games"][channel]["dealer blackjack"] + + if data["blackjack games"][channel]["all standing"] == False: + dealerHand = drawHand(data["blackjack games"][channel]["dealer hand"],True,False,False) + else: + dealerHand = drawHand(data["blackjack games"][channel]["dealer hand"],False,dealerBusted,dealerBlackjack) + table.paste(dealerHand,(800,20),dealerHand) - for x in range(len(data["blackjack games"][channel]["user hands"])): - userHand = drawHand(list(data["blackjack games"][channel]["user hands"].values())[x]["hand"],False) - table.paste(userHand,(32+(384*x),700),userHand) + for x in range(len(hands)): + key, value = list(hands.items())[x] + userHand = drawHand(value["hand"],False,value["busted"],value["blackjack"]) + textWidth = fnt.getsize(key)[0] + table.paste(userHand,(32+(384*placement[x]),680),userHand) + textImage.text((32+(384*placement[x])+117-int(textWidth/2),1010),key,fill=(0,0,0), font=fnt) table.save("resources/games/tables/blackjackTable"+channel+".png") return -def drawHand(hand, dealer): +def drawHand(hand, dealer, busted, blackjack): + fnt = ImageFont.truetype('resources/futura-bold.ttf', 200) + fnt2 = ImageFont.truetype('resources/futura-bold.ttf', 140) length = len(hand) background = Image.new("RGBA", (691+(125*(length-1)),1065),(0,0,0,0)) + textImage = ImageDraw.Draw(background) if dealer: img = Image.open("resources/games/cards/"+hand[0].upper()+".png") @@ -38,4 +53,19 @@ def drawHand(hand, dealer): w, h = background.size - return background.resize((int(w/4),int(h/4))) + if busted: + textWidth = fnt.getsize("BUSTED")[0] + textImage.text((int(w/2)-int(textWidth/2)-5,450-5),"BUSTED",fill=(0,0,0), font=fnt) + textImage.text((int(w/2)-int(textWidth/2)+5,450-5),"BUSTED",fill=(0,0,0), font=fnt) + textImage.text((int(w/2)-int(textWidth/2)-5,450+5),"BUSTED",fill=(0,0,0), font=fnt) + textImage.text((int(w/2)-int(textWidth/2)+5,450+5),"BUSTED",fill=(0,0,0), font=fnt) + textImage.text((int(w/2)-int(textWidth/2),430),"BUSTED",fill=(255,50,50), font=fnt) + elif blackjack: + textWidth = fnt2.getsize("BLACKJACK")[0] + textImage.text((int(w/2)-int(textWidth/2)-5,450-5),"BLACKJACK",fill=(0,0,0), font=fnt2) + textImage.text((int(w/2)-int(textWidth/2)+5,450-5),"BLACKJACK",fill=(0,0,0), font=fnt2) + textImage.text((int(w/2)-int(textWidth/2)-5,450+5),"BLACKJACK",fill=(0,0,0), font=fnt2) + textImage.text((int(w/2)-int(textWidth/2)+5,450+5),"BLACKJACK",fill=(0,0,0), font=fnt2) + textImage.text((int(w/2)-int(textWidth/2),430),"BLACKJACK",fill=(255,223,0), font=fnt2) + + return background.resize((int(w/3.5),int(h/3.5))) diff --git a/funcs/gwendolynFuncs.py b/funcs/gwendolynFuncs.py index 24a5e5e..85ecc6c 100644 --- a/funcs/gwendolynFuncs.py +++ b/funcs/gwendolynFuncs.py @@ -183,11 +183,9 @@ def makeFiles(): finally: f.close() - try: + if os.path.isdir("resources/games/tables") == False: os.makedirs("resources/games/tables") logThis("The tables directory didn't exist") - except: - logThis("The tables directory existed") def replaceMultiple(mainString, toBeReplaces, newString): # Iterate over the strings to be replaced diff --git a/resources/futura-bold.ttf b/resources/futura-bold.ttf new file mode 100644 index 0000000..5d8ce10 Binary files /dev/null and b/resources/futura-bold.ttf differ diff --git a/resources/games/blackjackTable.png b/resources/games/blackjackTable.png index 639d860..7275c76 100644 Binary files a/resources/games/blackjackTable.png and b/resources/games/blackjackTable.png differ