Skip to content

Commit

Permalink
1.3.1
Browse files Browse the repository at this point in the history
- Fixed some issues with CritterDB imports
- Added support for CritterDB bestiary importing
- Added "view creature" button to settings
  • Loading branch information
valentine195 committed Jul 6, 2021
1 parent 70a694c commit 6fecd88
Show file tree
Hide file tree
Showing 8 changed files with 249 additions and 124 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-5e-statblocks",
"name": "5e Statblocks",
"version": "1.3.0",
"version": "1.3.1",
"description": "Create 5e styled statblocks in Obsidian.md",
"minAppVersion": "0.12.0",
"author": "Jeremy Valentine",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-5e-statblocks",
"version": "1.3.0",
"version": "1.3.1",
"description": "Create 5e styled statblocks in Obsidian.md",
"main": "main.js",
"scripts": {
Expand Down
4 changes: 0 additions & 4 deletions src/importers/5eToolsImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,6 @@ async function buildMonsterFromFile(file: File): Promise<Monster> {
value: string
]
) => {
console.log(
"🚀 ~ file: 5eToolsImport.ts ~ line 62 ~ thr.value",
thr
);
const [, v] = thr[1].match(/.*(\d+)/);
return { [abilityMap[thr[0]]]: v };
}
Expand Down
254 changes: 149 additions & 105 deletions src/importers/CritterDBImport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,127 +3,171 @@ import { Monster } from "@types";
export const ImportFromCritterDB = async (
...files: File[]
): Promise<Map<string, Monster>> => {
const importedMonsters: Map<string, Monster> = new Map();
let importedMonsters: Map<string, Monster> = new Map();
for (let file of files) {
try {
const monster = await buildMonsterFromFile(file);
importedMonsters.set(monster.name, monster);
const monsters = await buildMonsterFromFile(file);
importedMonsters = new Map([...importedMonsters, ...monsters]);
} catch (e) {}
}
return importedMonsters;
};

async function buildMonsterFromFile(file: File): Promise<Monster> {
async function buildMonsterFromFile(file: File): Promise<Map<string, Monster>> {
return new Promise((resolve, reject) => {
const reader = new FileReader();

reader.onload = async (event: any) => {
const importedMonsters: Map<string, Monster> = new Map();
try {
const monster = JSON.parse(event.target.result);

const importedMonster: Monster = {
name: monster.name,
source: "CritterDB",
type: monster.stats.race,
subtype: "",
size: monster.stats.size,
alignment: monster.stats.alignment,
hp: monster.stats.hitPoints,
hit_dice: monster.stats.hitPointsStr,
ac: monster.stats.armorClass,
speed: monster.stats.speed,
stats: [
monster.stats.abilityScores.strength,
monster.stats.abilityScores.dexterity,
monster.stats.abilityScores.constitution,
monster.stats.abilityScores.intelligence,
monster.stats.abilityScores.wisdom,
monster.stats.abilityScores.charisma
],
damage_immunities:
monster.stats.damageImmunities
?.join("; ")
.toLowerCase()
.trim() ?? "",
damage_resistances:
monster.stats.damageResistances
?.join(", ")
.toLowerCase()
.trim() ?? "",
damage_vulnerabilities:
monster.stats.damageVulnerabilities
?.join(", ")
.toLowerCase()
.trim() ?? "",
condition_immunities:
monster.stats.conditionImmunities
?.join(", ")
.toLowerCase()
.trim() ?? "",
saves: monster.stats.savingThrows?.map(
(thr: {
ability: keyof Monster["saves"];
value: number;
}) => {
return { [thr.ability]: thr.value };
}
),
skillsaves:
monster.stats.skills?.map(
({
name,
value
}: {
name: string;
const parsed = JSON.parse(event.target.result);
let monsters = [];
if (parsed.creatures) {
monsters = parsed.creatures;
} else {
monsters = [parsed];
}
for (let monster of monsters) {
const importedMonster: Monster = {
name: monster.name,
source: "CritterDB",
type: monster.stats.race,
subtype: "",
size: monster.stats.size,
alignment: monster.stats.alignment,
hp: monster.stats.hitPoints,
hit_dice: `${Math.floor(
monster.stats.extraHealthFromConstitution /
monster.stats.abilityScoreModifiers.constitution
)}d${monster.stats.hitDieSize} + ${
monster.stats.extraHealthFromConstitution
}`,
ac: monster.stats.armorClass,
speed: monster.stats.speed,
stats: [
monster.stats.abilityScores.strength,
monster.stats.abilityScores.dexterity,
monster.stats.abilityScores.constitution,
monster.stats.abilityScores.intelligence,
monster.stats.abilityScores.wisdom,
monster.stats.abilityScores.charisma
],
damage_immunities:
monster.stats.damageImmunities
?.join("; ")
.toLowerCase()
.trim() ?? "",
damage_resistances:
monster.stats.damageResistances
?.join(", ")
.toLowerCase()
.trim() ?? "",
damage_vulnerabilities:
monster.stats.damageVulnerabilities
?.join(", ")
.toLowerCase()
.trim() ?? "",
condition_immunities:
monster.stats.conditionImmunities
?.join(", ")
.toLowerCase()
.trim() ?? "",
saves: monster.stats.savingThrows?.map(
(thr: {
ability: keyof Monster["saves"];
value: number;
}) => {
return {
[name]: value
};
}
) ?? [],
senses: monster.stats.senses?.join(", ").trim() ?? "",
languages: monster.stats.languages?.join(", ").trim() ?? "",
cr: monster.stats.challengeRating ?? "",
traits:
monster.stats.additionalAbilities?.map(
(trait: { name: string; description: string }) => {
return {
name: trait.name,
desc: trait.description
};
}
) ?? [],
actions:
monster.stats.actions?.map(
(trait: { name: string; description: string }) => {
return {
name: trait.name,
desc: trait.description
};
}
) ?? [],
reactions:
monster.stats.reactions?.map(
(trait: { name: string; description: string }) => {
return {
name: trait.name,
desc: trait.description
};
}
) ?? [],
legendary_actions:
monster.stats.legendaryActions?.map(
(trait: { name: string; description: string }) => {
return {
name: trait.name,
desc: trait.description
};
return { [thr.ability]: thr.value };
}
) ?? []
};
),
skillsaves:
monster.stats.skills
?.map(
({
name,
value,
modifier
}: {
name: string;
value: number;
modifier: number;
}) => {
if (!value && !modifier) return;
return {
[name]: value ?? modifier
};
}
)
.filter((x: any) => x) ?? [],
senses: monster.stats.senses?.join(", ").trim() ?? "",
languages:
monster.stats.languages?.join(", ").trim() ?? "",
cr: monster.stats.challengeRating ?? "",
traits:
monster.stats.additionalAbilities?.map(
(trait: {
name: string;
description: string;
}) => {
return {
name: trait.name,
desc: trait.description.replace(
/<[^>]*>/g,
""
)
};
}
) ?? [],
actions:
monster.stats.actions?.map(
(trait: {
name: string;
description: string;
}) => {
return {
name: trait.name,
desc: trait.description.replace(
/<[^>]*>/g,
""
)
};
}
) ?? [],
reactions:
monster.stats.reactions?.map(
(trait: {
name: string;
description: string;
}) => {
return {
name: trait.name,
desc: trait.description.replace(
/<[^>]*>/g,
""
)
};
}
) ?? [],
legendary_actions:
monster.stats.legendaryActions?.map(
(trait: {
name: string;
description: string;
}) => {
return {
name: trait.name,
desc: trait.description.replace(
/<[^>]*>/g,
""
)
};
}
) ?? []
};
importedMonsters.set(importedMonster.name, importedMonster);
}

resolve(importedMonster);
resolve(importedMonsters);
} catch (e) {
reject();
}
Expand Down
13 changes: 8 additions & 5 deletions src/renderer/statblock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export default class StatBlockRenderer extends MarkdownRenderChild {
container: HTMLElement,
monster: Monster,
private plugin: StatblockMonsterPlugin,
private canSave: boolean
private canSave: boolean,
private canExport: boolean = true
) {
super(container);
this.monster = monster;
Expand Down Expand Up @@ -326,10 +327,12 @@ export default class StatBlockRenderer extends MarkdownRenderChild {
saveEl.onclick = () => this.plugin.saveMonster(this.monster);
setIcon(saveEl, SAVE_SYMBOL);
}
const iconEl = iconsEl.createDiv("clickable-icon");
iconEl.onclick = () =>
this.plugin.exportAsPng(name, this.containerEl);
setIcon(iconEl, EXPORT_SYMBOL);
if (this.canExport) {
const iconEl = iconsEl.createDiv("clickable-icon");
iconEl.onclick = () =>
this.plugin.exportAsPng(name, this.containerEl);
setIcon(iconEl, EXPORT_SYMBOL);
}

needRule = true;
}
Expand Down
6 changes: 4 additions & 2 deletions src/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StatblockMonsterPlugin } from "@types";
import { Monster, StatblockMonsterPlugin } from "@types";
import {
App,
Modal,
Notice,
PluginSettingTab,
Setting,
Expand Down Expand Up @@ -307,7 +308,7 @@ export default class StatblockSettingTab extends PluginSettingTab {
}`
);
}
this.display();
suggester._onInputChanged();
};
suggester.onInputChanged = () =>
searchMonsters.setDesc(
Expand All @@ -320,3 +321,4 @@ export default class StatblockSettingTab extends PluginSettingTab {
}
}
}

Loading

0 comments on commit 6fecd88

Please sign in to comment.