Game loops are separate

This commit is contained in:
NikolajDanger
2020-08-04 00:05:24 +02:00
parent 10d238503d
commit b09a881e06
2 changed files with 140 additions and 132 deletions

138
gameLoops.py Normal file
View File

@ -0,0 +1,138 @@
import asyncio
import discord
import json
from funcs import logThis, addMoney, deleteGame, parseFourInARow, fourInARowAI, blackjackContinue, blackjackFinish
# Deletes a message
async def deleteMessage(imageLocation,channel):
try:
logThis("Finding old image")
with open("resources/games/oldImages/"+imageLocation, "r") as f:
oldImage = await channel.fetch_message(int(f.read()))
logThis("Deleting old image")
await oldImage.delete()
except:
oldImage = ""
return oldImage
# Runs Four in a Row
async def fiar(channel,command,user):
try:
response, showImage, deleteImage, gameDone, gwendoTurn = parseFourInARow(command,str(channel),user)
except:
logThis("Error parsing command (error code 1410)")
await channel.send(response)
logThis(response,str(channel))
if showImage:
if deleteImage:
try:
oldImage = await deleteMessage("fourInARow"+str(channel),channel)
except:
logThis("Error deleting message (error code 1401)")
oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel)+".png"))
if gameDone == False:
if gwendoTurn:
try:
response, showImage, deleteImage, gameDone, gwendoTurn = fourInARowAI(str(channel))
except:
logThis("AI error (error code 1420)")
await channel.send(response)
logThis(response,str(channel))
if showImage:
if deleteImage:
await oldImage.delete()
oldImage = await channel.send(file = discord.File("resources/games/4InARowBoards/board"+str(channel)+".png"))
if gameDone == False:
with open("resources/games/oldImages/fourInARow"+str(channel), "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), "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), "w") as f:
# f.write(str(oldImage.id))
if gameDone:
with open("resources/games/games.json", "r") as f:
data = json.load(f)
try:
with open("resources/games/oldImages/fourInARow"+str(channel), "r") as f:
oldImage = await channel.fetch_message(int(f.read()))
await oldImage.delete()
except:
logThis("The old image was already deleted")
winner = data["4 in a row games"][str(channel)]["winner"]
if winner != 0:
addMoney(data["4 in a row games"][str(channel)]["players"][winner-1].lower(),20)
with open("resources/games/games.json", "r") as f:
data = json.load(f)
deleteGame("4 in a row games",str(channel))
# Loop of blackjack game rounds
async def blackjackLoop(channel,gameRound,gameID):
logThis("Loop "+str(gameRound),str(channel))
with open("resources/games/oldImages/blackjack"+str(channel), "r") as f:
oldImage = await channel.fetch_message(int(f.read()))
new_message, allStanding, gamedone = blackjackContinue(str(channel))
if new_message != "":
logThis(new_message,str(channel))
await channel.send(new_message)
if gamedone == False:
await oldImage.delete()
oldImage = await channel.send(file = discord.File("resources/games/blackjackTables/blackjackTable"+str(channel)+".png"))
with open("resources/games/oldImages/blackjack"+str(channel), "w") as f:
f.write(str(oldImage.id))
try:
if allStanding:
await asyncio.sleep(5)
else:
await asyncio.sleep(120)
except:
logThis("Loop "+str(gameRound)+" interrupted (error code 1321)")
with open("resources/games/games.json", "r") as f:
data = json.load(f)
if str(channel) in data["blackjack games"]:
realRound = data["blackjack games"][str(channel)]["round"]
realGameID = data["blackjack games"][str(channel)]["id"]
if gameRound == realRound and realGameID == gameID:
if gamedone == False:
logThis("Loop "+str(gameRound)+" calling blackjackLoop()",str(channel))
await blackjackLoop(channel,gameRound+1,gameID)
else:
try:
new_message = blackjackFinish(str(channel))
except:
logThis("Something fucked up (error code 1310)")
await channel.send(new_message)
else:
logThis("Ending loop on round "+str(gameRound),str(channel))
else:
logThis("Ending loop on round "+str(gameRound),str(channel))