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

128 lines
4.0 KiB
JavaScript
Raw Normal View History

import { Misc } from "./misc.js";
/**
* 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);
2021-01-20 00:44:19 +01:00
}
}
/* -------------------------------------------- */
static onRemoveMessages(data) {
if (game.user.isGM || game.user.id == data.gmId) {
if (data.part){
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();
}
2021-01-20 00:44:19 +01:00
}
}
/* -------------------------------------------- */
2021-01-20 00:44:19 +01:00
static removeChatMessageContaining(part) {
const removeMessageData = {
part: part,
gmId: Misc.connectedGM()
};
2021-01-20 00:44:19 +01:00
if (game.user.isGM) {
ChatUtility.onRemoveMessages(removeMessageData);
2021-01-20 00:44:19 +01:00
}
else {
game.socket.emit("system.foundryvtt-reve-de-dragon", { msg: "msg_delete_chat_message", data: removeMessageData });
}
}
static removeChatMessageId(messageId) {
const removeMessageData = { messageId: messageId, gmId: Misc.connectedGM() };
if (game.user.isGM) {
ChatUtility.onRemoveMessages(removeMessageData);
}
else {
game.socket.emit("system.foundryvtt-reve-de-dragon", { msg: "msg_delete_chat_message", data: removeMessageData });
2021-01-20 00:44:19 +01:00
}
}
/* -------------------------------------------- */
static createChatWithRollMode(name, chatOptions) {
ChatUtility.createChatMessage(name, game.settings.get("core", "rollMode"), chatOptions);
}
2020-12-11 08:29:24 +01:00
/* -------------------------------------------- */
static 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;
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) {
return ChatMessage.getWhisperRecipients(name)
.concat(ChatMessage.getWhisperRecipients('GM'));
}
2020-12-11 08:29:24 +01:00
/* -------------------------------------------- */
static getUsers(filter) {
return game.users.filter(filter).map(user => user.data._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);
game.socket.emit("system.foundryvtt-reve-de-dragon", { 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);
}
}
}