Skip to content

Commit

Permalink
feat: add support for statblocks defined in yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Jan 21, 2022
1 parent 7e6b3fc commit de81930
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 46 deletions.
79 changes: 35 additions & 44 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import {
addIcon,
MarkdownPostProcessorContext,
Notice,
parseYaml,
Plugin,
TFile
} from "obsidian";
import domtoimage from "dom-to-image";

import { BESTIARY_BY_NAME } from "./data/srd-bestiary";
import StatBlockRenderer from "./view/statblock";
import { getParamsFromSource, stringifyWithKeys } from "./util/util";
import { transformTraits } from "./util/util";
import {
CR,
EXPORT_ICON,
EXPORT_SYMBOL,
Layout,
Expand All @@ -27,6 +27,7 @@ import fastCopy from "fast-copy";
import { sort } from "fast-sort";
import type { Plugins } from "../../obsidian-overload";
import type { HomebrewCreature } from "../../obsidian-initiative-tracker/@types";
import { Watcher } from "./watcher/watcher";
declare module "obsidian" {
interface App {
plugins: {
Expand Down Expand Up @@ -80,7 +81,7 @@ export default class StatBlockPlugin extends Plugin {
settings: StatblockData;
data: Map<string, Monster>;
bestiary: Map<string, Monster>;
CR = CR;
watcher = new Watcher(this);
private _sorted: Monster[] = [];
get canUseDiceRoller() {
return this.app.plugins.getPlugin("obsidian-dice-roller") != null;
Expand Down Expand Up @@ -110,6 +111,16 @@ export default class StatBlockPlugin extends Plugin {

await this.saveSettings();

this.watcher.load();

this.addCommand({
id: "parse-frontmatter",
name: "Parse Frontmatter for Creatures",
callback: () => {
this.watcher.start(true);
}
});

addIcon(
"dropzone-grip",
`<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" data-prefix="fas" data-icon="grip-lines-vertical" class="svg-inline--fa fa-grip-lines-vertical fa-w-8" role="img" viewBox="0 0 256 512"><path fill="currentColor" d="M96 496V16c0-8.8-7.2-16-16-16H48c-8.8 0-16 7.2-16 16v480c0 8.8 7.2 16 16 16h32c8.8 0 16-7.2 16-16zm128 0V16c0-8.8-7.2-16-16-16h-32c-8.8 0-16 7.2-16 16v480c0 8.8 7.2 16 16 16h32c8.8 0 16-7.2 16-16z"/></svg>`
Expand Down Expand Up @@ -231,16 +242,21 @@ export default class StatBlockPlugin extends Plugin {
);
}

async deleteMonster(monster: string) {
async deleteMonster(monster: string, sortFields = true, save = true) {
if (!this.data.has(monster)) return;
this.data.delete(monster);
this.bestiary.delete(monster);

await this.saveSettings();
if (BESTIARY_BY_NAME.has(monster)) {
this.bestiary.set(monster, BESTIARY_BY_NAME.get(monster));
}

this._sorted = sort<Monster>(Array.from(this.data.values())).asc(
(m) => m.name
);
if (save) await this.saveSettings();

if (sortFields)
this._sorted = sort<Monster>(Array.from(this.data.values())).asc(
(m) => m.name
);
}

private _transformData(
Expand All @@ -251,6 +267,7 @@ export default class StatBlockPlugin extends Plugin {
});
}
onunload() {
this.watcher.unload();
console.log("TTRPG StatBlocks unloaded");
}

Expand Down Expand Up @@ -347,12 +364,15 @@ export default class StatBlockPlugin extends Plugin {
) {
try {
/** Get Parameters */
const params: StatblockParameters = getParamsFromSource(source);
let params: StatblockParameters = parseYaml(source);

//replace escapes
params = JSON.parse(JSON.stringify(params).replace(/\\/g, ""));

const canSave = params && "name" in params;

if (!params || !Object.values(params ?? {}).length) {
params.note = ctx.sourcePath;
params = Object.assign({}, params, { note: ctx.sourcePath });
}
if (params.note) {
const note = Array.isArray(params.note)
Expand All @@ -376,19 +396,19 @@ export default class StatBlockPlugin extends Plugin {
);
//TODO: The traits are breaking because it expects { name, desc }, not array.
if (monster) {
let traits = this.transformTraits(
let traits = transformTraits(
monster.traits ?? [],
params.traits ?? []
);
let actions = this.transformTraits(
let actions = transformTraits(
monster.actions ?? [],
params.actions ?? []
);
let legendary_actions = this.transformTraits(
let legendary_actions = transformTraits(
monster.legendary_actions ?? [],
params.legendary_actions ?? []
);
let reactions = this.transformTraits(
let reactions = transformTraits(
monster.reactions ?? [],
params.reactions ?? []
);
Expand Down Expand Up @@ -444,36 +464,7 @@ ${e.stack
\`\`\``);
}
}
transformTraits(
monsterTraits: Trait[] = [],
paramsTraits: { desc: string; name: string }[] | [string, string][] = []
) {
if (!monsterTraits) monsterTraits = [];
if (!paramsTraits) paramsTraits = [];
for (const trait of paramsTraits ?? []) {
if (!trait) continue;
if (Array.isArray(trait)) {
monsterTraits = monsterTraits.filter((t) => t.name != trait[0]);
monsterTraits.push({
name: trait[0],
desc: stringifyWithKeys(trait.slice(1))
});
} else if (
typeof trait == "object" &&
"name" in trait &&
"desc" in trait
) {
monsterTraits = monsterTraits.filter(
(t) => t.name != trait.name
);
monsterTraits.push({
name: trait.name,
desc: stringifyWithKeys(trait.desc)
});
}
}
return monsterTraits;
}

render(creature: HomebrewCreature, el: HTMLElement) {
const monster: Monster = Object.assign<
Partial<Monster>,
Expand Down
12 changes: 10 additions & 2 deletions src/watcher/watcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component, TAbstractFile, TFile, TFolder } from "obsidian";
import { Component, Notice, TAbstractFile, TFile, TFolder } from "obsidian";
import type StatBlockPlugin from "src/main";
//have to ignore until i fix typing issue
//@ts-expect-error
Expand All @@ -18,6 +18,7 @@ declare global {
}

export class Watcher extends Component {
announce: boolean;
get metadataCache() {
return this.plugin.app.metadataCache;
}
Expand Down Expand Up @@ -118,6 +119,12 @@ export class Watcher extends Component {
);
this.startTime = 0;
}
if (this.announce) {
new Notice(
"TTRPG Statblocks: Frontmatter Parsing complete."
);
this.announce = false;
}
}
}
);
Expand All @@ -129,7 +136,8 @@ export class Watcher extends Component {
this.watchPaths.delete(path);
}
startTime: number;
start() {
start(announce = false) {
this.announce = announce;
this.startTime = Date.now();
console.info("TTRPG Statblocks: Starting Frontmatter Parsing.");
const folder = this.vault.getAbstractFileByPath(
Expand Down

0 comments on commit de81930

Please sign in to comment.