Skip to content

Commit

Permalink
[foundryvtt#3926] Fix bugs, adjust how applied effects are stored
Browse files Browse the repository at this point in the history
  • Loading branch information
arbron committed Aug 5, 2024
1 parent db0ca19 commit 6b4e97d
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 40 deletions.
2 changes: 1 addition & 1 deletion lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@
"hint": "Custom formula or flat value for defining the save DC."
},
"CustomFormula": "Custom Formula",
"DefaultFormula": "8 + Ability + Proficiency"
"DefaultFormula": "8 + @mod + @prof"
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions module/applications/activity/activity-sheet.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export default class ActivitySheet extends Application5e {
context.tab = context.tabs.effect;

if ( context.activity.effects ) {
const appliedEffects = new Set(context.activity.effects?.map(e => e.id) ?? []);
const appliedEffects = new Set(context.activity.effects?.map(e => e._id) ?? []);
context.allEffects = this.item.effects.map(effect => ({
value: effect.id, label: effect.name, selected: appliedEffects.has(effect.id)
}));
Expand Down Expand Up @@ -548,7 +548,7 @@ export default class ActivitySheet extends Application5e {
transfer: false
};
const [created] = await this.item.createEmbeddedDocuments("ActiveEffect", [effectData]);
this.activity.update({ effects: [...this.activity.toObject().effects, { id: created.id }] });
this.activity.update({ effects: [...this.activity.toObject().effects, { _id: created.id }] });
}

/* -------------------------------------------- */
Expand Down Expand Up @@ -614,7 +614,7 @@ export default class ActivitySheet extends Application5e {
const effectId = target.closest("[data-effect-id]")?.dataset.effectId;
const result = await this.item.effects.get(effectId)?.deleteDialog();
if ( result instanceof ActiveEffect ) {
const effects = this.activity.toObject().effects.filter(e => e.id !== effectId);
const effects = this.activity.toObject().effects.filter(e => e._id !== effectId);
this.activity.update({ effects });
}
}
Expand Down Expand Up @@ -681,10 +681,10 @@ export default class ActivitySheet extends Application5e {
}
if ( foundry.utils.hasProperty(submitData, "appliedEffects") ) {
const effects = submitData.effects ?? this.activity.toObject().effects;
submitData.effects = effects.filter(e => submitData.appliedEffects.includes(e.id));
for ( const id of submitData.appliedEffects ) {
if ( submitData.effects.find(e => e.id === id) ) continue;
submitData.effects.push({ id });
submitData.effects = effects.filter(e => submitData.appliedEffects.includes(e._id));
for ( const _id of submitData.appliedEffects ) {
if ( submitData.effects.find(e => e._id === _id) ) continue;
submitData.effects.push({ _id });
}
}
return submitData;
Expand Down
2 changes: 2 additions & 0 deletions module/data/activity/_module.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ export {default as AttackActivityData} from "./attack-data.mjs";
export {default as SaveActivityData} from "./save-data.mjs";
export {default as SummonActivityData} from "./summon-data.mjs";
export {default as UtilityActivityData} from "./utility-data.mjs";

export {default as AppliedEffectField} from "./fields/applied-effect-field.mjs";
19 changes: 4 additions & 15 deletions module/data/activity/base-activity.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { formatNumber } from "../../utils.mjs";
import FormulaField from "../fields/formula-field.mjs";
import UsesField from "../shared/uses-field.mjs";
import AppliedEffectField from "./fields/applied-effect-field.mjs";

const {
ArrayField, BooleanField, DocumentIdField, FilePathField, NumberField, SchemaField, StringField
Expand All @@ -23,7 +24,7 @@ const {
* Data for effects that can be applied.
*
* @typedef {object} EffectApplicationData
* @property {string} effect ID of the effect to apply.
* @property {string} _id ID of the effect to apply.
*/

/**
Expand Down Expand Up @@ -117,7 +118,7 @@ export default class BaseActivityData extends foundry.abstract.DataModel {
units: new StringField({ initial: "inst" }),
special: new StringField()
}),
effects: new ArrayField(new SchemaField(this.defineEffectSchema())),
effects: new ArrayField(new AppliedEffectField()),
range: new SchemaField({
value: new FormulaField({ deterministic: true }),
units: new StringField(),
Expand Down Expand Up @@ -145,18 +146,6 @@ export default class BaseActivityData extends foundry.abstract.DataModel {
};
}

/* -------------------------------------------- */

/**
* Return the fields that will be included in the schema for applied effects.
* @returns {Record<string, DataField>}
*/
static defineEffectSchema() {
return {
id: new DocumentIdField()
};
}

/* -------------------------------------------- */
/* Data Preparation */
/* -------------------------------------------- */
Expand All @@ -169,7 +158,7 @@ export default class BaseActivityData extends foundry.abstract.DataModel {
this.img = this.img || this.metadata?.img;
const item = this.item;
this.effects?.forEach(e => Object.defineProperty(e, "effect", {
get() { return item.effects.get(e.id); },
get() { return item.effects.get(e._id); },
configurable: true
}));
UsesField.prepareData.call(this, this.getRollData({ deterministic: true }));
Expand Down
15 changes: 15 additions & 0 deletions module/data/activity/fields/applied-effect-field.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { DocumentIdField, SchemaField } = foundry.data.fields;

/**
* Field for storing an active effects applied by an activity.
*
* @property {string} _id ID of the effect to apply.
*/
export default class AppliedEffectField extends SchemaField {
constructor(fields={}, options={}) {
super({
_id: new DocumentIdField(),
...fields
}, options);
}
}
19 changes: 5 additions & 14 deletions module/data/activity/save-data.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import FormulaField from "../fields/formula-field.mjs";
import DamageField from "../shared/damage-field.mjs";
import BaseActivityData from "./base-activity.mjs";
import AppliedEffectField from "./fields/applied-effect-field.mjs";

const { ArrayField, BooleanField, SchemaField, StringField } = foundry.data.fields;

Expand All @@ -12,7 +13,7 @@ const { ArrayField, BooleanField, SchemaField, StringField } = foundry.data.fiel
/**
* Data model for an save activity.
*
* @property {string} ability Ability used to make the attack and determine damage.
* @property {string} ability Make the saving throw with this ability.
* @property {object} damage
* @property {string} damage.onSave How much damage is done on a successful save?
* @property {DamageData[]} damage.parts Parts of damage to inflict.
Expand All @@ -32,6 +33,9 @@ export default class SaveActivityData extends BaseActivityData {
onSave: new StringField(),
parts: new ArrayField(new DamageField())
}),
effects: new ArrayField(new AppliedEffectField({
onSave: new BooleanField()
})),
save: new SchemaField({
dc: new SchemaField({
calculation: new StringField(),
Expand All @@ -41,19 +45,6 @@ export default class SaveActivityData extends BaseActivityData {
};
}

/* -------------------------------------------- */

/**
* Return the fields that will be included in the schema for applied effects.
* @returns {Record<string, DataField>}
*/
static defineEffectSchema() {
return {
...super.defineEffectSchema(),
onSave: new BooleanField()
};
}

/* -------------------------------------------- */
/* Data Preparation */
/* -------------------------------------------- */
Expand Down
4 changes: 2 additions & 2 deletions templates/activity/parts/activity-effects.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
</multi-select>
<ul class="separated-list effects-list">
{{#each appliedEffects}}
<li data-index="{{ @index }}" data-effect-id="{{ id }}" data-expand-id="effects.{{ id }}">
<li data-index="{{ @index }}" data-effect-id="{{ effect.id }}" data-expand-id="effects.{{ effect.id }}">
{{> ".effect" }}
</li>
{{/each}}
Expand All @@ -27,7 +27,7 @@
</button>
</div>
</div>
<input type="hidden" name="{{ prefix }}id" value="{{ data.id }}">
<input type="hidden" name="{{ prefix }}_id" value="{{ data._id }}">
{{#if additionalSettings}}
<div class="additional-tray collapsible" data-action="toggleCollapsed">
<label class="roboto-upper">
Expand Down
2 changes: 1 addition & 1 deletion templates/activity/parts/save-details.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{{ formField fields.formula value=../source.save.dc.formula }}
{{else}}
{{ formField fields.formula name="" value=(localize "DND5E.SAVE.FIELDS.save.dc.DefaultFormula") disabled=true }}
<input type="hidden" name="save.dc.formula" value="{{ ../source.save.dv.formula }}">
<input type="hidden" name="save.dc.formula" value="{{ ../source.save.dc.formula }}">
{{/if}}
{{/with}}
</fieldset>

0 comments on commit 6b4e97d

Please sign in to comment.