foundryvtt-reve-de-dragon/module/chat-utility.js

182 lines
5.5 KiB
JavaScript
Raw Normal View History

import { Misc } from "./misc.js";
import { SYSTEM_RDD, SYSTEM_SOCKET_ID } from "./constants.js";
export const MESSAGE_DATA = 'message-data';
/**
* Class providing helper methods to get the list of users, and
*/
export class ChatUtility {
2021-01-20 00:44:19 +01:00
/* -------------------------------------------- */
2021-01-20 00:44:19 +01:00
static onSocketMessage(sockmsg) {
switch (sockmsg.msg) {
case "msg_delete_chat_message": return ChatUtility.onRemoveMessages(sockmsg.data);
case "msg_user_ui_notifications": return ChatUtility.onNotifyUser(sockmsg.data);
}
}
/* -------------------------------------------- */
static notifyUser(userId, level = 'info', message) {
const data = {
userId: userId, level: level, message: message
};
if (game.user.id == userId) {
ChatUtility.onNotifyUser(data);
}
else {
2022-01-29 22:49:34 +01:00
game.socket.emit(SYSTEM_SOCKET_ID, {
msg: "msg_user_ui_notifications", data: data
});
}
}
static onNotifyUser(data) {
if (game.user.id == data.userId) {
switch (data.level) {
case 'warn': ui.notifications.warn(data.message); break;
case 'error': ui.notifications.error(data.message); break;
default: ui.notifications.info(data.message); break;
}
2021-01-20 00:44:19 +01:00
}
}
/* -------------------------------------------- */
static onRemoveMessages(data) {
if (Misc.isUniqueConnectedGM()) {
if (data.part) {
2022-06-12 08:17:59 +02:00
const toDelete = game.messages.filter(it => it.data.content.includes(data.part));
toDelete.forEach(it => it.delete());
}
if (data.messageId) {
game.messages.get(data.messageId)?.delete();
}
}
}
static onRemoveMessages(data) {
if (Misc.isUniqueConnectedGM()) {
if (data.part) {
const toDelete = game.messages.filter(it => it.content.includes(data.part));
toDelete.forEach(it => it.delete());
}
if (data.messageId) {
2022-06-12 08:17:59 +02:00
game.messages.get(data.messageId)?.delete();
}
}
}
/* -------------------------------------------- */
2022-06-12 08:17:59 +02:00
static removeMessages(data) {
if (Misc.isUniqueConnectedGM()) {
ChatUtility.onRemoveMessages(data);
}
else {
2022-01-29 22:49:34 +01:00
game.socket.emit(SYSTEM_SOCKET_ID, { msg: "msg_delete_chat_message", data: data });
}
}
/* -------------------------------------------- */
2021-01-20 00:44:19 +01:00
static removeChatMessageContaining(part) {
ChatUtility.removeMessages({ part: part });
}
static removeChatMessageId(messageId) {
if (messageId){
ChatUtility.removeMessages({ messageId: messageId });
}
}
/* -------------------------------------------- */
static async createChatWithRollMode(name, chatOptions) {
2022-06-12 08:17:59 +02:00
return await ChatUtility.createChatMessage(name, game.settings.get("core", "rollMode"), chatOptions);
}
2020-12-11 08:29:24 +01:00
/* -------------------------------------------- */
static async createChatMessage(name, rollMode, chatOptions) {
switch (rollMode) {
case "blindroll": // GM only
if (!game.user.isGM) {
ChatUtility.blindMessageToGM(chatOptions);
2021-03-25 03:18:27 +01:00
chatOptions.whisper = [game.user.id];
chatOptions.content = "Message envoyé en aveugle au Gardien";
}
else {
chatOptions.whisper = ChatUtility.getUsers(user => user.isGM);
}
break;
default:
chatOptions.whisper = ChatUtility.getWhisperRecipients(rollMode, name);
break;
}
chatOptions.alias = chatOptions.alias || name;
return await ChatMessage.create(chatOptions);
}
2020-12-11 08:29:24 +01:00
/* -------------------------------------------- */
static prepareChatMessage(rollMode, name) {
return {
2021-03-25 03:18:27 +01:00
user: game.user.id,
whisper: ChatUtility.getWhisperRecipients(rollMode, name)
}
}
2020-12-11 08:29:24 +01:00
/* -------------------------------------------- */
static getWhisperRecipients(rollMode, name) {
switch (rollMode) {
case "blindroll": return ChatUtility.getUsers(user => user.isGM);
case "gmroll": return ChatUtility.getWhisperRecipientsAndGMs(name);
2021-03-25 03:18:27 +01:00
case "selfroll": return [game.user.id];
}
return undefined;
}
2020-12-11 08:29:24 +01:00
/* -------------------------------------------- */
static getWhisperRecipientsAndGMs(name) {
2021-09-30 21:03:43 +02:00
let recep1 = ChatMessage.getWhisperRecipients(name) || [];
return recep1.concat(ChatMessage.getWhisperRecipients('GM'));
}
2020-12-11 08:29:24 +01:00
/* -------------------------------------------- */
static getUsers(filter) {
return Misc.getUsers().filter(filter).map(user => user.id);
}
2020-12-11 08:29:24 +01:00
/* -------------------------------------------- */
static blindMessageToGM(chatOptions) {
let chatGM = duplicate(chatOptions);
chatGM.whisper = ChatUtility.getUsers(user => user.isGM);
chatGM.content = "Message aveugle de " + game.user.name + "<br>" + chatOptions.content;
console.log("blindMessageToGM", chatGM);
2022-01-29 22:49:34 +01:00
game.socket.emit(SYSTEM_SOCKET_ID, { msg: "msg_gm_chat_message", data: chatGM });
}
2020-12-11 08:29:24 +01:00
/* -------------------------------------------- */
static handleGMChatMessage(data) {
console.log("blindMessageToGM", data);
if (game.user.isGM) { // message privé pour GM only
2021-03-25 03:18:27 +01:00
data.user = game.user.id;
ChatMessage.create(data);
}
}
static async setMessageData(chatMessage, key, data) {
if (data) {
await chatMessage.setFlag(SYSTEM_RDD, key, JSON.stringify(data));
}
}
static getMessageData(chatMessage, key) {
const json = chatMessage.getFlag(SYSTEM_RDD, key);
return json ? JSON.parse(json) : undefined;
}
static getChatMessage(event) {
const chatMessageId = $(event.currentTarget).closest('.chat-message').attr('data-message-id');
return game.messages.get(chatMessageId);
}
}