🗃️ UserID's instead of display names

This commit is contained in:
Nikolaj Danger
2020-08-04 16:59:13 +02:00
parent 4bcc8cbf59
commit 2ea11eb51f
11 changed files with 201 additions and 111 deletions

View File

@ -5,7 +5,7 @@ import datetime
from shutil import copyfile
from funcs import logThis, replaceMultiple
from funcs import logThis, replaceMultiple, getName
from . import money, blackjackDraw
# Shuffles the blackjack cards
@ -352,7 +352,7 @@ def blackjackDouble(channel,user,handNumber = 0):
roundDone = False
return "Adding another "+str(bet)+" GwendoBucks to "+user+"'s bet and drawing another card.",str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
return "Adding another "+str(bet)+" GwendoBucks to "+getName(user)+"'s bet and drawing another card.",str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
else:
logThis(user+" doesn't have enough GwendoBucks")
return "You don't have enough GwendoBucks",""
@ -544,7 +544,7 @@ def blackjackSplit(channel,user,handNumber = 0):
if person["fourth hand"]["hit"] == False and person["fourth hand"]["standing"] == False:
roundDone = False
return "Splitting "+user+"'s hand into 2. Adding their original bet to the second hand. You can use \"!Blackjack hit/stand/double 1\" and \"!Blackjack hit/stand/double 2\" to play the different hands.",str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
return "Splitting "+getName(user)+"'s hand into 2. Adding their original bet to the second hand. You can use \"!Blackjack hit/stand/double 1\" and \"!Blackjack hit/stand/double 2\" to play the different hands.",str(roundDone)[0] + str(data["blackjack games"][channel]["round"])
else:
logThis(user+" doesn't have enough GwendoBucks")
return "You don't have enough GwendoBucks",""
@ -572,7 +572,7 @@ def blackjackPlayerDrawHand(channel,user,bet):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
logThis(user+" is trying to join the game in "+channel)
logThis(getName(user)+" is trying to join the game in "+channel)
if channel in data["blackjack games"]:
if user not in data["blackjack games"][channel]["user hands"]:
@ -600,8 +600,8 @@ def blackjackPlayerDrawHand(channel,user,bet):
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
logThis(user+" entered the game")
return user+" entered the game"
logThis(getName(user)+" entered the game")
return getName(user)+" entered the game"
else:
logThis(user+" doesn't have enough GwendoBucks")
return "You don't have enough GwendoBucks to place that bet"
@ -669,14 +669,14 @@ def blackjackFinish(channel):
if winnings < 0:
if winnings == -1:
finalWinnings += user+" lost "+str(-1 * winnings)+" GwendoBuck "+reason+"\n"
finalWinnings += getName(user)+" lost "+str(-1 * winnings)+" GwendoBuck "+reason+"\n"
else:
finalWinnings += user+" lost "+str(-1 * winnings)+" GwendoBucks "+reason+"\n"
finalWinnings += getName(user)+" lost "+str(-1 * winnings)+" GwendoBucks "+reason+"\n"
else:
if winnings == 1:
finalWinnings += user+" won "+str(winnings)+" GwendoBuck "+reason+"\n"
finalWinnings += getName(user)+" won "+str(winnings)+" GwendoBuck "+reason+"\n"
else:
finalWinnings += user+" won "+str(winnings)+" GwendoBucks "+reason+"\n"
finalWinnings += getName(user)+" won "+str(winnings)+" GwendoBucks "+reason+"\n"
money.addMoney(user,netWinnings)

View File

@ -1,7 +1,7 @@
import json
from PIL import Image, ImageDraw, ImageFont
from funcs import logThis
from funcs import logThis, getName
border = 100
placement = [0,0]
@ -35,6 +35,7 @@ def drawImage(channel):
for x in range(len(hands)):
key, value = list(hands.items())[x]
key = getName(key)
logThis("drawing "+key+"'s hand")
userHand = drawHand(value["hand"],False,value["busted"],value["blackjack"])
try:

View File

@ -4,7 +4,7 @@ import copy
import math
from . import fourInARowDraw
from funcs import logThis
from funcs import logThis, getName, getID
AIScores = {
"middle": 3,
@ -26,48 +26,52 @@ def fourInARowStart(channel, user, opponent):
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if user.lower() != opponent.lower():
if channel not in data["4 in a row games"]:
if channel not in data["4 in a row games"]:
if opponent in ["1","2","3","4","5"]:
difficulty = int(opponent)
diffText = " with difficulty "+opponent
opponent = "Gwendolyn"
elif opponent.lower() == "gwendolyn":
difficulty = 3
diffText = " with difficulty 3"
opponent = "Gwendolyn"
else:
try:
int(opponent)
return "That difficulty doesn't exist", False, False, False, False
except:
# Opponent is another player
if opponent in ["1","2","3","4","5"]:
difficulty = int(opponent)
diffText = " with difficulty "+opponent
opponent = "Gwendolyn"
elif opponent.lower() == "gwendolyn":
difficulty = 3
diffText = " with difficulty 3"
opponent = "Gwendolyn"
else:
try:
int(opponent)
return "That difficulty doesn't exist", False, False, False, False
except:
# Opponent is another player
opponent = getID(opponent)
if opponent != None:
difficulty = 5
diffText = ""
else:
return "I can't find that user", False, False, False, False
if user == opponent:
return "You can't play against yourself", False, False, False, False
board = [ [ 0 for i in range(columnCount) ] for j in range(rowCount) ]
players = [user,opponent]
random.shuffle(players)
board = [ [ 0 for i in range(columnCount) ] for j in range(rowCount) ]
players = [user,opponent]
random.shuffle(players)
data["4 in a row games"][channel] = {"board": board,"winner":0,"win direction":"",
"win coordinates":[0,0],"players":players,"turn":0,"difficulty":difficulty}
data["4 in a row games"][channel] = {"board": board,"winner":0,"win direction":"",
"win coordinates":[0,0],"players":players,"turn":0,"difficulty":difficulty}
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)
fourInARowDraw.drawImage(channel)
fourInARowDraw.drawImage(channel)
gwendoTurn = False
gwendoTurn = False
if players[0] == "Gwendolyn":
gwendoTurn = True
if players[0] == "Gwendolyn":
gwendoTurn = True
return "Started game against "+opponent+diffText+". It's "+players[0]+"'s turn", True, False, False, gwendoTurn
else:
return "There's already a 4 in a row game going on in this channel", False, False, False, False
return "Started game against "+getName(opponent)+diffText+". It's "+getName(players[0])+"'s turn", True, False, False, gwendoTurn
else:
return "You can't play against yourself", False, False, False, False
return "There's already a 4 in a row game going on in this channel", False, False, False, False
# Places a piece at the lowest available point in a specific column
def placePiece(channel : str,player : int,column : int):
@ -97,7 +101,7 @@ def placePiece(channel : str,player : int,column : int):
data["4 in a row games"][channel]["win direction"] = winDirection
data["4 in a row games"][channel]["win coordinates"] = winCoordinates
message = data["4 in a row games"][channel]["players"][won-1]+" won."
message = getName(data["4 in a row games"][channel]["players"][won-1])+" won."
winAmount = int(data["4 in a row games"][channel]["difficulty"])^2+5
if data["4 in a row games"][channel]["players"][won-1] != "Gwendolyn":
message += " Adding "+str(winAmount)+" GwendoBucks to their account."
@ -106,7 +110,7 @@ def placePiece(channel : str,player : int,column : int):
message = "It's a draw!"
else:
gameWon = False
message = data["4 in a row games"][channel]["players"][player-1]+" placed a piece in column "+str(column+1)+". It's now "+data["4 in a row games"][channel]["players"][turn]+"'s turn."
message = getName(data["4 in a row games"][channel]["players"][player-1])+" placed a piece in column "+str(column+1)+". It's now "+getName(data["4 in a row games"][channel]["players"][turn])+"'s turn."
with open("resources/games/games.json", "w") as f:
json.dump(data,f,indent=4)

View File

@ -2,7 +2,7 @@ import json
import math
from PIL import Image, ImageDraw, ImageFont
from funcs import logThis
from funcs import logThis, getName
# Draws the whole thing
def drawImage(channel):
@ -32,6 +32,16 @@ def drawImage(channel):
pieceStartx = (border+gridBorder)+math.floor(placeGridSize[0]/2)-math.floor(placeSize/2)
pieceStarty = (border+gridBorder)+math.floor(placeGridSize[1]/2)-math.floor(placeSize/2)
if data["4 in a row games"][channel]["players"][0] == "Gwendolyn":
player1 = "Gwendolyn"
else:
player1 = getName(data["4 in a row games"][channel]["players"][0])
if data["4 in a row games"][channel]["players"][1] == "Gwendolyn":
player2 = "Gwendolyn"
else:
player2 = getName(data["4 in a row games"][channel]["players"][1])
background = Image.new("RGB", (w,h+bottomBorder),backgroundColor)
d = ImageDraw.Draw(background,"RGBA")
@ -124,11 +134,11 @@ def drawImage(channel):
exampleHeight = h - border + int((bottomBorder+border)/2) - int(exampleCircles/2)
d.ellipse([(border,exampleHeight),(border+exampleCircles),(exampleHeight+exampleCircles)],fill=(255,255,0),outline=(0,0,0),width=3)
d.text((border+exampleCircles+textPadding,exampleHeight),data["4 in a row games"][channel]["players"][0],font=fnt,fill=(0,0,0))
d.text((border+exampleCircles+textPadding,exampleHeight),player1,font=fnt,fill=(0,0,0))
textWidth = fnt.getsize(data["4 in a row games"][channel]["players"][1])[0]
textWidth = fnt.getsize(player2)[0]
d.ellipse([(w-border-exampleCircles-textWidth-textPadding,exampleHeight),(w-border-textWidth-textPadding),(exampleHeight+exampleCircles)],fill=(255,0,0),outline=(0,0,0),width=3)
d.text((w-border-textWidth,exampleHeight),data["4 in a row games"][channel]["players"][1],font=fnt,fill=(0,0,0))
d.text((w-border-textWidth,exampleHeight),player2,font=fnt,fill=(0,0,0))
background.save("resources/games/4InARowBoards/board"+channel+".png")

View File

@ -1,47 +1,52 @@
import json
from funcs import logThis
from funcs import logThis, getID
# Returns the account balance for a user
def checkBalance(user):
user = user.lower()
logThis("checking "+user+"'s account balance")
with open("resources/games/games.json", "r") as f:
with open("resources/users.json", "r") as f:
data = json.load(f)
if user in data["users"]:
return data["users"][user]
if user in data:
return data[user]["money"]
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")
with open("resources/games/games.json", "r") as f:
with open("resources/users.json", "r") as f:
data = json.load(f)
if user in data["users"]:
points = data["users"][user]
data["users"][user] = points + amount
if user in data:
points = data[user]["money"]
data[user]["money"] = points + amount
else:
data["users"][user] = amount
logThis("Error adding money")
with open("resources/games/games.json", "w") as f:
with open("resources/users.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:
with open("resources/users.json", "r") as f:
data = json.load(f)
if user in data["users"]:
if data["users"][user] >= amount:
addMoney(user,-1 * amount)
addMoney(targetUser,amount)
return "Transferred "+str(amount)+" GwendoBucks to "+targetUser
targetUser == getID(targetUser)
if targetUser.startswith("#"):
if user in data:
if data[user]["money"] >= amount:
addMoney(user,-1 * amount)
addMoney(targetUser,amount)
return "Transferred "+str(amount)+" GwendoBucks to "+targetUser
else:
logThis("They didn't have enough GwendoBucks (error code 1223b)")
return "You don't have that many GwendoBucks (error code 1223b)"
else:
logThis("They didn't have enough GwendoBucks (error code 1223b)")
return "You don't have that many GwendoBucks (error code 1223b)"
logThis("They didn't have enough GwendoBucks (error code 1223a)")
return "You don't have that many GwendoBucks (error code 1223a)"
else:
logThis("They didn't have enough GwendoBucks (error code 1223a)")
return "You don't have that many GwendoBucks (error code 1223a)"
logThis("They weren't in the system")
return "The target doesn't exist"