2021-04-15 01:14:24 +02:00
|
|
|
import { Misc } from "./misc.js";
|
2022-01-29 23:33:31 +01:00
|
|
|
import { SYSTEM_RDD, SYSTEM_SOCKET_ID } from "./constants.js";
|
|
|
|
|
2020-11-24 15:20:05 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class providing helper methods to get the list of users, and
|
|
|
|
*/
|
|
|
|
export class ChatUtility {
|
2021-01-20 00:44:19 +01:00
|
|
|
|
2021-02-10 11:24:14 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-20 00:44:19 +01:00
|
|
|
static onSocketMessage(sockmsg) {
|
|
|
|
switch (sockmsg.msg) {
|
2021-04-15 01:14:24 +02:00
|
|
|
case "msg_delete_chat_message": return ChatUtility.onRemoveMessages(sockmsg.data);
|
2021-10-07 23:52:02 +02:00
|
|
|
case "msg_user_ui_notifications": return ChatUtility.onNotifyUser(sockmsg.data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
|
|
|
static notifyUser(userId, level = 'info', message) {
|
2022-09-07 18:47:56 +02:00
|
|
|
const socketData = {
|
2021-10-07 23:52:02 +02:00
|
|
|
userId: userId, level: level, message: message
|
|
|
|
};
|
|
|
|
if (game.user.id == userId) {
|
2022-09-07 18:47:56 +02:00
|
|
|
ChatUtility.onNotifyUser(socketData);
|
2021-10-07 23:52:02 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-01-29 22:49:34 +01:00
|
|
|
game.socket.emit(SYSTEM_SOCKET_ID, {
|
2022-09-07 18:47:56 +02:00
|
|
|
msg: "msg_user_ui_notifications", data: socketData
|
2021-10-07 23:52:02 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-07 18:47:56 +02:00
|
|
|
static onNotifyUser(socketData) {
|
|
|
|
if (game.user.id == socketData.userId) {
|
|
|
|
switch (socketData.level) {
|
|
|
|
case 'warn': ui.notifications.warn(socketData.message); break;
|
|
|
|
case 'error': ui.notifications.error(socketData.message); break;
|
|
|
|
default: ui.notifications.info(socketData.message); break;
|
2021-10-07 23:52:02 +02:00
|
|
|
}
|
2021-01-20 00:44:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:24:14 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-09-07 18:47:56 +02:00
|
|
|
static onRemoveMessages(socketData) {
|
2021-11-26 00:45:21 +01:00
|
|
|
if (Misc.isUniqueConnectedGM()) {
|
2022-09-07 18:47:56 +02:00
|
|
|
if (socketData.part) {
|
|
|
|
const toDelete = game.messages.filter(it => it.content.includes(socketData.part));
|
2022-06-12 08:17:59 +02:00
|
|
|
toDelete.forEach(it => it.delete());
|
|
|
|
}
|
2022-09-07 18:47:56 +02:00
|
|
|
if (socketData.messageId) {
|
|
|
|
game.messages.get(socketData.messageId)?.delete();
|
2022-06-12 08:17:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-07 23:52:02 +02:00
|
|
|
/* -------------------------------------------- */
|
2022-06-12 08:17:59 +02:00
|
|
|
|
2022-09-07 18:47:56 +02:00
|
|
|
static removeMessages(socketData) {
|
2022-01-29 18:50:37 +01:00
|
|
|
if (Misc.isUniqueConnectedGM()) {
|
2022-09-07 18:47:56 +02:00
|
|
|
ChatUtility.onRemoveMessages(socketData);
|
2021-05-27 01:47:18 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-09-07 18:47:56 +02:00
|
|
|
game.socket.emit(SYSTEM_SOCKET_ID, { msg: "msg_delete_chat_message", data: socketData });
|
2021-05-27 01:47:18 +02:00
|
|
|
}
|
2021-05-27 00:19:31 +02:00
|
|
|
}
|
2021-05-22 02:19:22 +02:00
|
|
|
|
2021-01-09 19:36:19 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-20 00:44:19 +01:00
|
|
|
static removeChatMessageContaining(part) {
|
2021-05-27 00:19:31 +02:00
|
|
|
ChatUtility.removeMessages({ part: part });
|
2021-04-15 01:14:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static removeChatMessageId(messageId) {
|
2021-06-01 00:10:02 +02:00
|
|
|
if (messageId){
|
|
|
|
ChatUtility.removeMessages({ messageId: messageId });
|
|
|
|
}
|
2021-01-07 00:32:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-03 15:40:48 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-01-29 18:50:37 +01:00
|
|
|
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-06 23:31:23 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-01-29 18:50:37 +01:00
|
|
|
static async createChatMessage(name, rollMode, chatOptions) {
|
2020-11-24 15:20:05 +01:00
|
|
|
switch (rollMode) {
|
2020-11-29 18:06:19 +01:00
|
|
|
case "blindroll": // GM only
|
2020-11-24 15:20:05 +01:00
|
|
|
if (!game.user.isGM) {
|
|
|
|
ChatUtility.blindMessageToGM(chatOptions);
|
|
|
|
|
2021-03-25 03:18:27 +01:00
|
|
|
chatOptions.whisper = [game.user.id];
|
2020-11-29 18:06:19 +01:00
|
|
|
chatOptions.content = "Message envoyé en aveugle au Gardien";
|
2020-11-24 15:20:05 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
chatOptions.whisper = ChatUtility.getUsers(user => user.isGM);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
2020-12-06 23:31:23 +01:00
|
|
|
chatOptions.whisper = ChatUtility.getWhisperRecipients(rollMode, name);
|
2020-11-24 15:20:05 +01:00
|
|
|
break;
|
|
|
|
}
|
2021-01-07 00:32:22 +01:00
|
|
|
chatOptions.alias = chatOptions.alias || name;
|
2022-01-29 18:50:37 +01:00
|
|
|
return await ChatMessage.create(chatOptions);
|
2020-11-24 15:20:05 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-07 00:32:22 +01:00
|
|
|
static prepareChatMessage(rollMode, name) {
|
2020-12-06 23:31:23 +01:00
|
|
|
return {
|
2021-03-25 03:18:27 +01:00
|
|
|
user: game.user.id,
|
2020-12-06 23:31:23 +01:00
|
|
|
whisper: ChatUtility.getWhisperRecipients(rollMode, name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-07 00:32:22 +01:00
|
|
|
static getWhisperRecipients(rollMode, name) {
|
2020-12-06 23:31:23 +01:00
|
|
|
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];
|
2020-12-06 23:31:23 +01:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-24 15:20:05 +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-11-24 15:20:05 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-24 15:20:05 +01:00
|
|
|
static getUsers(filter) {
|
2022-10-08 15:38:56 +02:00
|
|
|
return game.users.filter(filter).map(user => user.id);
|
2020-11-24 15:20:05 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-24 15:20:05 +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-11-24 15:20:05 +01:00
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2022-09-07 18:47:56 +02:00
|
|
|
static handleGMChatMessage(socketData) {
|
|
|
|
console.log("blindMessageToGM", socketData);
|
2020-11-24 15:20:05 +01:00
|
|
|
if (game.user.isGM) { // message privé pour GM only
|
2022-09-07 18:47:56 +02:00
|
|
|
socketData.user = game.user.id;
|
|
|
|
ChatMessage.create(socketData);
|
2020-11-24 15:20:05 +01:00
|
|
|
}
|
|
|
|
}
|
2021-04-15 01:14:24 +02:00
|
|
|
|
2022-09-07 18:47:56 +02:00
|
|
|
static async setMessageData(chatMessage, key, flag) {
|
|
|
|
if (flag) {
|
|
|
|
await chatMessage.setFlag(SYSTEM_RDD, key, JSON.stringify(flag));
|
2022-01-29 23:33:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2020-11-24 15:20:05 +01:00
|
|
|
}
|