🏗️ Built framework for games

This commit is contained in:
Nikolaj Danger
2020-07-27 17:18:07 +02:00
parent 6dac0e8bdf
commit 95d5fa685a
7 changed files with 31 additions and 29 deletions

View File

@ -4,6 +4,8 @@ from .swfuncs import parseChar, parseRoll, parseDestiny, critRoll
from .lookup import spellFunc, monsterFunc
from .other import nameGen, tavernGen, movieFunc, triviaStart, triviaOtherThing, triviaCountPoints
from .other import nameGen, tavernGen, movieFunc
from .games import triviaStart, triviaOtherThing, triviaCountPoints
from .roll import roll_dice

1
funcs/games/__init__.py Normal file
View File

@ -0,0 +1 @@
from .trivia import triviaCountPoints, triviaStart, triviaOtherThing

View File

@ -5,12 +5,12 @@ import random
from funcs import logThis
def triviaStart(channel : str):
with open("resources/trivia.json", "r") as f:
with open("resources/games.json", "r") as f:
triviaFile = json.load(f)
logThis("Trying to find a trivia question for "+channel)
if channel not in triviaFile["questions"]:
if channel not in triviaFile["trivia questions"]:
with urllib.request.urlopen("https://opentdb.com/api.php?amount=10&type=multiple") as response:
data = json.loads(response.read())
@ -20,8 +20,8 @@ def triviaStart(channel : str):
random.shuffle(answers)
correctAnswer = answers.index(data["results"][0]["correct_answer"]) + 97
triviaFile["questions"][channel] = {"answer" : str(chr(correctAnswer)),"players" : {}}
with open("resources/trivia.json", "w") as f:
triviaFile["trivia questions"][channel] = {"answer" : str(chr(correctAnswer)),"players" : {}}
with open("resources/games.json", "w") as f:
json.dump(triviaFile,f,indent=4)
question = data["results"][0]["question"].replace("'","\'").replace(""","\"")
@ -32,17 +32,17 @@ def triviaStart(channel : str):
return "There's already a trivia question going on. Try again in like, a minute", "", ""
def triviaOtherThing(user : str, channel : str, command : str):
with open("resources/trivia.json", "r") as f:
with open("resources/games.json", "r") as f:
data = json.load(f)
if command in ["a","b","c","d"]:
if channel in data["questions"]:
if user not in data["questions"][channel]["players"]:
if channel in data["trivia questions"]:
if user not in data["trivia questions"][channel]["players"]:
logThis(user+" answered the question in "+channel)
data["questions"][channel]["players"][user] = command
data["trivia questions"][channel]["players"][user] = command
with open("resources/trivia.json", "w") as f:
with open("resources/games.json", "w") as f:
json.dump(data,f,indent=4)
return "Locked in "+user+"'s answer"
@ -58,21 +58,21 @@ def triviaOtherThing(user : str, channel : str, command : str):
return "I didn't quite understand that"
def triviaCountPoints(channel : str):
with open("resources/trivia.json", "r") as f:
with open("resources/games.json", "r") as f:
data = json.load(f)
logThis("Counting points for question in "+channel)
if channel in data["questions"]:
for player, answer in data["questions"][channel]["players"].items():
if answer == data["questions"][channel]["answer"]:
if channel in data["trivia questions"]:
for player, answer in data["trivia questions"][channel]["players"].items():
if answer == data["trivia questions"][channel]["answer"]:
if player in data["users"]:
points = data["users"][player]
data["users"][player] = points + 1
else:
data["users"][player] = 1
with open("resources/trivia.json", "w") as f:
with open("resources/games.json", "w") as f:
json.dump(data,f,indent=4)
else:

View File

@ -137,13 +137,13 @@ def makeFiles():
finally:
f.close()
# Creates trivia.json if it doesn't exist
# Creates games.json if it doesn't exist
try:
f = open("resources/trivia.json","r")
f = open("resources/games.json","r")
except:
logThis("trivia.json didn't exist. Making it now.")
data = {"questions":{},"users":{}}
with open("resources/trivia.json","w") as f:
logThis("games.json didn't exist. Making it now.")
data = {"trivia questions":{},"users":{}}
with open("resources/games.json","w") as f:
json.dump(data,f,indent = 4)
finally:
f.close()

View File

@ -1,3 +1,2 @@
from .generators import nameGen, tavernGen
from .movie import movieFunc
from .trivia import triviaCountPoints, triviaStart, triviaOtherThing
from .movie import movieFunc