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) { if (source.damage && source.damage.resources === undefined) {
source.damage.main = null;
source.damage.resources = {}; source.damage.resources = {};
for (const [partKey, part] of Object.entries(source.damage.parts)) { for (const [partKey, part] of Object.entries(source.damage.parts)) {
if (partKey === 'hitPoints') { if (partKey === 'hitPoints') {
@ -484,6 +485,8 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
source.damage.resources[partKey] = part; 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) { static migrateData(source) {
if (source.hasDamage && !source.damage.types) { if (source.hasDamage && !source.damage.resources === undefined) {
source.damage = { const getRoll = key => {
types: Object.keys(source.damage).reduce((acc, key) => { const damageData = source.damage[key];
const damageData = source.damage[key]; const oldRoll = damageData.parts[0]?.roll;
const oldRoll = damageData.parts[0]?.roll; return oldRoll ? {
acc[key] = oldRoll ? { ...oldRoll,
...oldRoll, options: {
options: { ...oldRoll.options,
...oldRoll.options, damageTypes: damageData.parts[0].damageTypes ?? []
damageTypes: damageData.parts[0].damageTypes ?? [] }
} } : null;
} : 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; return acc;
}, {}) }, {})
}; };

View file

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

View file

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