From 9fcd7c159a3999cacc34c9385ab68d79dbfcc4c0 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 19 Mar 2023 09:49:10 +0100 Subject: [PATCH 1/8] add input file argument for import script --- bulk_import.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/bulk_import.py b/bulk_import.py index 92f698b..b924e5a 100644 --- a/bulk_import.py +++ b/bulk_import.py @@ -1,8 +1,9 @@ - -import argparse -import yaml from dotenv import load_dotenv load_dotenv() + +import sys +import yaml + from sqlalchemy.orm import sessionmaker from truthinquiry.ext.database.models import * from truthinquiry.ext.database.sa import engine @@ -126,7 +127,7 @@ def bulk_import(data): for lid in lm.get_used_lids(): print("lid :"+ str(lid)) - session.add(Locale(lid)); + session.add(Locale(lid)) for text in TEXT_LIST: print("Text : "+str(text)) @@ -163,6 +164,8 @@ def bulk_import(data): session.add(room) session.commit() -file = open("bulk_data.yml", "r") - -bulk_import(yaml.load(file, yaml.Loader)) +if len(sys.argv) <= 1: + print("Please enter input file") +else: + file = open(sys.argv[1], "r") + bulk_import(yaml.load(file, yaml.Loader)) From 3aae90aab861ea45df075e216e116602d74fd473 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 19 Mar 2023 10:29:13 +0100 Subject: [PATCH 2/8] add autoincrement to some primary keys --- truthinquiry/ext/database/models.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/truthinquiry/ext/database/models.py b/truthinquiry/ext/database/models.py index 11e4610..dabfc81 100644 --- a/truthinquiry/ext/database/models.py +++ b/truthinquiry/ext/database/models.py @@ -11,7 +11,7 @@ class Text(Base): __tablename__ = 'T_TEXT' - TEXT_ID = Column(Integer, primary_key=True, comment="ID of this specific text. These IDs may be recycled in the future and may only be used for a short time period.") + TEXT_ID = Column(Integer, autoincrement=True, primary_key=True, comment="ID of this specific text. These IDs may be recycled in the future and may only be used for a short time period.") LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Reference to the locale that this text provides") LANG = Column(VARCHAR(2), comment="lang ID of the text value in this row, e.g FR, EN, ES") TEXT = Column(Text, comment="Actual text stored") @@ -39,7 +39,7 @@ class Locale(Base): """ __tablename__ = 'T_LOCALE' - LID = Column(Integer, primary_key=True, comment="ID of this locale (the other tables references to this with *_LID columns)") + LID = Column(Integer, primary_key=True, autoincrement=True, comment="ID of this locale (the other tables references to this with *_LID columns)") def __init__(self, LID): self.LID = LID @@ -57,7 +57,7 @@ class Place(Base): """ __tablename__ = 'T_PLACE' - PLACE_ID = Column(Integer, primary_key=True, comment="ID of this place") + PLACE_ID = Column(Integer, primary_key=True, autoincrement=True, comment="ID of this place") NAME_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Place name") LOCALE = relationship("Locale") @@ -100,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") @@ -145,7 +145,7 @@ class Trait(Base): Store reaction types, e.g 'happy', 'sad', without relation with NPCs """ __tablename__ = "T_TRAIT" - TRAIT_ID = Column(Integer, primary_key=True, comment="ID of this trait") + TRAIT_ID = Column(Integer, primary_key=True, autoincrement=True, comment="ID of this trait") NAME_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Name of this trait") DESC_LID = Column(Integer, ForeignKey("T_LOCALE.LID"), comment="Description of this trait") @@ -170,7 +170,7 @@ class Reaction(Base): Relation between a NPC and a Trait """ __tablename__ = "T_REACTION" - REACTION_ID = Column(Integer, primary_key=True, comment="ID of this reaction") + REACTION_ID = Column(Integer, primary_key=True, autoincrement=True, comment="ID of this reaction") NPC_ID = Column(Integer, ForeignKey("T_NPC.NPC_ID"), primary_key=True, comment="Name of the NPC that will have this reaction") TRAIT_ID = Column(Integer, ForeignKey("T_TRAIT.TRAIT_ID"), primary_key=True, comment="ID of the trait of this reaction") NPC = relationship("Npc") From 23dc6962d81c553d72a6ac11ed46ceaa2f9cac42 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 19 Mar 2023 10:43:18 +0100 Subject: [PATCH 3/8] add npc reaction images to database --- truthinquiry/ext/database/models.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/truthinquiry/ext/database/models.py b/truthinquiry/ext/database/models.py index dabfc81..9c9db4c 100644 --- a/truthinquiry/ext/database/models.py +++ b/truthinquiry/ext/database/models.py @@ -1,4 +1,4 @@ -from sqlalchemy import Column, Integer, VARCHAR, Text, ForeignKey +from sqlalchemy import Column, Integer, VARCHAR, Text, LargeBinary, ForeignKey from sqlalchemy.orm import relationship, declarative_base Base = declarative_base() @@ -173,6 +173,7 @@ class Reaction(Base): REACTION_ID = Column(Integer, primary_key=True, autoincrement=True, comment="ID of this reaction") NPC_ID = Column(Integer, ForeignKey("T_NPC.NPC_ID"), primary_key=True, comment="Name of the NPC that will have this reaction") TRAIT_ID = Column(Integer, ForeignKey("T_TRAIT.TRAIT_ID"), primary_key=True, comment="ID of the trait of this reaction") + IMG = Column(LargeBinary(length=2**24), comment="Binary data of the image associated to this npc and trait") NPC = relationship("Npc") TRAIT = relationship("Trait") From d97b100b96af9d3b382fef604010cade13acfa05 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 19 Mar 2023 10:43:00 +0100 Subject: [PATCH 4/8] import npc reaction images --- bulk_import.py | 38 +++++++++++++++++++++----------------- datasets/bulk_data.yml | 13 ++++++++----- 2 files changed, 29 insertions(+), 22 deletions(-) diff --git a/bulk_import.py b/bulk_import.py index b924e5a..d57091a 100644 --- a/bulk_import.py +++ b/bulk_import.py @@ -3,6 +3,7 @@ load_dotenv() import sys import yaml +import os from sqlalchemy.orm import sessionmaker from truthinquiry.ext.database.models import * @@ -30,19 +31,17 @@ class LocaleManager(): return self.used_lids[1:] -def bulk_import(data): +def bulk_import(data, dir): # Data list that will be commited to the db TEXT_LIST = [] - TRAIT_LIST = [] + TRAIT_DICT = {} REACTION_LIST = [] QUESTIONS_LIST = [] ANSWER_LIST = [] NPC_LIST = [] ROOMS_LIST = [] - # helper list to simplify - trait_names = {} lm = LocaleManager() getid = lm.get_unused_lid @@ -71,20 +70,19 @@ def bulk_import(data): # Traits traits = data["traits"] - for trait in traits.values(): + for trait_key, trait in traits.items(): # create the new trait - new_trait = Trait(0,getid(), getid()) + new_trait = Trait(None, getid(), getid()) for lang in trait["name"]: TEXT_LIST.append(Text(0,new_trait.NAME_LID, lang, trait["name"][lang])) - trait_names[trait["name"][lang]] = new_trait.TRAIT_ID for lang in trait["description"]: TEXT_LIST.append(Text(0,new_trait.DESC_LID, lang, trait["description"][lang])) - TRAIT_LIST.append(new_trait) + TRAIT_DICT[trait_key] = new_trait # Npcs npcs = data["npcs"] @@ -96,12 +94,18 @@ def bulk_import(data): for lang in npc["name"]: TEXT_LIST.append(Text(0,new_npc.NAME_LID, lang, npc["name"][lang])) - # TODO handle reactions - """ - for reaction in npc["reactions"]: - if reaction in list(trait_names.keys()): - new_reaction = Reaction(0,new_npc.NPC_ID, trait_names[reaction]) - REACTION_LIST.append(new_reaction) """ + for trait_key, reaction in npc.get("reactions", {}).items(): + trait = TRAIT_DICT[trait_key] + new_reaction = Reaction(None, new_npc.NPC_ID, None) + new_reaction.TRAIT = trait + + img_path = os.path.join(dir, reaction["img"]) + + with open(img_path, "rb") as f: + new_reaction.IMG = f.read() + + REACTION_LIST.append(new_reaction) + for question_type in npc["answers"]: question_type_id = question_type_zero.QUESTION_TYPE_ID if question_type == "where" else question_type_one.QUESTION_TYPE_ID @@ -139,7 +143,7 @@ def bulk_import(data): session.add(question) session.commit() - for trait in TRAIT_LIST: + for trait in TRAIT_DICT.values(): print("Trait : "+ str(trait)) session.add(trait) session.commit() @@ -167,5 +171,5 @@ def bulk_import(data): if len(sys.argv) <= 1: print("Please enter input file") else: - file = open(sys.argv[1], "r") - bulk_import(yaml.load(file, yaml.Loader)) + path = sys.argv[1] + bulk_import(yaml.load(open(path, "r"), yaml.Loader), os.path.dirname(path)) diff --git a/datasets/bulk_data.yml b/datasets/bulk_data.yml index b07d406..eda61b2 100644 --- a/datasets/bulk_data.yml +++ b/datasets/bulk_data.yml @@ -9,6 +9,9 @@ npcs: - FR: "Je suis pratiquement s\xFBr que j'\xE9tais avec {NPC}." name: FR: "Le M\xE9decin" + reactions: + mefiant: + img: imgs/medecin/mefiant.png 2: answers: where: @@ -110,7 +113,7 @@ rooms: 6: FR: Le jardin traits: - 1: + mefiant: description: FR: "Un maintien rigide des traits du visage, un regard de travers. Une crispation\ \ des sourcils et parfois des rides autour de la bouche. Ces caract\xE9ristiques\ @@ -118,7 +121,7 @@ traits: \ la personne en face." name: FR: "m\xE9fiant(e)," - 2: + decontracte: description: FR: "Un visage d\xE9contract\xE9 et ouvert, les muscles des joues contract\xE9\ s qui laissent appara\xEEtre un sourire. On le d\xE9termine aussi par des\ @@ -128,7 +131,7 @@ traits: \ para\xEEtre ses r\xE9elles \xE9motions." name: FR: heureux(se), - 3: + triste: description: FR: "Des sourcils contract\xE9s et resserr\xE9s vers le centre du visage auxquels\ \ s'ajoute un regard vide ou fuyant de l'interlocuteur, soit en fermant les\ @@ -137,7 +140,7 @@ traits: \ ou accusations de son interlocuteur." name: FR: triste - 4: + stresse: description: FR: "Un visage crisp\xE9 qui s'accompagne habituellement de sourcils fronc\xE9\ s, un regard perdu qui se d\xE9tourne de celui de son interlocuteur. Cela\ @@ -146,7 +149,7 @@ traits: \ ou une peur de ce qu'annonce ou peut nous annoncer l'interlocuteur en face." name: FR: "stress\xE9(e)," - 5: + surpris: description: FR: "G\xE9n\xE9ralement par des yeux \xE9carquill\xE9s et un haussement des\ \ sourcils. Cela peut \xE9galement se distinguer par une bouche ouverte ou,\ From 066943d8a812874127c5d99136eaae22735887d3 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 19 Mar 2023 11:11:33 +0100 Subject: [PATCH 5/8] change reaction import system to read from a directory + add default npc image --- bulk_import.py | 43 +++++++++++++++++------------ datasets/bulk_data.yml | 23 ++++++++------- truthinquiry/ext/database/models.py | 1 + 3 files changed, 38 insertions(+), 29 deletions(-) diff --git a/bulk_import.py b/bulk_import.py index d57091a..40f6208 100644 --- a/bulk_import.py +++ b/bulk_import.py @@ -39,11 +39,12 @@ def bulk_import(data, dir): REACTION_LIST = [] QUESTIONS_LIST = [] ANSWER_LIST = [] - NPC_LIST = [] + NPC_DICT = {} ROOMS_LIST = [] lm = LocaleManager() getid = lm.get_unused_lid + reactions_img_dir = os.path.join(dir, data["reactions_img_dir"]) # Questions @@ -87,26 +88,13 @@ def bulk_import(data, dir): # Npcs npcs = data["npcs"] npcid = 1 - for npc in npcs.values(): + for npc_key, npc in npcs.items(): new_npc = Npc(npcid, getid()) # handle the names for lang in npc["name"]: TEXT_LIST.append(Text(0,new_npc.NAME_LID, lang, npc["name"][lang])) - for trait_key, reaction in npc.get("reactions", {}).items(): - trait = TRAIT_DICT[trait_key] - new_reaction = Reaction(None, new_npc.NPC_ID, None) - new_reaction.TRAIT = trait - - img_path = os.path.join(dir, reaction["img"]) - - with open(img_path, "rb") as f: - new_reaction.IMG = f.read() - - REACTION_LIST.append(new_reaction) - - for question_type in npc["answers"]: question_type_id = question_type_zero.QUESTION_TYPE_ID if question_type == "where" else question_type_one.QUESTION_TYPE_ID @@ -118,9 +106,30 @@ def bulk_import(data, dir): text = list(answer.values())[0] TEXT_LIST.append(Text(0,new_answer.TEXT_LID, lang, text)) - NPC_LIST.append(new_npc) + NPC_DICT[npc_key] = new_npc npcid += 1 + # Reactions + for npc_key in os.listdir(reactions_img_dir): + for reaction_file in os.listdir(os.path.join(reactions_img_dir, npc_key)): + + img_path = os.path.join(reactions_img_dir, npc_key, reaction_file) + with open(img_path, "rb") as f: + img_data = f.read() + + npc = NPC_DICT[npc_key] + trait_key = os.path.splitext(reaction_file)[0] + if trait_key == 'default': + npc.DEFAULT_IMG = img_data + else: + trait = TRAIT_DICT[trait_key] + + new_reaction = Reaction(None, npc.NPC_ID, None) + new_reaction.TRAIT = trait + new_reaction.IMG = img_data + + REACTION_LIST.append(new_reaction) + # rooms rooms = data["rooms"] for room in rooms.values(): @@ -148,7 +157,7 @@ def bulk_import(data, dir): session.add(trait) session.commit() - for npc in NPC_LIST: + for npc in NPC_DICT.values(): print("Npc : "+ str(npc)) session.add(npc) session.commit() diff --git a/datasets/bulk_data.yml b/datasets/bulk_data.yml index eda61b2..adbf11c 100644 --- a/datasets/bulk_data.yml +++ b/datasets/bulk_data.yml @@ -1,5 +1,7 @@ +reactions_img_dir: imgs + npcs: - 1: + medecin: answers: where: - FR: "Il y avait {SALLE} \xE7a m'a intrigu\xE9." @@ -9,10 +11,7 @@ npcs: - FR: "Je suis pratiquement s\xFBr que j'\xE9tais avec {NPC}." name: FR: "Le M\xE9decin" - reactions: - mefiant: - img: imgs/medecin/mefiant.png - 2: + diplomate: answers: where: - FR: Je profitais d'une collation dans {SALLE}. @@ -23,7 +22,7 @@ npcs: - FR: "Avec {NPC} pour exposer nos diff\xE9rents points de vus sur divers sujets." name: FR: Le Diplomate - 3: + combattant: answers: where: - FR: '{SALLE} nous a servi de salle de duel.' @@ -33,7 +32,7 @@ npcs: - FR: "{NPC} et moi nous sommes engag\xE9s dans une joute verbale des plus palpitante." name: FR: Le Combattant - 4: + duchesse: answers: where: - FR: Pour votre gouverne je me trouvais dans {SALLE}. @@ -43,7 +42,7 @@ npcs: - FR: "J'\xE9tais avec {NPC}." name: FR: La Duchesse - 5: + diva: answers: where: - FR: "{SALLE} me semblait \xEAtre la plus belle pi\xE8ce de la maison." @@ -54,7 +53,7 @@ npcs: \ qu'une coupe de champagne." name: FR: La Diva - 6: + parieuse: answers: where: - FR: "J'avais mont\xE9 une table de jeu dans {SALLE}." @@ -66,7 +65,7 @@ npcs: - FR: "Si vous tenez \xE0 votre argent ne jouez jamais au poker avec {NPC}." name: FR: La Parieuse - 7: + agent: answers: where: - FR: On pouvait me retrouver dans {SALLE}. @@ -76,7 +75,7 @@ npcs: - FR: '{NPC}' name: FR: L'Agent - 8: + voyageuse: answers: where: - FR: '{SALLE} me semblait un bon endroit pour me poser' @@ -121,7 +120,7 @@ traits: \ la personne en face." name: FR: "m\xE9fiant(e)," - decontracte: + heureux: description: FR: "Un visage d\xE9contract\xE9 et ouvert, les muscles des joues contract\xE9\ s qui laissent appara\xEEtre un sourire. On le d\xE9termine aussi par des\ diff --git a/truthinquiry/ext/database/models.py b/truthinquiry/ext/database/models.py index 9c9db4c..f47cb99 100644 --- a/truthinquiry/ext/database/models.py +++ b/truthinquiry/ext/database/models.py @@ -127,6 +127,7 @@ class Npc(Base): __tablename__ = "T_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") + DEFAULT_IMG = Column(LargeBinary(length=2**24), comment="Binary data of the default image of this Npc") LOCALE = relationship("Locale") def __init__(self, NPC_ID, NAME_LID): From 4d7ac75ecc79e90b0583c106c58be5b7a8152a88 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 19 Mar 2023 11:12:04 +0100 Subject: [PATCH 6/8] move reaction files --- .../npc/7/0.png => datasets/imgs/agent/default.png | Bin .../npc/7/2.png => datasets/imgs/agent/heureux.png | Bin .../npc/7/1.png => datasets/imgs/agent/mefiant.png | Bin .../npc/7/4.png => datasets/imgs/agent/stresse.png | Bin .../npc/7/5.png => datasets/imgs/agent/surpris.png | Bin .../npc/7/3.png => datasets/imgs/agent/triste.png | Bin .../3/0.png => datasets/imgs/combattant/default.png | Bin .../3/2.png => datasets/imgs/combattant/heureux.png | Bin .../3/1.png => datasets/imgs/combattant/mefiant.png | Bin .../3/4.png => datasets/imgs/combattant/stresse.png | Bin .../3/5.png => datasets/imgs/combattant/surpris.png | Bin .../3/3.png => datasets/imgs/combattant/triste.png | Bin .../2/0.png => datasets/imgs/diplomate/default.png | Bin .../2/2.png => datasets/imgs/diplomate/heureux.png | Bin .../2/1.png => datasets/imgs/diplomate/mefiant.png | Bin .../2/4.png => datasets/imgs/diplomate/stresse.png | Bin .../2/5.png => datasets/imgs/diplomate/surpris.png | Bin .../2/3.png => datasets/imgs/diplomate/triste.png | Bin .../npc/5/0.png => datasets/imgs/diva/default.png | Bin .../npc/5/2.png => datasets/imgs/diva/heureux.png | Bin .../npc/5/1.png => datasets/imgs/diva/mefiant.png | Bin .../npc/5/4.png => datasets/imgs/diva/stresse.png | Bin .../npc/5/5.png => datasets/imgs/diva/surpris.png | Bin .../npc/5/3.png => datasets/imgs/diva/triste.png | Bin .../4/0.png => datasets/imgs/duchesse/default.png | Bin .../4/2.png => datasets/imgs/duchesse/heureux.png | Bin .../4/1.png => datasets/imgs/duchesse/mefiant.png | Bin .../4/4.png => datasets/imgs/duchesse/stresse.png | Bin .../4/5.png => datasets/imgs/duchesse/surpris.png | Bin .../4/3.png => datasets/imgs/duchesse/triste.png | Bin .../1/0.png => datasets/imgs/medecin/default.png | Bin .../1/2.png => datasets/imgs/medecin/heureux.png | Bin .../1/1.png => datasets/imgs/medecin/mefiant.png | Bin .../1/4.png => datasets/imgs/medecin/stresse.png | Bin .../1/5.png => datasets/imgs/medecin/surpris.png | Bin .../npc/1/3.png => datasets/imgs/medecin/triste.png | Bin .../6/0.png => datasets/imgs/parieuse/default.png | Bin .../6/2.png => datasets/imgs/parieuse/heureux.png | Bin .../6/1.png => datasets/imgs/parieuse/mefiant.png | Bin .../6/4.png => datasets/imgs/parieuse/stresse.png | Bin .../6/5.png => datasets/imgs/parieuse/surpris.png | Bin .../6/3.png => datasets/imgs/parieuse/triste.png | Bin .../8/0.png => datasets/imgs/voyageuse/default.png | Bin .../8/2.png => datasets/imgs/voyageuse/heureux.png | Bin .../8/1.png => datasets/imgs/voyageuse/mefiant.png | Bin .../8/4.png => datasets/imgs/voyageuse/stresse.png | Bin .../8/5.png => datasets/imgs/voyageuse/surpris.png | Bin .../8/3.png => datasets/imgs/voyageuse/triste.png | Bin 48 files changed, 0 insertions(+), 0 deletions(-) rename truthinquiry/static/images/npc/7/0.png => datasets/imgs/agent/default.png (100%) rename truthinquiry/static/images/npc/7/2.png => datasets/imgs/agent/heureux.png (100%) rename truthinquiry/static/images/npc/7/1.png => datasets/imgs/agent/mefiant.png (100%) rename truthinquiry/static/images/npc/7/4.png => datasets/imgs/agent/stresse.png (100%) rename truthinquiry/static/images/npc/7/5.png => datasets/imgs/agent/surpris.png (100%) rename truthinquiry/static/images/npc/7/3.png => datasets/imgs/agent/triste.png (100%) rename truthinquiry/static/images/npc/3/0.png => datasets/imgs/combattant/default.png (100%) rename truthinquiry/static/images/npc/3/2.png => datasets/imgs/combattant/heureux.png (100%) rename truthinquiry/static/images/npc/3/1.png => datasets/imgs/combattant/mefiant.png (100%) rename truthinquiry/static/images/npc/3/4.png => datasets/imgs/combattant/stresse.png (100%) rename truthinquiry/static/images/npc/3/5.png => datasets/imgs/combattant/surpris.png (100%) rename truthinquiry/static/images/npc/3/3.png => datasets/imgs/combattant/triste.png (100%) rename truthinquiry/static/images/npc/2/0.png => datasets/imgs/diplomate/default.png (100%) rename truthinquiry/static/images/npc/2/2.png => datasets/imgs/diplomate/heureux.png (100%) rename truthinquiry/static/images/npc/2/1.png => datasets/imgs/diplomate/mefiant.png (100%) rename truthinquiry/static/images/npc/2/4.png => datasets/imgs/diplomate/stresse.png (100%) rename truthinquiry/static/images/npc/2/5.png => datasets/imgs/diplomate/surpris.png (100%) rename truthinquiry/static/images/npc/2/3.png => datasets/imgs/diplomate/triste.png (100%) rename truthinquiry/static/images/npc/5/0.png => datasets/imgs/diva/default.png (100%) rename truthinquiry/static/images/npc/5/2.png => datasets/imgs/diva/heureux.png (100%) rename truthinquiry/static/images/npc/5/1.png => datasets/imgs/diva/mefiant.png (100%) rename truthinquiry/static/images/npc/5/4.png => datasets/imgs/diva/stresse.png (100%) rename truthinquiry/static/images/npc/5/5.png => datasets/imgs/diva/surpris.png (100%) rename truthinquiry/static/images/npc/5/3.png => datasets/imgs/diva/triste.png (100%) rename truthinquiry/static/images/npc/4/0.png => datasets/imgs/duchesse/default.png (100%) rename truthinquiry/static/images/npc/4/2.png => datasets/imgs/duchesse/heureux.png (100%) rename truthinquiry/static/images/npc/4/1.png => datasets/imgs/duchesse/mefiant.png (100%) rename truthinquiry/static/images/npc/4/4.png => datasets/imgs/duchesse/stresse.png (100%) rename truthinquiry/static/images/npc/4/5.png => datasets/imgs/duchesse/surpris.png (100%) rename truthinquiry/static/images/npc/4/3.png => datasets/imgs/duchesse/triste.png (100%) rename truthinquiry/static/images/npc/1/0.png => datasets/imgs/medecin/default.png (100%) rename truthinquiry/static/images/npc/1/2.png => datasets/imgs/medecin/heureux.png (100%) rename truthinquiry/static/images/npc/1/1.png => datasets/imgs/medecin/mefiant.png (100%) rename truthinquiry/static/images/npc/1/4.png => datasets/imgs/medecin/stresse.png (100%) rename truthinquiry/static/images/npc/1/5.png => datasets/imgs/medecin/surpris.png (100%) rename truthinquiry/static/images/npc/1/3.png => datasets/imgs/medecin/triste.png (100%) rename truthinquiry/static/images/npc/6/0.png => datasets/imgs/parieuse/default.png (100%) rename truthinquiry/static/images/npc/6/2.png => datasets/imgs/parieuse/heureux.png (100%) rename truthinquiry/static/images/npc/6/1.png => datasets/imgs/parieuse/mefiant.png (100%) rename truthinquiry/static/images/npc/6/4.png => datasets/imgs/parieuse/stresse.png (100%) rename truthinquiry/static/images/npc/6/5.png => datasets/imgs/parieuse/surpris.png (100%) rename truthinquiry/static/images/npc/6/3.png => datasets/imgs/parieuse/triste.png (100%) rename truthinquiry/static/images/npc/8/0.png => datasets/imgs/voyageuse/default.png (100%) rename truthinquiry/static/images/npc/8/2.png => datasets/imgs/voyageuse/heureux.png (100%) rename truthinquiry/static/images/npc/8/1.png => datasets/imgs/voyageuse/mefiant.png (100%) rename truthinquiry/static/images/npc/8/4.png => datasets/imgs/voyageuse/stresse.png (100%) rename truthinquiry/static/images/npc/8/5.png => datasets/imgs/voyageuse/surpris.png (100%) rename truthinquiry/static/images/npc/8/3.png => datasets/imgs/voyageuse/triste.png (100%) diff --git a/truthinquiry/static/images/npc/7/0.png b/datasets/imgs/agent/default.png similarity index 100% rename from truthinquiry/static/images/npc/7/0.png rename to datasets/imgs/agent/default.png diff --git a/truthinquiry/static/images/npc/7/2.png b/datasets/imgs/agent/heureux.png similarity index 100% rename from truthinquiry/static/images/npc/7/2.png rename to datasets/imgs/agent/heureux.png diff --git a/truthinquiry/static/images/npc/7/1.png b/datasets/imgs/agent/mefiant.png similarity index 100% rename from truthinquiry/static/images/npc/7/1.png rename to datasets/imgs/agent/mefiant.png diff --git a/truthinquiry/static/images/npc/7/4.png b/datasets/imgs/agent/stresse.png similarity index 100% rename from truthinquiry/static/images/npc/7/4.png rename to datasets/imgs/agent/stresse.png diff --git a/truthinquiry/static/images/npc/7/5.png b/datasets/imgs/agent/surpris.png similarity index 100% rename from truthinquiry/static/images/npc/7/5.png rename to datasets/imgs/agent/surpris.png diff --git a/truthinquiry/static/images/npc/7/3.png b/datasets/imgs/agent/triste.png similarity index 100% rename from truthinquiry/static/images/npc/7/3.png rename to datasets/imgs/agent/triste.png diff --git a/truthinquiry/static/images/npc/3/0.png b/datasets/imgs/combattant/default.png similarity index 100% rename from truthinquiry/static/images/npc/3/0.png rename to datasets/imgs/combattant/default.png diff --git a/truthinquiry/static/images/npc/3/2.png b/datasets/imgs/combattant/heureux.png similarity index 100% rename from truthinquiry/static/images/npc/3/2.png rename to datasets/imgs/combattant/heureux.png diff --git a/truthinquiry/static/images/npc/3/1.png b/datasets/imgs/combattant/mefiant.png similarity index 100% rename from truthinquiry/static/images/npc/3/1.png rename to datasets/imgs/combattant/mefiant.png diff --git a/truthinquiry/static/images/npc/3/4.png b/datasets/imgs/combattant/stresse.png similarity index 100% rename from truthinquiry/static/images/npc/3/4.png rename to datasets/imgs/combattant/stresse.png diff --git a/truthinquiry/static/images/npc/3/5.png b/datasets/imgs/combattant/surpris.png similarity index 100% rename from truthinquiry/static/images/npc/3/5.png rename to datasets/imgs/combattant/surpris.png diff --git a/truthinquiry/static/images/npc/3/3.png b/datasets/imgs/combattant/triste.png similarity index 100% rename from truthinquiry/static/images/npc/3/3.png rename to datasets/imgs/combattant/triste.png diff --git a/truthinquiry/static/images/npc/2/0.png b/datasets/imgs/diplomate/default.png similarity index 100% rename from truthinquiry/static/images/npc/2/0.png rename to datasets/imgs/diplomate/default.png diff --git a/truthinquiry/static/images/npc/2/2.png b/datasets/imgs/diplomate/heureux.png similarity index 100% rename from truthinquiry/static/images/npc/2/2.png rename to datasets/imgs/diplomate/heureux.png diff --git a/truthinquiry/static/images/npc/2/1.png b/datasets/imgs/diplomate/mefiant.png similarity index 100% rename from truthinquiry/static/images/npc/2/1.png rename to datasets/imgs/diplomate/mefiant.png diff --git a/truthinquiry/static/images/npc/2/4.png b/datasets/imgs/diplomate/stresse.png similarity index 100% rename from truthinquiry/static/images/npc/2/4.png rename to datasets/imgs/diplomate/stresse.png diff --git a/truthinquiry/static/images/npc/2/5.png b/datasets/imgs/diplomate/surpris.png similarity index 100% rename from truthinquiry/static/images/npc/2/5.png rename to datasets/imgs/diplomate/surpris.png diff --git a/truthinquiry/static/images/npc/2/3.png b/datasets/imgs/diplomate/triste.png similarity index 100% rename from truthinquiry/static/images/npc/2/3.png rename to datasets/imgs/diplomate/triste.png diff --git a/truthinquiry/static/images/npc/5/0.png b/datasets/imgs/diva/default.png similarity index 100% rename from truthinquiry/static/images/npc/5/0.png rename to datasets/imgs/diva/default.png diff --git a/truthinquiry/static/images/npc/5/2.png b/datasets/imgs/diva/heureux.png similarity index 100% rename from truthinquiry/static/images/npc/5/2.png rename to datasets/imgs/diva/heureux.png diff --git a/truthinquiry/static/images/npc/5/1.png b/datasets/imgs/diva/mefiant.png similarity index 100% rename from truthinquiry/static/images/npc/5/1.png rename to datasets/imgs/diva/mefiant.png diff --git a/truthinquiry/static/images/npc/5/4.png b/datasets/imgs/diva/stresse.png similarity index 100% rename from truthinquiry/static/images/npc/5/4.png rename to datasets/imgs/diva/stresse.png diff --git a/truthinquiry/static/images/npc/5/5.png b/datasets/imgs/diva/surpris.png similarity index 100% rename from truthinquiry/static/images/npc/5/5.png rename to datasets/imgs/diva/surpris.png diff --git a/truthinquiry/static/images/npc/5/3.png b/datasets/imgs/diva/triste.png similarity index 100% rename from truthinquiry/static/images/npc/5/3.png rename to datasets/imgs/diva/triste.png diff --git a/truthinquiry/static/images/npc/4/0.png b/datasets/imgs/duchesse/default.png similarity index 100% rename from truthinquiry/static/images/npc/4/0.png rename to datasets/imgs/duchesse/default.png diff --git a/truthinquiry/static/images/npc/4/2.png b/datasets/imgs/duchesse/heureux.png similarity index 100% rename from truthinquiry/static/images/npc/4/2.png rename to datasets/imgs/duchesse/heureux.png diff --git a/truthinquiry/static/images/npc/4/1.png b/datasets/imgs/duchesse/mefiant.png similarity index 100% rename from truthinquiry/static/images/npc/4/1.png rename to datasets/imgs/duchesse/mefiant.png diff --git a/truthinquiry/static/images/npc/4/4.png b/datasets/imgs/duchesse/stresse.png similarity index 100% rename from truthinquiry/static/images/npc/4/4.png rename to datasets/imgs/duchesse/stresse.png diff --git a/truthinquiry/static/images/npc/4/5.png b/datasets/imgs/duchesse/surpris.png similarity index 100% rename from truthinquiry/static/images/npc/4/5.png rename to datasets/imgs/duchesse/surpris.png diff --git a/truthinquiry/static/images/npc/4/3.png b/datasets/imgs/duchesse/triste.png similarity index 100% rename from truthinquiry/static/images/npc/4/3.png rename to datasets/imgs/duchesse/triste.png diff --git a/truthinquiry/static/images/npc/1/0.png b/datasets/imgs/medecin/default.png similarity index 100% rename from truthinquiry/static/images/npc/1/0.png rename to datasets/imgs/medecin/default.png diff --git a/truthinquiry/static/images/npc/1/2.png b/datasets/imgs/medecin/heureux.png similarity index 100% rename from truthinquiry/static/images/npc/1/2.png rename to datasets/imgs/medecin/heureux.png diff --git a/truthinquiry/static/images/npc/1/1.png b/datasets/imgs/medecin/mefiant.png similarity index 100% rename from truthinquiry/static/images/npc/1/1.png rename to datasets/imgs/medecin/mefiant.png diff --git a/truthinquiry/static/images/npc/1/4.png b/datasets/imgs/medecin/stresse.png similarity index 100% rename from truthinquiry/static/images/npc/1/4.png rename to datasets/imgs/medecin/stresse.png diff --git a/truthinquiry/static/images/npc/1/5.png b/datasets/imgs/medecin/surpris.png similarity index 100% rename from truthinquiry/static/images/npc/1/5.png rename to datasets/imgs/medecin/surpris.png diff --git a/truthinquiry/static/images/npc/1/3.png b/datasets/imgs/medecin/triste.png similarity index 100% rename from truthinquiry/static/images/npc/1/3.png rename to datasets/imgs/medecin/triste.png diff --git a/truthinquiry/static/images/npc/6/0.png b/datasets/imgs/parieuse/default.png similarity index 100% rename from truthinquiry/static/images/npc/6/0.png rename to datasets/imgs/parieuse/default.png diff --git a/truthinquiry/static/images/npc/6/2.png b/datasets/imgs/parieuse/heureux.png similarity index 100% rename from truthinquiry/static/images/npc/6/2.png rename to datasets/imgs/parieuse/heureux.png diff --git a/truthinquiry/static/images/npc/6/1.png b/datasets/imgs/parieuse/mefiant.png similarity index 100% rename from truthinquiry/static/images/npc/6/1.png rename to datasets/imgs/parieuse/mefiant.png diff --git a/truthinquiry/static/images/npc/6/4.png b/datasets/imgs/parieuse/stresse.png similarity index 100% rename from truthinquiry/static/images/npc/6/4.png rename to datasets/imgs/parieuse/stresse.png diff --git a/truthinquiry/static/images/npc/6/5.png b/datasets/imgs/parieuse/surpris.png similarity index 100% rename from truthinquiry/static/images/npc/6/5.png rename to datasets/imgs/parieuse/surpris.png diff --git a/truthinquiry/static/images/npc/6/3.png b/datasets/imgs/parieuse/triste.png similarity index 100% rename from truthinquiry/static/images/npc/6/3.png rename to datasets/imgs/parieuse/triste.png diff --git a/truthinquiry/static/images/npc/8/0.png b/datasets/imgs/voyageuse/default.png similarity index 100% rename from truthinquiry/static/images/npc/8/0.png rename to datasets/imgs/voyageuse/default.png diff --git a/truthinquiry/static/images/npc/8/2.png b/datasets/imgs/voyageuse/heureux.png similarity index 100% rename from truthinquiry/static/images/npc/8/2.png rename to datasets/imgs/voyageuse/heureux.png diff --git a/truthinquiry/static/images/npc/8/1.png b/datasets/imgs/voyageuse/mefiant.png similarity index 100% rename from truthinquiry/static/images/npc/8/1.png rename to datasets/imgs/voyageuse/mefiant.png diff --git a/truthinquiry/static/images/npc/8/4.png b/datasets/imgs/voyageuse/stresse.png similarity index 100% rename from truthinquiry/static/images/npc/8/4.png rename to datasets/imgs/voyageuse/stresse.png diff --git a/truthinquiry/static/images/npc/8/5.png b/datasets/imgs/voyageuse/surpris.png similarity index 100% rename from truthinquiry/static/images/npc/8/5.png rename to datasets/imgs/voyageuse/surpris.png diff --git a/truthinquiry/static/images/npc/8/3.png b/datasets/imgs/voyageuse/triste.png similarity index 100% rename from truthinquiry/static/images/npc/8/3.png rename to datasets/imgs/voyageuse/triste.png 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 7/8] 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) From 4438e8f2ff9072f8e479c262f268ceea1ddcc116 Mon Sep 17 00:00:00 2001 From: Thomas Rubini <74205383+ThomasRubini@users.noreply.github.com> Date: Sun, 19 Mar 2023 12:06:05 +0100 Subject: [PATCH 8/8] remove code 500 on invalid npc --- truthinquiry/routes/routes_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/truthinquiry/routes/routes_api.py b/truthinquiry/routes/routes_api.py index b4c65a6..4f797d0 100644 --- a/truthinquiry/routes/routes_api.py +++ b/truthinquiry/routes/routes_api.py @@ -149,7 +149,7 @@ def get_npc_reaction(): image = game.get_npc_reaction(npc_id) errors = ["npc not in game","error reading file"] if image in [0,1]: - return {"error" :1, "msg": errors[image]} , 500 + return {"error": 1, "msg": errors[image]} response = flask.make_response(image) response.headers.set('Content-Type', 'image/png')