TheMillsMessages/scripts/utils.mjs
trotFunky d2b14dbe94 Dialog: Provide hints for the sender name
The dialog form has a simple text input for the sender's name.
This can lead to typos or mistakes if the GM wants to send a message
using the same sender name as a previous message.

Add a new function that goes through all the players' histories and
compiles a list of all unique senders, which we can then use to build
a list of hints that will be used to show suggestions when the GM fills
the sender name in the form.

This is currently quite ineficcient as it re-does all the work for every
single dialog.
A better way would be to cache it or store it explicitly somewhere when
we send a message, but this would require a way for the GM to edit it.
2025-06-11 18:48:44 +01:00

77 lines
2.8 KiB
JavaScript

/*
* This file contains some module-wide constants and utility functions
* that do not really fit elsewhere.
*/
import { module_settings } from "./settings.mjs";
export const module_id = "the-mills-messages";
// User flag to point to the ID of the journal to store message history to
export const history_flag = "history-journal";
/**
* This is a copy of Foundry's internal formatting used by
* localize.format(), so that we can be consistent.
* @param {String} str The string whose "{named_args}" will be formatted
* @param {Object} data An object with named_args members, whose data is replaced
* @returns {String} The formatted string
*/
export function str_format(str, data) {
const fmt = /{[^}]+}/g;
str = str.replace(fmt, k => {
return data[k.slice(1, -1)];
});
return str;
}
/**
* Create the journal entry for the history of the specified user,
* update the user's history flag and return the entry for further
* manipulation if needed.
* @param {User} user The user object that we are creating the history for
* @return {Promise<JournalEntry>} The JournalEntry that was created.
*/
export async function create_history_journal(user) {
// Use the settings' title for the journal, or revert to the default one if it is empty.
let format_name = {name: user.character ? user.character.name : user.name};
let formated_journal_title = str_format(game.settings.get(module_id, module_settings.HistoryJournalTitle), format_name)
let title = formated_journal_title.length > 0 ?
formated_journal_title :
game.i18n.format("MM.UI.HistoryJournalTitle", format_name)
return JournalEntry.create({
name: title,
ownership: {
default: CONST.DOCUMENT_OWNERSHIP_LEVELS.NONE,
[user.id]: CONST.DOCUMENT_OWNERSHIP_LEVELS.OBSERVER // Make the history private and unmodifiable
},
}).then(journal => {
return user.setFlag(module_id, history_flag, journal.id).then(() => journal);
});
}
/**
* Go through all the user's histories to build a list of existing senders,
* so that they can be displayed or re-used.
* The final list is sorted and does not contain duplicates, as one sender
* can send messages to multiple players at once.
* @returns {String[]} A sorted array of the sender names, if any
*/
export function get_existing_senders() {
/** @type {String[]} */
let senders = []
for (let user of game.users.players) {
/** @type {JournalEntry} */
let history = game.journal.get(user.getFlag(module_id, history_flag))
if (!history) {
continue;
}
for (let page of history.pages) {
if (!senders.includes(page.name)) {
senders.push(page.name);
}
}
}
return senders.sort();
}