Separate blackjack cards for each chann

This commit is contained in:
Nikolaj Danger
2020-07-30 17:42:48 +02:00
parent d807afe36d
commit 796b209a5b
4 changed files with 50 additions and 46 deletions

4
.gitignore vendored
View File

@ -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/

View File

@ -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","")

View File

@ -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}

View File

@ -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):