Merged with main

This commit is contained in:
WBHarry 2025-07-08 21:20:30 +02:00
commit f679e87a4b
23 changed files with 317 additions and 118 deletions

View file

@ -1,11 +1,20 @@
import DamageSelectionDialog from '../applications/dialogs/damageSelectionDialog.mjs';
import { GMUpdateEvent, socketEvent } from '../systemRegistration/socket.mjs';
import { emitAsGM, emitAsOwner, GMUpdateEvent, socketEvent } from '../systemRegistration/socket.mjs';
import DamageReductionDialog from '../applications/dialogs/damageReductionDialog.mjs';
import { LevelOptionType } from '../data/levelTier.mjs';
import DHFeature from '../data/item/feature.mjs';
import { damageKeyToNumber, getDamageKey } from '../helpers/utils.mjs';
export default class DhpActor extends foundry.documents.Actor {
export default class DhpActor extends Actor {
/**
* Return the first Actor active owner.
*/
get owner() {
const user =
this.hasPlayerOwner && game.users.players.find(u => this.testUserPermission(u, 'OWNER') && u.active);
if (!user) return game.user.isGM ? game.user : null;
return user;
}
/**
* Whether this actor is an NPC.
@ -361,16 +370,16 @@ export default class DhpActor extends foundry.documents.Actor {
const modifier = roll.modifier !== null ? Number.parseInt(roll.modifier) : null;
return modifier !== null
? [
{
value: modifier,
label: roll.label
? modifier >= 0
? `${roll.label} +${modifier}`
: `${roll.label} ${modifier}`
: null,
title: roll.label
}
]
{
value: modifier,
label: roll.label
? modifier >= 0
? `${roll.label} +${modifier}`
: `${roll.label} ${modifier}`
: null,
title: roll.label
}
]
: [];
}
@ -457,6 +466,8 @@ export default class DhpActor extends foundry.documents.Actor {
}
async takeDamage(baseDamage, type) {
if (Hooks.call(`${CONFIG.DH.id}.preTakeDamage`, this, baseDamage, type) === false) return null;
if (this.type === 'companion') {
await this.modifyResource([{ value: 1, type: 'stress' }]);
return;
@ -464,41 +475,33 @@ export default class DhpActor extends foundry.documents.Actor {
const flatReduction = this.system.bonuses.damageReduction[type];
const damage = Math.max(baseDamage - (flatReduction ?? 0), 0);
const hpDamage = this.convertDamageToThreshold(damage);
const hpDamage =
damage >= this.system.damageThresholds.severe
? 3
: damage >= this.system.damageThresholds.major
? 2
: damage >= this.system.damageThresholds.minor
? 1
: 0;
if (Hooks.call(`${CONFIG.DH.id}.postDamageTreshold`, this, hpDamage, damage, type) === false) return null;
if (hpDamage && this.type === 'character' && this.#canReduceDamage(hpDamage, type)) {
new Promise((resolve, reject) => {
new DamageReductionDialog(resolve, reject, this, hpDamage, type).render(true);
})
.then(async ({ modifiedDamage, armorSpent, stressSpent }) => {
const resources = [
{ value: modifiedDamage, type: 'hitPoints' },
...(armorSpent ? [{ value: armorSpent, type: 'armorStack' }] : []),
...(stressSpent ? [{ value: stressSpent, type: 'stress' }] : [])
];
await this.modifyResource(resources);
})
.catch(() => {
const cls = getDocumentClass('ChatMessage');
const msg = new cls({
user: game.user.id,
content: game.i18n.format('DAGGERHEART.UI.Notifications.damageIgnore', {
character: this.name
})
});
cls.create(msg.toObject());
});
} else {
await this.modifyResource([{ value: hpDamage, type: 'hitPoints' }]);
if (!hpDamage) return;
const updates = [{ value: hpDamage, type: 'hitPoints' }];
if (this.type === 'character' && this.system.armor && this.#canReduceDamage(hpDamage, type)) {
const armorStackResult = await this.owner.query('armorStack', {
actorId: this.uuid,
damage: hpDamage,
type: type
});
if (armorStackResult) {
const { modifiedDamage, armorSpent, stressSpent } = armorStackResult;
updates.find(u => u.type === 'hitPoints').value = modifiedDamage;
updates.push(
...(armorSpent ? [{ value: armorSpent, type: 'armorStack' }] : []),
...(stressSpent ? [{ value: stressSpent, type: 'stress' }] : [])
);
}
}
await this.modifyResource(updates);
if (Hooks.call(`${CONFIG.DH.id}.postTakeDamage`, this, damage, type) === false) return null;
}
async takeHealing(resources) {
@ -508,6 +511,8 @@ export default class DhpActor extends foundry.documents.Actor {
async modifyResource(resources) {
if (!resources.length) return;
if (resources.find(r => r.type === 'stress')) this.convertStressDamageToHP(resources);
let updates = { actor: { target: this, resources: {} }, armor: { target: this.system.armor, resources: {} } };
resources.forEach(r => {
switch (r.type) {
@ -535,7 +540,13 @@ export default class DhpActor extends foundry.documents.Actor {
});
Object.values(updates).forEach(async u => {
if (Object.keys(u.resources).length > 0) {
if (game.user.isGM) {
await emitAsGM(
GMUpdateEvent.UpdateDocument,
u.target.update.bind(u.target),
u.resources,
u.target.uuid
);
/* if (game.user.isGM) {
await u.target.update(u.resources);
} else {
await game.socket.emit(`system.${CONFIG.DH.id}`, {
@ -546,8 +557,35 @@ export default class DhpActor extends foundry.documents.Actor {
update: u.resources
}
});
}
} */
}
});
}
convertDamageToThreshold(damage) {
return damage >= this.system.damageThresholds.severe
? 3
: damage >= this.system.damageThresholds.major
? 2
: damage >= this.system.damageThresholds.minor
? 1
: 0;
}
convertStressDamageToHP(resources) {
const stressDamage = resources.find(r => r.type === 'stress'),
newValue = this.system.resources.stress.value + stressDamage.value;
if (newValue <= this.system.resources.stress.maxTotal) return;
const hpDamage = resources.find(r => r.type === 'hitPoints');
if (hpDamage) hpDamage.value++;
else
resources.push({
type: 'hitPoints',
value: 1
});
}
}
export const registerDHActorHooks = () => {
CONFIG.queries.armorStack = DamageReductionDialog.armorStackQuery;
};