From 796b209a5bec573d7e6e1f5fe6a40326f686823a Mon Sep 17 00:00:00 2001 From: Nikolaj Danger Date: Thu, 30 Jul 2020 17:42:48 +0200 Subject: [PATCH] :sparkles: Separate blackjack cards for each chann --- .gitignore | 4 ++-- Gwendolyn.py | 31 ++++++++++++++++++------------- funcs/games/blackjack.py | 34 +++++++++++++++++----------------- funcs/miscFuncs.py | 27 +++++++++++++-------------- 4 files changed, 50 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index 107b037..f58d1a2 100644 --- a/.gitignore +++ b/.gitignore @@ -152,8 +152,8 @@ static token.txt resources/swcharacters.json resources/games/games.json -resources/games/blackjackCards.txt -resources/games/hilo.txt +resources/games/blackjackCards/ +resources/games/hilo/ resources/destinyPoints.txt resources/games/blackjackTables/ resources/games/oldImages/ diff --git a/Gwendolyn.py b/Gwendolyn.py index d466c77..cb55b5b 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -8,6 +8,7 @@ import string import json import random import math +import os from funcs import * @@ -415,13 +416,14 @@ async def parseCommands(message,content): # Starts the game if content == "blackjack" or content == "blackjack ": cardsLeft = 0 - with open("resources/games/blackjackCards.txt","r") as f: - for line in f: - cardsLeft += 1 + if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"): + with open("resources/games/blackjackCards/"+str(message.channel)+".txt","r") as f: + for line in f: + cardsLeft += 1 # Shuffles if not enough cards if cardsLeft < blackjackMinCards: - blackjackShuffle(blackjackDecks) + blackjackShuffle(blackjackDecks,str(message.channel)) logThis("Shuffling the blackjack deck...",str(message.channel)) await message.channel.send("Shuffling the deck...") @@ -560,21 +562,27 @@ async def parseCommands(message,content): # Returning current hi-lo value elif content.startswith("blackjack hilo") and message.author.display_name == "Nikolaj": - with open("resources/games/hilo.txt", "r") as f: - data = f.read() + if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"): + with open("resources/games/hilo/"+str(message.channel)+".txt", "r") as f: + data = f.read() + else: + data = "0" await message.channel.send(data) # Shuffles the blackjack deck elif content.startswith("blackjack shuffle"): - blackjackShuffle(blackjackDecks) + blackjackShuffle(blackjackDecks,str(message.channel)) logThis("Shuffling the blackjack deck...",str(message.channel)) await message.channel.send("Shuffling the deck...") + + # Tells you the amount of cards left elif content.startswith("blackjack cards"): cardsLeft = 0 - with open("resources/games/blackjackCards.txt","r") as f: - for line in f: - cardsLeft += 1 + if os.path.exists("resources/games/blackjackCards/"+str(message.channel)+".txt"): + with open("resources/games/blackjackCards/"+str(message.channel)+".txt","r") as f: + for line in f: + cardsLeft += 1 decksLeft = round(cardsLeft/52,1) await message.channel.send(str(cardsLeft)+" cards, "+str(decksLeft)+" decks") @@ -618,9 +626,6 @@ async def parseCommands(message,content): # Makes files if they don't exist yet makeFiles() -# Shuffling cards -blackjackShuffle(4) - # Gets secret bot token with open("token.txt","r") as f: token = f.read().replace("\n","") diff --git a/funcs/games/blackjack.py b/funcs/games/blackjack.py index dd42dd0..e9f9a6d 100644 --- a/funcs/games/blackjack.py +++ b/funcs/games/blackjack.py @@ -9,7 +9,7 @@ from funcs import logThis, replaceMultiple from . import money, blackjackDraw # Shuffles the blackjack cards -def blackjackShuffle(decks): +def blackjackShuffle(decks,channel): logThis("Shuffling the blackjack deck") with open("resources/games/deckofCards.txt","r") as f: @@ -19,13 +19,13 @@ def blackjackShuffle(decks): random.shuffle(allDecks) data = "\n".join(allDecks) - with open("resources/games/blackjackCards.txt","w") as f: + with open("resources/games/blackjackCards/"+channel+".txt","w") as f: f.write(data) - # Creates hilo.txt - logThis("creating hilo.txt.") + # Creates hilo file + logThis("creating hilo/"+channel+".txt.") data = "0" - with open("resources/games/hilo.txt","w") as f: + with open("resources/games/hilo/"+channel+".txt","w") as f: f.write(data) return @@ -59,11 +59,11 @@ def calcHandValue(hand : list): return handValue # Draws a card from the deck -def drawCard(): +def drawCard(channel): logThis("drawing a card") - with open("resources/games/blackjackCards.txt","r") as f: + with open("resources/games/blackjackCards/"+channel+".txt","r") as f: cards = f.read().split("\n") - with open("resources/games/hilo.txt", "r") as f: + with open("resources/games/hilo/"+channel+".txt", "r") as f: data = int(f.read()) drawnCard = cards.pop(0) @@ -76,9 +76,9 @@ def drawCard(): elif value >= 10: data -= 1 - with open("resources/games/blackjackCards.txt","w") as f: + with open("resources/games/blackjackCards/"+channel+".txt","w") as f: f.write(deck) - with open("resources/games/hilo.txt", "w") as f: + with open("resources/games/hilo/"+channel+".txt", "w") as f: f.write(str(data)) return drawnCard @@ -92,7 +92,7 @@ def dealerDraw(channel): dealerHand = data["blackjack games"][channel]["dealer hand"] if calcHandValue(dealerHand) < 17: - data["blackjack games"][channel]["dealer hand"].append(drawCard()) + data["blackjack games"][channel]["dealer hand"].append(drawCard(channel)) else: done = True @@ -211,7 +211,7 @@ def blackjackHit(channel,user,handNumber = 0): if data["blackjack games"][channel]["round"] > 0: if hand["hit"] == False: if hand["standing"] == False: - hand["hand"].append(drawCard()) + hand["hand"].append(drawCard(channel)) hand["hit"] = True handValue = calcHandValue(hand["hand"]) @@ -289,7 +289,7 @@ def blackjackDouble(channel,user,handNumber = 0): with open("resources/games/games.json", "r") as f: data = json.load(f) - hand["hand"].append(drawCard()) + hand["hand"].append(drawCard(channel)) hand["hit"] = True hand["doubled"] = True hand["bet"] += bet @@ -418,8 +418,8 @@ def blackjackSplit(channel,user): data["blackjack games"][channel]["user hands"][user]["other hand"]["bet"] = data["blackjack games"][channel]["user hands"][user]["bet"] data["blackjack games"][channel]["user hands"][user]["other hand"]["hand"].append(data["blackjack games"][channel]["user hands"][user]["hand"].pop(1)) - data["blackjack games"][channel]["user hands"][user]["other hand"]["hand"].append(drawCard()) - data["blackjack games"][channel]["user hands"][user]["hand"].append(drawCard()) + data["blackjack games"][channel]["user hands"][user]["other hand"]["hand"].append(drawCard(channel)) + data["blackjack games"][channel]["user hands"][user]["hand"].append(drawCard(channel)) handValue = calcHandValue(data["blackjack games"][channel]["user hands"][user]["hand"]) otherHandValue = calcHandValue(data["blackjack games"][channel]["user hands"][user]["other hand"]["hand"]) @@ -481,7 +481,7 @@ def blackjackPlayerDrawHand(channel,user,bet): if bet >= 0: if money.checkBalance(user) >= bet: money.addMoney(user,-1 * bet) - playerHand = [drawCard(),drawCard()] + playerHand = [drawCard(channel),drawCard(channel)] handValue = calcHandValue(playerHand) @@ -534,7 +534,7 @@ def blackjackStart(channel:str): if channel not in data["blackjack games"]: - dealerHand = [drawCard(),drawCard()] + dealerHand = [drawCard(channel),drawCard(channel)] gameID = datetime.datetime.now().strftime('%Y%m%d%H%M%S') data["blackjack games"][channel] = {"dealer hand": dealerHand,"dealer busted":False,"dealer blackjack":False,"user hands": {},"all standing":False,"round":0,"id":gameID} diff --git a/funcs/miscFuncs.py b/funcs/miscFuncs.py index 2a5949f..45709fc 100644 --- a/funcs/miscFuncs.py +++ b/funcs/miscFuncs.py @@ -159,17 +159,6 @@ def makeFiles(): f.close() - # Creates blackjackCards.txt if it doesn't exist - try: - f = open("resources/games/blackjackCards.txt","r") - except: - logThis("blackjackCards.txt didn't exist. Making it now.") - data = "" - with open("resources/games/blackjackCards.txt","w") as f: - f.write(data) - finally: - f.close() - # Creates destinyPoints.txt if it doesn't exist try: f = open("resources/destinyPoints.txt","r") @@ -200,12 +189,22 @@ def makeFiles(): # Creates the 4InARowBoards foulder if it doesn't exist if os.path.isdir("resources/games/4InARowBoards") == False: os.makedirs("resources/games/4InARowBoards") - logThis("The tables directory didn't exist") + logThis("The 4 in a row boards directory didn't exist") - # Creates the 4InARowBoards foulder if it doesn't exist + # Creates the oldImages foulder if it doesn't exist if os.path.isdir("resources/games/oldImages") == False: os.makedirs("resources/games/oldImages") - logThis("The tables directory didn't exist") + logThis("The old images directory didn't exist") + + # Creates the blackjackCards foulder if it doesn't exist + if os.path.isdir("resources/games/blackjackCards") == False: + os.makedirs("resources/games/blackjackCards") + logThis("The blackjack cards directory didn't exist") + + # Creates the hilo foulder if it doesn't exist + if os.path.isdir("resources/games/hilo") == False: + os.makedirs("resources/games/hilo") + logThis("The hi-lo directory didn't exist") # Replaces multiple things with the same thing def replaceMultiple(mainString, toBeReplaces, newString):