Single armor rework start

This commit is contained in:
WBHarry 2026-03-19 13:26:11 +01:00
parent 6e75b81806
commit caea55ade4
7 changed files with 53 additions and 32 deletions

View file

@ -153,6 +153,10 @@ export default class DhActiveEffectConfig extends foundry.applications.sheets.Ac
minLength: 0
});
});
htmlElement
.querySelector('.armor-change-checkbox')
?.addEventListener('change', this.armorChangeToggle.bind(this));
}
async _prepareContext(options) {
@ -190,10 +194,17 @@ export default class DhActiveEffectConfig extends foundry.applications.sheets.Ac
break;
case 'changes':
const fields = this.document.system.schema.fields.changes.element.fields;
const singleTypes = ['armor'];
const { base, ...typedChanges } = context.source.changes.reduce((acc, change, index) => {
const type = CONFIG.DH.GENERAL.baseActiveEffectModes[change.type] ? 'base' : change.type;
if (!acc[type]) acc[type] = [];
acc[type].push({ ...change, index });
if (singleTypes.includes(type)) {
acc[type] = { ...change, index };
} else {
if (!acc[type]) acc[type] = [];
acc[type].push({ ...change, index });
}
return acc;
}, {});
partContext.changes = await Promise.all(
@ -206,14 +217,30 @@ export default class DhActiveEffectConfig extends foundry.applications.sheets.Ac
return partContext;
}
armorChangeToggle(event) {
if (event.target.checked) {
this.addArmorChange();
} else {
this.removeTypedChange(event.target.dataset.index);
}
}
/* Could be generalised if needed later */
static #addTypedChange() {
addArmorChange() {
const submitData = this._processFormData(null, this.form, new FormDataExtended(this.form));
const changes = Object.values(submitData.system?.changes ?? {});
changes.push(game.system.api.data.activeEffects.changeTypes.armor.getInitialValue());
return this.submit({ updateData: { system: { changes } } });
}
removeTypedChange(indexString) {
const submitData = this._processFormData(null, this.form, new FormDataExtended(this.form));
const changes = Object.values(submitData.system.changes);
const index = Number(indexString);
changes.splice(index, 1);
return this.submit({ updateData: { system: { changes } } });
}
_prepareChangeContext(change, fields) {
if (typeof change.value !== 'string') change.value = JSON.stringify(change.value);
const defaultPriority = game.system.api.documents.DhActiveEffect.CHANGE_TYPES[change.type]?.defaultPriority;

View file

@ -89,6 +89,13 @@ export default class BaseEffect extends foundry.data.ActiveEffectTypeDataModel {
return true;
}
get armorData() {
const armorChange = this.changes.find(x => x.type === CONFIG.DH.GENERAL.activeEffectModes.armor.id);
if (!armorChange) return null;
return armorChange.armorData;
}
static getDefaultObject() {
return {
name: 'New Effect',

View file

@ -76,36 +76,27 @@ export default class Armor extends foundry.abstract.DataModel {
/* Helpers */
get armorChange() {
if (this.changes.length !== 1)
throw new Error('Unexpected error. An armor effect should have a changes field of length 1.');
const actor = this.parent.actor?.type === 'character' ? this.parent.actor : null;
const changeData = this.changes[0];
const maxParse = actor ? itemAbleRollParse(changeData.max, actor, this.parent.parent) : null;
get armorData() {
const actor = this.parent.parent?.actor?.type === 'character' ? this.parent.parent.actor : null;
const maxParse = actor ? itemAbleRollParse(this.max, actor, this.parent.parent.parent) : null;
const maxRoll = maxParse ? new Roll(maxParse).evaluateSync() : null;
const maxEvaluated = maxRoll ? (maxRoll.isDeterministic ? maxRoll.total : null) : null;
return {
...changeData,
max: maxEvaluated ?? changeData.max
value: this.value,
max: maxEvaluated ?? this.max
};
}
get armorData() {
return { value: this.armorChange.value, max: this.armorChange.max };
}
async updateArmorMax(newMax) {
const { effect, ...baseChange } = this.armorChange;
const newChanges = [
{
...baseChange,
max: newMax,
value: Math.min(this.armorChange.value, newMax)
}
...this.parent.changes.map(change => ({
...change,
value: change.type === 'armor' ? Math.min(this.parent.value, newMax) : change.value,
typeData: change.type === 'armor' ? { ...change.typeData, max: newMax } : change.typeData
}))
];
await this.parent.update({ 'system.changes': newChanges });
await this.parent.parent.update({ 'system.changes': newChanges });
}
static orderEffectsForAutoChange(armorEffects, increasing) {