Skip to content

Commit

Permalink
import/export for OLD and JDA character sheets
Browse files Browse the repository at this point in the history
  • Loading branch information
pyanderson committed Nov 15, 2023
1 parent c5e6ac9 commit f273968
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 29 deletions.
50 changes: 33 additions & 17 deletions src/features/character-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/features/equipments.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
Expand Down
117 changes: 110 additions & 7 deletions src/features/import-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -418,6 +498,7 @@ export class ImportExportSheet {
return equipment;
},
);
data.skills = [];
return data;
}

Expand All @@ -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. */
Expand Down
4 changes: 2 additions & 2 deletions src/features/powers.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,15 +188,15 @@ export class PowerSheet {

/** Add a new power to the character sheet. */
addPower(power) {
this.character.addAtttributes(
this.character.addAttributes(
'repeating_powers',
this.getPowerAttributes(power),
);
}

/** Add a new ability to the character sheet. */
addAbility(ability) {
this.character.addAtttributes(
this.character.addAttributes(
'repeating_abilities',
this.getAbilityAttributes(ability),
);
Expand Down
2 changes: 1 addition & 1 deletion src/features/races.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
2 changes: 1 addition & 1 deletion src/features/spells.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
Expand Down

0 comments on commit f273968

Please sign in to comment.