Files
Gwendolyn/funcs/games/hangman.py
NikolajDanger 5a4eebdd7b :broon:
2020-08-07 18:01:47 +02:00

44 lines
1.5 KiB
Python

import json, urllib, random, datetime, string
from . import hangmanDraw
from funcs import getName, logThis
def hangmanStart(channel,user):
with open("resources/games/hangmanGames.json", "r") as f:
data = json.load(f)
if channel not in data:
with urllib.request.urlopen("https://random-word-api.herokuapp.com/word?number=10") as p:
words = json.load(p)
word = list(random.choice(words).upper())
gameID = datetime.datetime.now().strftime('%Y%m%d%H%M%S')
data[channel] = {"player" : user,"guessed letters" : [],"word" : word,"game ID" : gameID}
remainingLetters = list(string.ascii_uppercase)
with open("resources/games/hangmanGames.json", "w") as f:
json.dump(data,f)
try:
hangmanDraw.drawImage(channel)
except:
logThis("Error drawing image (error code 1710)")
return f"{getName(user)} started game of hangman.", True, False, remainingLetters
else:
return "There's already a game going on in the channel", False, False, []
def parseHangman(channel,user,command):
if command == "start":
return hangmanStart(channel,user)
elif command == "stop":
with open("resources/games/hangmanGames.json", "r") as f:
data = json.load(f)
del data[channel]
with open("resources/games/hangmanGames.json", "w") as f:
json.dump(data,f,indent=4)
return "Game stopped.", False, False, []
else:
return "I didn't understand that", False, False, []