From c2aeede3541a4174e1dd13ccd770c09fe879a62f Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 19 Mar 2023 12:05:50 +0100 Subject: [PATCH] make backend use database to return images --- truthinquiry/logic/game_logic.py | 16 +++++++++++++--- truthinquiry/routes/routes_api.py | 2 +- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/truthinquiry/logic/game_logic.py b/truthinquiry/logic/game_logic.py index fd3b8e9..f5a7bcf 100644 --- a/truthinquiry/logic/game_logic.py +++ b/truthinquiry/logic/game_logic.py @@ -2,7 +2,10 @@ import string import random from typing import Union +from sqlalchemy import select, and_ + from truthinquiry.ext.database.models import * +from truthinquiry.ext.database.fsa import db from truthinquiry.ext.database import dbutils games_list = {} @@ -129,10 +132,16 @@ class Game: :param npc_id: the id of the npc, to get the reactions from, must be in the current game :return: the reaction image as bytes """ + print(self.reaction_table) if npc_id not in self.reaction_table: return 0 - reaction_id = self.reaction_table[npc_id] - return read_image(f"./truthinquiry/static/images/npc/{npc_id}/{reaction_id}.png") + trait_id = self.reaction_table[npc_id] + + reaction = db.session.execute( + select(Reaction) + .where(and_(Reaction.NPC_ID == int(npc_id), Reaction.TRAIT_ID == int(trait_id))) + ).one()[0] + return reaction.IMG def get_player_results(self, responses: dict) -> Union[dict, None]: """ @@ -322,4 +331,5 @@ 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") \ No newline at end of file + npc = db.session.execute(select(Npc).where(Npc.NPC_ID==npc_id)).one()[0] + return npc.DEFAULT_IMG diff --git a/truthinquiry/routes/routes_api.py b/truthinquiry/routes/routes_api.py index a678df1..b4c65a6 100644 --- a/truthinquiry/routes/routes_api.py +++ b/truthinquiry/routes/routes_api.py @@ -124,7 +124,7 @@ def get_data(): @routes_api.route("/getNpcImage", methods=["GET", "POST"]) def get_npc_image(): - npc_id = flask.request.values.get("npcid") + npc_id = int(flask.request.values.get("npcid")) if npc_id is None: return {"error": 1, "msg": "no npc was given"} image = game_logic.get_npc_image(npc_id)