diff --git a/module/characterImporter.js b/module/characterImporter.js index e5af2c18..35b1ec35 100644 --- a/module/characterImporter.js +++ b/module/characterImporter.js @@ -1,52 +1,51 @@ export default class CharacterImporter { - // transform JSON from sw5e.com to Foundry friendly format // and insert new actor - static async transform(rawCharacter){ + static async transform(rawCharacter) { const sourceCharacter = JSON.parse(rawCharacter); //source character - + const details = { - species: sourceCharacter.attribs.find(e => e.name == "race").current, - background: sourceCharacter.attribs.find(e => e.name == "background").current, - alignment: sourceCharacter.attribs.find(e => e.name == "alignment").current - } - + species: sourceCharacter.attribs.find((e) => e.name == "race").current, + background: sourceCharacter.attribs.find((e) => e.name == "background").current, + alignment: sourceCharacter.attribs.find((e) => e.name == "alignment").current + }; + const hp = { - value: sourceCharacter.attribs.find(e => e.name == "hp").current, + value: sourceCharacter.attribs.find((e) => e.name == "hp").current, min: 0, - max: sourceCharacter.attribs.find(e => e.name == "hp").current, - temp: sourceCharacter.attribs.find(e => e.name == "hp_temp").current + max: sourceCharacter.attribs.find((e) => e.name == "hp").current, + temp: sourceCharacter.attribs.find((e) => e.name == "hp_temp").current }; const ac = { - value: sourceCharacter.attribs.find(e => e.name == "ac").current + value: sourceCharacter.attribs.find((e) => e.name == "ac").current }; const abilities = { str: { - value: sourceCharacter.attribs.find(e => e.name == "strength").current, - proficient: sourceCharacter.attribs.find(e => e.name == 'strength_save_prof').current ? 1 : 0 + value: sourceCharacter.attribs.find((e) => e.name == "strength").current, + proficient: sourceCharacter.attribs.find((e) => e.name == "strength_save_prof").current ? 1 : 0 }, dex: { - value: sourceCharacter.attribs.find(e => e.name == "dexterity").current, - proficient: sourceCharacter.attribs.find(e => e.name == 'dexterity_save_prof').current ? 1 : 0 + value: sourceCharacter.attribs.find((e) => e.name == "dexterity").current, + proficient: sourceCharacter.attribs.find((e) => e.name == "dexterity_save_prof").current ? 1 : 0 }, con: { - value: sourceCharacter.attribs.find(e => e.name == "constitution").current, - proficient: sourceCharacter.attribs.find(e => e.name == 'constitution_save_prof').current ? 1 : 0 + value: sourceCharacter.attribs.find((e) => e.name == "constitution").current, + proficient: sourceCharacter.attribs.find((e) => e.name == "constitution_save_prof").current ? 1 : 0 }, int: { - value: sourceCharacter.attribs.find(e => e.name == "intelligence").current, - proficient: sourceCharacter.attribs.find(e => e.name == 'intelligence_save_prof').current ? 1 : 0 + value: sourceCharacter.attribs.find((e) => e.name == "intelligence").current, + proficient: sourceCharacter.attribs.find((e) => e.name == "intelligence_save_prof").current ? 1 : 0 }, wis: { - value: sourceCharacter.attribs.find(e => e.name == "wisdom").current, - proficient: sourceCharacter.attribs.find(e => e.name == 'wisdom_save_prof').current ? 1 : 0 + value: sourceCharacter.attribs.find((e) => e.name == "wisdom").current, + proficient: sourceCharacter.attribs.find((e) => e.name == "wisdom_save_prof").current ? 1 : 0 }, cha: { - value: sourceCharacter.attribs.find(e => e.name == "charisma").current, - proficient: sourceCharacter.attribs.find(e => e.name == 'charisma_save_prof').current ? 1 : 0 - }, + value: sourceCharacter.attribs.find((e) => e.name == "charisma").current, + proficient: sourceCharacter.attribs.find((e) => e.name == "charisma_save_prof").current ? 1 : 0 + } }; const targetCharacter = { @@ -64,58 +63,70 @@ export default class CharacterImporter { let actor = await Actor.create(targetCharacter); - const profession = sourceCharacter.attribs.find(e => e.name == "class").current; - let professionLevel = sourceCharacter.attribs.find(e => e.name == "class_display").current; - professionLevel = parseInt( professionLevel.replace(/[^0-9]/g,'') ); //remove a-z, leaving only integers - CharacterImporter.addClasses(profession, professionLevel, actor); + const profession = sourceCharacter.attribs.find((e) => e.name == "class").current; + let professionLevel = sourceCharacter.attribs.find((e) => e.name == "class_display").current; + professionLevel = parseInt(professionLevel.replace(/[^0-9]/g, "")); //remove a-z, leaving only integers + this.addClasses(profession, professionLevel, actor); + + this.addSpecies(sourceCharacter.attribs.find((e) => e.name == "race").current, actor); } - static async addClasses(profession, level, actor){ - let classes = await game.packs.get('sw5e.classes').getContent(); - let assignedClass = classes.find( c => c.name === profession ); + static async addClasses(profession, level, actor) { + let classes = await game.packs.get("sw5e.classes").getContent(); + let assignedClass = classes.find((c) => c.name === profession); assignedClass.data.data.levels = level; await actor.createEmbeddedEntity("OwnedItem", assignedClass.data, { displaySheet: false }); } - static addImportButton(html){ + static async addSpecies(race, actor) { + let species = await game.packs.get("sw5e.species").getContent(); + let assignedSpecies = species.find((c) => c.name === race); + + await actor.createEmbeddedEntity("OwnedItem", assignedSpecies.data, { displaySheet: false }); + } + + static addImportButton(html) { const header = $("#actors").find("header.directory-header"); const search = $("#actors").children().find("div.header-search"); const newImportButtonDiv = $("#actors").children().find("div.header-actions").clone(); const newSearch = search.clone(); search.remove(); - newImportButtonDiv.attr('id', 'character-sheet-import'); + newImportButtonDiv.attr("id", "character-sheet-import"); header.append(newImportButtonDiv); newImportButtonDiv.children("button").remove(); - newImportButtonDiv.append(""); + newImportButtonDiv.append( + "" + ); newSearch.appendTo(header); let characterImportButton = $("#cs-import-button"); - characterImportButton.click(ev => { - let content = '

Saved Character JSON Import

' - + ' ' - + '
' - + ''; + characterImportButton.click((ev) => { + let content = + "

Saved Character JSON Import

" + + ' ' + + "
" + + ''; let importDialog = new Dialog({ title: "Import Character from SW5e.com", content: content, buttons: { - "Import": { + Import: { icon: '', label: "Import Character", callback: (e) => { - let characterData = $('#character-json').val(); - console.log('Parsing Character JSON'); + let characterData = $("#character-json").val(); + console.log("Parsing Character JSON"); CharacterImporter.transform(characterData); - } + } }, - "Cancel": { + Cancel: { icon: '', label: "Cancel", - callback: () => {}, + callback: () => {} } } - }) + }); importDialog.render(true); }); - } -} \ No newline at end of file + } +}