daggerheart/module/systemRegistration/socket.mjs
Dapoolp 9e771059f1 g
2025-07-27 12:39:47 +02:00

115 lines
4 KiB
JavaScript

import DamageReductionDialog from '../applications/dialogs/damageReductionDialog.mjs';
import ReactionRollDialog from '../applications/dialogs/reactionRollDialog.mjs';
export function handleSocketEvent({ action = null, data = {} } = {}) {
switch (action) {
case socketEvent.GMUpdate:
Hooks.callAll(socketEvent.GMUpdate, data);
break;
case socketEvent.DhpFearUpdate:
Hooks.callAll(socketEvent.DhpFearUpdate);
break;
case socketEvent.Refresh:
Hooks.call(socketEvent.Refresh, data);
break;
}
}
export const socketEvent = {
GMUpdate: 'DhGMUpdate',
Refresh: 'DhRefresh',
DhpFearUpdate: 'DhFearUpdate'
};
export const GMUpdateEvent = {
UpdateDocument: 'DhGMUpdateDocument',
UpdateSetting: 'DhGMUpdateSetting',
UpdateFear: 'DhGMUpdateFear'
};
export const RefreshType = {
Countdown: 'DhCoundownRefresh'
};
export const registerSocketHooks = () => {
Hooks.on(socketEvent.GMUpdate, async data => {
if (game.user.isGM) {
const document = data.uuid ? await fromUuid(data.uuid) : null;
switch (data.action) {
case GMUpdateEvent.UpdateDocument:
if (document && data.update) {
await document.update(data.update);
}
break;
case GMUpdateEvent.UpdateSetting:
await game.settings.set(CONFIG.DH.id, data.uuid, data.update);
break;
case GMUpdateEvent.UpdateFear:
await game.settings.set(
CONFIG.DH.id,
CONFIG.DH.SETTINGS.gameSettings.Resources.Fear,
Math.max(
0,
Math.min(
game.settings.get(CONFIG.DH.id, CONFIG.DH.SETTINGS.gameSettings.Homebrew).maxFear,
data.update
)
)
);
/* Hooks.callAll(socketEvent.DhpFearUpdate);
await game.socket.emit(`system.${CONFIG.DH.id}`, { action: socketEvent.DhpFearUpdate }); */
break;
}
if (data.refresh) {
await game.socket.emit(`system.${CONFIG.DH.id}`, {
action: socketEvent.Refresh,
data: data.refresh
});
Hooks.call(socketEvent.Refresh, data.refresh);
}
}
});
};
export const registerUserQueries = () => {
CONFIG.queries.armorStack = DamageReductionDialog.armorStackQuery;
// CONFIG.queries.reactionRoll = ReactionRollDialog.reactionRollQuery;
CONFIG.queries.reactionRoll = ({ actionId, actorId, event, message }) => {
// console.log('reactionRoll')
return new Promise(async (resolve, reject) => {
// resolve()
const actor = await fromUuid(actorId),
action = await fromUuid(actionId);
if (!actor || !actor?.isOwner) reject();
action.rollSave(actor, event, message).then(result => resolve(result));
// new ReactionRollDialog(resolve, reject, actor, trait).render({ force: true });
});
}
}
export const emitAsGM = async (eventName, callback, update, uuid = null) => {
if (!game.user.isGM) {
return await game.socket.emit(`system.${CONFIG.DH.id}`, {
action: socketEvent.GMUpdate,
data: {
action: eventName,
uuid,
update
}
});
} else return callback(update);
};
export const emitAsOwner = (eventName, userId, args) => {
if (userId === game.user.id) return;
if (!eventName || !userId) return false;
game.socket.emit(`system.${CONFIG.DH.id}`, {
action: eventName,
data: {
userId,
...args
}
});
return false;
};