From 95d5fa685a2d09ce806d5d82c7c9d8ff2b412312 Mon Sep 17 00:00:00 2001 From: Nikolaj Danger Date: Mon, 27 Jul 2020 17:18:07 +0200 Subject: [PATCH] :building_construction: Built framework for games --- .gitignore | 2 +- Gwendolyn.py | 12 ++++++------ funcs/__init__.py | 4 +++- funcs/games/__init__.py | 1 + funcs/{other => games}/trivia.py | 28 ++++++++++++++-------------- funcs/gwendolynFuncs.py | 10 +++++----- funcs/other/__init__.py | 3 +-- 7 files changed, 31 insertions(+), 29 deletions(-) create mode 100644 funcs/games/__init__.py rename funcs/{other => games}/trivia.py (72%) diff --git a/.gitignore b/.gitignore index ed03e5c..cfec52c 100644 --- a/.gitignore +++ b/.gitignore @@ -151,6 +151,6 @@ static .vscode/ token.txt resources/swcharacters.json -resources/trivia.json +resources/games.json resources/destinyPoints.txt gwendolynTest.py diff --git a/Gwendolyn.py b/Gwendolyn.py index fd374ba..6abaeb6 100644 --- a/Gwendolyn.py +++ b/Gwendolyn.py @@ -53,12 +53,12 @@ async def on_message(message): funcs.logThis(message.author.name+" ran \""+message.content+"\"") await message.channel.send("Logging out...") - with open("resources/trivia.json","r") as f: + with open("resources/games.json","r") as f: data = json.load(f) - data["questions"] = {} + data["trivia questions"] = {} - with open("resources/trivia.json","w") as f: + with open("resources/games.json","w") as f: json.dump(data,f,indent=4) await client.logout() @@ -254,11 +254,11 @@ async def on_message(message): funcs.triviaCountPoints(str(message.channel)) - with open("resources/trivia.json", "r") as f: + with open("resources/games.json", "r") as f: data = json.load(f) - del data["questions"][str(message.channel)] - with open("resources/trivia.json", "w") as f: + del data["trivia questions"][str(message.channel)] + with open("resources/games.json", "w") as f: json.dump(data,f,indent=4) funcs.logThis("Time's up for the trivia question in "+str(message.channel)) diff --git a/funcs/__init__.py b/funcs/__init__.py index 49590ac..a054d51 100644 --- a/funcs/__init__.py +++ b/funcs/__init__.py @@ -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 \ No newline at end of file diff --git a/funcs/games/__init__.py b/funcs/games/__init__.py new file mode 100644 index 0000000..2ac73e2 --- /dev/null +++ b/funcs/games/__init__.py @@ -0,0 +1 @@ +from .trivia import triviaCountPoints, triviaStart, triviaOtherThing \ No newline at end of file diff --git a/funcs/other/trivia.py b/funcs/games/trivia.py similarity index 72% rename from funcs/other/trivia.py rename to funcs/games/trivia.py index 6dd86a8..0be0da8 100644 --- a/funcs/other/trivia.py +++ b/funcs/games/trivia.py @@ -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: diff --git a/funcs/gwendolynFuncs.py b/funcs/gwendolynFuncs.py index 94b7717..d95a2bc 100644 --- a/funcs/gwendolynFuncs.py +++ b/funcs/gwendolynFuncs.py @@ -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() diff --git a/funcs/other/__init__.py b/funcs/other/__init__.py index bc40ec9..8f788c5 100644 --- a/funcs/other/__init__.py +++ b/funcs/other/__init__.py @@ -1,3 +1,2 @@ from .generators import nameGen, tavernGen -from .movie import movieFunc -from .trivia import triviaCountPoints, triviaStart, triviaOtherThing \ No newline at end of file +from .movie import movieFunc \ No newline at end of file