Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply damage to original attack target #368

Merged
merged 7 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
"OSE.Setting.applyDamageOptionHint": "Which token(s) to apply damage to on an attack roll",
"OSE.Setting.damageSelected": "Selected",
"OSE.Setting.damageTarget": "Target",
"OSE.Setting.damageOriginalTarget": "Original Target",
"OSE.Setting.InvertedCtrlBehavior": "Invert Roll Ctrl/Meta-key",
"OSE.Setting.InvertedCtrlBehaviorHint": "Reverses behavior for holding Ctrl/Meta when clicking on a roll.",

Expand Down
57 changes: 40 additions & 17 deletions src/module/chat.ts
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bakbakbakbakbak Just a heads up, I forget if you had tests written for this file or the other stuff in this PR, but this might change things for you.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the notice, I'll take a look at this

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably best to revisit in a later revision of 0.8.x and leave the test as-is until after merging. We're probably expecting a non-zero amount of other surprises after such a large merge

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I am having quite a bit on my plate right now either, and I have noticed that I procrastinate on this check here.

Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
import { OseActor } from "./actor/entity";

function canApplyDamage(html: JQuery) {
if (!html.find('.dice-total').length) return false;
switch (game.settings.get(game.system.id, "applyDamageOption")) {
case CONFIG.OSE.apply_damage_options.originalTarget:
return !!html.find(".chat-target").last().data("id");
case CONFIG.OSE.apply_damage_options.targeted:
return !!game.user?.targets?.size;
case CONFIG.OSE.apply_damage_options.selected:
return !!canvas.tokens?.controlled.length;
default: {
console.log('unknown setting');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should make this more specific, and a UI warning. We shouldn't end up here, but if we do, it'd be helpful if a user could tell us how they got here!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return false;
}
}
}

/**
* This function is used to hook into the Chat Log context menu to add additional options to each message
* These options make it easy to conveniently apply damage to controlled tokens based on the value of a Roll
Expand All @@ -8,8 +24,7 @@ export const addChatMessageContextOptions = function (
_: JQuery,
options: ContextMenuEntry[]
) {
let canApply: ContextMenuEntry["condition"] = (li) =>
!!canvas.tokens?.controlled.length && !!li.find(".dice-roll").length;
let canApply: ContextMenuEntry["condition"] = (li) => canApplyDamage(li) && !!li.find(".dice-roll").length;
options.push(
{
name: game.i18n.localize("OSE.messages.applyDamage"),
Expand Down Expand Up @@ -45,15 +60,15 @@ export const addChatMessageButtons = function (msg: ChatMessage, html: JQuery) {
}
// Buttons
let roll = html.find(".damage-roll");
if (roll.length > 0) {
if (roll.length > 0 && canApplyDamage(html)) {
roll.append(
$(
`<div class="dice-damage"><button type="button" data-action="apply-damage"><i class="fas fa-tint"></i></button></div>`
)
);
roll.find('button[data-action="apply-damage"]').on("click", (ev) => {
ev.preventDefault();
applyChatCardDamage(roll, 1);
applyChatCardDamage(html, 1);
});
}
};
Expand All @@ -62,25 +77,33 @@ export const addChatMessageButtons = function (msg: ChatMessage, html: JQuery) {
* Apply rolled dice damage to the token or tokens which are currently controlled.
* This allows for damage to be scaled by a multiplier to account for healing, critical hits, or resistance
*
* @param {HTMLElement} roll The chat entry which contains the roll data
* @param {HTMLElement} html The chat entry which contains the roll data
* @param {Number} multiplier A damage multiplier to apply to the rolled damage.
* @return {Promise}
*/
function applyChatCardDamage(roll: JQuery, multiplier: 1 | -1) {
const amount = roll.find(".dice-total").last().text();
function applyChatCardDamage(html: JQuery, multiplier: 1 | -1) {
const amount = html.find(".dice-total").last().text();
const dmgTgt = game.settings.get(game.system.id, "applyDamageOption");
if (dmgTgt === `targeted`) {
game.user?.targets.forEach(async (t) => {
if (game.user?.isGM && t.actor instanceof OseActor)
await t.actor.applyDamage(amount, multiplier);
});
if (dmgTgt === CONFIG.OSE.apply_damage_options.originalTarget) {
const victimId = html.find(".chat-target").last().data("id");
(async () => {
const actor = ((await fromUuid(victimId || '')) as TokenDocument)?.actor;
await applyDamageToTarget(actor, amount, multiplier, actor?.name || victimId || 'original target');
})();
}
if (dmgTgt === `selected`) {
canvas.tokens?.controlled.forEach(async (t) => {
if (game.user?.isGM && t.actor instanceof OseActor)
await t.actor.applyDamage(amount, multiplier);
});
if (dmgTgt === CONFIG.OSE.apply_damage_options.targeted) {
game.user?.targets.forEach((t) => applyDamageToTarget(t.actor, amount, multiplier, t.name));
}
if (dmgTgt === CONFIG.OSE.apply_damage_options.selected) {
canvas.tokens?.controlled.forEach((t) => applyDamageToTarget(t.actor, amount, multiplier, t.name));
}
}

async function applyDamageToTarget(actor: Actor | null, amount: string, multiplier: 1 | -1, nameOrId: string) {
if (!game.user?.isGM || !(actor instanceof OseActor)) {
ui.notifications?.error(`Can't deal damage to ${nameOrId}`);
return;
}
await actor.applyDamage(amount, multiplier);
}
/* -------------------------------------------- */
8 changes: 7 additions & 1 deletion src/module/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export type OseConfig = {
saves_short: Record<Save, string>;
saves_long: Record<Save, string>;
armor: Record<Armor, string>;
apply_damage_options: Record<ApplyDamageOption, ApplyDamageOption>;
colors: Record<Color, string>;
languages: string[];
auto_tags: {[n: string]: {label: string, icon: string}};
Expand All @@ -41,6 +42,7 @@ export type ExplorationSkill = "ld" | "od" | "sd" | "fs";
export type RollType = "result" | "above" | "below";
export type Save = "death" | "wand" | "paralysis" | "breath" | "spell";
export type Armor = "unarmored" | "light" | "heavy" | "shield";
export type ApplyDamageOption = "selected" | "targeted" | "originalTarget";
export type Color =
| "green"
| "red"
Expand All @@ -59,7 +61,6 @@ export type InventoryItemTag =
| "splash"
| "reload"
| "charge";

export const OSE: OseConfig = {
systemPath(): string {
return `${this.systemRoot}/dist`;
Expand Down Expand Up @@ -133,6 +134,11 @@ export const OSE: OseConfig = {
heavy: "OSE.armor.heavy",
shield: "OSE.armor.shield",
},
apply_damage_options: {
selected : "selected",
targeted : "selected",
originalTarget : "selected",
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this causes an error in the switch statement. It will always be the default case unless selected is the selected system setting

Copy link
Contributor Author

@amir-arad amir-arad Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eeek!
done

colors: {
green: "OSE.colors.green",
red: "OSE.colors.red",
Expand Down
8 changes: 5 additions & 3 deletions src/module/dice.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ export class OseDice {

const targetAc = data.roll.target ? targetActorData.ac.value : 9;
const targetAac = data.roll.target ? targetActorData.aac.value : 10;
result.victim = data.roll.target ? data.roll.target.name : null;
result.victim = data.roll.target || null;

if (game.settings.get(game.system.id, "ascendingAC")) {
if (
Expand Down Expand Up @@ -203,11 +203,13 @@ export class OseDice {
speaker = null,
form = null,
} = {}) {
if (!data.roll.dmg.filter(v => v !== '').length) {
if (!data.roll.dmg.filter((v) => v !== "").length) {
/**
* @todo should this error be localized?
*/
ui.notifications.error('Attack has no damage dice terms; be sure to set the attack\'s damage');
ui.notifications.error(
"Attack has no damage dice terms; be sure to set the attack's damage"
);
return;
}
const template = `${OSE.systemPath()}/templates/chat/roll-attack.html`;
Expand Down
5 changes: 4 additions & 1 deletion src/module/settings.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { ApplyDamageOption } from "./config";

export const registerSettings = function () {
game.settings.register(game.system.id, "initiative", {
name: game.i18n.localize("OSE.Setting.Initiative"),
Expand Down Expand Up @@ -84,6 +86,7 @@ export const registerSettings = function () {
choices: {
selected: "OSE.Setting.damageSelected",
targeted: "OSE.Setting.damageTarget",
originalTarget: "OSE.Setting.damageOriginalTarget",
},
});
game.settings.register(game.system.id, "invertedCtrlBehavior", {
Expand All @@ -107,7 +110,7 @@ declare global {
"ose.encumbranceOption": "disabled" | "basic" | "detailed" | "complete";
"ose.significantTreasure": number;
"ose.languages": string;
"ose.applyDamageOption": "selected" | "targeted";
"ose.applyDamageOption": ApplyDamageOption;
}
}
}
4 changes: 2 additions & 2 deletions src/templates/chat/roll-attack.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ <h2>{{title}}</h2>
{{/if}}
</div>
{{#if result.victim}}
<div class="chat-target">
vs {{result.victim}}
<div class="chat-target" data-id="{{result.victim.document.uuid}}">
vs {{result.victim.name}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this :)

</div>
{{/if}}
<div class="blindable" data-blind="{{data.roll.blindroll}}">
Expand Down