Skip to content

Commit d1849ad

Browse files
committed
fix: fixed issue importing 5e monsters with at-will and daily spells
1 parent f2fdab3 commit d1849ad

2 files changed

Lines changed: 56 additions & 17 deletions

File tree

src/importers/5eToolsImport.ts

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ async function buildMonsterFromFile(file: File): Promise<Monster> {
122122

123123
resolve(importedMonster);
124124
} catch (e) {
125-
console.error(e)
125+
console.error(e);
126126
reject(e);
127127
}
128128
};
@@ -157,20 +157,62 @@ const spellMap: { [key: string]: string } = {
157157
function getSpells(monster: any): any[] {
158158
if (!monster.spellcasting || !monster.spellcasting.length) return [];
159159

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

167-
const sp = spells
164+
const ret = [(monster.spellcasting[0].headerEntries ?? []).join("\n")];
165+
166+
if (spells) {
167+
try {
168+
ret.push(
169+
...Object.entries(spells).map(
170+
([level, { slots, spells }]) => {
171+
let name = `${spellMap[level]}`;
172+
name += slots != undefined ? ` (${slots} slots)` : "";
173+
174+
const sp = spells
175+
.join(", ")
176+
.replace(/\{@spell ([\s\S]+?)\}/g, `$1`);
177+
return { [name]: sp };
178+
}
179+
)
180+
);
181+
} catch (e) {
182+
throw new Error("There was an error parsing the spells.");
183+
}
184+
}
185+
if (will) {
186+
try {
187+
ret.push({
188+
"At will": will
168189
.join(", ")
169-
.replace(/\{@spell ([\s\S]+?)\}/g, `$1`);
170-
return { [name]: sp };
171-
}
172-
)
173-
];
190+
.replace(/\{@spell ([\s\S]+?)\}/g, `$1`)
191+
})
192+
} catch(e) {
193+
throw new Error("There was an error parsing the at-will spells.")
194+
}
195+
}
196+
if (daily) {
197+
try {
198+
ret.push(
199+
...Object.entries(daily).map(
200+
([num, spells]) => {
201+
let name = `Daily (${num})`;
202+
203+
const sp = spells
204+
.join(", ")
205+
.replace(/\{@spell ([\s\S]+?)\}/g, `$1`);
206+
return { [name]: sp };
207+
}
208+
)
209+
);
210+
} catch (e) {
211+
throw new Error("There was an error parsing the daily spells.");
212+
}
213+
}
214+
215+
return ret;
174216
}
175217

176218
function getAlignmentString(alignment: any) {

src/importers/DnDAppFilesImport.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,7 @@ async function buildMonsterFromFile(file: File): Promise<Map<string, Monster>> {
7575
};
7676
importedMonsters.set(importedMonster.name, importedMonster);
7777
} catch (e) {
78-
console.log(
79-
"🚀 ~ file: DnDAppFilesImport.ts ~ line 78 ~ e",
80-
e
81-
);
78+
console.error(e);
8279
continue;
8380
}
8481
}

0 commit comments

Comments
 (0)