query all places at once

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

View File

@ -14,13 +14,13 @@ def get_text_from_lid(lang: str, lid: int) -> str:
texts = db.session.query(Text).filter_by(LANG=lang, LID=lid).all()
return random.choice(texts).TEXT
def get_random_place() -> Place:
def get_random_places(n) -> Place:
"""
Returns a random place from the database.
:return: a Place object
"""
return random.choice(db.session.query(Place).all())
return random.sample(db.session.query(Place).all(), n)
def get_random_npcs(n: int = 1) -> Npc :
"""

View File

@ -282,11 +282,7 @@ def generate_game_data(lang: str) -> tuple[dict, dict]:
data["npcs"][str(npc.NPC_ID)] = generate_npc_text(npc, lang)
reactions_table[str(npc.NPC_ID)] = dbutils.get_npc_random_trait_id(npc)
places = []
while len(places) != 3:
place = dbutils.get_random_place()
if place not in places:
places.append(place)
places = list(dbutils.get_random_places(3))
data["rooms"] = generate_place_data(npcs, places, lang)
data["questions"] = {}