From 72442fb784d795ec3ab2943b9bfe062a35acbc51 Mon Sep 17 00:00:00 2001 From: Carlos Fernandez Date: Tue, 8 Aug 2023 04:45:52 -0400 Subject: [PATCH] Add getter for max affliction stage + minor cleanup (#9213) --- src/module/item/affliction/document.ts | 41 +++++++++++++------------- 1 file changed, 20 insertions(+), 21 deletions(-) diff --git a/src/module/item/affliction/document.ts b/src/module/item/affliction/document.ts index c447c30a6b5..e5448206610 100644 --- a/src/module/item/affliction/document.ts +++ b/src/module/item/affliction/document.ts @@ -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 extends AbstractEffectPF2e { constructor(source: object, context?: DocumentConstructionContext) { @@ -21,7 +21,7 @@ class AfflictionPF2e extend return { type: "counter", value: this.stage, - max: Object.keys(this.system.stages).length || 1, + max: this.maxStage, label, }; } @@ -30,11 +30,14 @@ class AfflictionPF2e extend return this.system.stage; } + get maxStage(): number { + return Object.keys(this.system.stages).length || 1; + } + override async increase(): Promise { - 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 } }); } @@ -50,8 +53,7 @@ class AfflictionPF2e 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)) { @@ -163,20 +165,17 @@ class AfflictionPF2e extend async rollRecovery(): Promise { 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(); } } }