Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { normalizePath, TFile } from "obsidian";
import type InitiativeTracker from "../main";
import type { Creature } from "../utils/creature";
import { statusDisplay } from "../utils/conditions";

import { tracker } from "src/tracker/stores/tracker";
import type { UpdateLogMessage } from "./logger.types";
Expand Down Expand Up @@ -82,7 +83,7 @@ export default class Logger {
"|",
[
...(player.status.size
? [...player.status].map((c) => c.name)
? [...player.status].map((c) => statusDisplay(c))
: ["-"])
]
.join(", ")
Expand All @@ -106,7 +107,7 @@ export default class Logger {
"|",
[
...(creature.status.size
? [...creature.status].map((c) => c.name)
? [...creature.status].map((c) => statusDisplay(c))
: ["-"])
]
.join(", ")
Expand Down Expand Up @@ -223,6 +224,15 @@ export default class Logger {
perCreature.push(`took ${status.join(" and ")} status`);
}
}
if (message.update_status) {
if (perCreature.length) {
perCreature.push("and");
} else {
perCreature.push(message.name);
}
// Tracker always sends status updates individually, so message creation is a bit easier
perCreature.push(`changed status ${message.update_status.name} to ${statusDisplay(message.update_status)}`);
}
if (message.remove_status) {
if (perCreature.length) {
perCreature.push("and");
Expand All @@ -240,7 +250,7 @@ export default class Logger {
} else {
status = [message.remove_status[0]];
}
perCreature.push(`relieved of ${status.join(" and ")} status`);
perCreature.push(`relieved of ${status.map(s => s.name).join(" and ")} status`);
}
toLog.push(perCreature.join(" "));
}
Expand Down
3 changes: 2 additions & 1 deletion src/tracker/player/PlayerView.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import { createEventDispatcher } from "svelte";

import { tracker } from "../stores/tracker";
import { statusDisplay } from "src/utils/conditions";
const { state, ordered, data } = tracker;

const hpIcon = (node: HTMLElement) => {
Expand Down Expand Up @@ -81,7 +82,7 @@
{/if}
</td>
<td class="center">
{[...creature.status].map((s) => s.name).join(", ")}
{[...creature.status.values()].map((s) => statusDisplay(s)).join(", ")}
</td>
</tr>
{/each}
Expand Down
37 changes: 30 additions & 7 deletions src/tracker/stores/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import type {
DifficultyThreshold
} from "src/utils/rpg-system";
import type { StackRoller } from "@javalent/dice-roller";
import { statusDisplay } from "src/utils/conditions";

type HPUpdate = {
saved: boolean;
Expand All @@ -41,6 +42,7 @@ type CreatureUpdate = {
temp?: number;
max?: number;
status?: Condition[];
update_status?: ConditionUpdate[];
remove_status?: Condition[];
hidden?: boolean;
enabled?: boolean;
Expand Down Expand Up @@ -262,9 +264,24 @@ function createTracker() {
creature.addCondition(status);
}
}
if (change.update_status?.length) {
for (const status of change.update_status) {
creature.addCondition(status);
const message: UpdateLogMessage = {
name: creature.name,
update_status: status,
};
_logger?.logUpdate([message]);
}
}
if (change.remove_status?.length) {
for (const status of change.remove_status) {
creature.removeCondition(status);
const message: UpdateLogMessage = {
name: creature.name,
remove_status: [status],
};
_logger?.logUpdate([message]);
}
}
if ("hidden" in change) {
Expand Down Expand Up @@ -549,7 +566,7 @@ function createTracker() {
}
}
if (statuses.length) {
message.status = statuses.map((s) => s.name);
message.status = statuses.map((s) => statusDisplay(s));
if (!entry.saved) {
change.status = statuses;
} else {
Expand Down Expand Up @@ -641,11 +658,17 @@ function createTracker() {
$round.set(round);

for (const creature of creatures) {
creature.status = new Set(
[...creature.status].filter(
(s) => !s.resetOnRound
)
);
let removedStatuses = [];

creature.status.forEach((status, name, _) => {
if (status.resetOnRound) {
removedStatuses.push(name);
}
});

for (const s of removedStatuses) {
creature.status.delete(s);
}
}

_logger?.log("###", `Round ${round}`);
Expand Down Expand Up @@ -1320,7 +1343,7 @@ class Tracker {
}
}
if (statuses.length) {
message.status = statuses.map((s) => s.name);
message.status = statuses.map((s) => statusDisplay(s));
if (!entry.saved) {
change.status = statuses;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/ui/Updating.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
}}
/>
<div class="status-list-entries">
{#each $statuses as status}
{#each $statuses.values() as status}
<Status
{status}
on:remove={function (evt) {
Expand Down
8 changes: 7 additions & 1 deletion src/tracker/ui/creatures/Creature.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,15 @@
<!-- svelte-ignore a11y-click-events-have-key-events -->
<div class="statuses" on:click={(e) => e.stopPropagation()}>
{#if statuses.size}
{#each [...statuses] as status}
{#each statuses.values() as status}
<Status
{status}
on:update={() => {
tracker.updateCreatures({
creature,
change: { update_status: [status]}
});
}}
on:remove={() => {
tracker.updateCreatures({
creature,
Expand Down
13 changes: 10 additions & 3 deletions src/tracker/ui/creatures/Status.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
const dispatch = createEventDispatcher();

export let status: Condition;
if (isNaN(status.amount) || status.amount < 0) {
if (!!status.hasAmount && (isNaN(status.amount) || status.amount < 0)) {
status.amount = status.startingAmount;
}
const deleteIcon = (node: HTMLElement) => {
Expand Down Expand Up @@ -34,11 +34,18 @@
use:minus
on:click={() => {
status.amount--;
if (status.amount <= 0) dispatch("remove");
if (status.amount <= 0) {
dispatch("remove");
} else {
dispatch("update");
}
}}
/>
<span>{status.amount}</span>
<div class="icon" use:plus on:click={() => status.amount++} />
<div class="icon" use:plus on:click={() => {
status.amount++;
dispatch("update");
}} />
</div>
{/if}
<div use:deleteIcon on:click={() => dispatch("remove")} />
Expand Down
8 changes: 7 additions & 1 deletion src/utils/conditions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import type { Condition } from "src/types/creatures";


export function statusDisplay (status: Status): string {
if (status.hasAmount) {
return status.name + " (" + status.amount + ")";
} else {
return status.name;
}
}

export const Conditions: Condition[] = [
{
Expand Down
65 changes: 45 additions & 20 deletions src/utils/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class Creature {
current_max: number;
level: number;
player: boolean;
status: Set<Condition> = new Set();
status: Map<string, Condition> = new Map();
marker: string;
initiative: number;
manualOrder: number;
Expand All @@ -59,14 +59,11 @@ export class Creature {
this.modifier = this.modifier ?? 0;
}
addCondition(condition: Condition) {
if (![...this.status].find(cond => cond.name === condition.name && cond.amount === condition.amount)) {
this.status.add(condition);
}
this.status.set(condition.name, structuredClone(condition));
}
removeCondition(condition: Condition) {
this.status = new Set(
[...this.status].filter((s) => s.id != condition.id)
); }
this.status.delete(condition.name);
}
constructor(public creature: HomebrewCreature, initiative: number = 0) {
this.name = creature.name;
this.display = creature.display;
Expand Down Expand Up @@ -233,7 +230,8 @@ export class Creature {
marker: this.marker,
currentHP: this.hp,
tempHP: this.temp,
status: Array.from(this.status).map((c) => c.name),
// Only the basic built-in conditions are available during loading, so we need to export hasAmount to make it display the amount after loading
status: Object.fromEntries(Array.from(this.status.values()).map((c) => [c.name, {'amount':c.amount, 'hasAmount': c.hasAmount}])),
enabled: this.enabled,
level: this.level,
player: this.player,
Expand Down Expand Up @@ -263,20 +261,47 @@ export class Creature {
creature.current_max = state.currentMaxHP;
creature.hp = state.currentHP;
creature.current_ac = state.currentAC;
let statuses: Condition[] = [];
for (const status of state.status) {
const existing = Conditions.find(({ name }) => status == name);
if (existing) {
statuses.push(existing);
} else {
statuses.push({
name: status,
description: null,
id: getId()
});
let statuses = new Map<string, Condition>();
if (Array.isArray(state.status)) {
// Old style state saving, array of status names (supports loading existing data when upgrading the plugin)
for (const status of state.status) {
const existing = Conditions.find(({ name }) => status == name);
let newStatus;
if (existing) {
newStatus = existing;
} else {
newStatus = {
name: status,
description: null,
id: getId()
};
}

statuses.set(newStatus.name, newStatus);
}
} else {
// New style state saving, saves data about condition in an object
for (const statusName in state.status) {
const existing = Conditions.find(({ name }) => statusName == name);
let newStatus;
if (existing) {
newStatus = existing;
} else {
newStatus = {
name: statusName,
description: null,
id: getId()
};
}
for (const field in state.status[statusName]) {
newStatus[field] = state.status[statusName][field];
}

statuses.set(newStatus.name, newStatus);
}
}
creature.status = new Set(statuses);

creature.status = new Map(statuses);
creature.active = state.active;
return creature;
}
Expand Down