query all npcs at once

This commit is contained in:
Thomas Rubini 2023-03-19 12:18:16 +01:00
parent 04fb04c7df
commit 8041be1fea
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
2 changed files with 3 additions and 7 deletions

View File

@ -22,13 +22,13 @@ def get_random_place() -> Place:
"""
return random.choice(db.session.query(Place).all())
def get_random_npc() -> Npc :
def get_random_npcs(n: int = 1) -> Npc :
"""
Returns a random npc from the database
:return: a Npc object
"""
return random.choice(db.session.query(Npc).all())
return random.sample(db.session.query(Npc).all(), n)
def get_npc_random_trait_id(npc_id: int) -> int:
"""

View File

@ -277,11 +277,7 @@ def generate_game_data(lang: str) -> tuple[dict, dict]:
data = {}
data["npcs"] = {}
reactions_table = {}
npcs = []
while len(npcs) != 5:
npc = dbutils.get_random_npc()
if npc not in npcs:
npcs.append(npc)
npcs = list(dbutils.get_random_npcs(5))
for npc in npcs:
data["npcs"][str(npc.NPC_ID)] = generate_npc_text(npc, lang)
reactions_table[str(npc.NPC_ID)] = dbutils.get_npc_random_trait_id(npc)