add a damage application button to Trefferpunkte chat messages

This commit is contained in:
Sven Balzer 2025-02-03 03:53:17 +01:00
parent 43459113d5
commit a3b6271bf3
4 changed files with 116 additions and 11 deletions

View File

@ -0,0 +1,11 @@
<div class="DSA41 chat-targets">
<div class="center">{{localize "DSA41.chat.targets"}}</div>
{{#each this}}
<div class="target" data-actor-id="{{uuid}}">
<img src="{{img}}" alt="{{name}}">
<span>{{name}}</span>
<button data-action="apply_damage">{{localize "DSA41.chat.trefferpunkte_apply"}}</button>
</div>
{{/each}}
</div>

View File

@ -35,7 +35,10 @@
"value": "Wert", "value": "Wert",
"roll": "Wurf", "roll": "Wurf",
"talentwert_short": "TaW" "talentwert_short": "TaW",
"targets": "Ziele",
"trefferpunkte_apply": "Zuweisen"
}, },
"basiswerte": { "basiswerte": {

View File

@ -158,6 +158,25 @@ html {
} }
} }
&.chat-targets {
& img {
width: 20px;
height: 20px;
}
& .target {
display: grid;
grid-template-columns: max-content minmax(0, max-content) auto minmax(min-content, max-content);
gap: 0.5em;
& button {
grid-column: 3;
line-height: normal;
}
}
}
& .die { & .die {
display: inline-grid; display: inline-grid;

View File

@ -83,10 +83,32 @@ Hooks.once("init", async function() {
"fernkampf_trefferpunkte_tooltip": "systems/dsa-4th-edition/src/Tooltips/FernkampfTrefferpunkte.hbs", "fernkampf_trefferpunkte_tooltip": "systems/dsa-4th-edition/src/Tooltips/FernkampfTrefferpunkte.hbs",
"CHAT_HEADER": "systems/dsa-4th-edition/src/Chat/Header.hbs", "CHAT_HEADER": "systems/dsa-4th-edition/src/Chat/Header.hbs",
"TrefferpunkteTargets": "systems/dsa-4th-edition/src/Chat/TrefferpunkteTargets.hbs",
"talent_chat": "systems/dsa-4th-edition/src/Chat/Talent.hbs", "talent_chat": "systems/dsa-4th-edition/src/Chat/Talent.hbs",
}); });
}); });
function get_targeted_actors() {
const targets = [];
for (const token of game.user.targets) {
if (token.actor) {
targets.push(token.actor.uuid);
}
}
return targets;
}
async function instantiateTemplate(template, context) {
const html = await renderTemplate(template, context);
const template_element = document.createElement("template");
template_element.innerHTML = html;
return template_element.content.firstChild;
}
class DSA41_ChatMessage extends ChatMessage { class DSA41_ChatMessage extends ChatMessage {
get actor() { get actor() {
if (this.speaker.scene && this.speaker.token) { if (this.speaker.scene && this.speaker.token) {
@ -100,17 +122,39 @@ class DSA41_ChatMessage extends ChatMessage {
async getHTML() { async getHTML() {
const html = (await super.getHTML())[0]; const html = (await super.getHTML())[0];
if (!html) return;
const img = this.actor?.img ?? this.author.avatar; const img = this.actor?.img ?? this.author.avatar;
const name = this.alias; const name = this.alias;
const header = await instantiateTemplate("CHAT_HEADER", { img: img, name: name, author: this.author?.name ?? "" });
const header_html = await renderTemplate("CHAT_HEADER", { img: img, name: name, author: this.author?.name ?? "" }); const sender = html.querySelector(".message-sender");
const header_template = document.createElement("template"); sender?.replaceChildren(header);
header_template.innerHTML = header_html;
const header = header_template.content.children;
const sender = html?.querySelector(".message-sender"); if (this.flags.type === "trefferpunkte" && this.flags.targets.length != 0) {
sender?.replaceChildren(...header); const targets = this.flags.targets.map(x => fromUuidSync(x, { strict: false })).filter(x => x !== null);
const targets_list = await instantiateTemplate("TrefferpunkteTargets", targets);
html.querySelector(".message-content")?.appendChild(targets_list);
}
for (const element of html.querySelectorAll("[data-action]")) {
element.addEventListener("click", event => {
const target = event.target;
const action = target.dataset.action;
if (action === null) return;
if (action === "apply_damage") {
const target_actor_id = target.closest("[data-actor-id]")?.dataset.actorId;
if (!target_actor_id) return;
const target_actor = fromUuidSync(target_actor_id);
if (!target_actor) return;
target_actor.update({ "system.lebenspunkte.aktuell": target_actor.system.lebenspunkte.aktuell - this.rolls[0].total });
}
});
}
return html; return html;
} }
@ -794,6 +838,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) {
roll.toMessage({ roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.document }), speaker: ChatMessage.getSpeaker({ actor: this.document }),
flavor: flavor, flavor: flavor,
flags: {
type: roll_type,
targets: get_targeted_actors(),
},
}); });
return; return;
@ -862,6 +910,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) {
roll.toMessage({ roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.document }), speaker: ChatMessage.getSpeaker({ actor: this.document }),
flavor: flavor, flavor: flavor,
flags: {
type: roll_type,
targets: get_targeted_actors(),
},
}); });
return; return;
@ -885,6 +937,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) {
roll.toMessage({ roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.document }), speaker: ChatMessage.getSpeaker({ actor: this.document }),
flavor: flavor, flavor: flavor,
flags: {
type: roll_type,
targets: get_targeted_actors(),
},
}); });
return; return;
@ -903,6 +959,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) {
roll.toMessage({ roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.document }), speaker: ChatMessage.getSpeaker({ actor: this.document }),
flavor: flavor, flavor: flavor,
flags: {
type: roll_type,
targets: get_targeted_actors(),
},
}); });
return; return;
@ -921,6 +981,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) {
roll.toMessage({ roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.document }), speaker: ChatMessage.getSpeaker({ actor: this.document }),
flavor: game.i18n.localize("DSA41.roll_types." + roll_type), flavor: game.i18n.localize("DSA41.roll_types." + roll_type),
flags: {
type: roll_type,
targets: get_targeted_actors(),
},
}); });
return; return;
@ -939,6 +1003,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) {
roll.toMessage({ roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.document }), speaker: ChatMessage.getSpeaker({ actor: this.document }),
flavor: game.i18n.localize("DSA41.roll_types." + roll_type), flavor: game.i18n.localize("DSA41.roll_types." + roll_type),
flags: {
type: roll_type,
targets: get_targeted_actors(),
},
}); });
return; return;
@ -948,6 +1016,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) {
roll.toMessage({ roll.toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.document }), speaker: ChatMessage.getSpeaker({ actor: this.document }),
flavor: flavor, flavor: flavor,
flags: {
type: roll_type,
targets: get_targeted_actors(),
},
}); });
}, },