Skip to content

Commit

Permalink
feat: Load saved encounters into builder
Browse files Browse the repository at this point in the history
  • Loading branch information
valentine195 committed May 9, 2023
1 parent cda408b commit de7181c
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/builder/stores/encounter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SRDMonster } from "../../../index";
import type { InitiativeViewState, SRDMonster } from "../../../index";
import { writable } from "svelte/store";
import { players } from "./players";
function createEncounter() {
Expand Down Expand Up @@ -34,7 +34,8 @@ function createEncounter() {
monsters.set(item, count);
return monsters;
}),
setMultiple: (creatures: [item: SRDMonster, count: number][]) => update((monsters) => {
setMultiple: (creatures: [item: SRDMonster, count: number][]) =>
update((monsters) => {
for (const [item, count] of creatures) {
monsters.set(item, count);
}
Expand Down
12 changes: 11 additions & 1 deletion src/builder/stores/players.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { EXPERIENCE_PER_LEVEL } from "../constants";

export const playerCount = writable(0);

interface Player extends Partial<CreatureState> {
export interface Player extends Partial<CreatureState> {
level: number;
name: string;
isPlayer: true;
Expand Down Expand Up @@ -80,6 +80,16 @@ function createPlayers() {
players.push(item);
return players;
}),
addFromState: (state: CreatureState) =>
update((players) => {
const item = state as Player;
item.isPlayer = true;
if (!item.count || item.count <= 0) {
item.count = 1;
}
players.push(item);
return players;
}),
remove: (item: CombinedPlayer) =>
update((players) => {
players = players.filter((p) => p != item);
Expand Down
1 change: 1 addition & 0 deletions src/builder/view/encounter/Creature.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@
<span>
<Nullable
str={`${
creature.xp ??
XP_PER_CR[creature.cr]?.toLocaleString() ??
DEFAULT_UNDEFINED
} XP`}
Expand Down
52 changes: 44 additions & 8 deletions src/builder/view/encounter/Encounter.svelte
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
<script lang="ts">
import { ExtraButtonComponent, Modal, Notice, Setting } from "obsidian";
import {
ExtraButtonComponent,
Menu,
Modal,
Notice,
Setting
} from "obsidian";
import Creature from "./Creature.svelte";
import { Creature as CreatureCreator } from "../../../utils/creature";
Expand All @@ -10,6 +16,7 @@
import { tracker } from "src/tracker/stores/tracker";
import type { CreatureState } from "index";
import { writable } from "svelte/store";
import type { SRDMonster } from "obsidian-overload";
const { players } = encounter;
Expand Down Expand Up @@ -93,10 +100,11 @@
const editIcon = (node: HTMLElement) => {
new ExtraButtonComponent(node).setIcon("pencil");
};
let saveButton: ExtraButtonComponent;
const saveIcon = (node: HTMLElement) => {
new ExtraButtonComponent(node).setIcon("save");
saveButton = new ExtraButtonComponent(node).setIcon("save");
};
$: saveButton?.setDisabled($encounter.size == 0);
const save = () => {
const modal = new Modal(app);
modal.contentEl.createEl("h4", { text: "Save Encounter" });
Expand Down Expand Up @@ -162,11 +170,38 @@
}
}
const load = (node: HTMLElement) => {
new ExtraButtonComponent(node)
.setIcon("import")
.setTooltip("Load Encounter")
.onClick(() => {});
let loadButton: ExtraButtonComponent;
const loadIcon = (node: HTMLElement) => {
loadButton = new ExtraButtonComponent(node)
.setIcon("upload")
.setTooltip("Load Encounter");
};
$: loadButton?.setDisabled(Object.keys(plugin.data.encounters).length == 0);
const load = (evt: MouseEvent) => {
const menu = new Menu();
for (const enc of Object.values(plugin.data.encounters)) {
menu.addItem((item) => {
item.setTitle(enc.name).onClick(() => {
console.log(enc);
$name = enc.name;
encounter.empty();
players.empty();
for (const creature of enc.creatures) {
if (creature.player) {
players.addFromState(creature);
} else {
encounter.add(
CreatureCreator.fromJSON(
creature
) as any as SRDMonster
);
}
}
console.log("🚀 ~ file: Encounter.svelte:198 ~ encounter:", $encounter);
});
});
}
menu.showAtMouseEvent(evt);
};
const clear = (node: HTMLElement) => {
new ExtraButtonComponent(node)
Expand All @@ -189,6 +224,7 @@
<div class="encounter-controls">
<div use:start />
<div use:saveIcon on:click={save} />
<div use:loadIcon on:click={load} />
<!-- <div use:exp /> -->
<!-- <div use:load /> -->

Expand Down
3 changes: 3 additions & 0 deletions src/utils/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export class Creature {
display: string;
friendly: boolean = false;
"statblock-link": string;
cr: string | number;

getXP(plugin: InitiativeTracker) {
if (this.xp) return this.xp;
Expand Down Expand Up @@ -89,6 +90,7 @@ export class Creature {
} else if ("cr" in creature) {
this.xp = XP_PER_CR[`${creature.cr}`];
}
this.cr = creature.cr;
this.id = creature.id ?? getId();
if ("statblock-link" in creature) {
this["statblock-link"] = (creature as any)[
Expand Down Expand Up @@ -205,6 +207,7 @@ export class Creature {
modifier: this.modifier,
hp: this.max,
currentMaxHP: this.current_max,
cr: this.cr,
ac: this.ac,
currentAC: this.current_ac,
note: this.note,
Expand Down

0 comments on commit de7181c

Please sign in to comment.