Skip to content

Commit

Permalink
Add getter for max affliction stage + minor cleanup (foundryvtt#9213)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlosFdez committed Aug 8, 2023
1 parent 1194a94 commit 72442fb
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions src/module/item/affliction/document.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { UserPF2e } from "@module/user/index.ts";
import { createDamageFormula, parseTermsFromSimpleFormula } from "@system/damage/formula.ts";
import { AfflictionDamageTemplate, BaseDamageData, DamagePF2e, DamageRollContext } from "@system/damage/index.ts";
import { DamageRoll } from "@system/damage/roll.ts";
import { DegreeOfSuccess } from "@system/degree-of-success.ts";
import { ErrorPF2e } from "@util";
import { AfflictionFlags, AfflictionSource, AfflictionSystemData } from "./data.ts";
import { DegreeOfSuccess } from "@system/degree-of-success.ts";

class AfflictionPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extends AbstractEffectPF2e<TParent> {
constructor(source: object, context?: DocumentConstructionContext<TParent>) {
Expand All @@ -21,7 +21,7 @@ class AfflictionPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extend
return {
type: "counter",
value: this.stage,
max: Object.keys(this.system.stages).length || 1,
max: this.maxStage,
label,
};
}
Expand All @@ -30,11 +30,14 @@ class AfflictionPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extend
return this.system.stage;
}

get maxStage(): number {
return Object.keys(this.system.stages).length || 1;
}

override async increase(): Promise<void> {
const maxStage = Object.keys(this.system.stages).length || 1;
if (this.stage === maxStage) return;
if (this.stage === this.maxStage) return;

const stage = Math.min(maxStage, this.system.stage + 1);
const stage = Math.min(this.maxStage, this.system.stage + 1);
await this.update({ system: { stage } });
}

Expand All @@ -50,8 +53,7 @@ class AfflictionPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extend

override prepareBaseData(): void {
super.prepareBaseData();
const maxStage = Object.keys(this.system.stages).length || 1;
this.system.stage = Math.clamped(this.system.stage, 1, maxStage);
this.system.stage = Math.clamped(this.system.stage, 1, this.maxStage);

// Set certain defaults
for (const stage of Object.values(this.system.stages)) {
Expand Down Expand Up @@ -163,20 +165,17 @@ class AfflictionPF2e<TParent extends ActorPF2e | null = ActorPF2e | null> extend
async rollRecovery(): Promise<void> {
if (!this.actor) return;

const saves = this?.actor?.saves;
if (saves) {
const stat = saves[this.system.save.type];
if (stat) {
const result = await stat.roll({
dc: { value: this.system.save.value },
extraRollOptions: this.getRollOptions("item"),
});

if ((result?.degreeOfSuccess ?? 0) >= DegreeOfSuccess.SUCCESS) {
this.decrease();
} else {
this.increase();
}
const save = this.actor.saves?.[this.system.save.type];
if (save) {
const result = await save.roll({
dc: { value: this.system.save.value },
extraRollOptions: this.getRollOptions("item"),
});

if ((result?.degreeOfSuccess ?? 0) >= DegreeOfSuccess.SUCCESS) {
this.decrease();
} else {
this.increase();
}
}
}
Expand Down

0 comments on commit 72442fb

Please sign in to comment.