Skip to content

Commit

Permalink
3.1.0
Browse files Browse the repository at this point in the history
- Initiative state now saves if an encounter is active. Fixes #12
  • Loading branch information
valentine195 committed Sep 25, 2021
1 parent 8d61d43 commit 7d5296a
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 19 deletions.
16 changes: 16 additions & 0 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ export interface InitiativeTrackerData {
leafletIntegration: boolean;
playerMarker: string;
monsterMarker: string;
state: InitiativeViewState;
}

export interface InitiativeViewState {
creatures: CreatureState[];
state: boolean;
current: number;
name: string;
}

export interface CreatureState extends HomebrewCreature {
status: string[];
enabled: boolean;
currentHP: number;
initiative: number;
player: boolean;
}

export interface SRDMonster {
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "initiative-tracker",
"name": "Initiative Tracker",
"version": "3.0.5",
"version": "3.1.0",
"minAppVersion": "0.12.10",
"author": "Jeremy Valentine",
"description": "TTRPG Initiative Tracker for Obsidian.md",
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": "initiative-tracker",
"version": "3.0.5",
"version": "3.1.0",
"description": "TTRPG Initiative Tracker for Obsidian.md",
"main": "main.js",
"scripts": {
Expand Down
3 changes: 2 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,8 @@ export default class InitiativeTracker extends Plugin {
console.log("Initiative Tracker v" + this.manifest.version + " loaded");
}

onunload() {
async onunload() {
await this.saveSettings();
this.app.workspace.trigger("initiative-tracker:unload");
this.app.workspace
.getLeavesOfType(INTIATIVE_TRACKER_VIEW)
Expand Down
25 changes: 17 additions & 8 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ export default class InitiativeTrackerSettings extends PluginSettingTab {
const marker = this.plugin.leaflet.markerIcons.find(
(icon) => icon.type == this.plugin.data.playerMarker
);
inner.innerHTML = marker.html;
if (marker) {
inner.innerHTML = marker.html;

playerMarker.descEl.appendChild(div);
playerMarker.descEl.appendChild(div);
}
}
const monsterMarker = new Setting(containerEl)
.setName("Default Monster Marker Type")
Expand All @@ -208,9 +210,11 @@ export default class InitiativeTrackerSettings extends PluginSettingTab {
const marker = this.plugin.leaflet.markerIcons.find(
(icon) => icon.type == this.plugin.data.monsterMarker
);
inner.innerHTML = marker.html;
if (marker) {
inner.innerHTML = marker.html;

monsterMarker.descEl.appendChild(div);
monsterMarker.descEl.appendChild(div);
}
}
}

Expand All @@ -230,6 +234,7 @@ export default class InitiativeTrackerSettings extends PluginSettingTab {
}
});
} catch (e) {
console.error(e);
new Notice(
"There was an error displaying the settings tab for Obsidian Initiative Tracker."
);
Expand Down Expand Up @@ -807,9 +812,11 @@ class NewPlayerModal extends Modal {
const marker = this.plugin.leaflet.markerIcons.find(
(icon) => icon.type == this.player.marker
);
inner.innerHTML = marker.html;
if (marker) {
inner.innerHTML = marker.html;

markerSetting.descEl.appendChild(div);
markerSetting.descEl.appendChild(div);
}
}
}

Expand Down Expand Up @@ -1026,9 +1033,11 @@ class NewCreatureModal extends Modal {
const marker = this.plugin.leaflet.markerIcons.find(
(icon) => icon.type == this.creature.marker
);
inner.innerHTML = marker.html;
if (marker) {
inner.innerHTML = marker.html;

markerSetting.descEl.appendChild(div);
markerSetting.descEl.appendChild(div);
}
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@ export const DEFAULT_SETTINGS: InitiativeTrackerData = {
sync: false,
leafletIntegration: false,
playerMarker: "default",
monsterMarker: "default"
monsterMarker: "default",
state: {
creatures: [],
state: false,
current: null,
name: null
}
};
36 changes: 35 additions & 1 deletion src/utils/creature.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import type { Condition, HomebrewCreature, SRDMonster } from "@types";
import type {
Condition,
CreatureState,
HomebrewCreature,
SRDMonster
} from "@types";
import type InitiativeTracker from "src/main";
import { Conditions } from ".";
import { DEFAULT_UNDEFINED } from "./constants";

function getId() {
Expand Down Expand Up @@ -101,4 +107,32 @@ export class Creature {
toProperties() {
return { ...this };
}

toJSON(): CreatureState {
return {
name: this.name,
initiative: this.initiative - this.modifier,
modifier: this.modifier,
hp: this.max,
ac: this.ac,
note: this.note,
id: this.id,
marker: this.marker,
currentHP: this.hp,
status: Array.from(this.status).map((c) => c.name),
enabled: this.enabled,
player: this.player
};
}

static fromJSON(state: CreatureState) {
const creature = new Creature(state, state.initiative);
creature.enabled = state.enabled;

creature.hp = state.currentHP;
creature.status = new Set(
state.status.map((n) => Conditions.find(({ name }) => n == name))
);
return creature;
}
}
62 changes: 57 additions & 5 deletions src/view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ import { BASE, INTIATIVE_TRACKER_VIEW, MIN_WIDTH_FOR_HAMBURGER } from "./utils";
import type InitiativeTracker from "./main";

import App from "./svelte/App.svelte";
import type { Creature } from "./utils/creature";
import type { Condition, TrackerEvents, TrackerViewState } from "@types";
import { Creature } from "./utils/creature";
import type {
Condition,
InitiativeViewState,
TrackerEvents,
TrackerViewState
} from "@types";
import Creature__SvelteComponent_ from "./svelte/Creature.svelte";

export default class TrackerView extends ItemView {
public creatures: Creature[] = [];
Expand Down Expand Up @@ -42,7 +48,12 @@ export default class TrackerView extends ItemView {

constructor(public leaf: WorkspaceLeaf, public plugin: InitiativeTracker) {
super(leaf);
this.newEncounter();

if (this.plugin.data.state?.creatures?.length) {
this.newEncounterFromState(this.plugin.data.state);
} else {
this.newEncounter();
}

this.registerEvent(
this.app.workspace.on(
Expand Down Expand Up @@ -142,6 +153,27 @@ export default class TrackerView extends ItemView {
)
);
}
newEncounterFromState(initiativeState: InitiativeViewState) {
if (!initiativeState || !initiativeState?.creatures.length) {
this.newEncounter();
}
const { creatures, state, name, current } = initiativeState;
this.creatures = [...creatures.map((c) => Creature.fromJSON(c))];

if (name) {
this.name = name;
this.setAppState({
name: this.name
});
}
this.state = state;
this.current = current;
this.trigger("initiative-tracker:new-encounter", this.appState);

this.setAppState({
creatures: this.ordered
});
}
private _addCreature(creature: Creature) {
this.creatures.push(creature);

Expand Down Expand Up @@ -197,11 +229,13 @@ export default class TrackerView extends ItemView {
async newEncounter({
name,
players = true,
creatures = []
creatures = [],
roll = true
}: {
name?: string;
players?: boolean | string[];
creatures?: Creature[];
roll?: boolean;
} = {}) {
if (players instanceof Array && players.length) {
this.creatures = [
Expand All @@ -227,7 +261,12 @@ export default class TrackerView extends ItemView {

this.trigger("initiative-tracker:new-encounter", this.appState);

await this.rollInitiatives();
if (roll) await this.rollInitiatives();
else {
this.setAppState({
creatures: this.ordered
});
}
}

resetEncounter() {
Expand Down Expand Up @@ -483,4 +522,17 @@ export default class TrackerView extends ItemView {
const [name, ...data] = args;
this.app.workspace.trigger(name, ...data);
}
async onunload() {
if (this.state) {
this.plugin.data.state = {
creatures: [...this.ordered.map((c) => c.toJSON())],
state: this.state,
current: this.current,
name: this.name
};
} else {
this.plugin.data.state = null;
}
await this.plugin.saveSettings();
}
}
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"0.0.10": "0.12.4",
"1.0.1": "0.12.4",
"2.0.3": "0.12.10",
"3.0.5": "0.12.10"
"3.0.5": "0.12.10",
"3.1.0": "0.12.10"
}

0 comments on commit 7d5296a

Please sign in to comment.