2021-10-08 23:25:50 +02:00
|
|
|
import { Grammar } from "./grammar.js";
|
2020-11-14 03:16:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* This class is intended as a placeholder for utility methods unrelated
|
|
|
|
* to actual classes of the game system or of FoundryVTT
|
|
|
|
*/
|
|
|
|
export class Misc {
|
2021-01-05 18:43:13 +01:00
|
|
|
static isFunction(v) {
|
|
|
|
return v && {}.toString.call(v) === '[object Function]';
|
2021-03-22 20:10:37 +01:00
|
|
|
}
|
2021-01-05 18:43:13 +01:00
|
|
|
|
2020-11-15 02:07:41 +01:00
|
|
|
static upperFirst(text) {
|
2020-11-14 03:16:03 +01:00
|
|
|
return text.charAt(0).toUpperCase() + text.slice(1);
|
|
|
|
}
|
2021-01-05 18:43:13 +01:00
|
|
|
|
2021-06-30 01:01:24 +02:00
|
|
|
static lowerFirst(text) {
|
|
|
|
return text.charAt(0).toLowerCase() + text.slice(1);
|
|
|
|
}
|
|
|
|
|
2021-03-22 20:10:37 +01:00
|
|
|
static toSignedString(number) {
|
2020-11-15 02:07:41 +01:00
|
|
|
const value = parseInt(number)
|
|
|
|
const isPositiveNumber = value != NaN && value > 0;
|
2021-03-22 20:10:37 +01:00
|
|
|
return isPositiveNumber ? "+" + number : number
|
2020-11-15 02:07:41 +01:00
|
|
|
}
|
2023-12-08 04:00:17 +01:00
|
|
|
|
|
|
|
static modulo(n, m) {
|
|
|
|
return ((n % m) + m) % m;
|
|
|
|
}
|
2021-01-05 18:43:13 +01:00
|
|
|
|
2021-03-29 18:08:18 +02:00
|
|
|
static sum() {
|
2022-11-28 16:00:49 +01:00
|
|
|
return (a, b) => Number(a) + Number(b);
|
2021-03-29 18:08:18 +02:00
|
|
|
}
|
|
|
|
|
2021-05-11 21:45:43 +02:00
|
|
|
static ascending(orderFunction = x => x) {
|
2021-03-29 18:08:18 +02:00
|
|
|
return (a, b) => Misc.sortingBy(orderFunction(a), orderFunction(b));
|
2021-05-11 21:45:43 +02:00
|
|
|
}
|
2021-03-29 18:08:18 +02:00
|
|
|
|
2021-05-11 21:45:43 +02:00
|
|
|
static descending(orderFunction = x => x) {
|
2021-03-29 18:08:18 +02:00
|
|
|
return (a, b) => Misc.sortingBy(orderFunction(b), orderFunction(a));
|
2021-05-11 21:45:43 +02:00
|
|
|
}
|
2021-03-29 18:08:18 +02:00
|
|
|
|
|
|
|
static sortingBy(a, b) {
|
|
|
|
if (a > b) return 1;
|
|
|
|
if (a < b) return -1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2022-11-28 16:00:49 +01:00
|
|
|
static typeName(type, subType) {
|
2024-03-09 19:48:53 +01:00
|
|
|
return subType ? game.i18n.localize(`TYPES.${type}.${subType}`)
|
2022-12-03 15:33:16 +01:00
|
|
|
: '';
|
2022-11-28 16:00:49 +01:00
|
|
|
}
|
|
|
|
|
2022-12-28 23:36:48 +01:00
|
|
|
static arrayOrEmpty(items) {
|
|
|
|
return items?.length ? items : [];
|
|
|
|
}
|
2020-11-15 02:07:41 +01:00
|
|
|
/**
|
|
|
|
* Converts the value to an integer, or to 0 if undefined/null/not representing integer
|
|
|
|
* @param {*} value value to convert to an integer using parseInt
|
|
|
|
*/
|
2021-03-22 20:10:37 +01:00
|
|
|
static toInt(value) {
|
2020-11-15 02:07:41 +01:00
|
|
|
const parsed = parseInt(value);
|
|
|
|
return isNaN(parsed) ? 0 : parsed;
|
|
|
|
}
|
2021-04-15 00:13:50 +02:00
|
|
|
|
2021-04-11 23:01:10 +02:00
|
|
|
static keepDecimals(num, decimals) {
|
2021-05-11 21:45:43 +02:00
|
|
|
if (decimals <= 0 || decimals > 6) return num;
|
2023-11-04 03:42:39 +01:00
|
|
|
const power10n = Math.pow(10, parseInt(decimals));
|
|
|
|
return Math.round(num * power10n) / power10n;
|
2021-04-11 23:01:10 +02:00
|
|
|
}
|
2021-04-15 00:13:50 +02:00
|
|
|
|
2021-01-05 18:43:13 +01:00
|
|
|
static getFractionHtml(diviseur) {
|
|
|
|
if (!diviseur || diviseur <= 1) return undefined;
|
|
|
|
switch (diviseur || 1) {
|
|
|
|
case 2: return '½';
|
|
|
|
case 4: return '¼';
|
|
|
|
default: return '1/' + diviseur;
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 01:38:40 +01:00
|
|
|
|
2022-12-03 01:24:56 +01:00
|
|
|
static indexLowercase(list) {
|
|
|
|
const obj = {};
|
|
|
|
const addToObj = (map, val) => {
|
|
|
|
const key = Grammar.toLowerCaseNoAccent(val);
|
|
|
|
if (key && !map[key]) map[key] = val
|
|
|
|
}
|
|
|
|
list.forEach(it => addToObj(obj, it))
|
|
|
|
return obj;
|
|
|
|
}
|
2023-01-18 00:11:10 +01:00
|
|
|
|
2022-12-03 01:24:56 +01:00
|
|
|
static concat(lists) {
|
|
|
|
return lists.reduce((a, b) => a.concat(b), []);
|
|
|
|
}
|
|
|
|
|
2021-05-07 01:47:51 +02:00
|
|
|
static classify(items, classifier = it => it.type) {
|
2022-06-12 08:17:59 +02:00
|
|
|
let itemsBy = {}
|
|
|
|
Misc.classifyInto(itemsBy, items, classifier)
|
|
|
|
return itemsBy
|
2021-02-05 01:38:40 +01:00
|
|
|
}
|
|
|
|
|
2021-03-19 22:20:01 +01:00
|
|
|
static classifyFirst(items, classifier) {
|
|
|
|
let itemsBy = {};
|
|
|
|
for (const item of items) {
|
|
|
|
const classification = classifier(item);
|
|
|
|
if (!itemsBy[classification]) {
|
|
|
|
itemsBy[classification] = item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return itemsBy;
|
|
|
|
}
|
2021-03-29 18:08:18 +02:00
|
|
|
|
2021-05-07 01:47:51 +02:00
|
|
|
static classifyInto(itemsBy, items, classifier = it => it.type) {
|
2021-02-05 01:38:40 +01:00
|
|
|
for (const item of items) {
|
2022-06-12 08:17:59 +02:00
|
|
|
const classification = classifier(item)
|
2021-02-05 01:38:40 +01:00
|
|
|
let list = itemsBy[classification];
|
|
|
|
if (!list) {
|
2022-06-12 08:17:59 +02:00
|
|
|
list = []
|
|
|
|
itemsBy[classification] = list
|
2021-02-05 01:38:40 +01:00
|
|
|
}
|
2022-06-12 08:17:59 +02:00
|
|
|
list.push(item)
|
2021-02-05 01:38:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-08 02:00:38 +01:00
|
|
|
/**
|
|
|
|
* @returns an array of incremental integers (including from / excluding to).
|
|
|
|
* if max<min, the array is decrementing integers
|
|
|
|
*/
|
|
|
|
static intArray(from, to) {
|
|
|
|
if (from > to) {
|
|
|
|
return Array.from(Array(from - to).keys()).map(i => from - i)
|
|
|
|
}
|
|
|
|
return Array.from(Array(to - from).keys()).map(i => from + i)
|
|
|
|
}
|
|
|
|
|
2021-02-12 01:11:03 +01:00
|
|
|
static distinct(array) {
|
|
|
|
return [...new Set(array)];
|
|
|
|
}
|
|
|
|
|
2021-11-26 23:29:06 +01:00
|
|
|
static join(params, separator = '') {
|
2024-11-03 21:56:43 +01:00
|
|
|
return (!params || params.length == 0) ? '' : params.reduce(Misc.joining(separator))
|
2022-11-28 16:00:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static joining(separator = '') {
|
|
|
|
return (a, b) => a + separator + b;
|
2021-11-26 23:29:06 +01:00
|
|
|
}
|
|
|
|
|
2021-04-16 23:07:09 +02:00
|
|
|
static connectedGMOrUser(ownerId = undefined) {
|
2021-05-11 21:45:43 +02:00
|
|
|
if (ownerId && game.user.id == ownerId) {
|
2021-04-16 23:07:09 +02:00
|
|
|
return ownerId;
|
|
|
|
}
|
2021-11-26 00:45:21 +01:00
|
|
|
return Misc.firstConnectedGM()?.id ?? game.user.id;
|
2021-04-16 23:07:09 +02:00
|
|
|
}
|
2021-11-26 00:45:21 +01:00
|
|
|
|
2022-12-01 01:22:25 +01:00
|
|
|
static isRollModeHiddenToPlayer() {
|
2022-12-28 23:36:48 +01:00
|
|
|
switch (game.settings.get("core", "rollMode")) {
|
|
|
|
case CONST.DICE_ROLL_MODES.BLIND:
|
|
|
|
case CONST.DICE_ROLL_MODES.SELF: return true;
|
|
|
|
}
|
|
|
|
return false
|
2022-12-01 01:22:25 +01:00
|
|
|
}
|
|
|
|
|
2022-01-11 23:39:51 +01:00
|
|
|
static getActiveUser(id) {
|
2022-10-08 15:38:56 +02:00
|
|
|
return game.users.find(u => u.id == id && u.active);
|
2022-12-01 01:22:25 +01:00
|
|
|
}
|
2022-01-11 23:39:51 +01:00
|
|
|
|
2021-11-26 00:45:21 +01:00
|
|
|
static firstConnectedGM() {
|
2024-08-30 23:29:26 +02:00
|
|
|
if (foundry.utils.isNewerVersion(game.release.version, '12.0')) {
|
|
|
|
return game.users.activeGM
|
2024-06-13 21:52:48 +02:00
|
|
|
}
|
2024-08-30 23:29:26 +02:00
|
|
|
return game.users.find(u => u.isGM && u.active);
|
2024-06-11 02:48:37 +02:00
|
|
|
}
|
2024-08-30 23:29:26 +02:00
|
|
|
|
2024-06-11 02:48:37 +02:00
|
|
|
static connectedGMs() {
|
|
|
|
return game.users.filter(u => u.isGM && u.active);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This helper method allows to get the docuument, for a single user (either first connected GM, or the owner
|
|
|
|
* if there is no connected GMs), or else return undefined.
|
|
|
|
*
|
|
|
|
* This allows for example update hooks that should apply modifications to actors to be called only for one
|
|
|
|
* user (preventing the "User ... lacks permission to update Item" that was occuring on hooks when Item updates
|
|
|
|
* were triggering other changes)
|
|
|
|
*
|
|
|
|
* @param {*} document the Document with is potentially an Actor
|
|
|
|
* @returns the actor if either the game.user is the first connected GM, or if the game.user is the owner
|
|
|
|
* and there is no connected GM
|
|
|
|
*/
|
|
|
|
static documentIfResponsible(document) {
|
2024-11-24 16:25:40 +01:00
|
|
|
if (Misc.isFirstConnectedGM() || (Misc.connectedGMs().length == 0 && Misc.isFirstOwnerPlayer(document))) {
|
2024-08-30 23:29:26 +02:00
|
|
|
return document
|
|
|
|
}
|
2024-06-11 02:48:37 +02:00
|
|
|
return undefined
|
2021-04-15 00:13:50 +02:00
|
|
|
}
|
2021-10-08 23:25:50 +02:00
|
|
|
|
2024-08-30 23:29:26 +02:00
|
|
|
static isOwnerPlayer(document) {
|
|
|
|
return document.testUserPermission && document.testUserPermission(game.user, CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER)
|
2022-01-29 18:49:06 +01:00
|
|
|
}
|
|
|
|
|
2024-11-24 16:25:40 +01:00
|
|
|
static isFirstOwnerPlayer(document) {
|
|
|
|
if (!document.testUserPermission){
|
|
|
|
return false
|
|
|
|
}
|
2024-12-04 18:55:34 +01:00
|
|
|
return game.users.find(u => document.testUserPermission(u, CONST.DOCUMENT_OWNERSHIP_LEVELS.OWNER)) == game.user
|
2022-01-29 18:49:06 +01:00
|
|
|
}
|
|
|
|
|
2021-11-26 00:45:21 +01:00
|
|
|
/**
|
|
|
|
* @returns true pour un seul utilisateur: le premier GM connecté par ordre d'id
|
|
|
|
*/
|
2024-10-23 23:42:38 +02:00
|
|
|
static isFirstConnectedGM() {
|
|
|
|
return game.user == Misc.firstConnectedGM();
|
2022-11-13 21:56:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static firstConnectedGMId() {
|
|
|
|
return Misc.firstConnectedGM()?.id;
|
2021-11-26 00:45:21 +01:00
|
|
|
}
|
2021-11-26 23:29:06 +01:00
|
|
|
|
2021-10-08 23:28:55 +02:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static findPlayer(name) {
|
2022-10-08 15:38:56 +02:00
|
|
|
return Misc.findFirstLike(name, game.users, { description: 'joueur' });
|
2021-10-08 23:28:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* -------------------------------------------- */
|
2021-11-20 01:04:22 +01:00
|
|
|
static findActor(name, actors = game.actors) {
|
|
|
|
return Misc.findFirstLike(name, actors, { description: 'acteur' });
|
2021-10-08 23:28:55 +02:00
|
|
|
}
|
|
|
|
|
2021-10-08 23:25:50 +02:00
|
|
|
/* -------------------------------------------- */
|
2021-11-20 01:04:22 +01:00
|
|
|
static findFirstLike(value, elements, options = {}) {
|
2024-05-01 09:13:21 +02:00
|
|
|
options = foundry.utils.mergeObject({
|
2021-11-20 01:04:22 +01:00
|
|
|
mapper: it => it.name,
|
|
|
|
preFilter: it => true,
|
|
|
|
description: 'valeur',
|
|
|
|
onMessage: m => ui.notifications.info(m)
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
const subset = this.findAllLike(value, elements, options);
|
2021-10-08 23:25:50 +02:00
|
|
|
if (subset.length == 0) {
|
2023-11-21 16:03:26 +01:00
|
|
|
console.log(`Aucune ${options.description} pour ${value}`);
|
2021-11-20 01:04:22 +01:00
|
|
|
return undefined
|
|
|
|
}
|
|
|
|
if (subset.length == 1) {
|
|
|
|
return subset[0]
|
2021-10-08 23:25:50 +02:00
|
|
|
}
|
2021-11-20 01:04:22 +01:00
|
|
|
let single = subset.find(it => Grammar.toLowerCaseNoAccent(options.mapper(it)) == Grammar.toLowerCaseNoAccent(value));
|
2021-10-08 23:25:50 +02:00
|
|
|
if (!single) {
|
|
|
|
single = subset[0];
|
2021-11-26 23:29:06 +01:00
|
|
|
const choices = Misc.join(subset.map(it => options.mapper(it)), '<br>');
|
2022-07-08 01:22:53 +02:00
|
|
|
options.onMessage(`Plusieurs choix de ${options.description}s possibles:<br>${choices}<br>Le premier sera choisi: ${options.mapper(single)}`);
|
2021-10-08 23:25:50 +02:00
|
|
|
}
|
|
|
|
return single;
|
|
|
|
}
|
2021-11-20 01:04:22 +01:00
|
|
|
|
|
|
|
static findAllLike(value, elements, options = {}) {
|
2024-05-01 09:13:21 +02:00
|
|
|
options = foundry.utils.mergeObject({
|
2021-11-20 01:04:22 +01:00
|
|
|
mapper: it => it.name,
|
|
|
|
preFilter: it => true,
|
|
|
|
description: 'valeur',
|
|
|
|
onMessage: m => ui.notifications.info(m)
|
|
|
|
}, options);
|
|
|
|
|
|
|
|
if (!value) {
|
|
|
|
options.onMessage(`Pas de ${options.description} correspondant à une valeur vide`);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
value = Grammar.toLowerCaseNoAccent(value);
|
|
|
|
const subset = elements.filter(options.preFilter)
|
2022-07-08 01:22:53 +02:00
|
|
|
.filter(it => Grammar.toLowerCaseNoAccent(options.mapper(it))?.includes(value));
|
2021-11-20 01:04:22 +01:00
|
|
|
if (subset.length == 0) {
|
|
|
|
options.onMessage(`Pas de ${options.description} correspondant à ${value}`);
|
|
|
|
}
|
|
|
|
return subset;
|
|
|
|
}
|
2023-03-29 22:53:40 +02:00
|
|
|
|
|
|
|
static cssRotation(angle) {
|
|
|
|
const rotation = `rotate(${angle}deg)`;
|
|
|
|
return {
|
|
|
|
'transform': rotation,
|
|
|
|
'-ms-transform': rotation,
|
|
|
|
'-moz-transform': rotation,
|
|
|
|
'-webkit-transform': rotation,
|
|
|
|
'-o-transform': rotation
|
|
|
|
};
|
|
|
|
}
|
2022-11-17 01:18:06 +01:00
|
|
|
}
|