From b8fa37532ce91856f3330b30d0e2e9f30590cff4 Mon Sep 17 00:00:00 2001 From: Jendrik Date: Thu, 25 Sep 2025 22:21:08 +0200 Subject: [PATCH] export and import function for importing characters --- src/module/Actors/dsa-actor.mjs | 2 + src/module/xml-import/xml-import.mjs | 311 +++++++++++++-------------- 2 files changed, 157 insertions(+), 156 deletions(-) diff --git a/src/module/Actors/dsa-actor.mjs b/src/module/Actors/dsa-actor.mjs index b1927e98..05c3f174 100644 --- a/src/module/Actors/dsa-actor.mjs +++ b/src/module/Actors/dsa-actor.mjs @@ -1,3 +1,5 @@ +import { importCharacter } from '../xml-import/xml-import.mjs' + export class DsaActor extends Actor { import() { diff --git a/src/module/xml-import/xml-import.mjs b/src/module/xml-import/xml-import.mjs index cdb093b1..52b5b403 100644 --- a/src/module/xml-import/xml-import.mjs +++ b/src/module/xml-import/xml-import.mjs @@ -20,7 +20,7 @@ let months = [ * @param actorId the actor-id of the character * @param file the file from which the character should be imported */ -async function importCharacter(actorId, file) { +export async function importCharacter(actorId, file) { let actor = game.actors.get(actorId) let xmlString = await parseFileToString(file) let domParser = new DOMParser() @@ -108,169 +108,168 @@ function calculateBirthdate(json) { */ function mapRawJson(rawJson) { let json = {} - with (rawJson.held) { - json.name = name - json.meta = {} - json.meta.spezies = basis.rasse.string - json.meta.kultur = basis.kultur.string - let professions = [] - for (ausbildung in basis.ausbildungen) { - if (ausbildung.tarnidentitaet) { - professions = [ausbildung.tarnidentitaet] - break; + let held = rawJson.held; + json.name = held.name + json.meta = {} + json.meta.spezies = held.basis.rasse.string + json.meta.kultur = held.basis.kultur.string + let professions = [] + for (let ausbildung in held.basis.ausbildungen) { + if (ausbildung.tarnidentitaet) { + professions = [ausbildung.tarnidentitaet] + break; + } + let ausbildungString = ausbildung.string + professions.push(ausbildungString) + } + json.meta.profession = professions + json.geschlecht = held.basis.geschlecht.name + json.haarfarbe = held.basis.rasse.aussehen.haarfarbe + json.groesse = held.basis.rasse.groesse.value + json.augenfarbe = held.basis.rasse.aussehen.augenfarbe + json.geburtstag = calculateBirthdate(held.basis.rasse.aussehen) + json.alter = held.basis.rasse.aussehen.alter + json.gewicht = held.basis.rasse.groesse.gewicht + json.aussehen = [ + held.basis.rasse.aussehen.aussehentext0, + held.basis.rasse.aussehen.aussehentext1, + held.basis.rasse.aussehen.aussehentext2, + held.basis.rasse.aussehen.aussehentext3, + ] + json.familie = [ + held.basis.rasse.aussehen.familietext0, + held.basis.rasse.aussehen.familietext1, + held.basis.rasse.aussehen.familietext2, + held.basis.rasse.aussehen.familietext3, + held.basis.rasse.aussehen.familietext4, + held.basis.rasse.aussehen.familietext5, + ] + json.titel = held.basis.rasse.aussehen.titel + json.stand = held.basis.rasse.aussehen.stand + json.attribute = {} + json.attribute.mu = getAttributeJson(held.eigenschaften, "Mut") + json.attribute.kl = getAttributeJson(held.eigenschaften, "Klugheit") + json.attribute.in = getAttributeJson(held.eigenschaften, "Intuition") + json.attribute.ch = getAttributeJson(held.eigenschaften, "Charisma") + json.attribute.ff = getAttributeJson(held.eigenschaften, "Fingerfertigkeit") + json.attribute.ge = getAttributeJson(held.eigenschaften, "Gewandtheit") + json.attribute.ko = getAttributeJson(held.eigenschaften, "Konstitution") + json.attribute.kk = getAttributeJson(held.eigenschaften, "Körperkraft") + json.attribute.mr = { + mod: filterAttribute(held.eigenschaften ,"Magieresistenz").mod + } + json.attribute.lep = { + mod: filterAttribute(held.eigenschaften ,"Lebensenergie").mod + } + json.attribute.aup = { + mod: filterAttribute(held.eigenschaften ,"Ausdauer").mod + } + json.attribute.asp = { + mod: filterAttribute(held.eigenschaften ,"Astralenergie").mod + } + json.attribute.kap = { + mod: filterAttribute(held.eigenschaften ,"Karmaenergie").mod + } + let attribute = filterAttribute(held.eigenschaften ,"Karmaenergie") + json.attribute.at = { + mod: attribute.mod, + aktuell: attribute.value + } + attribute = filterAttribute(held.eigenschaften ,"at") + json.attribute.pa = { + mod: attribute.mod, + aktuell: attribute.value + } + attribute = filterAttribute(held.eigenschaften ,"pa") + json.attribute.at = { + mod: attribute.mod, + aktuell: attribute.value + } + attribute = filterAttribute(held.eigenschaften ,"fk") + json.attribute.fk = { + mod: attribute.mod, + aktuell: attribute.value + } + attribute = filterAttribute(held.eigenschaften ,"ini") + json.attribute.ini = { + mod: attribute.mod, + aktuell: attribute.value + } + json.attribute.so = getAttributeJson(held.eigenschaften, "Sozialstatus") + let specialAbilities = [] + let liturgies = [] + for (let specialAbility in held.sf) { + if (specialAbility.name.startsWith("Liturgie:")) { + liturgies.push({ + name: specialAbility.name.replace("Liturgie:", "").trim(), + }) + } else { + let specialAbilityJson= { + name: specialAbility.name, + auswahlen: [] } - let ausbildungString = ausbildung.string - professions.push(ausbildungString) - } - json.meta.profession = professions - json.geschlecht = basis.geschlecht.name - json.haarfarbe = basis.rasse.aussehen.haarfarbe - json.groesse = basis.rasse.groesse.value - json.augenfarbe = basis.rasse.aussehen.augenfarbe - json.geburtstag = calculateBirthdate(basis.rasse.aussehen) - json.alter = basis.rasse.aussehen.alter - json.gewicht = basis.rasse.groesse.gewicht - json.aussehen = [ - basis.rasse.aussehen.aussehentext0, - basis.rasse.aussehen.aussehentext1, - basis.rasse.aussehen.aussehentext2, - basis.rasse.aussehen.aussehentext3, - ] - json.familie = [ - basis.rasse.aussehen.familietext0, - basis.rasse.aussehen.familietext1, - basis.rasse.aussehen.familietext2, - basis.rasse.aussehen.familietext3, - basis.rasse.aussehen.familietext4, - basis.rasse.aussehen.familietext5, - ] - json.titel = basis.rasse.aussehen.titel - json.stand = basis.rasse.aussehen.stand - json.attribute = {} - json.attribute.mu = getAttributeJson(eigenschaften, "Mut") - json.attribute.kl = getAttributeJson(eigenschaften, "Klugheit") - json.attribute.in = getAttributeJson(eigenschaften, "Intuition") - json.attribute.ch = getAttributeJson(eigenschaften, "Charisma") - json.attribute.ff = getAttributeJson(eigenschaften, "Fingerfertigkeit") - json.attribute.ge = getAttributeJson(eigenschaften, "Gewandtheit") - json.attribute.ko = getAttributeJson(eigenschaften, "Konstitution") - json.attribute.kk = getAttributeJson(eigenschaften, "Körperkraft") - json.attribute.mr = { - mod: filterAttribute(eigenschaften ,"Magieresistenz").mod - } - json.attribute.lep = { - mod: filterAttribute(eigenschaften ,"Lebensenergie").mod - } - json.attribute.aup = { - mod: filterAttribute(eigenschaften ,"Ausdauer").mod - } - json.attribute.asp = { - mod: filterAttribute(eigenschaften ,"Astralenergie").mod - } - json.attribute.kap = { - mod: filterAttribute(eigenschaften ,"Karmaenergie").mod - } - let attribute = filterAttribute(eigenschaften ,"Karmaenergie") - json.attribute.at = { - mod: attribute.mod, - aktuell: attribute.value - } - attribute = filterAttribute(eigenschaften ,"at") - json.attribute.pa = { - mod: attribute.mod, - aktuell: attribute.value - } - attribute = filterAttribute(eigenschaften ,"pa") - json.attribute.at = { - mod: attribute.mod, - aktuell: attribute.value - } - attribute = filterAttribute(eigenschaften ,"fk") - json.attribute.fk = { - mod: attribute.mod, - aktuell: attribute.value - } - attribute = filterAttribute(eigenschaften ,"ini") - json.attribute.ini = { - mod: attribute.mod, - aktuell: attribute.value - } - json.attribute.so = getAttributeJson(eigenschaften, "Sozialstatus") - let specialAbilities = [] - let liturgies = [] - for (specialAbility in sf) { - if (specialAbility.name.startsWith("Liturgie:")) { - liturgies.push({ - name: specialAbility.name.replace("Liturgie:", "").trim(), - }) - } else { - let specialAbilityJson= { - name: specialAbility.name, - auswahlen: [] - } - let fields = Object.keys(specialAbility) - if (fields.length > 1) { - for (field in fields) { - if (field !== "name") { - let choices = specialAbility[field] - if (choices.hasOwnProperty("name")) { - specialAbilityJson.auswahlen.push(choices.name) - } else { - for (choice in choices) { - specialAbilityJson.auswahlen.push(choice.value) - } + let fields = Object.keys(specialAbility) + if (fields.length > 1) { + for (let field in fields) { + if (field !== "name") { + let choices = specialAbility[field] + if (choices.hasOwnProperty("name")) { + specialAbilityJson.auswahlen.push(choices.name) + } else { + for (let choice in choices) { + specialAbilityJson.auswahlen.push(choice.value) } } } } - specialAbilities.push(specialAbilityJson) } + specialAbilities.push(specialAbilityJson) } - json.sonderfertigkeiten = specialAbilities - json.liturgien = liturgies - let talents = [] - for (talent in talentliste) { - talents.push({ - name: talent.name, - taw: talent.value, - probe: talent.probe.trim(), - be: talent.be, - komplexitaet: talent.k, - }) - } - json.talente = talents - let spells = [] - for (spell in zauberliste) { - spells.push({ - name: spell.name, - rep: spell.repraesentation, - hauszauber: spell.hauszauber === "true", - zfw: spell.value, - anmerkungen: spell.zauberkommentar, - komplexitaet: spell.k, - }) - } - json.zauber = spells - let combatValues = [] - for (combatValue in kampfwerte) { - combatValues.push({ - name: combatValue.name, - at: combatValue.attacke.value, - pa: combatValue.parade.value, - }) - } - json.kampfwerte = combatValues - let notes = [] - for (note in kommentare) { - if (note.hasOwnProperty("key")) { - notes.push({ - key: note.key, - notiz: note.kommentar, - }) - } - } - json.notizen = notes } + json.sonderfertigkeiten = specialAbilities + json.liturgien = liturgies + let talents = [] + for (let talent in held.talentliste) { + talents.push({ + name: talent.name, + taw: talent.value, + probe: talent.probe.trim(), + be: talent.be, + komplexitaet: talent.k, + }) + } + json.talente = talents + let spells = [] + for (let spell in held.zauberliste) { + spells.push({ + name: spell.name, + rep: spell.repraesentation, + hauszauber: spell.hauszauber === "true", + zfw: spell.value, + anmerkungen: spell.zauberkommentar, + komplexitaet: spell.k, + }) + } + json.zauber = spells + let combatValues = [] + for (let combatValue in held.kampfwerte) { + combatValues.push({ + name: combatValue.name, + at: combatValue.attacke.value, + pa: combatValue.parade.value, + }) + } + json.kampfwerte = combatValues + let notes = [] + for (let note in held.kommentare) { + if (note.hasOwnProperty("key")) { + notes.push({ + key: note.key, + notiz: note.kommentar, + }) + } + } + json.notizen = notes return json }