change reaction import system to read from a directory + add default npc image

This commit is contained in:
Thomas Rubini 2023-03-19 11:11:33 +01:00
parent d97b100b96
commit 066943d8a8
No known key found for this signature in database
GPG Key ID: C7D287C8C1CAC373
3 changed files with 38 additions and 29 deletions

View File

@ -39,11 +39,12 @@ def bulk_import(data, dir):
REACTION_LIST = [] REACTION_LIST = []
QUESTIONS_LIST = [] QUESTIONS_LIST = []
ANSWER_LIST = [] ANSWER_LIST = []
NPC_LIST = [] NPC_DICT = {}
ROOMS_LIST = [] ROOMS_LIST = []
lm = LocaleManager() lm = LocaleManager()
getid = lm.get_unused_lid getid = lm.get_unused_lid
reactions_img_dir = os.path.join(dir, data["reactions_img_dir"])
# Questions # Questions
@ -87,26 +88,13 @@ def bulk_import(data, dir):
# Npcs # Npcs
npcs = data["npcs"] npcs = data["npcs"]
npcid = 1 npcid = 1
for npc in npcs.values(): for npc_key, npc in npcs.items():
new_npc = Npc(npcid, getid()) new_npc = Npc(npcid, getid())
# handle the names # handle the names
for lang in npc["name"]: for lang in npc["name"]:
TEXT_LIST.append(Text(0,new_npc.NAME_LID, lang, npc["name"][lang])) 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"]: 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 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(answer.values())[0]
TEXT_LIST.append(Text(0,new_answer.TEXT_LID, lang, text)) TEXT_LIST.append(Text(0,new_answer.TEXT_LID, lang, text))
NPC_LIST.append(new_npc) NPC_DICT[npc_key] = new_npc
npcid += 1 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
rooms = data["rooms"] rooms = data["rooms"]
for room in rooms.values(): for room in rooms.values():
@ -148,7 +157,7 @@ def bulk_import(data, dir):
session.add(trait) session.add(trait)
session.commit() session.commit()
for npc in NPC_LIST: for npc in NPC_DICT.values():
print("Npc : "+ str(npc)) print("Npc : "+ str(npc))
session.add(npc) session.add(npc)
session.commit() session.commit()

View File

@ -1,5 +1,7 @@
reactions_img_dir: imgs
npcs: npcs:
1: medecin:
answers: answers:
where: where:
- FR: "Il y avait {SALLE} \xE7a m'a intrigu\xE9." - 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}." - FR: "Je suis pratiquement s\xFBr que j'\xE9tais avec {NPC}."
name: name:
FR: "Le M\xE9decin" FR: "Le M\xE9decin"
reactions: diplomate:
mefiant:
img: imgs/medecin/mefiant.png
2:
answers: answers:
where: where:
- FR: Je profitais d'une collation dans {SALLE}. - 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." - FR: "Avec {NPC} pour exposer nos diff\xE9rents points de vus sur divers sujets."
name: name:
FR: Le Diplomate FR: Le Diplomate
3: combattant:
answers: answers:
where: where:
- FR: '{SALLE} nous a servi de salle de duel.' - 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." - FR: "{NPC} et moi nous sommes engag\xE9s dans une joute verbale des plus palpitante."
name: name:
FR: Le Combattant FR: Le Combattant
4: duchesse:
answers: answers:
where: where:
- FR: Pour votre gouverne je me trouvais dans {SALLE}. - FR: Pour votre gouverne je me trouvais dans {SALLE}.
@ -43,7 +42,7 @@ npcs:
- FR: "J'\xE9tais avec {NPC}." - FR: "J'\xE9tais avec {NPC}."
name: name:
FR: La Duchesse FR: La Duchesse
5: diva:
answers: answers:
where: where:
- FR: "{SALLE} me semblait \xEAtre la plus belle pi\xE8ce de la maison." - FR: "{SALLE} me semblait \xEAtre la plus belle pi\xE8ce de la maison."
@ -54,7 +53,7 @@ npcs:
\ qu'une coupe de champagne." \ qu'une coupe de champagne."
name: name:
FR: La Diva FR: La Diva
6: parieuse:
answers: answers:
where: where:
- FR: "J'avais mont\xE9 une table de jeu dans {SALLE}." - 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}." - FR: "Si vous tenez \xE0 votre argent ne jouez jamais au poker avec {NPC}."
name: name:
FR: La Parieuse FR: La Parieuse
7: agent:
answers: answers:
where: where:
- FR: On pouvait me retrouver dans {SALLE}. - FR: On pouvait me retrouver dans {SALLE}.
@ -76,7 +75,7 @@ npcs:
- FR: '{NPC}' - FR: '{NPC}'
name: name:
FR: L'Agent FR: L'Agent
8: voyageuse:
answers: answers:
where: where:
- FR: '{SALLE} me semblait un bon endroit pour me poser' - FR: '{SALLE} me semblait un bon endroit pour me poser'
@ -121,7 +120,7 @@ traits:
\ la personne en face." \ la personne en face."
name: name:
FR: "m\xE9fiant(e)," FR: "m\xE9fiant(e),"
decontracte: heureux:
description: description:
FR: "Un visage d\xE9contract\xE9 et ouvert, les muscles des joues contract\xE9\ 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\ s qui laissent appara\xEEtre un sourire. On le d\xE9termine aussi par des\

View File

@ -127,6 +127,7 @@ class Npc(Base):
__tablename__ = "T_NPC" __tablename__ = "T_NPC"
NPC_ID = Column(Integer, autoincrement=True, 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") 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") LOCALE = relationship("Locale")
def __init__(self, NPC_ID, NAME_LID): def __init__(self, NPC_ID, NAME_LID):