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 = []
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()

View File

@ -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\

View File

@ -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):