[Rework] Damage and Damage Resource split (#2094)

This commit is contained in:
WBHarry 2026-07-18 23:31:38 +02:00 committed by GitHub
parent c90c6afc19
commit 3b68c6c895
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
31 changed files with 730 additions and 531 deletions

View file

@ -137,12 +137,17 @@ export default class DHActorRoll extends foundry.abstract.TypeDataModel {
if (!this.damage.active) return;
const rerolls = [];
const update = { system: { damage: { types: {} } } };
for (const key of Object.keys(this.damage.types)) {
const type = this.damage.types[key];
const reroll = await type.reroll();
const update = { system: { damage: { main: null, resources: _replace({}) } } };
if (this.damage.main) {
const reroll = await this.damage.main.reroll();
rerolls.push(reroll);
update.system.damage.types[key] = reroll.toJSON();
update.system.damage.main = reroll.toJSON();
}
for (const key of Object.keys(this.damage.resources)) {
const reroll = await this.damage.resources[key].reroll();
rerolls.push(reroll);
update.system.damage.resources[key] = reroll.toJSON();
}
await triggerChatRollFx(rerolls);
@ -188,22 +193,36 @@ 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;
const { main, resources, ...flatDamageKeys } = source.damage ?? {};
if (source.damage && !main && !resources) {
source.damage.main = null;
source.damage.resources = {};
return acc;
}, {})
};
const getRoll = key => {
const damageData = source.damage[key];
const oldRoll = damageData.parts[0]?.roll;
return oldRoll ? JSON.stringify({
...oldRoll,
class: 'BaseRoll',
options: {
...oldRoll.options,
damageTypes: damageData.parts[0].damageTypes ?? []
}
}) : null;
};
for (const key of Object.keys(flatDamageKeys)) {
if (key === 'hitPoints' && source.hasDamage && !source.hasHealing) {
source.damage.main = getRoll('hitPoints');
}
else {
source.damage.resources[key] = getRoll(key);
}
}
}
for (const key of Object.keys(flatDamageKeys)) {
delete source.damage[key];
}
return source;

View file

@ -11,31 +11,31 @@ export class ChatDamageData extends foundry.abstract.DataModel {
const fields = foundry.data.fields;
return {
types: new fields.TypedObjectField(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}))
};
}
get active() {
return Boolean(Object.keys(this.types).length);
return !!this.main || Boolean(Object.keys(this.resources).length);
}
static #validateRoll(rollJSON) {
const roll = JSON.parse(rollJSON);
if (!roll.evaluated) throw new Error('Roll objects added to ChatMessage documents must be evaluated');
}
_prepareRolls() {
for (const key of Object.keys(this.types)) {
const type = this.types[key];
try {
this.types[key] = Roll.fromData(type);
this.types[key].options.modifierTotal = CONFIG.Dice.daggerheart.DHRoll.calculateTotalModifiers(type);
} catch {}
if (rollJSON) {
const roll = JSON.parse(rollJSON);
if (!roll.evaluated) throw new Error('Roll objects added to ChatMessage documents must be evaluated');
}
}
async rerollDamageDie(damageType, dice, resultIndex) {
const reroll = this.types[damageType];
_prepareRolls() {
this.main &&= Roll.fromData(this.main);
for (const key of Object.keys(this.resources)) {
this.resources[key] = Roll.fromData(this.resources[key]);
}
}
async rerollDamageDie(isResource, damageType, dice, resultIndex) {
const reroll = isResource ? this.resources[damageType] : this.main;
const rerollDice = reroll.dice[dice];
await rerollDice.rerollResult(resultIndex);
await reroll._evaluate();