From f27396839359f042b61711b6e6cf429f46d59c90 Mon Sep 17 00:00:00 2001 From: Anderson Lima Date: Wed, 15 Nov 2023 18:16:11 -0300 Subject: [PATCH] import/export for OLD and JDA character sheets --- src/features/character-sheet.js | 50 +++++++++----- src/features/equipments.js | 2 +- src/features/import-export.js | 117 ++++++++++++++++++++++++++++++-- src/features/powers.js | 4 +- src/features/races.js | 2 +- src/features/spells.js | 2 +- 6 files changed, 148 insertions(+), 29 deletions(-) diff --git a/src/features/character-sheet.js b/src/features/character-sheet.js index e8a7d04..f6ff8dc 100644 --- a/src/features/character-sheet.js +++ b/src/features/character-sheet.js @@ -81,25 +81,41 @@ export class CharacterSheet { .filter(filterFn) .map(transformFn) .reduce((acc, a) => ({ ...acc, [a.get('name')]: a }), {}); - this.character.updateAttributes = (prefix, attributes) => { - this.character.attribs.fetch({ - success: () => { - const regex = new RegExp(prefix); - const attrMap = this.character.getAttributes((a) => - regex.test(a.get('name')), - ); - Object.entries(attributes).forEach(([name, current]) => { - const attrName = `${prefix}${prefix ? '_' : ''}${name}`; - const attr = attrMap[attrName]; - if (attr) attr.save({ current }); - else this.character.attribs.create({ name: attrName, current }); - }); - }, - }); + this.character.updateAttributes = ( + prefix, + attributes, + forceFetch = true, + ) => { + const update = () => { + const regex = new RegExp(prefix); + const attrMap = this.character.getAttributes((a) => + regex.test(a.get('name')), + ); + Object.entries(attributes).forEach(([name, current]) => { + const attrName = `${prefix}${prefix ? '_' : ''}${name}`; + const attr = attrMap[attrName]; + if (attr) attr.save({ current }); + else this.character.attribs.create({ name: attrName, current }); + }); + }; + if (forceFetch) { + this.character.attribs.fetch({ success: update }); + return; + } + update(); }; - this.character.addAtttributes = (prefix, attributes) => { + this.character.addAttributes = (prefix, attributes, forceFetch = true) => { const id = generateUUID().replace(/_/g, 'Z'); - this.character.updateAttributes(`${prefix}_${id}`, attributes); + this.character.updateAttributes( + `${prefix}_${id}`, + attributes, + forceFetch, + ); + }; + this.character.addRepAttributes = (prefix, items, forceFetch = true) => { + items.forEach((item) => { + this.character.addAttributes(prefix, item, forceFetch); + }); }; this.character.deleteRow = (groupName, rowId) => { this.character.view.deleteRepeatingRow(groupName, rowId); diff --git a/src/features/equipments.js b/src/features/equipments.js index 43a2eaa..d0f385c 100644 --- a/src/features/equipments.js +++ b/src/features/equipments.js @@ -123,7 +123,7 @@ export class EquipmentSheet { /** Add a new equipment to the character sheet. */ addEquipment(equipment) { - this.character.addAtttributes( + this.character.addAttributes( 'repeating_equipment', this.getAttributes(equipment), ); diff --git a/src/features/import-export.js b/src/features/import-export.js index 98a9832..c12d9b2 100644 --- a/src/features/import-export.js +++ b/src/features/import-export.js @@ -293,8 +293,7 @@ export class ImportExportSheet { return acc; }, {}); Object.keys(allIds).forEach((rowId) => { - console.log({ groupName, rowId }); - // this.character.deleteRow(groupName, rowId); + this.character.deleteRow(groupName, rowId); }); }); } @@ -332,7 +331,88 @@ export class ImportExportSheet { * @returns {Object} */ parseToJDA(newData) { - return newData; + const data = {}; + const getValue = (source, attrName, defaultValue) => { + const value = source[attrName]; + return value !== undefined ? value : defaultValue; + }; + // The common data between both character sheets formats + Object.entries(ATTRS).forEach(([attrName, defaultValue]) => { + data[attrName] = getValue(newData, attrName, defaultValue); + }); + // JDA + Object.entries(JDA_ATTRS).forEach(([attrName, defaultValue]) => { + data[attrName] = getValue(newData, attrName, defaultValue); + }); + if ('isJDA' in newData && !newData.isJDA) { + ['for', 'des', 'con', 'int', 'sab', 'car'].forEach((attr) => { + data[attr] = `${Math.floor((parseFloat(data[attr]) - 10) / 2)}`; + }); + } + // skills + const getAttrValue = (value, defaultValue) => { + if (value in SKILL_JDA_TO_OLD) { + return value; + } else if (value in SKILL_OLD_TO_JDA) { + return SKILL_OLD_TO_JDA[value]; + } + return defaultValue; + }; + Object.entries(SKILLS_ATTRS).forEach(([skillName, defaultArray]) => { + ['_treinada', 'atributo2', 'outros'].forEach((suffix, index) => { + const attrName = `${skillName}${suffix}`; + const value = newData[attrName]; + if (value !== undefined) { + data[attrName] = + suffix === 'atributo2' + ? getAttrValue(value, defaultArray[index]) + : value; + } else { + data[attrName] = + suffix === 'atributo2' + ? SKILL_JDA_TO_OLD[defaultArray[index]] + : defaultArray[index]; + } + }); + }); + data.attacks = this.getRepImportData( + newData.attacks, + ATTACK_GROUP, + (attack) => { + const skill = attack.ataquepericia; + if (skill in ATTACK_SKILL_OLD_TO_JDA) { + attack.ataquepericia = ATTACK_SKILL_OLD_TO_JDA[skill]; + } else if (!(skill in ATTACK_SKILL_JDA_TO_OLD)) { + attack.ataquepericia = ATTACK_SKILL_MELEE; + } + return attack; + }, + ); + data.abilities = this.getRepImportData(newData.abilities, ABILITY_GROUP); + data.powers = this.getRepImportData(newData.powers, POWER_GROUP); + const pSpell = (spell) => { + if (spell.spellcd) { + spell.spellcd = '0'; + } + return spell; + }; + data.spells1 = this.getRepImportData(newData.spells1, SPELL_GROUP, pSpell); + data.spells2 = this.getRepImportData(newData.spells2, SPELL_GROUP, pSpell); + data.spells3 = this.getRepImportData(newData.spells3, SPELL_GROUP, pSpell); + data.spells4 = this.getRepImportData(newData.spells4, SPELL_GROUP, pSpell); + data.spells5 = this.getRepImportData(newData.spells5, SPELL_GROUP, pSpell); + data.equipments = this.getRepImportData( + newData.equipments, + EQUIPMENT_GROUP, + (equipment) => { + if ('isJDA' in newData && !newData.isJDA) { + equipment.equipslot = equipment.equipweight; + } + return equipment; + }, + ); + data.skills = this.getRepImportData(newData.skills, SKILL_GROUP); + return data; } /** @@ -418,6 +498,7 @@ export class ImportExportSheet { return equipment; }, ); + data.skills = []; return data; } @@ -427,10 +508,32 @@ export class ImportExportSheet { * @param {Object} newData */ importNewData(newData) { - const data = this.isJDA - ? this.parseToJDA(newData) - : this.parseToOld(newData); - console.log({ data }); + const { + attacks, + abilities, + powers, + spells1, + spells2, + spells3, + spells4, + spells5, + equipments, + skills, + ...data + } = this.isJDA ? this.parseToJDA(newData) : this.parseToOld(newData); + this.character.updateAttributes('', data, false); + this.character.addRepAttributes('repeating_attacks', attacks, false); + this.character.addRepAttributes('repeating_abilities', abilities, false); + this.character.addRepAttributes('repeating_powers', powers, false); + this.character.addRepAttributes('repeating_spells1', spells1, false); + this.character.addRepAttributes('repeating_spells2', spells2, false); + this.character.addRepAttributes('repeating_spells3', spells3, false); + this.character.addRepAttributes('repeating_spells4', spells4, false); + this.character.addRepAttributes('repeating_spells5', spells5, false); + this.character.addRepAttributes('repeating_equipment', equipments, false); + if (this.isJDA) { + this.character.addRepAttributes('repeating_skills', skills, false); + } } /** Add the import button on the dialog header. */ diff --git a/src/features/powers.js b/src/features/powers.js index 2e49a45..5fb819a 100644 --- a/src/features/powers.js +++ b/src/features/powers.js @@ -188,7 +188,7 @@ export class PowerSheet { /** Add a new power to the character sheet. */ addPower(power) { - this.character.addAtttributes( + this.character.addAttributes( 'repeating_powers', this.getPowerAttributes(power), ); @@ -196,7 +196,7 @@ export class PowerSheet { /** Add a new ability to the character sheet. */ addAbility(ability) { - this.character.addAtttributes( + this.character.addAttributes( 'repeating_abilities', this.getAbilityAttributes(ability), ); diff --git a/src/features/races.js b/src/features/races.js index ec664d9..7726252 100644 --- a/src/features/races.js +++ b/src/features/races.js @@ -110,7 +110,7 @@ export class RaceSheet { // add the race abilities for (const ability of race.abilities) { if (!allAttributes.find((a) => a.get('current') === race.name)) { - this.character.addAtttributes('repeating_abilities', { + this.character.addAttributes('repeating_abilities', { nameability: ability.name, abilitydescription: ability.description, }); diff --git a/src/features/spells.js b/src/features/spells.js index 99be801..c54bb7e 100644 --- a/src/features/spells.js +++ b/src/features/spells.js @@ -173,7 +173,7 @@ export class SpellSheet { /** Add a new spell to the character sheet. */ addSpell(spell) { - this.character.addAtttributes( + this.character.addAttributes( `repeating_spells${spell.circle}`, this.getAttributes(spell), );