From cf90a238f712381c518c0281c879732cb006488d Mon Sep 17 00:00:00 2001 From: Byroks Date: Wed, 21 Feb 2024 12:23:53 +0100 Subject: [PATCH] #57 Damage auf tokens MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changes: + Fix um Heilung anzurechnen + Finde token über Canvas Selected + möglich schaden auf mehrere token zeitlgiehc anzurechnen. --- source/index.ts | 88 ++++++++++++++++++-------------------- templates/chat/roll-m5.hbs | 2 +- 2 files changed, 42 insertions(+), 48 deletions(-) diff --git a/source/index.ts b/source/index.ts index f00d480..e11f94a 100644 --- a/source/index.ts +++ b/source/index.ts @@ -92,14 +92,14 @@ Hooks.once("init", async () => { Handlebars.registerHelper("count", (object: any) => { var length = 0; - for( var key in object ) { - if( object.hasOwnProperty(key) ) { + for (var key in object) { + if (object.hasOwnProperty(key)) { ++length; } } return length; }); - + // Default Sheet für Items definieren und das Standardsheet deaktivieren Items.unregisterSheet("core", ItemSheet); Items.registerSheet("midgard5", M5ItemSheet, { makeDefault: true }); @@ -124,12 +124,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) { name: "LP & AP Schaden", icon: '', condition: (li) => { - // Only show this context menu if there are re-rollable dice in the message - const damageRolls = li.find(".apply-damage").length; + const damageRolls = li.find(".damage").length; // All must be true to show the reroll dialogue - // The button doesn't work for the GM right now, so we don't need to show it - return game["user"].character && damageRolls > 0; + return (game["user"].character || game["canvas"].tokens.controlled) && damageRolls > 0; }, callback: (li) => applyDamage(li, 2), }, @@ -137,12 +135,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) { name: "AP Schaden", icon: '', condition: (li) => { - // Only show this context menu if there are re-rollable dice in the message - const damageRolls = li.find(".apply-damage").length; + const damageRolls = li.find(".damage").length; // All must be true to show the reroll dialogue - // The button doesn't work for the GM right now, so we don't need to show it - return game["user"].character && damageRolls > 0; + return (game["user"].character || game["canvas"].tokens.controlled) && damageRolls > 0; }, callback: (li) => applyDamage(li, 1), }, @@ -150,12 +146,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) { name: "LP & AP Heilen", icon: '', condition: (li) => { - // Only show this context menu if there are re-rollable dice in the message - const damageRolls = li.find(".apply-damage").length; + const damageRolls = li.find(".heal").length; // All must be true to show the reroll dialogue - // The button doesn't work for the GM right now, so we don't need to show it - return game["user"].character && damageRolls > 0; + return (game["user"].character || game["canvas"].tokens.controlled) && damageRolls > 0; }, callback: (li) => applyDamage(li, -1), }, @@ -163,12 +157,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) { name: "AP Heilen", icon: '', condition: (li) => { - // Only show this context menu if there are re-rollable dice in the message - const damageRolls = li.find(".apply-damage").length; + const damageRolls = li.find(".heal").length; // All must be true to show the reroll dialogue - // The button doesn't work for the GM right now, so we don't need to show it - return game["user"].character && damageRolls > 0; + return (game["user"].character || game["canvas"].tokens.controlled) && damageRolls > 0; }, callback: (li) => applyDamage(li, -2), } @@ -219,15 +211,6 @@ Hooks.on("updateCombat", function (combat: Combat, updateData: { round: number; } }); -function limitHeal(heal: number, current: number, max: number): number { - if (current === max) { - return 0; - } else if (heal + current > max) { - return max - current; - } - return heal; -} - Hooks.on("renderCombatTracker", (combatTracker, html, context) => { if (context.combat === null) { html.find("h3.encounter-title")[0].innerHTML = game["i18n"].localize("midgard5.no-encounter"); @@ -244,27 +227,29 @@ Hooks.once("ready", () => { }); async function applyDamage(roll, direction) { - const damageValue = Array.from(roll.find(".apply-damage") as HTMLElement[]) + console.log(roll, direction); + const damageValue = Array.from(roll.find(".apply") as HTMLElement[]) .map((x) => Math.max(0, Number(x.innerText))) .reduce((prev, curr) => prev + curr, 0); - const userId = game["user"].character.id; - const viewedSceneId = game["user"].viewedScene; - const token = game["actors"].get(userId).getDependentTokens(viewedSceneId)[0]?.delta.syntheticActor; + const controlledTokens = game["canvas"].tokens.controlled; const actor = game["user"].character; - if (token) { - switch (direction) { - case 2: - token["system"].lp.value -= Math.max(0, damageValue - token["system"].calc.stats.lpProtection.value); - case 1: - token["system"].ap.value -= Math.max(0, damageValue - token["system"].calc.stats.apProtection.value); - break; - case -1: - token["system"].lp.value += limitHeal(damageValue, token["system"].lp.value, token["system"].lp.max); - case -2: - token["system"].ap.value += limitHeal(damageValue, token["system"].ap.value, token["system"].ap.max); - } - token.render(); + if (controlledTokens) { + controlledTokens.forEach((controlledToken) => { + let token = controlledToken.document.delta.syntheticActor; + switch (direction) { + case 2: + token["system"].lp.value -= Math.max(0, damageValue - token["system"].calc.stats.lpProtection.value); + case 1: + token["system"].ap.value -= Math.max(0, damageValue - token["system"].calc.stats.apProtection.value); + break; + case -1: + token["system"].lp.value += limitHeal(damageValue, token["system"].lp.value, token["system"].lp.max); + case -2: + token["system"].ap.value += limitHeal(damageValue, token["system"].ap.value, token["system"].ap.max); + } + token.render(); + }); } else { switch (direction) { case 2: @@ -273,10 +258,19 @@ async function applyDamage(roll, direction) { actor["system"].ap.value -= Math.max(0, damageValue - actor["system"].calc.stats.apProtection.value); break; case -1: - actor["system"].lp.value += limitHeal(damageValue, token["system"].lp.value, token["system"].lp.max); + actor["system"].lp.value += limitHeal(damageValue, actor["system"].lp.value, actor["system"].lp.max); case -2: - actor["system"].ap.value += limitHeal(damageValue, token["system"].ap.value, token["system"].ap.max); + actor["system"].ap.value += limitHeal(damageValue, actor["system"].ap.value, actor["system"].ap.max); } actor.render(); } } + +function limitHeal(heal: number, current: number, max: number): number { + if (current === max) { + return 0; + } else if (heal + current > max) { + return max - current; + } + return heal; +} diff --git a/templates/chat/roll-m5.hbs b/templates/chat/roll-m5.hbs index 856e72b..0a10a2c 100644 --- a/templates/chat/roll-m5.hbs +++ b/templates/chat/roll-m5.hbs @@ -82,7 +82,7 @@ {{roll.label}} - {{roll.totalStr}} + {{roll.totalStr}} {{roll.result}} -- 2.40.1