2021-05-22 17:51:30 +02:00
|
|
|
import { Misc } from "./misc.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-05-22 02:03:46 +02:00
|
|
|
case "msg_delete_chat_message": return ChatUtility.onRemoveMessages(sockmsg.part);
|
2021-01-20 00:44:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:24:14 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-05-22 02:03:46 +02:00
|
|
|
static onRemoveMessages(part) {
|
|
|
|
if (Misc.isElectedUser()) {
|
2021-01-20 00:44:19 +01:00
|
|
|
const toDelete = game.messages.filter(it => it.data.content.includes(part));
|
|
|
|
toDelete.forEach(it => it.delete());
|
|
|
|
}
|
2021-03-14 19:05:42 +01:00
|
|
|
}
|
2021-05-22 02:03:46 +02:00
|
|
|
|
2021-01-09 19:36:19 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-20 00:44:19 +01:00
|
|
|
static removeChatMessageContaining(part) {
|
2021-05-22 02:03:46 +02:00
|
|
|
if (Misc.isElectedUser()) {
|
|
|
|
ChatUtility.onRemoveMessages(part);
|
2021-01-20 00:44:19 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
game.socket.emit("system.foundryvtt-reve-de-dragon", {
|
2021-05-22 02:03:46 +02:00
|
|
|
msg: "msg_delete_chat_message", data: { part: part }
|
|
|
|
});
|
2021-01-20 00:44:19 +01:00
|
|
|
}
|
2021-01-07 00:32:22 +01:00
|
|
|
}
|
|
|
|
|
2021-01-03 15:40:48 +01:00
|
|
|
/* -------------------------------------------- */
|
2021-01-09 19:36:19 +01:00
|
|
|
static createChatWithRollMode(name, chatOptions) {
|
|
|
|
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
|
|
|
/* -------------------------------------------- */
|
2021-01-09 19:36:19 +01:00
|
|
|
static 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);
|
|
|
|
|
2020-11-29 18:06:19 +01:00
|
|
|
chatOptions.whisper = [game.user._id];
|
|
|
|
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;
|
2020-11-24 15:20:05 +01:00
|
|
|
ChatMessage.create(chatOptions);
|
|
|
|
}
|
|
|
|
|
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 {
|
|
|
|
user: game.user._id,
|
|
|
|
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);
|
|
|
|
case "selfroll": return [game.user._id];
|
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-24 15:20:05 +01:00
|
|
|
static getWhisperRecipientsAndGMs(name) {
|
|
|
|
return ChatMessage.getWhisperRecipients(name)
|
2021-01-15 19:09:44 +01:00
|
|
|
.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) {
|
|
|
|
return game.users.filter(filter).map(user => user.data._id);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
game.socket.emit("system.foundryvtt-reve-de-dragon", { msg: "msg_gm_chat_message", data: chatGM });
|
|
|
|
}
|
|
|
|
|
2020-12-11 08:29:24 +01:00
|
|
|
/* -------------------------------------------- */
|
2020-11-24 15:20:05 +01:00
|
|
|
static handleGMChatMessage(data) {
|
|
|
|
console.log("blindMessageToGM", data);
|
|
|
|
if (game.user.isGM) { // message privé pour GM only
|
|
|
|
data.user = game.user._id;
|
|
|
|
ChatMessage.create(data);
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 02:03:46 +02:00
|
|
|
|
2020-11-24 15:20:05 +01:00
|
|
|
}
|