From daa2d0ddde0bea5fb01a93e9c47470e479142313 Mon Sep 17 00:00:00 2001 From: NikolajDanger Date: Fri, 31 Jul 2020 15:49:58 +0200 Subject: [PATCH] :sparkles: Four in a row is almost done --- Gwendolyn.py | 72 ++++++---- funcs/__init__.py | 2 +- funcs/games/draw4InARow.py | 20 ++- funcs/games/fourInARow.py | 28 +++- funcs/miscFuncs.py | 38 ++++++ resources/games/blackjackCards.txt | 208 +++++++++++++++++++++++++++++ resources/games/hilo.txt | 1 + 7 files changed, 338 insertions(+), 31 deletions(-) create mode 100644 resources/games/blackjackCards.txt create mode 100644 resources/games/hilo.txt diff --git a/Gwendolyn.py b/Gwendolyn.py index cb55b5b..254e701 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -592,29 +592,8 @@ async def parseCommands(message,content): # Runs a game of four in a row elif content.startswith("fourinarow"): - response, showImage, deleteImage, gameDone = parseFourInARow(content.replace("fourinarow",""),str(message.channel),message.author.display_name) - await message.channel.send(response) - logThis(response,str(message.channel)) - if showImage: - if deleteImage: - try: - with open("resources/games/oldImages/fourInARow"+str(message.channel), "r") as f: - oldImage = await message.channel.fetch_message(int(f.read())) - await oldImage.delete() - except: - oldImage = "" - oldImage = await message.channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(message.channel)+".png")) - with open("resources/games/oldImages/fourInARow"+str(message.channel), "w") as f: - f.write(str(oldImage.id)) - - if gameDone: - with open("resources/games/games.json", "r") as f: - data = json.load(f) - - del data["4 in a row games"][str(message.channel)] - - with open("resources/games/games.json","w") as f: - json.dump(data,f,indent=4) + command = content.replace("fourinarow","") + await fiar(message.channel,command,message.author.display_name) @@ -623,6 +602,42 @@ async def parseCommands(message,content): logThis("That's not a command (error code 001)",str(message.channel)) await message.channel.send("That's not a command (error code 001)") +async def fiar(channel,command,user): + response, showImage, deleteImage, gameDone = parseFourInARow(command,str(channel),user) + await channel.send(response) + logThis(response,str(channel)) + if showImage: + if deleteImage: + try: + logThis("Finding old image") + with open("resources/games/oldImages/fourInARow"+str(channel), "r") as f: + oldImage = await channel.fetch_message(int(f.read())) + logThis("Deleting old image") + await oldImage.delete() + except: + oldImage = "" + oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel)+".png")) + if gameDone == False: + await oldImage.add_reaction("1️⃣") + await oldImage.add_reaction("2️⃣") + await oldImage.add_reaction("3️⃣") + await oldImage.add_reaction("4️⃣") + await oldImage.add_reaction("5️⃣") + await oldImage.add_reaction("6️⃣") + await oldImage.add_reaction("7️⃣") + with open("resources/games/oldImages/fourInARow"+str(channel), "w") as f: + f.write(str(oldImage.id)) + + if gameDone: + with open("resources/games/games.json", "r") as f: + data = json.load(f) + + del data["4 in a row games"][str(channel)] + + with open("resources/games/games.json","w") as f: + json.dump(data,f,indent=4) + + # Makes files if they don't exist yet makeFiles() @@ -657,5 +672,16 @@ async def on_message(message): emoji = random.choice(["😠", "🖕", "👎"]) await message.add_reaction(emoji) +@client.event +async def on_reaction_add(reaction,user): + message = reaction.message + channel = message.channel + logThis(user.display_name+" reacted to a message",str(channel)) + fourInARowTheirTurn, piece = fiarReactionTest(channel,message,user.display_name) + + if fourInARowTheirTurn: + place = emojiToNumber(reaction.emoji) + await fiar(channel," place "+str(piece)+" "+str(place),user.display_name) + # Runs the whole shabang client.run(token) diff --git a/funcs/__init__.py b/funcs/__init__.py index 2aa149c..763d981 100644 --- a/funcs/__init__.py +++ b/funcs/__init__.py @@ -1,4 +1,4 @@ -from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple +from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToNumber, fiarReactionTest from .swfuncs import * diff --git a/funcs/games/draw4InARow.py b/funcs/games/draw4InARow.py index 476d7e7..84eb29b 100644 --- a/funcs/games/draw4InARow.py +++ b/funcs/games/draw4InARow.py @@ -16,18 +16,22 @@ def drawImage(channel): gridBorder = 40 cornerSize = 100 outlineSize = 20 + bottomBorder = 110 + exampleCircles = 100 w, h = 2800,2000 backgroundColor = (128,128,128,255) boardColor = (0,0,170) - placeSize = 285 + + fnt = ImageFont.truetype('resources/futura-bold.ttf', exampleCircles) + boardSize = [w-(2*(border+gridBorder)),h-(2*(border+gridBorder))] placeGridSize = [math.floor(boardSize[0]/7),math.floor(boardSize[1]/6)] pieceStartx = (border+gridBorder)+math.floor(placeGridSize[0]/2)-math.floor(placeSize/2) pieceStarty = (border+gridBorder)+math.floor(placeGridSize[1]/2)-math.floor(placeSize/2) - background = Image.new("RGB", (w,h),backgroundColor) + background = Image.new("RGB", (w,h+bottomBorder),backgroundColor) d = ImageDraw.Draw(background,"RGBA") # This whole part was the easiest way to make a rectangle with rounded corners and an outline @@ -110,4 +114,16 @@ def drawImage(channel): winBar.putalpha(mask) background.paste(winBar,(startx,starty),winBar) + # Bottom + textPadding = 20 + + 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,stroke_fill=(0,0,0)) + + textWidth = fnt.getsize(data["4 in a row games"][channel]["players"][1])[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,stroke_fill=(0,0,0)) + + background.save("resources/games/4InARowBoards/board"+channel+".png") diff --git a/funcs/games/fourInARow.py b/funcs/games/fourInARow.py index 2442412..d3f37d2 100644 --- a/funcs/games/fourInARow.py +++ b/funcs/games/fourInARow.py @@ -4,7 +4,7 @@ from . import draw4InARow from funcs import logThis # Starts the game -def fourInARowStart(channel): +def fourInARowStart(channel, user, opponent): with open("resources/games/games.json", "r") as f: data = json.load(f) @@ -13,14 +13,14 @@ def fourInARowStart(channel): board = [ [ 0 for i in range(7) ] for j in range(6) ] data["4 in a row games"][channel] = {"board": board,"winner":0,"win direction":"", - "win coordinates":[0,0],"players":["",""]} + "win coordinates":[0,0],"players":[user,opponent],"turn":0} with open("resources/games/games.json", "w") as f: json.dump(data,f,indent=4) draw4InARow.drawImage(channel) - return "Started game", True, False, False + return "Started game. It's "+user+"'s turn", True, False, False else: return "There's already a 4 in a row game going on in this channel", False, False, False @@ -42,6 +42,8 @@ def placePiece(channel : str,player : int,column : int): if placementx != -1: board[placementx][placementy] = player data["4 in a row games"][channel]["board"] = board + turn = (data["4 in a row games"][channel]["turn"]+1)%2 + data["4 in a row games"][channel]["turn"] = turn with open("resources/games/games.json", "w") as f: json.dump(data,f,indent=4) @@ -57,7 +59,7 @@ def placePiece(channel : str,player : int,column : int): message = data["4 in a row games"][channel]["players"][won-1]+" won" else: gameWon = False - message = "Placed a piece" + message = "Placed a piece. It's now "+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) @@ -72,8 +74,24 @@ def placePiece(channel : str,player : int,column : int): # Parses command def parseFourInARow(command, channel, user): + + print(command) if command == "" or command == " ": - return fourInARowStart(channel) + return "I didn't get that. \"Use !fourinarow start\" to start a game.", False, False, False + + + elif command.startswith(" start "): + command = command.replace(" start ","") + return fourInARowStart(channel,user,command) + + elif command.startswith(" stop"): + with open("resources/games/games.json", "r") as f: + data = json.load(f) + + if user in data["4 in a row games"][channel]["players"]: + return "Ending game.", False, False, True + else: + return "You can't end a game where you're not a player.", False, False, False elif command.startswith(" place"): commands = command.split(" ") #try: diff --git a/funcs/miscFuncs.py b/funcs/miscFuncs.py index 45709fc..09f2ffa 100644 --- a/funcs/miscFuncs.py +++ b/funcs/miscFuncs.py @@ -217,3 +217,41 @@ def replaceMultiple(mainString, toBeReplaces, newString): return mainString +def emojiToNumber(emoji): + if emoji == "1️⃣": + return 1 + elif emoji == "2️⃣": + return 2 + elif emoji == "3️⃣": + return 3 + elif emoji == "4️⃣": + return 4 + elif emoji == "5️⃣": + return 5 + elif emoji == "6️⃣": + return 6 + elif emoji == "7️⃣": + return 7 + else: return "" + +def fiarReactionTest(channel,message,user): + with open("resources/games/games.json", "r") as f: + data = json.load(f) + + with open("resources/games/oldImages/fourInARow"+str(channel), "r") as f: + oldImage = int(f.read()) + + if message.id == oldImage: + logThis("They reacted to the fourinarow game") + turn = data["4 in a row games"][str(channel)]["turn"] + if user.lower() == data["4 in a row games"][str(channel)]["players"][turn].lower(): + if user != data["4 in a row games"][str(channel)]["players"][turn]: + data["4 in a row games"][str(channel)]["players"][turn] = user + with open("resources/games/games.json","w") as f: + json.dump(data,f,indent=4) + return True, turn+1 + else: + logThis("It wasn't their turn") + return False, 0 + else: + return False, 0 \ No newline at end of file diff --git a/resources/games/blackjackCards.txt b/resources/games/blackjackCards.txt new file mode 100644 index 0000000..fe40657 --- /dev/null +++ b/resources/games/blackjackCards.txt @@ -0,0 +1,208 @@ +2s +3s +0s +7s +jh +7h +8c +kh +0d +js +ks +qh +2c +4c +kc +3h +kc +3c +2h +5d +9h +2d +3s +ks +6s +9d +2s +8s +qd +8d +6s +9s +js +5c +9s +ac +6s +6h +6h +0c +2d +ad +qh +qc +qh +7c +0d +8d +2h +as +jc +3d +9c +kd +4c +4d +ah +kd +8c +8h +5d +qc +9s +0c +qh +jh +0s +8d +6c +8h +5c +3c +0h +4h +8c +6d +5c +4d +2c +7s +2s +ks +5s +ah +jd +ah +8h +0h +4h +3d +7c +4d +ad +3h +9h +3h +6c +5s +6d +jd +7c +ks +7d +0s +jd +8s +ah +0s +as +7d +2d +4s +qs +3s +9h +9c +4c +4h +3c +5c +as +8s +9d +7c +5d +6d +3d +kc +jc +6s +4s +5h +9d +2h +qd +qc +qs +6d +jh +ac +as +5h +6h +jc +ad +7h +4c +9d +5h +kh +0h +7d +7h +9c +8c +qc +4s +qs +0c +jh +ac +9s +4h +3h +ad +kc +kh +qs +5d +jc +qd +8s +js +ac +js +jd +kd +qd +2s +4d +2h +5h +7d +6c +0c +6h +4s +0d +8d +7h +2c +2d +9c +7s +6c +5s +3d +3s +9h +8h +3c +0d +0h +7s +2c +kd +5s +kh \ No newline at end of file diff --git a/resources/games/hilo.txt b/resources/games/hilo.txt new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/resources/games/hilo.txt @@ -0,0 +1 @@ +0 \ No newline at end of file