Current Target Save & Action without roll chat message (#528)

This commit is contained in:
Dapoulp 2025-08-02 21:29:11 +02:00 committed by GitHub
parent 73a19fc0bb
commit 2f39e04da5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 224 additions and 205 deletions

View file

@ -114,7 +114,7 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
async use(event, ...args) {
if (!this.actor) throw new Error("An Action can't be used outside of an Actor context.");
if (this.chatDisplay) this.toChat();
if (this.chatDisplay) await this.toChat();
let config = this.prepareConfig(event);
for (let i = 0; i < this.constructor.extraSchemas.length; i++) {
@ -139,11 +139,16 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
config = await this.actor.diceRoll(config);
if (!config) return;
}
if (this.doFollowUp()) {
if (this.rollDamage) await this.rollDamage(event, config);
if (this.rollHealing) await this.rollHealing(event, config);
if (this.trigger) await this.trigger(event, config);
if (this.rollDamage && this.damage.parts.length) await this.rollDamage(event, config);
else if (this.trigger) await this.trigger(event, config);
else if(this.hasSave || this.hasEffect) {
const roll = new Roll('');
roll._evaluated = true;
if(this.hasTarget) config.targetSelection = config.targets.length > 0;
await CONFIG.Dice.daggerheart.DHRoll.toMessage(roll, config);
}
}
// Consume resources
@ -254,6 +259,10 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
/* SAVE */
/* EFFECTS */
get hasEffect() {
return this.effects?.length > 0;
}
async applyEffects(event, data, targets) {
targets ??= data.system.targets;
const force = true; /* Where should this come from? */
@ -343,10 +352,16 @@ export default class DHBaseAction extends ActionMixin(foundry.abstract.DataModel
async updateChatMessage(message, targetId, changes, chain = true) {
setTimeout(async () => {
const chatMessage = ui.chat.collection.get(message._id),
msgTargets = chatMessage.system.targets,
msgTarget = msgTargets.find(mt => mt.id === targetId);
msgTarget = chatMessage.system.targets.find(mt => mt.id === targetId) ?? chatMessage.system.oldTargets.find(mt => mt.id === targetId);
msgTarget.saved = changes;
await chatMessage.update({ 'system.targets': msgTargets });
await chatMessage.update(
{
system: {
targets: chatMessage.system.targets,
oldTargets: chatMessage.system.oldTargets
}
}
);
}, 100);
if (chain) {
if (message.system.source.message)

View file

@ -45,12 +45,12 @@ export default class DHDamageAction extends DHBaseAction {
formulas = this.formatFormulas(formulas, systemData);
delete systemData.evaluate;
systemData.targets.forEach(t => t.hit = true);
const config = {
...systemData,
roll: formulas,
dialog: {},
data: this.getRollData()
data: this.getRollData(),
targetSelection: systemData.targets.length > 0
}
if (this.hasSave) config.onSave = this.save.damageMod;
if (data.system) {

View file

@ -2,17 +2,4 @@ import DHBaseAction from './baseAction.mjs';
export default class DHEffectAction extends DHBaseAction {
static extraSchemas = [...super.extraSchemas, 'effects', 'target'];
async trigger(event, data) {
if (this.effects.length) {
const cls = getDocumentClass('ChatMessage'),
msg = {
type: 'applyEffect',
user: game.user.id,
system: data
};
return await cls.create(msg);
} else this.toChat(this.id);
}
}

View file

@ -1,11 +1,9 @@
import DHAbilityUse from "./abilityUse.mjs";
import DHActorRoll from "./adversaryRoll.mjs";
import DHApplyEffect from './applyEffects.mjs'
export const config = {
abilityUse: DHAbilityUse,
adversaryRoll: DHActorRoll,
damageRoll: DHActorRoll,
dualityRoll: DHActorRoll,
applyEffect: DHApplyEffect
dualityRoll: DHActorRoll
};

View file

@ -1,25 +1,30 @@
const fields = foundry.data.fields;
const targetsField = () => new fields.ArrayField(
new fields.SchemaField({
id: new fields.StringField({}),
actorId: new fields.StringField({}),
name: new fields.StringField({}),
img: new fields.StringField({}),
difficulty: new fields.NumberField({ integer: true, nullable: true }),
evasion: new fields.NumberField({ integer: true }),
hit: new fields.BooleanField({ initial: false }),
saved: new fields.SchemaField({
result: new fields.NumberField(),
success: new fields.BooleanField({ nullable: true, initial: null })
})
})
)
export default class DHActorRoll extends foundry.abstract.TypeDataModel {
targetHook = null;
static defineSchema() {
return {
title: new fields.StringField(),
roll: new fields.ObjectField(),
targets: new fields.ArrayField(
new fields.SchemaField({
id: new fields.StringField({}),
actorId: new fields.StringField({}),
name: new fields.StringField({}),
img: new fields.StringField({}),
difficulty: new fields.NumberField({ integer: true, nullable: true }),
evasion: new fields.NumberField({ integer: true }),
hit: new fields.BooleanField({ initial: false }),
saved: new fields.SchemaField({
result: new fields.NumberField(),
success: new fields.BooleanField({ nullable: true, initial: null })
})
})
),
targets: targetsField(),
oldTargets: targetsField(),
targetSelection: new fields.BooleanField({ initial: false }),
hasRoll: new fields.BooleanField({ initial: false }),
hasDamage: new fields.BooleanField({ initial: false }),
@ -45,23 +50,96 @@ export default class DHActorRoll extends foundry.abstract.TypeDataModel {
return 'systems/daggerheart/templates/ui/chat/roll.hbs';
}
prepareDerivedData() {
this.hasHitTarget = this.targets.filter(t => t.hit === true).length > 0;
this.currentTargets =
this.targetSelection !== true
? Array.from(game.user.targets).map(t =>
game.system.api.fields.ActionFields.TargetField.formatTarget(t)
)
: this.targets;
if(this.targetSelection === true) {
this.targetShort = this.targets.reduce((a,c) => {
if(c.hit) a.hit += 1;
else c.miss += 1;
return a;
}, {hit: 0, miss: 0})
get targetMode() {
return this.targetSelection;
}
set targetMode(mode) {
this.targetSelection = mode;
this.updateTargets();
this.registerTargetHook();
this.parent.update(
{
system: {
targetSelection: this.targetSelection,
oldTargets: this.oldTargets
}
}
);
}
get hitTargets() {
return this.currentTargets.filter(t => (t.hit || !this.targetSelection));
}
async updateTargets() {
this.currentTargets = this.getTargetList();
if(!this.targetSelection && this.hasSave) {
this.currentTargets.forEach(ct => {
if(this.targets.find(t => t.actorId === ct.actorId)) return;
const indexTarget = this.oldTargets.findIndex(ot => ot.actorId === ct.actorId);
if(indexTarget === -1)
this.oldTargets.push(ct);
});
this.setPendingSaves();
if(this.currentTargets.length) {
if(!this.parent._id) return;
const updates = await this.parent.update(
{
system: {
oldTargets: this.oldTargets
}
}
);
if(!updates)
ui.chat.updateMessage(this.parent);
}
}
this.pendingSaves = this.targets.filter(
target => target.hit && target.saved.success === null
).length > 0;
}
registerTargetHook() {
if(this.targetSelection && this.targetHook !== null) {
Hooks.off("targetToken", this.targetHook);
this.targetHook = null;
} else if(!this.targetSelection && this.targetHook === null) {
this.targetHook = Hooks.on("targetToken", foundry.utils.debounce(this.updateTargets.bind(this), 50));
}
}
prepareDerivedData() {
if(this.hasTarget) {
this.hasHitTarget = this.targets.filter(t => t.hit === true).length > 0;
this.updateTargets();
this.registerTargetHook();
if(this.targetSelection === true) {
this.targetShort = this.targets.reduce((a,c) => {
if(c.hit) a.hit += 1;
else c.miss += 1;
return a;
}, {hit: 0, miss: 0})
}
if(this.hasSave) this.setPendingSaves();
}
}
getTargetList() {
return this.targetSelection !== true
? Array.from(game.user.targets).map(t =>{
const target = game.system.api.fields.ActionFields.TargetField.formatTarget(t),
oldTarget = this.targets.find(ot => ot.actorId === target.actorId) ?? this.oldTargets.find(ot => ot.actorId === target.actorId);
if(oldTarget) return oldTarget;
return target;
})
: this.targets;
}
setPendingSaves() {
this.pendingSaves = this.targetSelection
? this.targets.filter(
target => target.hit && target.saved.success === null
).length > 0
: this.currentTargets.filter(
target => target.saved.success === null
).length > 0;
}
}

View file

@ -1,37 +0,0 @@
export default class DHApplyEffect extends foundry.abstract.TypeDataModel {
static defineSchema() {
const fields = foundry.data.fields;
return {
title: new fields.StringField(),
targets: new fields.ArrayField(
new fields.SchemaField({
id: new fields.StringField({ required: true }),
name: new fields.StringField(),
img: new fields.StringField(),
hit: new fields.BooleanField({ initial: false })
})
),
targetSelection: new fields.BooleanField({ initial: true }),
source: new fields.SchemaField({
actor: new fields.StringField(),
item: new fields.StringField(),
action: new fields.StringField()
})
};
}
prepareDerivedData() {
this.hasHitTarget = this.targets.filter(t => t.hit === true).length > 0;
this.currentTargets =
this.targetSelection !== true
? Array.from(game.user.targets).map(t =>
game.system.api.fields.ActionFields.TargetField.formatTarget(t)
)
: this.targets;
}
get messageTemplate() {
return 'systems/daggerheart/templates/ui/chat/apply-effects.hbs';
}
}

View file

@ -26,7 +26,6 @@ export default class CostField extends fields.ArrayField {
}
static calcCosts(costs) {
// console.log(costs, CostField.getResources.call(this, costs));
const resources = CostField.getResources.call(this, costs);
return costs.map(c => {
c.scale = c.scale ?? 1;

View file

@ -57,7 +57,11 @@ export default class TargetField extends fields.SchemaField {
name: actor.actor.name,
img: actor.actor.img,
difficulty: actor.actor.system.difficulty,
evasion: actor.actor.system.evasion
evasion: actor.actor.system.evasion,
saved: {
value: null,
success: null
}
};
}
}