#57 Damage auf tokens

Changes:
 + Fix um Heilung anzurechnen
 + Finde token über Canvas Selected
 + möglich schaden auf mehrere token zeitlgiehc anzurechnen.
This commit is contained in:
Byroks 2024-02-21 12:23:53 +01:00
parent c2aa466ce1
commit cf90a238f7
2 changed files with 42 additions and 48 deletions

View File

@ -92,8 +92,8 @@ Hooks.once("init", async () => {
Handlebars.registerHelper("count", (object: any) => { Handlebars.registerHelper("count", (object: any) => {
var length = 0; var length = 0;
for( var key in object ) { for (var key in object) {
if( object.hasOwnProperty(key) ) { if (object.hasOwnProperty(key)) {
++length; ++length;
} }
} }
@ -124,12 +124,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) {
name: "LP & AP Schaden", name: "LP & AP Schaden",
icon: '<i class="fas fa-tint"></i>', icon: '<i class="fas fa-tint"></i>',
condition: (li) => { condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message const damageRolls = li.find(".damage").length;
const damageRolls = li.find(".apply-damage").length;
// All must be true to show the reroll dialogue // 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 || game["canvas"].tokens.controlled) && damageRolls > 0;
return game["user"].character && damageRolls > 0;
}, },
callback: (li) => applyDamage(li, 2), callback: (li) => applyDamage(li, 2),
}, },
@ -137,12 +135,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) {
name: "AP Schaden", name: "AP Schaden",
icon: '<i class="fas fa-shield-alt"></i>', icon: '<i class="fas fa-shield-alt"></i>',
condition: (li) => { condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message const damageRolls = li.find(".damage").length;
const damageRolls = li.find(".apply-damage").length;
// All must be true to show the reroll dialogue // 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 || game["canvas"].tokens.controlled) && damageRolls > 0;
return game["user"].character && damageRolls > 0;
}, },
callback: (li) => applyDamage(li, 1), callback: (li) => applyDamage(li, 1),
}, },
@ -150,12 +146,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) {
name: "LP & AP Heilen", name: "LP & AP Heilen",
icon: '<i class="fas fa-heart"></i>', icon: '<i class="fas fa-heart"></i>',
condition: (li) => { condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message const damageRolls = li.find(".heal").length;
const damageRolls = li.find(".apply-damage").length;
// All must be true to show the reroll dialogue // 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 || game["canvas"].tokens.controlled) && damageRolls > 0;
return game["user"].character && damageRolls > 0;
}, },
callback: (li) => applyDamage(li, -1), callback: (li) => applyDamage(li, -1),
}, },
@ -163,12 +157,10 @@ Hooks.on("getChatLogEntryContext", function (html, options) {
name: "AP Heilen", name: "AP Heilen",
icon: '<i class="far fa-heart"></i>', icon: '<i class="far fa-heart"></i>',
condition: (li) => { condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message const damageRolls = li.find(".heal").length;
const damageRolls = li.find(".apply-damage").length;
// All must be true to show the reroll dialogue // 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 || game["canvas"].tokens.controlled) && damageRolls > 0;
return game["user"].character && damageRolls > 0;
}, },
callback: (li) => applyDamage(li, -2), 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) => { Hooks.on("renderCombatTracker", (combatTracker, html, context) => {
if (context.combat === null) { if (context.combat === null) {
html.find("h3.encounter-title")[0].innerHTML = game["i18n"].localize("midgard5.no-encounter"); html.find("h3.encounter-title")[0].innerHTML = game["i18n"].localize("midgard5.no-encounter");
@ -244,15 +227,16 @@ Hooks.once("ready", () => {
}); });
async function applyDamage(roll, direction) { 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))) .map((x) => Math.max(0, Number(x.innerText)))
.reduce((prev, curr) => prev + curr, 0); .reduce((prev, curr) => prev + curr, 0);
const userId = game["user"].character.id; const controlledTokens = game["canvas"].tokens.controlled;
const viewedSceneId = game["user"].viewedScene;
const token = game["actors"].get(userId).getDependentTokens(viewedSceneId)[0]?.delta.syntheticActor;
const actor = game["user"].character; const actor = game["user"].character;
if (token) { if (controlledTokens) {
controlledTokens.forEach((controlledToken) => {
let token = controlledToken.document.delta.syntheticActor;
switch (direction) { switch (direction) {
case 2: case 2:
token["system"].lp.value -= Math.max(0, damageValue - token["system"].calc.stats.lpProtection.value); token["system"].lp.value -= Math.max(0, damageValue - token["system"].calc.stats.lpProtection.value);
@ -265,6 +249,7 @@ async function applyDamage(roll, direction) {
token["system"].ap.value += limitHeal(damageValue, token["system"].ap.value, token["system"].ap.max); token["system"].ap.value += limitHeal(damageValue, token["system"].ap.value, token["system"].ap.max);
} }
token.render(); token.render();
});
} else { } else {
switch (direction) { switch (direction) {
case 2: 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); actor["system"].ap.value -= Math.max(0, damageValue - actor["system"].calc.stats.apProtection.value);
break; break;
case -1: 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: 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(); 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;
}

View File

@ -82,7 +82,7 @@
<tr class="roll-row {{roll.css}}"> <tr class="roll-row {{roll.css}}">
<td>{{roll.label}}</td> <td>{{roll.label}}</td>
<td class="roll-result"> <td class="roll-result">
<span class="roll-total {{#if (contains roll.label "Schaden")}} apply-damage{{/if}}">{{roll.totalStr}}</span> <span class="roll-total {{#if (contains roll.label "Schaden")}} damage{{/if}}{{#if (contains roll.label "Heilung")}} heal{{/if}}">{{roll.totalStr}}</span>
<span class="roll-detail">{{roll.result}}</span> <span class="roll-detail">{{roll.result}}</span>
</td> </td>
</tr> </tr>