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";
|
2024-05-27 00:37:19 +02:00
|
|
|
import { RdDTimestamp } from "./time/rdd-timestamp.js";
|
2022-01-29 23:33:31 +01:00
|
|
|
|
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
|
|
|
|
2024-10-16 23:18:15 +02:00
|
|
|
static async init() {
|
|
|
|
Hooks.on("renderChatMessage", async (app, html, msg) => await ChatUtility.onRenderChatMessage(app, html, msg))
|
|
|
|
Hooks.on("createChatMessage", async (chatMessage, options, id) => await ChatUtility.onCreateChatMessage(chatMessage, options, id))
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:24:14 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-20 00:44:19 +01:00
|
|
|
static onSocketMessage(sockmsg) {
|
|
|
|
switch (sockmsg.msg) {
|
2024-10-16 23:18:15 +02:00
|
|
|
case "msg_gm_chat_message": return ChatUtility.handleGMChatMessage(sockmsg.data)
|
|
|
|
case "msg_delete_chat_message": return ChatUtility.onRemoveMessages(sockmsg.data)
|
|
|
|
case "msg_user_ui_notifications": return ChatUtility.onNotifyUser(sockmsg.data)
|
2021-10-07 23:52:02 +02:00
|
|
|
}
|
|
|
|
}
|
2023-11-23 16:11:38 +01:00
|
|
|
|
2021-10-07 23:52:02 +02:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
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) {
|
2024-10-23 23:42:38 +02:00
|
|
|
if (Misc.isFirstConnectedGM()) {
|
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) {
|
2024-10-23 23:42:38 +02:00
|
|
|
if (Misc.isFirstConnectedGM()) {
|
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) {
|
2023-11-23 16:11:38 +01:00
|
|
|
if (messageId) {
|
2021-06-01 00:10:02 +02:00
|
|
|
ChatUtility.removeMessages({ messageId: messageId });
|
|
|
|
}
|
2021-01-07 00:32:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-03 15:40:48 +01:00
|
|
|
/* -------------------------------------------- */
|
2024-10-16 23:18:15 +02:00
|
|
|
static async createChatWithRollMode(messageData, actor = undefined) {
|
|
|
|
switch (game.settings.get("core", "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) {
|
2024-10-16 23:18:15 +02:00
|
|
|
ChatUtility.blindMessageToGM(messageData)
|
|
|
|
messageData.whisper = [game.user];
|
|
|
|
messageData.content = "Message envoyé en aveugle au Gardien"
|
2020-11-24 15:20:05 +01:00
|
|
|
}
|
|
|
|
else {
|
2024-10-16 23:18:15 +02:00
|
|
|
messageData.whisper = ChatUtility.getGMs()
|
2020-11-24 15:20:05 +01:00
|
|
|
}
|
2024-10-16 23:18:15 +02:00
|
|
|
break
|
|
|
|
case "gmroll":
|
|
|
|
messageData.whisper = ChatUtility.getOwners(actor)
|
|
|
|
break
|
|
|
|
case "selfroll":
|
|
|
|
messageData.whisper = [game.user]
|
|
|
|
break
|
2020-12-06 23:31:23 +01:00
|
|
|
}
|
2024-10-16 23:18:15 +02:00
|
|
|
messageData.alias = messageData.alias ?? actor?.name ?? game.user.name
|
|
|
|
return await ChatMessage.create(messageData)
|
2020-12-06 23:31:23 +01:00
|
|
|
}
|
|
|
|
|
2024-10-16 23:18:15 +02:00
|
|
|
static getOwners(document) {
|
|
|
|
return game.users.filter(it => document.getUserLevel(it) == CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER)
|
2020-12-06 23:31:23 +01:00
|
|
|
}
|
|
|
|
|
2024-10-16 23:18:15 +02:00
|
|
|
static getUserAndGMs() {
|
|
|
|
return [game.user, ...ChatUtility.getGMs()]
|
2024-10-14 03:47:04 +02:00
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2024-10-16 23:18:15 +02:00
|
|
|
static getMultipleActorsOwners(...actors) {
|
|
|
|
return Misc.concat(actors.map(it => it == undefined ? [] : ChatUtility.getOwners(it)))
|
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) {
|
2024-10-16 23:18:15 +02:00
|
|
|
return game.users.filter(filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
static getGMs() {
|
|
|
|
return game.users.filter(user => user.isGM)
|
|
|
|
}
|
|
|
|
|
|
|
|
static applyRollMode(chatMessageData = {}, rollMode = game.settings.get("core", "rollMode")) {
|
|
|
|
switch (rollMode) {
|
|
|
|
case "blindroll":
|
|
|
|
chatMessageData.blind = true
|
|
|
|
chatMessageData.whisper = ChatUtility.getGMs()
|
|
|
|
break
|
|
|
|
case "gmroll":
|
|
|
|
chatMessageData.whisper = ChatUtility.getGMs()
|
|
|
|
chatMessageData.blind = false
|
|
|
|
break
|
|
|
|
case "roll":
|
|
|
|
chatMessageData.whisper = ChatUtility.getUsers(user => user.active)
|
|
|
|
chatMessageData.blind = false
|
|
|
|
break
|
|
|
|
case "selfroll":
|
|
|
|
chatMessageData.whisper = [game.user]
|
|
|
|
chatMessageData.blind = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return chatMessageData
|
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) {
|
2024-10-16 23:18:15 +02:00
|
|
|
const chatGM = foundry.utils.duplicate(chatOptions)
|
|
|
|
chatGM.content = "Message aveugle de " + game.user.name + "<br>" + chatOptions.content
|
|
|
|
console.log("blindMessageToGM", chatGM)
|
|
|
|
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);
|
2024-10-23 23:42:38 +02:00
|
|
|
if (Misc.isFirstConnectedGM()) {
|
2024-10-16 23:18:15 +02:00
|
|
|
ChatMessage.create({
|
|
|
|
user: game.user.id,
|
|
|
|
whisper: ChatUtility.getGMs(),
|
|
|
|
content: socketData.content
|
|
|
|
})
|
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) {
|
2024-06-11 02:48:37 +02:00
|
|
|
if (flag && chatMessage.isAuthor) {
|
|
|
|
await chatMessage.setFlag(SYSTEM_RDD, key, flag)
|
2022-01-29 23:33:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static getMessageData(chatMessage, key) {
|
2024-06-11 02:48:37 +02:00
|
|
|
return chatMessage.getFlag(SYSTEM_RDD, key);
|
2022-01-29 23:33:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static getChatMessage(event) {
|
|
|
|
const chatMessageId = $(event.currentTarget).closest('.chat-message').attr('data-message-id');
|
|
|
|
return game.messages.get(chatMessageId);
|
|
|
|
}
|
|
|
|
|
2024-05-27 00:37:19 +02:00
|
|
|
static async onRenderChatMessage(chatMessage, html, data) {
|
|
|
|
const rddTimestamp = chatMessage.getFlag(SYSTEM_RDD, 'rdd-timestamp')
|
|
|
|
if (rddTimestamp) {
|
|
|
|
const timestamp = new RdDTimestamp(rddTimestamp);
|
|
|
|
const timestampData = timestamp.toCalendrier();
|
|
|
|
const dateHeure = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/common/date-heure.hbs', timestampData);
|
|
|
|
html.find('header.message-header .message-sender').after(dateHeure)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async onCreateChatMessage(chatMessage, options, id) {
|
2024-06-11 02:48:37 +02:00
|
|
|
if (chatMessage.isAuthor) {
|
|
|
|
await chatMessage.setFlag(SYSTEM_RDD, 'rdd-timestamp', game.system.rdd.calendrier.getTimestamp());
|
|
|
|
}
|
2024-05-27 00:37:19 +02:00
|
|
|
}
|
2020-11-24 15:20:05 +01:00
|
|
|
}
|