📝 Commenting and formatting

This commit is contained in:
NikolajDanger
2020-07-29 10:38:37 +02:00
parent 94c6289cc1
commit ef434ecee8
12 changed files with 260 additions and 220 deletions

View File

@ -1,4 +1,4 @@
from .money import checkBalance, giveMoney, addMoney
from .trivia import triviaCountPoints, triviaStart, triviaOtherThing
from .blackjack import shuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit
from .fourInARow import decipherCommand
from .trivia import triviaCountPoints, triviaStart, triviaAnswer
from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit
from .fourInARow import parseFourInARow

View File

@ -7,15 +7,14 @@ from shutil import copyfile
from funcs import logThis, replaceMultiple
from . import money, blackjackDraw
deckAmount = 4
def shuffle():
# Shuffles the blackjack cards
def blackjackShuffle(decks):
logThis("Shuffling the blackjack deck")
with open("resources/games/deckofCards.txt","r") as f:
deck = f.read()
allDecks = deck.split("\n") * 4
allDecks = deck.split("\n") * decks
random.shuffle(allDecks)
data = "\n".join(allDecks)
@ -24,6 +23,7 @@ def shuffle():
return
# Calculates the value of a blackjack hand
def calcHandValue(hand : list):
logThis("Calculating hand value")
values = [0]
@ -51,13 +51,13 @@ def calcHandValue(hand : list):
return handValue
# Draws a card from the deck
def drawCard():
logThis("drawing a card")
with open("resources/games/blackjackCards.txt","r") as f:
cards = f.read().split("\n")
drawnCard = cards[0]
cards = cards[1:]
drawnCard = cards.pop(0)
data = "\n".join(cards)
with open("resources/games/blackjackCards.txt","w") as f:
@ -65,6 +65,7 @@ def drawCard():
return drawnCard
# Dealer draws a card and checks if they should draw another one
def dealerDraw(channel):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -85,6 +86,7 @@ def dealerDraw(channel):
return done
# Goes to the next round and calculates some stuff
def blackjackContinue(channel):
logThis("Continuing blackjack game")
with open("resources/games/games.json", "r") as f:
@ -163,6 +165,7 @@ def blackjackContinue(channel):
firstRoundMessage = ""
return "You have 30 seconds to either hit or stand with \"!blackjack hit\" or \"!blackjack stand\""+firstRoundMessage+". It's assumed you're standing if you don't make a choice.", False, done
# When players try to hit
def blackjackHit(channel,user,handNumber = 0):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -216,6 +219,7 @@ def blackjackHit(channel,user,handNumber = 0):
return "You can't hit before you see your cards"
# When players try to double down
def blackjackDouble(channel,user,handNumber = 0):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -287,6 +291,7 @@ def blackjackDouble(channel,user,handNumber = 0):
logThis(user+" tried to double on the 0th round")
return "You can't double down before you see your cards"
# When players try to stand
def blackjackStand(channel,user,handNumber = 0):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -310,6 +315,7 @@ def blackjackStand(channel,user,handNumber = 0):
logThis(user+" tried to stand on the first round")
return "You can't stand before you see your cards"
# When players try to split
def blackjackSplit(channel,user):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -374,6 +380,7 @@ def blackjackSplit(channel,user):
logThis(user+" tried to split on the 0th round")
return "You can't split before you see your cards"
# Player enters the game and draws a hand
def blackjackPlayerDrawHand(channel,user,bet):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -431,6 +438,7 @@ def blackjackPlayerDrawHand(channel,user,bet):
logThis("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(channel:str):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -456,6 +464,7 @@ def blackjackStart(channel:str):
logThis("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
def blackjackFinish(channel):
finalWinnings = "*Final Winnings:*\n"
@ -480,6 +489,7 @@ def blackjackFinish(channel):
winnings += 2 * data["blackjack games"][channel]["user hands"][user]["bet"]
elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["hand"]) > dealerValue:
winnings += 2 * data["blackjack games"][channel]["user hands"][user]["bet"]
reason = "(highest value)"
elif calcHandValue(data["blackjack games"][channel]["user hands"][user]["hand"]) == dealerValue:
reason = "(pushed)"
winnings += data["blackjack games"][channel]["user hands"][user]["bet"]

View File

@ -4,6 +4,7 @@ import math
from PIL import Image, ImageDraw, ImageFont
from funcs import logThis
# Draws the whole thing
def drawImage(channel):
logThis("Drawing four in a row board")
with open("resources/games/games.json", "r") as f:
@ -29,13 +30,16 @@ def drawImage(channel):
background = Image.new("RGBA", (w,h),backgroundColor)
d = ImageDraw.Draw(background)
# This whole part was the easiest way to make a rectangle with rounded corners and an outline
# - Corners:
d.ellipse([(border,border),(border+cornerSize,border+cornerSize)],fill=boardColor,outline=(0,0,0),width=outlineSize)
d.ellipse([(w-(border+cornerSize),h-(border+cornerSize)),(w-border,h-border)],fill=boardColor,outline=(0,0,0),width=outlineSize)
d.ellipse([(border,h-(border+cornerSize)),(border+cornerSize,h-border)],fill=boardColor,outline=(0,0,0),width=outlineSize)
d.ellipse([(w-(border+cornerSize),border),(w-border,border+cornerSize)],fill=boardColor,outline=(0,0,0),width=outlineSize)
# - Rectangle:
d.rectangle([(border+math.floor(cornerSize/2),border),(w-(border+math.floor(cornerSize/2)),h-border)],fill=boardColor,outline=(0,0,0),width=outlineSize)
d.rectangle([(border,border+math.floor(cornerSize/2)),(w-border,h-(border+math.floor(cornerSize/2)))],fill=boardColor,outline=(0,0,0),width=outlineSize)
# - Removing outline on the inside:
d.rectangle([(border+math.floor(cornerSize/2),border+math.floor(cornerSize/2)),(w-(border+math.floor(cornerSize/2)),h-(border+math.floor(cornerSize/2)))],fill=boardColor)
d.ellipse([(border+outlineSize,border+outlineSize),(border+cornerSize-outlineSize,border+cornerSize-outlineSize)],fill=boardColor)
d.ellipse([(w-(border+cornerSize)+outlineSize,h-(border+cornerSize)+outlineSize),(w-border-outlineSize,h-border-outlineSize)],fill=boardColor)

View File

@ -2,6 +2,7 @@ import json
from . import draw4InARow
# Starts the game
def fourInARowStart(channel):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -21,7 +22,7 @@ def fourInARowStart(channel):
else:
return "There's already a 4 in a row game going on in this channel", False
# Places a piece at the lowest available point in a specific column
def placePiece(channel : str,player : int,column : int):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -51,8 +52,8 @@ def placePiece(channel : str,player : int,column : int):
return "There's no game in this channel", False
def decipherCommand(command, channel, user):
# Parses command
def parseFourInARow(command, channel, user):
if command == "" or command == " ":
return fourInARowStart(channel)
elif command.startswith(" place"):

View File

@ -2,6 +2,7 @@ import json
from funcs import logThis
# Returns the account balance for a user
def checkBalance(user):
user = user.lower()
logThis("checking "+user+"'s account balance")
@ -12,6 +13,7 @@ def checkBalance(user):
return data["users"][user]
else: return 0
# Adds money to the account of a user
def addMoney(user,amount):
user = user.lower()
logThis("adding "+str(amount)+" to "+user+"'s account")
@ -27,6 +29,7 @@ def addMoney(user,amount):
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
# Transfers money from one user to another
def giveMoney(user,targetUser,amount):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -35,7 +38,7 @@ def giveMoney(user,targetUser,amount):
if data["users"][user] >= amount:
addMoney(user,-1 * amount)
addMoney(targetUser,amount)
return "Transferred the GwendoBucks"
return "Transferred "+str(amount)+" GwendoBucks to "+user
else:
logThis("They didn't have enough GwendoBucks")
return "You don't have that many GwendoBucks"

View File

@ -5,6 +5,8 @@ import random
from . import money
from funcs import logThis
# Starts a game of trivia. Downloads a question with answers, shuffles the wrong answers with the
# correct answer and returns the questions and answers. Also saves the question in the games.json file.
def triviaStart(channel : str):
with open("resources/games/games.json", "r") as f:
triviaFile = json.load(f)
@ -44,7 +46,8 @@ def triviaStart(channel : str):
logThis("There was already a trivia question for that channel")
return "There's already a trivia question going on. Try again in like, a minute", "", ""
def triviaOtherThing(user : str, channel : str, command : str):
# Lets players answer a trivia question
def triviaAnswer(user : str, channel : str, command : str):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
@ -66,6 +69,8 @@ def triviaOtherThing(user : str, channel : str, command : str):
else:
return "I didn't quite understand that"
# Adds 1 GwendoBuck to each player that got the question right and deletes question from games.json.
def triviaCountPoints(channel : str):
with open("resources/games/games.json", "r") as f:
data = json.load(f)