Skip to content

Commit

Permalink
fix: fixed issue importing 5e monsters with at-will and daily spells
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Nov 23, 2021
1 parent f2fdab3 commit d1849ad
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
68 changes: 55 additions & 13 deletions src/importers/5eToolsImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ async function buildMonsterFromFile(file: File): Promise<Monster> {

resolve(importedMonster);
} catch (e) {
console.error(e)
console.error(e);
reject(e);
}
};
Expand Down Expand Up @@ -157,20 +157,62 @@ const spellMap: { [key: string]: string } = {
function getSpells(monster: any): any[] {
if (!monster.spellcasting || !monster.spellcasting.length) return [];

return [
monster.spellcasting[0].headerEntries.join("\n"),
...Object.entries(monster.spellcasting[0].spells).map(
([level, { slots, spells }]) => {
let name = `${spellMap[level]}`;
name += slots != undefined ? ` (${slots} slots)` : "";
const spells = monster.spellcasting[0].spells ?? {};
const will = monster.spellcasting[0].will ?? [];
const daily = monster.spellcasting[0].daily ?? {};

const sp = spells
const ret = [(monster.spellcasting[0].headerEntries ?? []).join("\n")];

if (spells) {
try {
ret.push(
...Object.entries(spells).map(
([level, { slots, spells }]) => {
let name = `${spellMap[level]}`;
name += slots != undefined ? ` (${slots} slots)` : "";

const sp = spells
.join(", ")
.replace(/\{@spell ([\s\S]+?)\}/g, `$1`);
return { [name]: sp };
}
)
);
} catch (e) {
throw new Error("There was an error parsing the spells.");
}
}
if (will) {
try {
ret.push({
"At will": will
.join(", ")
.replace(/\{@spell ([\s\S]+?)\}/g, `$1`);
return { [name]: sp };
}
)
];
.replace(/\{@spell ([\s\S]+?)\}/g, `$1`)
})
} catch(e) {
throw new Error("There was an error parsing the at-will spells.")
}
}
if (daily) {
try {
ret.push(
...Object.entries(daily).map(
([num, spells]) => {
let name = `Daily (${num})`;

const sp = spells
.join(", ")
.replace(/\{@spell ([\s\S]+?)\}/g, `$1`);
return { [name]: sp };
}
)
);
} catch (e) {
throw new Error("There was an error parsing the daily spells.");
}
}

return ret;
}

function getAlignmentString(alignment: any) {
Expand Down
5 changes: 1 addition & 4 deletions src/importers/DnDAppFilesImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,7 @@ async function buildMonsterFromFile(file: File): Promise<Map<string, Monster>> {
};
importedMonsters.set(importedMonster.name, importedMonster);
} catch (e) {
console.log(
"🚀 ~ file: DnDAppFilesImport.ts ~ line 78 ~ e",
e
);
console.error(e);
continue;
}
}
Expand Down

0 comments on commit d1849ad

Please sign in to comment.