daggerheart/module/documents/chatMessage.mjs
WBHarry 261a3a68b0
[PR] [Feature] Party Sheet (#1230)
* start development

* finish party members tab

* start resources tab

* finish resources tab

* finish inventory tab and add inital template to projects tab

* add resource buttons actions methods

* add group roll dialog

* Main implementation

* Fixed costs

* Minor fixes and tweaks for the party sheet (#1239)

* Minor fixes and tweaks for the party sheet

* Fix scroll restoration for party sheet tabs

* Finished GroupRoll

* Removed/commented-out not yet implemented things

* Commented out Difficulty since it's not used yet

* Re-render party when members update (#1242)

* Fixed so style applies in preview chat message

* Added the clown car

* Fixed so items can be dropped into the Party sheet

* Added delete icon to inventory

* Fixed TokenHUD token property useage. Fixed skipping roll message

* Added visible modifier to GroupRoll leader result

* Leader roll displays the large result display right away after rolling

* Corrected tokenHUD for non-player-tokens

* Fixed clowncar tokenData

* Fixed TagTeam roll message and sound

* Removed final TagTeamRoll roll sound

* [PR] [Party Sheets] Sidebar character sheet changes (#1249)

* Something experimenting

* I am silly (wearning Dunce hat)

* Stressful task

* Armor functional to be hit

* CSS Changes to accomadate pip boy

* last minute change to resource section for better visual feeling

* restoring old css for toggle

* Added setting to toggle pip/number display

* toggle functionality added

* Fixed light-mode in characterSheet

* Fixed multi-row resource pips display for character

* Fixed separators

* Added pip-display to Adversary and Companion. Some fixing on armor display

---------

Co-authored-by: WBHarry <williambjrklund@gmail.com>

* Fixed party height and resource armor update

* Fixed deletebutton padding

* Only showing expand-me icon on InventoryItem if there is a description to show

* .

* Fixed menu icon to be beige instead of white in dark mode

---------

Co-authored-by: moliloo <dev.murilobrito@gmail.com>
Co-authored-by: Carlos Fernandez <CarlosFdez@users.noreply.github.com>
Co-authored-by: Nikhil Nagarajan <potter.nikhil@gmail.com>
2025-11-11 16:02:45 +01:00

275 lines
11 KiB
JavaScript

import { emitAsGM, GMUpdateEvent, RefreshType, socketEvent } from '../systemRegistration/socket.mjs';
export default class DhpChatMessage extends foundry.documents.ChatMessage {
targetHook = null;
async renderHTML() {
const actor = game.actors.get(this.speaker.actor);
const actorData =
actor && this.isContentVisible
? actor
: {
img: this.author.avatar ? this.author.avatar : 'icons/svg/mystery-man.svg',
name: ''
};
/* We can change to fully implementing the renderHTML function if needed, instead of augmenting it. */
const html = await super.renderHTML({ actor: actorData, author: this.author });
if (this.flags.core?.RollTable) {
html.querySelector('.roll-buttons.apply-buttons')?.remove();
}
this.enrichChatMessage(html);
this.addChatListeners(html);
return html;
}
/* -------------------------------------------- */
/** @inheritDoc */
prepareData() {
if (this.isAuthor && this.targetSelection === undefined) this.targetSelection = this.system.targets?.length > 0;
super.prepareData();
}
/* -------------------------------------------- */
/** @inheritDoc */
_onCreate(data, options, userId) {
super._onCreate(data, options, userId);
if (this.system.registerTargetHook) this.system.registerTargetHook();
}
/* -------------------------------------------- */
/** @inheritDoc */
async _preDelete(options, user) {
if (this.targetHook !== null) Hooks.off('targetToken', this.targetHook);
return super._preDelete(options, user);
}
/** @inheritDoc */
_onUpdate(changes, options, userId) {
super._onUpdate(changes, options, userId);
const lastMessage = Array.from(game.messages).sort((a, b) => b.timestamp - a.timestamp)[0];
if (lastMessage.id === this.id && ui.chat.isAtBottom) {
setTimeout(() => {
ui.chat.scrollBottom();
}, 5);
}
}
enrichChatMessage(html) {
const elements = html.querySelectorAll('[data-perm-id]');
elements.forEach(e => {
const uuid = e.dataset.permId,
document = fromUuidSync(uuid);
if (!document) return;
e.setAttribute('data-view-perm', document.testUserPermission(game.user, 'OBSERVER'));
e.setAttribute('data-use-perm', document.testUserPermission(game.user, 'OWNER'));
});
if (this.isContentVisible) {
if (this.type === 'dualityRoll') {
html.classList.add('duality');
switch (this.system.roll?.result?.duality) {
case 1:
html.classList.add('hope');
break;
case -1:
html.classList.add('fear');
break;
default:
html.classList.add('critical');
break;
}
}
const autoExpandRoll = game.settings.get(
CONFIG.DH.id,
CONFIG.DH.SETTINGS.gameSettings.appearance
).expandRollMessage,
rollSections = html.querySelectorAll('.roll-part'),
itemDesc = html.querySelector('.domain-card-move');
rollSections.forEach(s => {
if (s.classList.contains('roll-section')) {
const toExpand = s.querySelector('[data-action="expandRoll"]');
toExpand.classList.toggle('expanded', autoExpandRoll.roll);
} else if (s.classList.contains('damage-section'))
s.classList.toggle('expanded', autoExpandRoll.damage);
else if (s.classList.contains('target-section')) s.classList.toggle('expanded', autoExpandRoll.target);
});
if (itemDesc && autoExpandRoll.desc) itemDesc.setAttribute('open', '');
}
if (!this.isAuthor && !this.speakerActor?.isOwner) {
const applyButtons = html.querySelector('.apply-buttons');
applyButtons?.remove();
const buttons = html.querySelectorAll('.ability-card-footer > .ability-use-button');
buttons.forEach(b => b.remove());
}
}
addChatListeners(html) {
html.querySelectorAll('.duality-action-damage').forEach(element =>
element.addEventListener('click', this.onRollDamage.bind(this))
);
html.querySelectorAll('.damage-button').forEach(element =>
element.addEventListener('click', this.onApplyDamage.bind(this))
);
html.querySelectorAll('.target-save').forEach(element =>
element.addEventListener('click', this.onRollSave.bind(this))
);
html.querySelectorAll('.roll-all-save-button').forEach(element =>
element.addEventListener('click', this.onRollAllSave.bind(this))
);
html.querySelectorAll('.duality-action-effect').forEach(element =>
element.addEventListener('click', this.onApplyEffect.bind(this))
);
html.querySelectorAll('.roll-target').forEach(element => {
element.addEventListener('mouseenter', this.hoverTarget);
element.addEventListener('mouseleave', this.unhoverTarget);
element.addEventListener('click', this.clickTarget);
});
html.querySelectorAll('.button-target-selection').forEach(element => {
element.addEventListener('click', this.onTargetSelection.bind(this));
});
html.querySelectorAll('.token-target-container').forEach(element => {
element.addEventListener('pointerover', this.hoverTarget);
element.addEventListener('pointerout', this.unhoverTarget);
element.addEventListener('click', this.clickTarget);
});
}
async onRollDamage(event) {
event.stopPropagation();
const config = foundry.utils.deepClone(this.system);
config.event = event;
await this.system.action?.workflow.get('damage')?.execute(config, this._id, true);
Hooks.callAll(socketEvent.Refresh, { refreshType: RefreshType.TagTeamRoll });
await game.socket.emit(`system.${CONFIG.DH.id}`, {
action: socketEvent.Refresh,
data: {
refreshType: RefreshType.TagTeamRoll
}
});
}
async onApplyDamage(event) {
event.stopPropagation();
const targets = this.filterPermTargets(this.system.hitTargets),
config = foundry.utils.deepClone(this.system);
config.event = event;
if (this.system.onSave) {
const pendingingSaves = targets.filter(t => t.saved.success === null);
if (pendingingSaves.length) {
const confirm = await foundry.applications.api.DialogV2.confirm({
window: { title: 'Pending Reaction Rolls found' },
content: `<p>Some Tokens still need to roll their Reaction Roll.</p><p>Are you sure you want to continue ?</p><p><i>Undone reaction rolls will be considered as failed</i></p>`
});
if (!confirm) return;
}
}
if (targets.length === 0)
return ui.notifications.info(game.i18n.localize('DAGGERHEART.UI.Notifications.noTargetsSelectedOrPerm'));
this.consumeOnSuccess();
this.system.action?.workflow.get('applyDamage')?.execute(config, targets, true);
}
async onRollSave(event) {
event.stopPropagation();
const tokenId = event.target.closest('[data-token]')?.dataset.token,
token = game.canvas.tokens.get(tokenId);
if (!token?.actor || !token.isOwner) return true;
if (this.system.source.item && this.system.source.action) {
const action = this.system.action;
if (!action || !action?.hasSave) return;
game.system.api.fields.ActionFields.SaveField.rollSave.call(action, token.actor, event).then(result =>
emitAsGM(
GMUpdateEvent.UpdateSaveMessage,
game.system.api.fields.ActionFields.SaveField.updateSaveMessage.bind(
action,
result,
this,
token.id
),
{
action: action.uuid,
message: this._id,
token: token.id,
result
}
)
);
}
}
async onRollAllSave(event) {
event.stopPropagation();
if (!game.user.isGM) return;
const targets = this.system.hitTargets,
config = foundry.utils.deepClone(this.system);
config.event = event;
this.system.action?.workflow.get('save')?.execute(config, targets, true);
}
async onApplyEffect(event) {
event.stopPropagation();
const targets = this.filterPermTargets(this.system.hitTargets),
config = foundry.utils.deepClone(this.system);
config.event = event;
if (targets.length === 0)
ui.notifications.info(game.i18n.localize('DAGGERHEART.UI.Notifications.noTargetsSelectedOrPerm'));
this.consumeOnSuccess();
this.system.action?.workflow.get('effects')?.execute(config, targets, true);
}
filterPermTargets(targets) {
return targets.filter(t => fromUuidSync(t.actorId)?.canUserModify(game.user, 'update'));
}
consumeOnSuccess() {
if (!this.system.successConsumed && !this.targetSelection) this.system.action?.consume(this.system, true);
}
hoverTarget(event) {
event.stopPropagation();
const token = canvas.tokens.get(event.currentTarget.dataset.token);
if (token && !token?.controlled) token._onHoverIn(event, { hoverOutOthers: true });
}
unhoverTarget(event) {
const token = canvas.tokens.get(event.currentTarget.dataset.token);
if (token && !token?.controlled) token._onHoverOut(event);
}
clickTarget(event) {
event.stopPropagation();
const token = canvas.tokens.get(event.currentTarget.dataset.token);
if (!token) {
ui.notifications.info(game.i18n.localize('DAGGERHEART.UI.Notifications.attackTargetDoesNotExist'));
return;
}
game.canvas.pan(token);
}
onTargetSelection(event) {
event.stopPropagation();
if (!event.target.classList.contains('target-selected'))
this.system.targetMode = Boolean(event.target.dataset.targetHit);
}
}