Corrected ChatDamageData and made damage.main nullable

This commit is contained in:
WBHarry 2026-07-18 00:30:14 +02:00
parent 578e6e6c76
commit ce47c63ce6
4 changed files with 30 additions and 16 deletions

View file

@ -471,6 +471,7 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
}
if (source.damage && source.damage.resources === undefined) {
source.damage.main = null;
source.damage.resources = {};
for (const [partKey, part] of Object.entries(source.damage.parts)) {
if (partKey === 'hitPoints') {
@ -484,6 +485,8 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
source.damage.resources[partKey] = part;
}
}
delete source.damage.parts;
}
}
}

View file

@ -188,19 +188,28 @@ export default class DHActorRoll extends foundry.abstract.TypeDataModel {
}
static migrateData(source) {
if (source.hasDamage && !source.damage.types) {
source.damage = {
types: Object.keys(source.damage).reduce((acc, key) => {
const damageData = source.damage[key];
const oldRoll = damageData.parts[0]?.roll;
acc[key] = oldRoll ? {
...oldRoll,
options: {
...oldRoll.options,
damageTypes: damageData.parts[0].damageTypes ?? []
}
} : null;
if (source.hasDamage && !source.damage.resources === undefined) {
const getRoll = key => {
const damageData = source.damage[key];
const oldRoll = damageData.parts[0]?.roll;
return oldRoll ? {
...oldRoll,
options: {
...oldRoll.options,
damageTypes: damageData.parts[0].damageTypes ?? []
}
} : null;
};
source.damage = {
main: source.damage.hitPoints ? getRoll('hitPoints') : null,
resources: Object.keys(source.damage).reduce((acc, key) => {
if (key === 'hitPoints') return acc;
const roll = getRoll(key);
if (!roll) return acc;
acc[key] = roll;
return acc;
}, {})
};

View file

@ -11,7 +11,7 @@ export class ChatDamageData extends foundry.abstract.DataModel {
const fields = foundry.data.fields;
return {
damage: new fields.JSONField({validate: ChatDamageData.#validateRoll}),
main: new fields.JSONField({ nullable: true, validate: ChatDamageData.#validateRoll}),
resources: new fields.TypedObjectField(new fields.JSONField({validate: ChatDamageData.#validateRoll}))
};
}
@ -21,8 +21,10 @@ export class ChatDamageData extends foundry.abstract.DataModel {
}
static #validateRoll(rollJSON) {
const roll = JSON.parse(rollJSON);
if (!roll.evaluated) throw new Error('Roll objects added to ChatMessage documents must be evaluated');
if (rollJSON) {
const roll = JSON.parse(rollJSON);
if (!roll.evaluated) throw new Error('Roll objects added to ChatMessage documents must be evaluated');
}
}
_prepareRolls() {

View file

@ -13,7 +13,7 @@ export default class DamageField extends fields.SchemaField {
/** @inheritDoc */
constructor(options, context = {}) {
const damageFields = {
main: new fields.EmbeddedDataField(DHDamageData),
main: new fields.EmbeddedDataField(DHDamageData, { nullable: true }),
resources: new IterableTypedObjectField(DHResourceData)
};
super(damageFields, options, context);