diff --git a/src/Chat/TrefferpunkteTargets.hbs b/src/Chat/TrefferpunkteTargets.hbs new file mode 100644 index 0000000..4b51c0e --- /dev/null +++ b/src/Chat/TrefferpunkteTargets.hbs @@ -0,0 +1,11 @@ +
+
{{localize "DSA41.chat.targets"}}
+ + {{#each this}} +
+ {{name}} + {{name}} + +
+ {{/each}} +
\ No newline at end of file diff --git a/src/lang/de.json b/src/lang/de.json index 4b641ed..642dcd8 100644 --- a/src/lang/de.json +++ b/src/lang/de.json @@ -35,7 +35,10 @@ "value": "Wert", "roll": "Wurf", - "talentwert_short": "TaW" + "talentwert_short": "TaW", + + "targets": "Ziele", + "trefferpunkte_apply": "Zuweisen" }, "basiswerte": { diff --git a/src/main.css b/src/main.css index 522a8e9..80dcbf3 100644 --- a/src/main.css +++ b/src/main.css @@ -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 { display: inline-grid; diff --git a/src/main.mjs b/src/main.mjs index e344d7e..13ac50a 100644 --- a/src/main.mjs +++ b/src/main.mjs @@ -82,11 +82,33 @@ Hooks.once("init", async function() { "fernkampf_attacke_tooltip": "systems/dsa-4th-edition/src/Tooltips/FernkampfAttacke.hbs", "fernkampf_trefferpunkte_tooltip": "systems/dsa-4th-edition/src/Tooltips/FernkampfTrefferpunkte.hbs", - "CHAT_HEADER": "systems/dsa-4th-edition/src/Chat/Header.hbs", - "talent_chat": "systems/dsa-4th-edition/src/Chat/Talent.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", }); }); +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 { get actor() { if (this.speaker.scene && this.speaker.token) { @@ -100,17 +122,39 @@ class DSA41_ChatMessage extends ChatMessage { async getHTML() { const html = (await super.getHTML())[0]; + if (!html) return; - const img = this.actor?.img ?? this.author.avatar; - const name = this.alias; + const img = this.actor?.img ?? this.author.avatar; + 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 header_template = document.createElement("template"); - header_template.innerHTML = header_html; - const header = header_template.content.children; + const sender = html.querySelector(".message-sender"); + sender?.replaceChildren(header); - const sender = html?.querySelector(".message-sender"); - sender?.replaceChildren(...header); + if (this.flags.type === "trefferpunkte" && this.flags.targets.length != 0) { + 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; } @@ -794,6 +838,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) { roll.toMessage({ speaker: ChatMessage.getSpeaker({ actor: this.document }), flavor: flavor, + flags: { + type: roll_type, + targets: get_targeted_actors(), + }, }); return; @@ -862,6 +910,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) { roll.toMessage({ speaker: ChatMessage.getSpeaker({ actor: this.document }), flavor: flavor, + flags: { + type: roll_type, + targets: get_targeted_actors(), + }, }); return; @@ -885,6 +937,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) { roll.toMessage({ speaker: ChatMessage.getSpeaker({ actor: this.document }), flavor: flavor, + flags: { + type: roll_type, + targets: get_targeted_actors(), + }, }); return; @@ -903,6 +959,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) { roll.toMessage({ speaker: ChatMessage.getSpeaker({ actor: this.document }), flavor: flavor, + flags: { + type: roll_type, + targets: get_targeted_actors(), + }, }); return; @@ -921,6 +981,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) { roll.toMessage({ speaker: ChatMessage.getSpeaker({ actor: this.document }), flavor: game.i18n.localize("DSA41.roll_types." + roll_type), + flags: { + type: roll_type, + targets: get_targeted_actors(), + }, }); return; @@ -939,6 +1003,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) { roll.toMessage({ speaker: ChatMessage.getSpeaker({ actor: this.document }), flavor: game.i18n.localize("DSA41.roll_types." + roll_type), + flags: { + type: roll_type, + targets: get_targeted_actors(), + }, }); return; @@ -948,6 +1016,10 @@ class DSA41_ActorSheet extends DSA41_ApplicationMixin(ActorSheetV2) { roll.toMessage({ speaker: ChatMessage.getSpeaker({ actor: this.document }), flavor: flavor, + flags: { + type: roll_type, + targets: get_targeted_actors(), + }, }); },