diff --git a/truthinquiry/ext/database/dbutils.py b/truthinquiry/ext/database/dbutils.py index 2de5f48..99ebe1a 100644 --- a/truthinquiry/ext/database/dbutils.py +++ b/truthinquiry/ext/database/dbutils.py @@ -11,7 +11,8 @@ def get_text_from_lid(lang: str, lid: int) -> str: :param lid: the locale id the get the text from :return: the text associated to the lang and lid """ - return db.session.query(Locale).filter_by(LANG=lang, TEXT_ID=lid).one().TEXT + texts = db.session.query(Text).filter_by(LANG=lang, TEXT_ID=lid).all() + return random.choice(texts).TEXT def get_random_place() -> Place: """ @@ -48,7 +49,7 @@ def get_npc_random_answer(npc_id:int, qa_type:int) -> Answer : :param qa_type: the type of the question :return: an Answer object """ - answers = db.session.query(Answer).filter_by(QA_TYPE=qa_type,NPC_ID=npc_id.NPC_ID).all() + answers = db.session.query(Answer).filter_by(QUESTION_TYPE_ID=qa_type,NPC_ID=npc_id.NPC_ID).all() return random.choice(answers) def get_random_question(qa_type: int) -> QuestionType : @@ -58,9 +59,8 @@ def get_random_question(qa_type: int) -> QuestionType : :param qa_type: the type of the question :return: a Question object """ - answers = db.session.query(QuestionType).filter_by(QUESTION_TYPE_ID=qa_type).all() - return random.choice(answers) - + return db.session.query(QuestionType).filter_by(QUESTION_TYPE_ID=qa_type).one() + def get_trait_from_text(text: str) -> int: """ Returns the trait_id from its text value @@ -68,7 +68,7 @@ def get_trait_from_text(text: str) -> int: :param text: the text representation of the trait in any lang :return: the trait_id linked to this text """ - trait_lid = db.session.query(Locale).filter_by(TEXT=text).one().TEXT_ID + trait_lid = db.session.query(Text).filter_by(TEXT=text).one().TEXT_ID return db.session.query(Trait).filter_by(NAME_LID=trait_lid).one().TRAIT_ID def get_trait_from_trait_id(trait_id: int) -> Trait: @@ -81,7 +81,7 @@ def get_trait_from_trait_id(trait_id: int) -> Trait: trait = db.session.query(Trait).filter_by(TRAIT_ID=trait_id).one() return trait -def get_reaction_description(lang, npc_id, trait_id) -> str: +def get_reaction_description(lang, trait_id) -> str: """ Returns the description of the reaction of a given npc in the language specified by the parametter lang diff --git a/truthinquiry/ext/database/models.py b/truthinquiry/ext/database/models.py index 433c7dd..c53a8e7 100644 --- a/truthinquiry/ext/database/models.py +++ b/truthinquiry/ext/database/models.py @@ -1,6 +1,5 @@ from sqlalchemy import Column, Integer, VARCHAR, Text, ForeignKey -from sqlalchemy.orm import relationship -from sqlalchemy.ext.declarative import declarative_base +from sqlalchemy.orm import relationship, declarative_base Base = declarative_base() @@ -79,7 +78,7 @@ class QuestionType(Base): """ __tablename__ = "T_QUESTION_TYPE" - QUESTION_TYPE_ID = Column(Integer, primary_key=True, comment="ID of this question type.") + QUESTION_TYPE_ID = Column(Integer, default=0, primary_key=True, comment="ID of this question type.") TEXT_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Question text") LOCALE = relationship("Locale") @@ -101,7 +100,7 @@ class Answer(Base): """ __tablename__ = "T_ANSWER" - QUESTION_TYPE_ID = Column(Integer, ForeignKey("T_QUESTION_TYPE.QUESTION_TYPE_ID"), primary_key=True, comment="Question type ID") + QUESTION_TYPE_ID = Column(Integer,ForeignKey("T_QUESTION_TYPE.QUESTION_TYPE_ID"),primary_key=True, comment="Question type ID") NPC_ID = Column(Integer, ForeignKey("T_NPC.NPC_ID"), primary_key=True, comment="ID of the NPC that will say this answer") TEXT_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Text of the answer") LOCALE = relationship("Locale") @@ -126,7 +125,7 @@ class Npc(Base): """ __tablename__ = "T_NPC" - NPC_ID = Column(Integer, primary_key=True, comment="ID of this Npc") + NPC_ID = Column(Integer, autoincrement=True, primary_key=True, comment="ID of this Npc") NAME_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Name of this Npc") LOCALE = relationship("Locale") @@ -154,15 +153,13 @@ class Trait(Base): Desc = relationship("Locale",foreign_keys=[DESC_LID]) - def __init__(self, TRAIT_ID, NAME_LID): + def __init__(self, TRAIT_ID, NAME_LID, DESC_LID): self.TRAIT_ID = TRAIT_ID self.NAME_LID = NAME_LID + self.DESC_LID = DESC_LID def __str__(self) -> str: - return f"Trait(TRAIT_ID={self.TRAIT_ID}, NAME_LID={self.NAME_LID})" - - def __repr__(self) -> str: - return self.__str__() + return f"{self.TRAIT_ID} {self.NAME_LID}" class Reaction(Base): diff --git a/truthinquiry/logic/game_logic.py b/truthinquiry/logic/game_logic.py index 9e7d773..fd3b8e9 100644 --- a/truthinquiry/logic/game_logic.py +++ b/truthinquiry/logic/game_logic.py @@ -84,7 +84,7 @@ class Game: trait_id = self.reaction_table[npc_id] trait = dbutils.get_trait_from_trait_id(trait_id) npcs[npc_id]["reaction"] = dbutils.get_text_from_lid("FR", trait.NAME_LID) - npcs[npc_id]["description"] = dbutils.get_reaction_description("FR", npc_id, trait.TRAIT_ID) + npcs[npc_id]["description"] = dbutils.get_reaction_description("FR", trait.TRAIT_ID) player_results = data["player"] = {} for member in self.members: player_results[member.username] = member.results @@ -322,4 +322,4 @@ def get_npc_image(npc_id: int): :param npc_id: npc to get the neutral image from :return: the byte representation of the image, none if its not found or not readable """ - return read_image(f"./truthinquiry/static/images/npc/{npc_id}/0.png") + return read_image(f"./truthinquiry/static/images/npc/{npc_id}/0.png") \ No newline at end of file