diff --git a/.gitignore b/.gitignore index e09e81f..76cf4ae 100644 --- a/.gitignore +++ b/.gitignore @@ -153,6 +153,7 @@ token.txt resources/starWars/swcharacters.json resources/games/games.json resources/games/hexGames.json +resources/games/monopolyGames.json resources/games/blackjackCards/ resources/games/hilo/ resources/starWars/destinyPoints.txt diff --git a/Gwendolyn.py b/Gwendolyn.py index 96bc45b..ce549b1 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -10,9 +10,9 @@ import random #import math import os -from funcs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, emojiToNumber, fiarReactionTest, deleteGame, stopServer, checkBalance, giveMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, spellFunc, monsterFunc, nameGen, tavernGen, movieFunc, roll_dice, parseChar, parseRoll, critRoll, parseDestiny, parseHex, addToDict +from funcs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, emojiToCommand, fiarReactionTest, deleteGame, stopServer, checkBalance, giveMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, spellFunc, monsterFunc, nameGen, tavernGen, movieFunc, roll_dice, parseChar, parseRoll, critRoll, parseDestiny, parseHex, addToDict, monopolyReactionTest -from gameLoops import fiar, blackjackLoop, runHex +from gameLoops import fiar, blackjackLoop, runHex, runMonopoly commandPrefix = "!" @@ -569,6 +569,14 @@ async def parseCommands(message,content): except: logThis("Something went wrong (error code 1500)") + # Runs a game of monopoly + elif content.startswith("monopoly"): + try: + command = content.replace("monopoly","",1) + await runMonopoly(message.channel,command,"#"+str(message.author.id)) + except: + logThis("Something went wrong (error code 1600)") + # Not a command else: logThis("That's not a command (error code 001)",str(message.channel)) @@ -615,11 +623,16 @@ 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,"#"+str(user.id)) + try: + fourInARowTheirTurn, piece = fiarReactionTest(channel,message,"#"+str(user.id)) + except: + fourInARowTheirTurn = False if fourInARowTheirTurn: - place = emojiToNumber(reaction.emoji) + place = emojiToCommand(reaction.emoji) await fiar(channel," place "+str(piece)+" "+str(place),user.id) + elif monopolyReactionTest(channel,message): + await runMonopoly(channel,"roll","#"+str(user.id)) # Runs the whole shabang client.run(token) diff --git a/funcs/__init__.py b/funcs/__init__.py index 581b835..c9aff3d 100644 --- a/funcs/__init__.py +++ b/funcs/__init__.py @@ -1,10 +1,10 @@ """A collection of all Gwendolyn functions.""" -__all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFiles", "emojiToNumber", "fiarReactionTest", "deleteGame", "stopServer", "checkBalance", "giveMoney", "addMoney", "triviaCountPoints", "triviaStart", "triviaAnswer", "blackjackShuffle", "blackjackStart", "blackjackPlayerDrawHand", "blackjackContinue", "blackjackFinish", "blackjackHit", "blackjackStand", "blackjackDouble", "blackjackSplit", "parseFourInARow", "fourInARowAI", "spellFunc", "monsterFunc", "nameGen", "tavernGen", "movieFunc", "roll_dice", "parseChar", "parseRoll", "critRoll", "parseDestiny", "parseHex", "addToDict", "getName", "getID","replaceMultiple","hexAI"] +__all__ = ["helloFunc", "cap", "imageFunc", "logThis", "findWikiPage", "makeFiles", "emojiToCommand", "fiarReactionTest", "deleteGame", "stopServer", "checkBalance", "giveMoney", "addMoney", "triviaCountPoints", "triviaStart", "triviaAnswer", "blackjackShuffle", "blackjackStart", "blackjackPlayerDrawHand", "blackjackContinue", "blackjackFinish", "blackjackHit", "blackjackStand", "blackjackDouble", "blackjackSplit", "parseFourInARow", "fourInARowAI", "spellFunc", "monsterFunc", "nameGen", "tavernGen", "movieFunc", "roll_dice", "parseChar", "parseRoll", "critRoll", "parseDestiny", "parseHex", "addToDict", "getName", "getID", "replaceMultiple", "hexAI", "parseMonopoly", "monopolyContinue", "monopolyReactionTest"] -from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToNumber, fiarReactionTest, deleteGame, stopServer, addToDict, getName, getID +from .miscFuncs import helloFunc, cap, imageFunc, logThis, findWikiPage, makeFiles, replaceMultiple, emojiToCommand, fiarReactionTest, deleteGame, stopServer, addToDict, getName, getID, monopolyReactionTest -from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, parseHex, hexAI +from .games import checkBalance, giveMoney, addMoney, triviaCountPoints, triviaStart, triviaAnswer, blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit, parseFourInARow, fourInARowAI, parseHex, hexAI, parseMonopoly, monopolyContinue from .lookup import spellFunc, monsterFunc diff --git a/funcs/games/__init__.py b/funcs/games/__init__.py index e577d3e..38b3487 100644 --- a/funcs/games/__init__.py +++ b/funcs/games/__init__.py @@ -1,9 +1,10 @@ """Functions for games Gwendolyn can play.""" -__all__ = ["checkBalance", "giveMoney", "addMoney","triviaCountPoints", "triviaStart", "triviaAnswer", "blackjackShuffle", "blackjackStart", "blackjackPlayerDrawHand", "blackjackContinue", "blackjackFinish", "blackjackHit", "blackjackStand", "blackjackDouble", "blackjackSplit", "parseFourInARow", "fourInARowAI", "parseHex"] +__all__ = ["checkBalance", "giveMoney", "addMoney","triviaCountPoints", "triviaStart", "triviaAnswer", "blackjackShuffle", "blackjackStart", "blackjackPlayerDrawHand", "blackjackContinue", "blackjackFinish", "blackjackHit", "blackjackStand", "blackjackDouble", "blackjackSplit", "parseFourInARow", "fourInARowAI", "parseHex", "parseMonopoly", "monopolyContinue"] from .money import checkBalance, giveMoney, addMoney from .trivia import triviaCountPoints, triviaStart, triviaAnswer from .blackjack import blackjackShuffle, blackjackStart, blackjackPlayerDrawHand, blackjackContinue, blackjackFinish, blackjackHit, blackjackStand, blackjackDouble, blackjackSplit from .fourInARow import parseFourInARow, fourInARowAI -from .hex import parseHex, hexAI \ No newline at end of file +from .hex import parseHex, hexAI +from .monopoly import parseMonopoly, monopolyContinue \ No newline at end of file diff --git a/funcs/games/monopoly.py b/funcs/games/monopoly.py new file mode 100644 index 0000000..4990add --- /dev/null +++ b/funcs/games/monopoly.py @@ -0,0 +1,124 @@ +import json, random + +from funcs import getName, logThis +from . import monopolyDraw + +def monopolyStart(channel): + logThis("Starting a monopoly game") + with open("resources/games/monopolyGames.json", "r") as f: + data = json.load(f) + + if channel not in data: + buildings = [0] * 40 + + data[channel] = {"players" : {}, "player list" : [],"turn" : 0, "buildings" : buildings, "last roll" : [0,0], "started" : False} + + with open("resources/games/monopolyGames.json", "w") as f: + json.dump(data,f,indent=4) + + try: + monopolyDraw.drawImage(channel) + except: + logThis("Error drawing board (error code 1640)") + + return "Started a monopoly game. Use \"!monopoly join\" to join within the next minute.", True, False, True, True + else: + return "There's already a monopoly game going on.", False, False, False, False + +def monopolyJoin(channel,user): + with open("resources/games/monopolyGames.json", "r") as f: + data = json.load(f) + + if channel in data: + if not data[channel]["started"]: + if user not in data[channel]["players"]: + if len(data[channel]["players"]) < 6: + + data[channel]["players"][user] = {"position" : 0, "properties" : [], "money" : 1500, "doubles" : 0} + + with open("resources/games/monopolyGames.json", "w") as f: + json.dump(data,f,indent=4) + + return getName(user)+" has joined the game.", False, False, False, False + else: + return "There are already 6 players in the game.", False, False, False, False + else: + return "You're already in the game!", False, False, False, False + else: + return "It's too late to join.", False, False, False, False + else: + return "There's no game going on.", False, False, False, False + +def parseMonopoly(command, channel, user): + logThis("Parsing "+command) + commands = command.split() + if command in [" ", ""] or commands[0] == "start": + try: + return monopolyStart(channel) + except: + logThis("Error starting game (error code 1620)") + elif commands[0] == "join": + try: + return monopolyJoin(channel,user) + except: + logThis("Error joining game (error code 1630)") + elif commands[0] == "roll": + try: + return monopolyRoll(channel,user) + except: + logThis("Error rolling (error code 1650)") + else: + return "I didn't understand that (error code 1602)", False, False, False, False + +def monopolyContinue(channel): + with open("resources/games/monopolyGames.json", "r") as f: + data = json.load(f) + + if channel in data: + if data[channel]["started"] == False: + data[channel]["started"] = True + playerList = list(data[channel]["players"].keys()) + random.shuffle(playerList) + data[channel]["player list"] = playerList + turn = 0 + + else: + turn = (data[channel]["turn"])%len(data[channel]["player list"]) + data[channel]["turn"] = turn + playerList = list(data[channel]["players"].keys()) + + with open("resources/games/monopolyGames.json", "w") as f: + json.dump(data,f,indent=4) + + try: + monopolyDraw.drawImage(channel) + except: + logThis("Error drawing board (error code 1640)") + + message = "It's "+getName(playerList[turn])+"'s turn. Use the 🎲 reaction to roll. You can also use \"!monopoly trade\" at any time." + return message, True, True, False + +def monopolyRoll(channel,user): + with open("resources/games/monopolyGames.json", "r") as f: + data = json.load(f) + + turn = data[channel]["turn"] + currentPlayer = data[channel]["player list"][turn] + + if user == currentPlayer: + rolls = [random.randint(1,6),random.randint(1,6)] + roll = rolls[0] + rolls[1] + newPosition = (data[channel]["players"][user]["position"] + roll)%40 + data[channel]["players"][user]["position"] = newPosition + data[channel]["last roll"] = rolls + + with open("resources/games/monopolyGames.json", "w") as f: + json.dump(data,f,indent=4) + + try: + monopolyDraw.drawImage(channel) + except: + logThis("Error drawing board (error code 1640)") + + return "Rolled a "+str(rolls[0])+" and a "+str(rolls[1])+".", True, True, False, True + else: return "", False, False, False, False diff --git a/funcs/games/monopolyDraw.py b/funcs/games/monopolyDraw.py new file mode 100644 index 0000000..598cc8c --- /dev/null +++ b/funcs/games/monopolyDraw.py @@ -0,0 +1,57 @@ +import math, json + +from funcs import logThis + +from PIL import Image, ImageDraw + +w, h = 1440, 1440 +largeSpace = 191 +smallSpace = math.floor((w - 2*largeSpace)/9) +avatarSize = 50 +avatarHalf = math.floor(avatarSize/2) + +def drawImage(channel): + logThis("Drawing monopoly board for "+channel) + with open("resources/games/monopolyGames.json", "r") as f: + data = json.load(f) + + board = Image.open("resources/games/monopolyBoard.png") + d = ImageDraw.Draw(board,"RGBA") + + for key, value in list(data[channel]["players"].items()): + logThis("Drawing "+key) + try: + x, y = getPosition(value["position"]) + except: + logThis("Error getting position (error code 1641)") + print(str(x)+", "+str(y)) + d.ellipse([(x-avatarHalf,y-avatarHalf),(x+avatarHalf,y+avatarHalf)],fill=(255,0,0)) + + board.save("resources/games/monopolyBoards/monopolyBoard"+channel+".png") + + +def getPosition(positionNumber): + print(positionNumber) + + x, y = 0, 0 + if positionNumber == 0 or positionNumber >= 30: + x = math.floor(largeSpace/2) + elif positionNumber > 0 and positionNumber < 10: + x = math.floor(largeSpace - (smallSpace/2)) + (smallSpace*positionNumber) + elif positionNumber >= 10 and positionNumber <= 20: + x = w - math.floor(largeSpace/2) + elif positionNumber > 20 and positionNumber < 30: + x = w - math.floor(largeSpace - (smallSpace/2)) - (smallSpace*(positionNumber - 20)) + + if positionNumber >= 0 and positionNumber <= 9: + y = math.floor(largeSpace/2) + elif positionNumber > 10 and positionNumber < 19: + y = math.floor(largeSpace - (smallSpace/2)) + (smallSpace*(positionNumber-10)) + elif positionNumber >= 20 and positionNumber <= 30: + y = h - math.floor(largeSpace/2) + elif positionNumber > 30: + y = h - math.floor(largeSpace - (smallSpace/2)) - (smallSpace*(positionNumber - 30)) + + return x, y + + diff --git a/funcs/miscFuncs.py b/funcs/miscFuncs.py index b7fbf78..6e17e5d 100644 --- a/funcs/miscFuncs.py +++ b/funcs/miscFuncs.py @@ -186,7 +186,7 @@ def replaceMultiple(mainString, toBeReplaces, newString): return mainString -def emojiToNumber(emoji): +def emojiToCommand(emoji): if emoji == "1️⃣": return 1 elif emoji == "2️⃣": @@ -201,6 +201,8 @@ def emojiToNumber(emoji): return 6 elif emoji == "7️⃣": return 7 + elif emoji == "🎲": + return "roll" else: return "" def fiarReactionTest(channel,message,user): @@ -223,16 +225,31 @@ def fiarReactionTest(channel,message,user): else: return False, 0 +def monopolyReactionTest(channel,message): + with open("resources/games/oldImages/monopoly"+str(channel), "r") as f: + oldImage = int(f.read()) + + if message.id == oldImage: + logThis("They reacted to the monopoly game") + return True + else: + return False + def stopServer(): with open("resources/games/games.json","r") as f: - data = json.load(f) + games = json.load(f) - data["trivia questions"] = {} - data["blackjack games"] = {} - data["4 in a row games"] = {} + games["trivia questions"] = {} + games["blackjack games"] = {} + games["4 in a row games"] = {} with open("resources/games/games.json","w") as f: - json.dump(data,f,indent=4) + json.dump(games,f,indent=4) + + emptyDict = {} + + with open("resources/games/monopolyGames.json", "w") as f: + json.dump(emptyDict,f,indent=4) def deleteGame(gameType,channel): with open("resources/games/games.json", "r") as f: diff --git a/gameLoops.py b/gameLoops.py index e8ef39f..058f033 100644 --- a/gameLoops.py +++ b/gameLoops.py @@ -2,7 +2,7 @@ import asyncio import discord import json -from funcs import logThis, addMoney, deleteGame, parseFourInARow, fourInARowAI, blackjackContinue, blackjackFinish, parseHex, hexAI +from funcs import logThis, addMoney, deleteGame, parseFourInARow, fourInARowAI, blackjackContinue, blackjackFinish, parseHex, hexAI, parseMonopoly, monopolyContinue # Deletes a message async def deleteMessage(imageLocation,channel): @@ -119,9 +119,6 @@ async def fiar(channel,command,user): await oldImage.add_reaction(reaction) except: logThis("Image deleted before I could react to all of them") -# else: -# 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: @@ -192,3 +189,45 @@ async def blackjackLoop(channel,gameRound,gameID): logThis("Ending loop on round "+str(gameRound),str(channel)) else: logThis("Ending loop on round "+str(gameRound),str(channel)) + +async def runMonopoly(channel, command, user): + try: + response, showImage, deleteImage, gameStarted, gameContinue = parseMonopoly(command,str(channel),user) + except: + logThis("Error parsing command (error code 1610)") + if response != "": + await channel.send(response) + logThis(response,str(channel)) + if showImage: + if deleteImage: + try: + oldImage = await deleteMessage("monopoly"+str(channel),channel) + except: + logThis("Error deleting message (error code 1601)") + oldImage = await channel.send(file = discord.File("resources/games/monopolyBoards/monopolyBoard"+str(channel)+".png")) + with open("resources/games/oldImages/monopoly"+str(channel), "w") as f: + f.write(str(oldImage.id)) + + if gameContinue: + if gameStarted: + await asyncio.sleep(60) + else: + await asyncio.sleep(5) + response, showImage, deleteImage, gameDone = monopolyContinue(str(channel)) + em = discord.Embed(description=response,colour = 0x59f442) + await channel.send(embed=em) + if showImage: + if deleteImage: + try: + oldImage = await deleteMessage("monopoly"+str(channel),channel) + except: + logThis("Error deleting message (error code 1601)") + oldImage = await channel.send(file = discord.File("resources/games/monopolyBoards/monopolyBoard"+str(channel)+".png")) + with open("resources/games/oldImages/monopoly"+str(channel), "w") as f: + f.write(str(oldImage.id)) + if gameDone == False: + try: + await oldImage.add_reaction("🎲") + except: + logThis("Image deleted before I could react to all of them") + \ No newline at end of file diff --git a/resources/errorCodes.txt b/resources/errorCodes.txt index adde8e4..1e08b02 100644 --- a/resources/errorCodes.txt +++ b/resources/errorCodes.txt @@ -113,3 +113,14 @@ 1531 - Invalid position 1532 - Cannot place on existing piece 1533 - Position out of bounds + +16 - Monopoly +1600 - Unspecified error +1601 - Error deleting old image +1602 - Not a command +1610 - Unspecified parsing error +1620 - Unspecified monopolyStart() error +1630 - Unspecified monopolyJoin() error +1640 - Unspecified monopolyDraw error +1641 - Error in getPosition() +1650 - Error in monopolyRoll() diff --git a/resources/games/monopolyBoard.png b/resources/games/monopolyBoard.png new file mode 100644 index 0000000..a7cb5c8 Binary files /dev/null and b/resources/games/monopolyBoard.png differ diff --git a/resources/games/monopolyBoards/monopolyBoardtest.png b/resources/games/monopolyBoards/monopolyBoardtest.png new file mode 100644 index 0000000..50d8787 Binary files /dev/null and b/resources/games/monopolyBoards/monopolyBoardtest.png differ diff --git a/resources/startingFiles.json b/resources/startingFiles.json index 8846a9d..85537f4 100644 --- a/resources/startingFiles.json +++ b/resources/startingFiles.json @@ -2,6 +2,7 @@ "json":{ "resources/starWars/swcharacters.json" : {}, "resources/games/hexGames.json": {}, + "resources/games/monopolyGames.json": {}, "resources/users.json" : {}, "resources/games/games.json" : { "trivia questions":{}, @@ -75,6 +76,7 @@ "resources/games/hexBoards", "resources/games/oldImages", "resources/games/blackjackCards", - "resources/games/hilo" + "resources/games/hilo", + "resources/games/monopolyBoards" ] } \ No newline at end of file