Merge pull request #96 from ThomasRubini/model_fixup

This commit is contained in:
Thomas Rubini 2023-03-15 16:09:14 +01:00 committed by GitHub
commit 2968997381
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 16 additions and 19 deletions

View File

@ -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,8 +59,7 @@ 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:
"""
@ -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

View File

@ -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):

View File

@ -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