Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: reapplying conditions no longer removes them #298

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
19 changes: 19 additions & 0 deletions src/logger/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,25 @@ export default class Logger {
perCreature.push(`took ${status.join(" and ")} status`);
}
}
if (message.remove_status) {
if (perCreature.length) {
perCreature.push("and");
} else {
perCreature.push(message.name);
}
let status;
if (message.remove_status.length > 1) {
status = [
message.remove_status
.slice(0, message.remove_status.length - 1)
.join(", ")
];
status.push(message.remove_status[message.remove_status.length - 1]);
} else {
status = [message.remove_status[0]];
}
perCreature.push(`relieved of ${status.join(" and ")} status`);
}
toLog.push(perCreature.join(" "));
}
this.log(`${toLog.join(". ")}.`);
Expand Down
1 change: 1 addition & 0 deletions src/logger/logger.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface UpdateLogMessage {
hp: number | null;
temp: boolean;
status: string[] | null;
remove_status: string[] | null;
max: boolean;
saved: boolean;
unc: boolean;
Expand Down
61 changes: 31 additions & 30 deletions src/tracker/stores/tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type CreatureUpdate = {
temp?: number;
max?: number;
status?: Condition[];
remove_status?: Condition[];
hidden?: boolean;
enabled?: boolean;
//this is so dirty
Expand Down Expand Up @@ -183,7 +184,7 @@ function createTracker() {
const unc = _settings.statuses.find(
(s) => s.id == _settings.unconsciousId
);
if (unc) creature.status.add(unc);
if (unc) creature.addCondition(unc);
}
}
if (change.max) {
Expand Down Expand Up @@ -226,18 +227,12 @@ function createTracker() {
}
if (change.status?.length) {
for (const status of change.status) {
if ([...creature.status].find((s) => s.id == status.id)) {
creature.status = new Set(
[...creature.status].filter(
(s) => s.id != status.id
)
);
_logger?.log(
`${creature.name} relieved of status ${status.name}`
);
} else {
creature.status.add(status);
}
creature.addCondition(status);
}
}
if (change.remove_status?.length) {
for (const status of change.remove_status) {
creature.removeCondition(status);
}
}
if ("hidden" in change) {
Expand Down Expand Up @@ -432,12 +427,12 @@ function createTracker() {
description: "",
id: getId()
};
creature.status.add(cond);
creature.addCondition(cond);
} else if (
typeof status == "object" &&
status.name?.length
) {
creature.status.add(status as Condition);
creature.addCondition(status as Condition);
}
}
}
Expand All @@ -463,7 +458,7 @@ function createTracker() {
}
return creatures;
}),
doUpdate: (toAddString: string, statuses: Condition[], ac: string) =>
doUpdate: (toAddString: string, statuses: Condition[], ac: string, removeStatuses: Condition[] = []) =>
updating.update((updatingCreatures) => {
const messages: UpdateLogMessage[] = [];
const updates: CreatureUpdates[] = [];
Expand All @@ -485,6 +480,7 @@ function createTracker() {
temp: false,
max: false,
status: null,
remove_status: null,
saved: false,
unc: false,
ac: null,
Expand Down Expand Up @@ -522,6 +518,9 @@ function createTracker() {
message.saved = true;
}
}
if (removeStatuses.length) {
change.remove_status = [...removeStatuses];
}
if (ac) {
if (ac.charAt(0) == "+" || ac.charAt(0) == "-") {
const current_ac = parseInt(
Expand Down Expand Up @@ -1134,7 +1133,7 @@ class Tracker {
const unc = this.#data.statuses.find(
(s) => s.id == this.#data.unconsciousId
);
if (unc) creature.status.add(unc);
if (unc) creature.addCondition(unc);
}
}
if (change.max) {
Expand Down Expand Up @@ -1177,18 +1176,15 @@ class Tracker {
}
if (change.status?.length) {
for (const status of change.status) {
if ([...creature.status].find((s) => s.id == status.id)) {
creature.status = new Set(
[...creature.status].filter(
(s) => s.id != status.id
)
);
this.tryLog(
`${creature.name} relieved of status ${status.name}`
);
} else {
creature.status.add(status);
}
creature.addCondition(status);
}
}
if (change.remove_status?.length) {
for (const status of change.remove_status) {
creature.removeCondition(status);
this.tryLog(
`${creature.name} relieved of status ${status.name}`
);
}
}
if ("hidden" in change) {
Expand Down Expand Up @@ -1227,7 +1223,7 @@ class Tracker {
return creatures;
});
}
doUpdate(toAddString: string, statuses: Condition[], ac: string) {
doUpdate(toAddString: string, statuses: Condition[], ac: string, removeStatuses: Condition[]) {
this.updating.update((updatingCreatures) => {
const messages: UpdateLogMessage[] = [];
const updates: CreatureUpdates[] = [];
Expand All @@ -1249,6 +1245,7 @@ class Tracker {
temp: false,
max: false,
status: null,
remove_status: null,
saved: false,
unc: false,
ac: null,
Expand Down Expand Up @@ -1286,6 +1283,10 @@ class Tracker {
message.saved = true;
}
}
if (removeStatuses.length) {
message.remove_status = removeStatuses.map((s) => s.name);
change.remove_status = [...removeStatuses]
}
if (ac) {
if (ac.charAt(0) == "+" || ac.charAt(0) == "-") {
const current_ac = parseInt(
Expand Down
2 changes: 1 addition & 1 deletion src/tracker/ui/creatures/Creature.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@
on:remove={() => {
tracker.updateCreatures({
creature,
change: { status: [status] }
change: { remove_status: [status] }
});
}}
/>
Expand Down
9 changes: 9 additions & 0 deletions src/utils/creature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ 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);
}
}
removeCondition(condition: Condition) {
this.status = new Set(
[...this.status].filter((s) => s.id != condition.id)
); }
constructor(public creature: HomebrewCreature, initiative: number = 0) {
this.name = creature.name;
this.display = creature.display;
Expand Down