From f682d39c5a1eeb6a97e7bea77fc5cb8a85835fb3 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 13 Jan 2023 13:32:41 +0100 Subject: [PATCH 1/2] remove some linting warnings --- truthseeker/__init__.py | 10 ++++------ truthseeker/discord_bot.py | 7 +++++-- truthseeker/logic/data_persistance/tables.py | 2 +- truthseeker/logic/game_logic.py | 4 ++-- truthseeker/routes/routes_api.py | 4 +++- truthseeker/routes/routes_socketio.py | 4 +++- truthseeker/routes/routes_ui.py | 2 -- 7 files changed, 18 insertions(+), 15 deletions(-) diff --git a/truthseeker/__init__.py b/truthseeker/__init__.py index 909bb11..87a0a1d 100644 --- a/truthseeker/__init__.py +++ b/truthseeker/__init__.py @@ -1,9 +1,11 @@ -import flask -from flask_socketio import SocketIO import os +import flask +from flask_socketio import SocketIO + from truthseeker import discord_bot + class TruthSeekerApp(flask.Flask): """ Main class of the app @@ -26,14 +28,10 @@ class TruthSeekerApp(flask.Flask): self.discord_bot = discord_bot.DiscordBot() token = os.getenv("DISCORD_BOT_TOKEN") if token: - pass self.discord_bot.start(token) else: print("No token set. Not starting discord bot") - def run_app(self): - self.socketio_app.run(self) - APP = TruthSeekerApp() from truthseeker.routes import routes_api, routes_ui, routes_socketio diff --git a/truthseeker/discord_bot.py b/truthseeker/discord_bot.py index 9a9a354..3205de6 100644 --- a/truthseeker/discord_bot.py +++ b/truthseeker/discord_bot.py @@ -1,14 +1,17 @@ -import discord import threading -import truthseeker import asyncio +import discord +import truthseeker + + class DiscordBot: """ Wrapper around a discord bot, providing utility methods to interact with it :attr Client bot: the underlying discord bot from discord.py :attr TextChannel __channel__: the channel used by the bot to send messages + :attr event_loop: event_loop used by discord.py. Used to schedule tasks from another thread """ def __init__(self): diff --git a/truthseeker/logic/data_persistance/tables.py b/truthseeker/logic/data_persistance/tables.py index 26bebca..f8960ab 100644 --- a/truthseeker/logic/data_persistance/tables.py +++ b/truthseeker/logic/data_persistance/tables.py @@ -1,8 +1,8 @@ from sqlalchemy import Column, Integer, Text, ForeignKey, VARCHAR from sqlalchemy.orm import declarative_base, relationship -Base = declarative_base() +Base = declarative_base() class Locale(Base): __tablename__ = 'T_LOCALE' diff --git a/truthseeker/logic/game_logic.py b/truthseeker/logic/game_logic.py index 62717fd..8764b77 100644 --- a/truthseeker/logic/game_logic.py +++ b/truthseeker/logic/game_logic.py @@ -1,9 +1,9 @@ import string import random +from typing import Union + from truthseeker.logic.data_persistance.data_access import * -from datetime import datetime, timedelta from truthseeker import APP -from typing import Union, Optional def random_string(length: int) ->str: """ diff --git a/truthseeker/routes/routes_api.py b/truthseeker/routes/routes_api.py index 74548f9..af56efc 100644 --- a/truthseeker/routes/routes_api.py +++ b/truthseeker/routes/routes_api.py @@ -1,5 +1,7 @@ -import flask import json + +import flask + from truthseeker import APP from truthseeker.logic import game_logic diff --git a/truthseeker/routes/routes_socketio.py b/truthseeker/routes/routes_socketio.py index d94816e..8288d4a 100644 --- a/truthseeker/routes/routes_socketio.py +++ b/truthseeker/routes/routes_socketio.py @@ -1,9 +1,11 @@ -from flask_socketio import join_room import socketio +from flask_socketio import join_room + from truthseeker import APP from truthseeker.logic import game_logic + @APP.socketio_app.on('connect') def connect(auth): if not (auth and "game_id" in auth): diff --git a/truthseeker/routes/routes_ui.py b/truthseeker/routes/routes_ui.py index a439b70..63a0e47 100644 --- a/truthseeker/routes/routes_ui.py +++ b/truthseeker/routes/routes_ui.py @@ -1,7 +1,5 @@ import flask -from truthseeker.logic import game_logic - routes_ui = flask.Blueprint("ui", __name__) From ad76bda3e6d9bf4406a407f90196f6a9e594e7ed Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Fri, 13 Jan 2023 13:38:35 +0100 Subject: [PATCH 2/2] use snake case for functions --- .../logic/data_persistance/data_access.py | 20 +++++------ truthseeker/logic/game_logic.py | 34 +++++++++---------- 2 files changed, 27 insertions(+), 27 deletions(-) diff --git a/truthseeker/logic/data_persistance/data_access.py b/truthseeker/logic/data_persistance/data_access.py index 346ee2a..4d76116 100644 --- a/truthseeker/logic/data_persistance/data_access.py +++ b/truthseeker/logic/data_persistance/data_access.py @@ -17,38 +17,38 @@ url_object = eg.URL.create( engine = create_engine(url_object) session = Session(engine) -def getTextFromLid(lang,lid) -> str: +def get_text_from_lid(lang,lid) -> str: return session.query(tables.Locale).filter_by(LANG=lang, TEXT_ID=lid).one().TEXT -def getRandomPlace() -> tables.Place: +def get_random_place() -> tables.Place: return random.choice(session.query(tables.Place).all()) -def getRandomNpc() -> tables.Npc : +def get_random_npc() -> tables.Npc : return random.choice(session.query(tables.Npc).all()) -def getNpcRandomTraitId(npc) -> int: +def get_npc_random_trait_id(npc) -> int: reactions = session.query(tables.Reaction).filter_by(NPC_ID=npc.NPC_ID).all() reaction = random.choice(reactions) return reaction.TRAIT_ID -def getNpcRandomAnswer(npc, QA_TYPE) -> tables.Answer : +def get_npc_random_answer(npc, QA_TYPE) -> tables.Answer : answers = session.query(tables.Answer).filter_by(QA_TYPE=QA_TYPE,NPC_ID=npc.NPC_ID).all() return random.choice(answers) -def getRandomQuestion(QA_TYPE) -> tables.Answer : +def get_random_question(QA_TYPE) -> tables.Answer : answers = session.query(tables.Question).filter_by(QUESTION_TYPE=QA_TYPE).all() return random.choice(answers) -def getTraitFromText(text): +def get_trait_from_text(text): trait_lid = session.query(tables.Locale).filter_by(TEXT=text).one().TEXT_ID return session.query(tables.Trait).filter_by(NAME_LID=trait_lid).one().TRAIT_ID -def getTraitFromTraitId(trait_id): +def get_trait_from_trait_id(trait_id): trait = session.query(tables.Trait).filter_by(TRAIT_ID=trait_id).one() return trait -def getTraits(lang): +def get_traits(lang): traits = [] for trait in session.query(tables.Trait).all(): - traits.append(getTextFromLid(lang,trait.NAME_LID)) + traits.append(get_text_from_lid(lang,trait.NAME_LID)) return traits \ No newline at end of file diff --git a/truthseeker/logic/game_logic.py b/truthseeker/logic/game_logic.py index 8764b77..8f1f8bb 100644 --- a/truthseeker/logic/game_logic.py +++ b/truthseeker/logic/game_logic.py @@ -76,8 +76,8 @@ class Game: npcs[npc_id] = {} npcs[npc_id]["name"] = self.gamedata["npcs"][npc_id]["name"] traitId = self.reaction_table[npc_id] - trait = getTraitFromTraitId(traitId) - npcs[npc_id]["reaction"] = getTextFromLid("FR",trait.NAME_LID) + trait = get_trait_from_trait_id(traitId) + npcs[npc_id]["reaction"] = get_text_from_lid("FR",trait.NAME_LID) player_results = data["player"] = {} for member in self.members: player_results[member.username] = member.results @@ -130,7 +130,7 @@ class Game: results = {} try: for npc_id in responses: - trait_id = getTraitIdFromString(responses[npc_id]) + trait_id = get_trait_id_from_string(responses[npc_id]) results[npc_id] = trait_id == str(self.reaction_table[npc_id]) return results except: @@ -200,20 +200,20 @@ def check_username(username: str) -> bool: def generateNpcText(npc: tables.Npc, lang: str) -> dict: data = {} - data["name"] = getTextFromLid(lang, npc.NAME_LID) - data["QA_0"] = getTextFromLid(lang, getNpcRandomAnswer(npc,0).TEXT_LID) - data["QA_1"] = getTextFromLid(lang, getNpcRandomAnswer(npc,1).TEXT_LID) + data["name"] = get_text_from_lid(lang, npc.NAME_LID) + data["QA_0"] = get_text_from_lid(lang, get_npc_random_answer(npc,0).TEXT_LID) + data["QA_1"] = get_text_from_lid(lang, get_npc_random_answer(npc,1).TEXT_LID) return data def generateNpcReactions(npc: tables.Npc) ->list: - return getNpcRandomTraitId(npc) + return get_npc_random_trait_id(npc) -def generatePlaceData(npcs: list, places: list, lang: str) -> dict: +def generate_place_data(npcs: list, places: list, lang: str) -> dict: data = {} random.shuffle(npcs) for place in places: placedata = data[str(place.PLACE_ID)] = {} - placedata["name"] = getTextFromLid(lang,place.NAME_LID) + placedata["name"] = get_text_from_lid(lang,place.NAME_LID) placedata["npcs"] = [] for _ in npcs: placedata["npcs"].append(npcs.pop().NPC_ID) @@ -227,7 +227,7 @@ def generateGameData(LANG): reactions_table = {} npcs = [] while len(npcs) != 5: - npc = getRandomNpc() + npc = get_random_npc() if npc not in npcs : npcs.append(npc) for npc in npcs: @@ -236,15 +236,15 @@ def generateGameData(LANG): places = [] while len(places) != 3: - place = getRandomPlace() + place = get_random_place() if place not in places: places.append(place) - data["rooms"] = generatePlaceData(npcs,places,LANG) + data["rooms"] = generate_place_data(npcs,places,LANG) data["questions"] = {} - data["questions"]["QA_0"] = getTextFromLid("FR",getRandomQuestion(0).TEXT_LID) - data["questions"]["QA_1"] = getTextFromLid("FR",getRandomQuestion(1).TEXT_LID) - data["traits"] = getTraits(LANG) + data["questions"]["QA_0"] = get_text_from_lid("FR",get_random_question(0).TEXT_LID) + data["questions"]["QA_1"] = get_text_from_lid("FR",get_random_question(1).TEXT_LID) + data["traits"] = get_traits(LANG) return data, reactions_table def read_image(path:str): @@ -253,8 +253,8 @@ def read_image(path:str): except: return 1 -def getTraitIdFromString(trait): - return getTraitFromText(trait) +def get_trait_id_from_string(trait): + return get_trait_from_text(trait) def get_npc_image(npc_id): return read_image(f"./truthseeker/static/images/npc/{npc_id}/0.png")