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

@ -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() {