:spakles: Database and OOP
This commit is contained in:
@@ -1,223 +1,213 @@
|
||||
import asyncio
|
||||
import discord
|
||||
import json
|
||||
|
||||
from funcs import logThis, deleteGame
|
||||
from .money import addMoney
|
||||
from .fourInARow import parseFourInARow, fourInARowAI
|
||||
from .hex import parseHex, hexAI
|
||||
from .monopoly import parseMonopoly, monopolyContinue
|
||||
from .hangman import parseHangman
|
||||
from funcs import logThis
|
||||
|
||||
# Deletes a message
|
||||
async def deleteMessage(imageLocation,channel):
|
||||
try:
|
||||
with open("resources/games/oldImages/"+imageLocation, "r") as f:
|
||||
messages = f.read().splitlines()
|
||||
|
||||
for message in messages:
|
||||
oldMessage = await channel.fetch_message(int(message))
|
||||
logThis("Deleting old message")
|
||||
await oldMessage.delete()
|
||||
except:
|
||||
oldMessage = ""
|
||||
|
||||
return oldMessage
|
||||
|
||||
# Runs Hex
|
||||
async def runHex(channel,command,user):
|
||||
try:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = parseHex(command,str(channel.id),user)
|
||||
except:
|
||||
logThis("Error parsing command (error code 1510)")
|
||||
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
try:
|
||||
oldImage = await deleteMessage("hex"+str(channel.id),channel)
|
||||
except:
|
||||
logThis("Error deleting old image (error code 1501)")
|
||||
oldImage = await channel.send(file = discord.File("resources/games/hexBoards/board"+str(channel.id)+".png"))
|
||||
if not gameDone:
|
||||
if gwendoTurn:
|
||||
try:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = hexAI(str(channel.id))
|
||||
except:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = "An AI error occured",False,False,False,False
|
||||
logThis("AI error (error code 1520)")
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
await oldImage.delete()
|
||||
oldImage = await channel.send(file = discord.File("resources/games/hexBoards/board"+str(channel.id)+".png"))
|
||||
|
||||
else:
|
||||
with open("resources/games/oldImages/hex"+str(channel.id), "w") as f:
|
||||
f.write(str(oldImage.id))
|
||||
|
||||
if gameDone:
|
||||
with open("resources/games/hexGames.json", "r") as f:
|
||||
data = json.load(f)
|
||||
winner = data[str(channel.id)]["winner"]
|
||||
if winner != 0 and data[channel]["players"][0] != data[channel]["players"][1]: # player1 != player2
|
||||
winnings = data[str(channel.id)]["difficulty"]*10
|
||||
addMoney(data[str(channel.id)]["players"][winner-1].lower(),winnings)
|
||||
|
||||
#deleteGame("hex games",str(channel.id))
|
||||
with open("resources/games/hexGames.json", "r") as f:
|
||||
data = json.load(f)
|
||||
del data[str(channel.id)]
|
||||
with open("resources/games/hexGames.json", "w") as f:
|
||||
json.dump(data,f,indent=4)
|
||||
class GameLoops():
|
||||
def __init__(self,bot):
|
||||
self.bot = bot
|
||||
|
||||
|
||||
|
||||
# Runs Four in a Row
|
||||
async def fiar(channel,command,user):
|
||||
try:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = parseFourInARow(command,str(channel.id),user)
|
||||
except:
|
||||
logThis("Error parsing command (error code 1410)")
|
||||
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
try:
|
||||
oldImage = await deleteMessage("fourInARow"+str(channel.id),channel)
|
||||
except:
|
||||
logThis("Error deleting message (error code 1401)")
|
||||
oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel.id)+".png"))
|
||||
if gameDone == False:
|
||||
if gwendoTurn:
|
||||
try:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = await fourInARowAI(str(channel.id))
|
||||
except:
|
||||
logThis("AI error (error code 1420)")
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
await oldImage.delete()
|
||||
oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel.id)+".png"))
|
||||
if gameDone == False:
|
||||
with open("resources/games/oldImages/fourInARow"+str(channel.id), "w") as f:
|
||||
f.write(str(oldImage.id))
|
||||
try:
|
||||
reactions = ["1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣","7️⃣"]
|
||||
for reaction in reactions:
|
||||
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.id), "w") as f:
|
||||
f.write(str(oldImage.id))
|
||||
try:
|
||||
reactions = ["1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣","7️⃣"]
|
||||
for reaction in reactions:
|
||||
await oldImage.add_reaction(reaction)
|
||||
except:
|
||||
logThis("Image deleted before I could react to all of them")
|
||||
|
||||
if gameDone:
|
||||
with open("resources/games/games.json", "r") as f:
|
||||
data = json.load(f)
|
||||
|
||||
# Deletes a message
|
||||
async def deleteMessage(self, imageLocation,channel):
|
||||
try:
|
||||
with open("resources/games/oldImages/fourInARow"+str(channel.id), "r") as f:
|
||||
oldImage = await channel.fetch_message(int(f.read()))
|
||||
with open("resources/games/oldImages/"+imageLocation, "r") as f:
|
||||
messages = f.read().splitlines()
|
||||
|
||||
await oldImage.delete()
|
||||
for message in messages:
|
||||
oldMessage = await channel.fetch_message(int(message))
|
||||
logThis("Deleting old message")
|
||||
await oldMessage.delete()
|
||||
except:
|
||||
logThis("The old image was already deleted")
|
||||
oldMessage = ""
|
||||
|
||||
winner = data["4 in a row games"][str(channel.id)]["winner"]
|
||||
difficulty = int(data["4 in a row games"][str(channel.id)]["difficulty"])
|
||||
reward = difficulty**2 + 5
|
||||
if winner != 0:
|
||||
if data["4 in a row games"][str(channel.id)]["players"][winner-1].lower() != "gwendolyn":
|
||||
addMoney(data["4 in a row games"][str(channel.id)]["players"][winner-1].lower(),reward)
|
||||
with open("resources/games/games.json", "r") as f:
|
||||
data = json.load(f) #why is this here?
|
||||
return oldMessage
|
||||
|
||||
deleteGame("4 in a row games",str(channel.id))
|
||||
# Runs Four in a Row
|
||||
async def fiar(self, channel,command,user):
|
||||
try:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = self.bot.fourInARow.parseFourInARow(command,str(channel.id),user)
|
||||
except:
|
||||
logThis("Error parsing command (error code 1410)")
|
||||
|
||||
async def runMonopoly(channel, command, user):
|
||||
try:
|
||||
response, showImage, deleteImage, gameStarted, gameContinue = parseMonopoly(command,str(channel.id),user)
|
||||
except:
|
||||
logThis("Error parsing command (error code 1610)")
|
||||
if response != "":
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
try:
|
||||
oldImage = await deleteMessage("monopoly"+str(channel.id),channel)
|
||||
except:
|
||||
logThis("Error deleting message (error code 1601)")
|
||||
oldImage = await channel.send(file = discord.File("resources/games/monopolyBoards/monopolyBoard"+str(channel.id)+".png"))
|
||||
with open("resources/games/oldImages/monopoly"+str(channel.id), "w") as f:
|
||||
f.write(str(oldImage.id))
|
||||
|
||||
if gameContinue:
|
||||
if gameStarted:
|
||||
await asyncio.sleep(60)
|
||||
else:
|
||||
await asyncio.sleep(3)
|
||||
response, showImage, deleteImage, gameDone = monopolyContinue(str(channel.id))
|
||||
em = discord.Embed(description=response,colour = 0x59f442)
|
||||
await channel.send(embed=em)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
try:
|
||||
oldImage = await deleteMessage("monopoly"+str(channel.id),channel)
|
||||
oldImage = await self.deleteMessage("fourInARow"+str(channel.id),channel)
|
||||
except:
|
||||
logThis("Error deleting message (error code 1401)")
|
||||
oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel.id)+".png"))
|
||||
if gameDone == False:
|
||||
if gwendoTurn:
|
||||
try:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = await self.bot.fourInARow.fourInARowAI(str(channel.id))
|
||||
except:
|
||||
logThis("AI error (error code 1420)")
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
await oldImage.delete()
|
||||
oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel.id)+".png"))
|
||||
if gameDone == False:
|
||||
with open("resources/games/oldImages/fourInARow"+str(channel.id), "w") as f:
|
||||
f.write(str(oldImage.id))
|
||||
try:
|
||||
reactions = ["1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣","7️⃣"]
|
||||
for reaction in reactions:
|
||||
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.id), "w") as f:
|
||||
f.write(str(oldImage.id))
|
||||
try:
|
||||
reactions = ["1️⃣","2️⃣","3️⃣","4️⃣","5️⃣","6️⃣","7️⃣"]
|
||||
for reaction in reactions:
|
||||
await oldImage.add_reaction(reaction)
|
||||
except:
|
||||
logThis("Image deleted before I could react to all of them")
|
||||
|
||||
if gameDone:
|
||||
game = self.bot.database["4 in a row games"].find_one({"_id":str(channel.id)})
|
||||
|
||||
try:
|
||||
with open("resources/games/oldImages/fourInARow"+str(channel.id), "r") as f:
|
||||
oldImage = await channel.fetch_message(int(f.read()))
|
||||
|
||||
await oldImage.delete()
|
||||
except:
|
||||
logThis("The old image was already deleted")
|
||||
|
||||
winner = game["winner"]
|
||||
difficulty = int(game["difficulty"])
|
||||
reward = difficulty**2 + 5
|
||||
if winner != 0:
|
||||
if game["players"][winner-1].lower() != "gwendolyn":
|
||||
self.bot.money.addMoney(game["players"][winner-1].lower(),reward)
|
||||
|
||||
self.bot.funcs.deleteGame("4 in a row games",str(channel.id))
|
||||
|
||||
async def runMonopoly(self,channel, command, user):
|
||||
try:
|
||||
response, showImage, deleteImage, gameStarted, gameContinue = self.bot.monopoly.parseMonopoly(command,str(channel.id),user)
|
||||
except:
|
||||
logThis("Error parsing command (error code 1610)")
|
||||
if response != "":
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
try:
|
||||
oldImage = await self.deleteMessage("monopoly"+str(channel.id),channel)
|
||||
except:
|
||||
logThis("Error deleting message (error code 1601)")
|
||||
oldImage = await channel.send(file = discord.File("resources/games/monopolyBoards/monopolyBoard"+str(channel.id)+".png"))
|
||||
with open("resources/games/oldImages/monopoly"+str(channel.id), "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")
|
||||
|
||||
async def runHangman(channel,user,apiKey = "",command = "start"):
|
||||
try:
|
||||
response, showImage, deleteImage, remainingLetters = parseHangman(str(channel.id),user,command,apiKey)
|
||||
except:
|
||||
logThis("Error parsing command (error code 1701)")
|
||||
if response != "":
|
||||
if gameContinue:
|
||||
if gameStarted:
|
||||
await asyncio.sleep(60)
|
||||
else:
|
||||
await asyncio.sleep(3)
|
||||
response, showImage, deleteImage, gameDone = self.bot.monopoly.monopolyContinue(str(channel.id))
|
||||
em = discord.Embed(description=response,colour = 0x59f442)
|
||||
await channel.send(embed=em)
|
||||
if deleteImage:
|
||||
try:
|
||||
oldImage = await self.deleteMessage("monopoly"+str(channel.id),channel)
|
||||
except:
|
||||
logThis("Error deleting message (error code 1601)")
|
||||
|
||||
if showImage:
|
||||
oldImage = await channel.send(file = discord.File("resources/games/monopolyBoards/monopolyBoard"+str(channel.id)+".png"))
|
||||
with open("resources/games/oldImages/monopoly"+str(channel.id), "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")
|
||||
|
||||
async def runHangman(self,channel,user,command = "start"):
|
||||
try:
|
||||
response, showImage, deleteImage, remainingLetters = self.bot.hangman.parseHangman(str(channel.id),user,command)
|
||||
except:
|
||||
logThis("Error parsing command (error code 1701)")
|
||||
if response != "":
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
await self.deleteMessage("hangman"+str(channel.id),channel)
|
||||
oldImage = await channel.send(file = discord.File("resources/games/hangmanBoards/hangmanBoard"+str(channel.id)+".png"))
|
||||
|
||||
if len(remainingLetters) > 15:
|
||||
otherMessage = await channel.send("_ _")
|
||||
reactionMessages = {oldImage : remainingLetters[:15],otherMessage : remainingLetters[15:]}
|
||||
else:
|
||||
otherMessage = ""
|
||||
reactionMessages = {oldImage : remainingLetters}
|
||||
|
||||
oldMessages = str(oldImage.id)
|
||||
if otherMessage != "":
|
||||
oldMessages += "\n"+str(otherMessage.id)
|
||||
with open("resources/games/oldImages/hangman"+str(channel.id), "w") as f:
|
||||
f.write(oldMessages)
|
||||
|
||||
try:
|
||||
for message, letters in reactionMessages.items():
|
||||
for letter in letters:
|
||||
emoji = chr(ord(letter)+127397)
|
||||
await message.add_reaction(emoji)
|
||||
except:
|
||||
logThis("Image deleted before adding all reactions")
|
||||
|
||||
# Runs Hex
|
||||
async def runHex(self,channel,command,user):
|
||||
try:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = self.bot.hex.parseHex(command,str(channel.id),user)
|
||||
except:
|
||||
logThis("Error parsing command (error code 1510)")
|
||||
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
await deleteMessage("hangman"+str(channel.id),channel)
|
||||
oldImage = await channel.send(file = discord.File("resources/games/hangmanBoards/hangmanBoard"+str(channel.id)+".png"))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
try:
|
||||
oldImage = await self.deleteMessage("hex"+str(channel.id),channel)
|
||||
except:
|
||||
logThis("Error deleting old image (error code 1501)")
|
||||
oldImage = await channel.send(file = discord.File("resources/games/hexBoards/board"+str(channel.id)+".png"))
|
||||
if not gameDone:
|
||||
if gwendoTurn:
|
||||
try:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = self.bot.hex.hexAI(str(channel.id))
|
||||
except:
|
||||
response, showImage, deleteImage, gameDone, gwendoTurn = "An AI error occured",False,False,False,False
|
||||
logThis("AI error (error code 1520)")
|
||||
await channel.send(response)
|
||||
logThis(response,str(channel.id))
|
||||
if showImage:
|
||||
if deleteImage:
|
||||
await oldImage.delete()
|
||||
oldImage = await channel.send(file = discord.File("resources/games/hexBoards/board"+str(channel.id)+".png"))
|
||||
|
||||
if len(remainingLetters) > 15:
|
||||
otherMessage = await channel.send("_ _")
|
||||
reactionMessages = {oldImage : remainingLetters[:15],otherMessage : remainingLetters[15:]}
|
||||
else:
|
||||
otherMessage = ""
|
||||
reactionMessages = {oldImage : remainingLetters}
|
||||
else:
|
||||
with open("resources/games/oldImages/hex"+str(channel.id), "w") as f:
|
||||
f.write(str(oldImage.id))
|
||||
|
||||
oldMessages = str(oldImage.id)
|
||||
if otherMessage != "":
|
||||
oldMessages += "\n"+str(otherMessage.id)
|
||||
with open("resources/games/oldImages/hangman"+str(channel.id), "w") as f:
|
||||
f.write(oldMessages)
|
||||
if gameDone:
|
||||
game = self.bot.database["hexGames"].find_one({"_id":str(channel.id)})
|
||||
print(game)
|
||||
winner = game["winner"]
|
||||
if winner != 0 and game["players"][0] != game["players"][1]: # player1 != player2
|
||||
winnings = game["difficulty"]*10
|
||||
self.bot.money.addMoney(game["players"][winner-1].lower(),winnings)
|
||||
|
||||
try:
|
||||
for message, letters in reactionMessages.items():
|
||||
for letter in letters:
|
||||
emoji = chr(ord(letter)+127397)
|
||||
await message.add_reaction(emoji)
|
||||
except:
|
||||
logThis("Image deleted before adding all reactions")
|
||||
self.bot.funcs.deleteGame("hex games",str(channel.id))
|
||||
|
||||
Reference in New Issue
Block a user