Skip to content

Commit

Permalink
feat: Basic API started
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed Oct 9, 2023
1 parent 6424b23 commit a136eb1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
7 changes: 7 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "obsidian";
import type { Spell, Trait, ability } from "obsidian-overload";
import type { API } from "src/api/api";
import type { Filter, FilterLayout } from "src/builder/stores/filter/filter";

// CUSTOM EVENTS
Expand Down Expand Up @@ -302,3 +303,9 @@ export type TableHeaderState = {
type: SortFunctions;
func?: string;
};

declare global {
interface Window {
InitiativeTracker?: API;
}
}
36 changes: 36 additions & 0 deletions src/api/api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { HomebrewCreature } from "obsidian-overload";
import type InitiativeTracker from "../main";
import { tracker } from "../tracker/stores/tracker";
import { type InitiativeViewState } from "../..";
import { Creature } from "src/utils/creature";

export class API {
#tracker = tracker;
constructor(public plugin: InitiativeTracker) {
(window["InitiativeTracker"] = this) &&
this.plugin.register(() => delete window["InitiativeTracker"]);
}

addCreatures(
creatures: HomebrewCreature[],
rollHP: boolean = this.plugin.data.rollHP
) {
console.log("🚀 ~ file: api.ts:18 ~ creatures:", creatures);
if (!creatures || !Array.isArray(creatures) || !creatures.length) {
throw new Error("Creatures must be an array.");
}
this.#tracker.add(
this.plugin,
rollHP,
...creatures.map((c) => Creature.from(c))
);
}
newEncounter(state?: InitiativeViewState) {
if (state?.creatures) {
state.creatures = state.creatures.map((c) =>
Creature.from(c).toJSON()
);
}
this.#tracker.new(this.plugin, state);
}
}
2 changes: 0 additions & 2 deletions src/encounter/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import {
Component,
MarkdownPostProcessorContext,
MarkdownRenderChild,
Notice,
parseYaml
Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import BuilderView from "./builder/view";
import PlayerView from "./tracker/player-view";
import { tracker } from "./tracker/stores/tracker";
import { EncounterSuggester } from "./encounter/editor-suggestor";
import { API } from "./api/api"
declare module "obsidian" {
interface App {
plugins: {
Expand All @@ -55,6 +56,7 @@ declare module "obsidian" {
}

export default class InitiativeTracker extends Plugin {
api = new API(this);
public data: InitiativeTrackerData;
public tracker = tracker;
playerCreatures: Map<string, Creature> = new Map();
Expand Down

0 comments on commit a136eb1

Please sign in to comment.