-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b5f2c63
commit d78d20d
Showing
20 changed files
with
1,664 additions
and
614 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ package-lock.json | |
# build | ||
main.js | ||
*.js.map | ||
dist | ||
|
||
# obsidian | ||
data.json | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
/* import { Spell } from "../../common/Spell"; | ||
import { StatBlock } from "../../common/StatBlock"; */ | ||
/* import { SpellImporter } from "./SpellImporter"; | ||
import { StatBlockImporter } from "./StatBlockImporter"; */ | ||
|
||
import type { HomebrewCreature, Spell, Trait } from "../../@types"; | ||
import { titleCase } from "title-case"; | ||
|
||
export const ImportEntitiesFromXml = async ( | ||
...files: File[] | ||
): Promise<HomebrewCreature[]> => { | ||
return new Promise((resolve) => { | ||
for (let xmlFile of files) { | ||
const reader = new FileReader(); | ||
|
||
reader.onload = async (event: any) => { | ||
const xml: string = event.target.result; | ||
|
||
const dom = new DOMParser().parseFromString( | ||
xml, | ||
"application/xml" | ||
); | ||
const monsters = dom.getElementsByTagName("monster"); | ||
const importedMonsters: HomebrewCreature[] = []; | ||
if (!monsters.length) return; | ||
for (let monster of Array.from(monsters)) { | ||
const importedMonster: HomebrewCreature = { | ||
name: getParameter(monster, "name"), | ||
ac: getAC(monster), | ||
hp: Number(getHP(monster, "hp")), | ||
stats: [ | ||
Number(getParameter(monster, "str")), | ||
Number(getParameter(monster, "dex")), | ||
Number(getParameter(monster, "con")), | ||
Number(getParameter(monster, "int")), | ||
Number(getParameter(monster, "wis")), | ||
Number(getParameter(monster, "cha")) | ||
], | ||
cr: getParameter(monster, "cr"), | ||
source: getSource(monster) | ||
}; | ||
|
||
importedMonsters.push(importedMonster); | ||
} | ||
resolve(importedMonsters); | ||
}; | ||
|
||
reader.readAsText(xmlFile); | ||
} | ||
}); | ||
}; | ||
|
||
function getParameter(monster: Element, tag: string): string { | ||
const element = monster.getElementsByTagName(tag); | ||
if (element && element.length) return element[0].textContent; | ||
} | ||
function getTraits( | ||
monster: Element, | ||
arg1: "trait" | "action" | "legendary" | "reaction" | ||
): Trait[] { | ||
if (!monster.getElementsByTagName(arg1)) return []; | ||
const traits = monster.getElementsByTagName(arg1); | ||
const traitList = []; | ||
for (let trait of Array.from(traits)) { | ||
const name = trait.getElementsByTagName("name"); | ||
if (!name) continue; | ||
if (name[0].textContent.includes("Spellcasting")) continue; | ||
const text = []; | ||
const traitTexts = trait.getElementsByTagName("text"); | ||
for (let index in traitTexts) { | ||
text.push(traitTexts[index].textContent); | ||
} | ||
traitList.push({ | ||
name: name[0].textContent, | ||
desc: text.join(" ") | ||
}); | ||
} | ||
return traitList; | ||
} | ||
|
||
function getSpells(monster: Element): Spell[] { | ||
if (!monster.getElementsByTagName("trait")) return []; | ||
const traits = Array.from(monster.getElementsByTagName("trait")); | ||
const spellcasting = traits.find((x) => | ||
x.getElementsByTagName("name")[0]?.textContent.includes("Spellcasting") | ||
); | ||
if (!spellcasting) return []; | ||
return Array.from(spellcasting.getElementsByTagName("text")) | ||
.map((x) => x.textContent.replace(/(•|•)/u, "").trim()) | ||
.filter((x) => x.length); | ||
} | ||
|
||
function getSkillSaves(monster: Element): { [key: string]: number }[] { | ||
if (!monster.getElementsByTagName("skill")) return []; | ||
let saves = monster | ||
.getElementsByTagName("skill")[0] | ||
.textContent.split(", "); | ||
let ret: { [key: string]: number }[] = []; | ||
saves.forEach((save) => { | ||
const skill = save.split(/\s[\+\-]/); | ||
ret.push({ [skill[0]]: Number(skill[1]) }); | ||
}); | ||
return ret; | ||
} | ||
|
||
const SAVES: Record< | ||
string, | ||
| "strength" | ||
| "dexterity" | ||
| "constitution" | ||
| "intelligence" | ||
| "wisdom" | ||
| "charisma" | ||
> = { | ||
Str: "strength", | ||
Dex: "dexterity", | ||
Con: "constitution", | ||
Int: "intelligence", | ||
Wis: "wisdom", | ||
Cha: "charisma" | ||
}; | ||
|
||
function getSaves(monster: Element): { | ||
strength?: number; | ||
dexterity?: number; | ||
constitution?: number; | ||
intelligence?: number; | ||
wisdom?: number; | ||
charisma?: number; | ||
}[] { | ||
if (!monster.getElementsByTagName("save")) return []; | ||
let saves = monster.getElementsByTagName("save")[0].textContent.split(", "); | ||
let ret: { | ||
strength?: number; | ||
dexterity?: number; | ||
constitution?: number; | ||
intelligence?: number; | ||
wisdom?: number; | ||
charisma?: number; | ||
}[] = []; | ||
saves.forEach((save) => { | ||
const stat = save.split(/\s[\+\-]/); | ||
ret.push({ [SAVES[stat[0]]]: Number(stat[1]) }); | ||
}); | ||
return ret; | ||
} | ||
|
||
function getHP(monster: Element, arg1: "hp" | "hit_dice"): string { | ||
if (!monster.getElementsByTagName("hp")) return ""; | ||
let [, hp, hit_dice] = monster | ||
.getElementsByTagName("hp")[0] | ||
.textContent.match(/(\d+) \(([\s\S]+)\)/); | ||
return { hp: hp, hit_dice: hit_dice }[arg1]; | ||
} | ||
const SIZES: { [key: string]: string } = { | ||
T: "tiny", | ||
S: "small", | ||
M: "medium", | ||
L: "large", | ||
H: "huge", | ||
G: "gargantuan" | ||
}; | ||
function getSize(monster: Element): string { | ||
if (monster.getElementsByTagName("size")) { | ||
return SIZES[monster.getElementsByTagName("size")[0].textContent] ?? ""; | ||
} | ||
return ""; | ||
} | ||
|
||
function getAC(monster: Element): number { | ||
if (monster.getElementsByTagName("ac")) { | ||
const [, ac] = monster | ||
.getElementsByTagName("ac")[0] | ||
?.textContent.match(/(\d+)/); | ||
return Number(ac); | ||
} | ||
return 0; | ||
} | ||
function getSource(monster: Element): string { | ||
let source = "Unknown"; | ||
const description = monster.getElementsByTagName("description"); | ||
if (description && description.length) { | ||
const searchString = "Source: "; | ||
const sourcePos = description[0].textContent.lastIndexOf(searchString); | ||
const sources = description[0].textContent | ||
.substr(sourcePos + searchString.length) | ||
.split(/, ?/); | ||
source = sources[0]; | ||
} else { | ||
const types = monster.getElementsByTagName("type"); | ||
if (types && types.length) { | ||
let type = types[0].textContent.split(/, ?/); | ||
source = titleCase( | ||
type.length > 1 ? type[type.length - 1] : source | ||
); | ||
} | ||
} | ||
return source; | ||
} |
Oops, something went wrong.