Add Rightclick menu to apply damage/heal

Changes:
 + add rightclick context menu for rolls
 + only appears if keyword Schaden exists
 + calculate damage from all rolls with keyword Schaden
 + possible to apply damage or heal actor or if present token

ToDo:
 + add button that does the same
This commit is contained in:
Byroks 2023-12-03 20:56:13 +01:00
parent 46cac30dcd
commit 13a67c5b4a
2 changed files with 99 additions and 1 deletions

View File

@ -86,6 +86,10 @@ Hooks.once("init", async () => {
return param.replace(regex, ""); return param.replace(regex, "");
}); });
Handlebars.registerHelper("contains", (label: string, contains: string) => {
return label.toLowerCase().includes(contains.toLowerCase());
});
// Default Sheet für Items definieren und das Standardsheet deaktivieren // Default Sheet für Items definieren und das Standardsheet deaktivieren
Items.unregisterSheet("core", ItemSheet); Items.unregisterSheet("core", ItemSheet);
Items.registerSheet("midgard5", M5ItemSheet, { makeDefault: true }); Items.registerSheet("midgard5", M5ItemSheet, { makeDefault: true });
@ -105,6 +109,100 @@ Hooks.once("setup", () => {
Logger.log("Template module is being setup."); Logger.log("Template module is being setup.");
}); });
Hooks.on("getChatLogEntryContext", function (html, options) {
options.push(
{
name: "LP & AP Schaden",
icon: '<i class="fas fa-tint"></i>',
condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message
const damageRolls = li.find(".apply-damage").length;
// All must be true to show the reroll dialogue
return damageRolls > 0;
},
callback: (li) => applyDamage(li, 2),
},
{
name: "AP Schaden",
icon: '<i class="fas fa-tint"></i>',
condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message
const damageRolls = li.find(".apply-damage").length;
// All must be true to show the reroll dialogue
return damageRolls > 0;
},
callback: (li) => applyDamage(li, 1),
},
{
name: "LP & AP Heilen",
icon: '<i class="fas fa-heart"></i>',
condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message
const damageRolls = li.find(".apply-damage").length;
// All must be true to show the reroll dialogue
return damageRolls > 0;
},
callback: (li) => applyDamage(li, -1),
},
{
name: "AP Schaden",
icon: '<i class="fas fa-heart-o"></i>',
condition: (li) => {
// Only show this context menu if there are re-rollable dice in the message
const damageRolls = li.find(".apply-damage").length;
// All must be true to show the reroll dialogue
return damageRolls > 0;
},
callback: (li) => applyDamage(li, -2),
}
);
});
Hooks.once("ready", () => { Hooks.once("ready", () => {
Logger.ok("Template module is now ready."); Logger.ok("Template module is now ready.");
}); });
async function applyDamage(roll, direction) {
const damageValue = Array.from(roll.find(".apply-damage") as HTMLElement[])
.map((x) => Number(x.innerText))
.reduce((prev, curr) => prev + curr, 0);
console.log(damageValue);
const userId = game["user"].character.id;
const viewedSceneId = game["user"].viewedScene;
const token = game["actors"].get(userId).getDependentTokens(viewedSceneId)[0]?.delta.syntheticActor;
const actor = game["user"].character;
console.log(token);
console.log(actor);
if (token) {
switch (direction) {
case 2:
token["system"].lp.value -= damageValue;
case 1:
token["system"].ap.value -= damageValue;
break;
case -1:
token.delta.system.lp += damageValue;
case -2:
token.delta.system.ap += damageValue;
}
} else {
switch (direction) {
case 2:
actor["system"].lp.value -= damageValue;
case 1:
actor["system"].ap.value -= damageValue;
break;
case -1:
actor["system"].lp.value += damageValue;
case -2:
actor["system"].ap.value += damageValue;
}
}
actor.render();
}

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">{{roll.totalStr}}</span> <span class="roll-total {{#if (contains roll.label "Schaden")}} apply-damage{{/if}}">{{roll.totalStr}}</span>
<span class="roll-detail">{{roll.result}}</span> <span class="roll-detail">{{roll.result}}</span>
</td> </td>
</tr> </tr>