Framework for Hangman

This commit is contained in:
NikolajDanger
2020-08-07 17:38:47 +02:00
parent e0e74f5bc0
commit 5839cd5a8f
12 changed files with 270 additions and 7 deletions

43
funcs/games/hangman.py Normal file
View File

@ -0,0 +1,43 @@
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, []