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
|
|
|
}
|
2021-01-05 18:43:13 +01:00
|
|
|
|
2021-03-29 18:08:18 +02:00
|
|
|
static sum() {
|
|
|
|
return (a, b) => a + b;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
if (value == undefined) {
|
2020-11-15 02:07:41 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
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;
|
2021-04-11 23:01:10 +02:00
|
|
|
const decimal = Math.pow(10, parseInt(decimals));
|
|
|
|
return Math.round(num * decimal) / decimal;
|
|
|
|
}
|
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
|
|
|
|
2021-05-07 01:47:51 +02:00
|
|
|
static classify(items, classifier = it => it.type) {
|
2021-02-05 01:38:40 +01:00
|
|
|
let itemsBy = {};
|
2021-05-07 01:47:51 +02:00
|
|
|
Misc.classifyInto(itemsBy, items, classifier);
|
2021-02-05 01:38:40 +01:00
|
|
|
return itemsBy;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
const classification = classifier(item);
|
|
|
|
let list = itemsBy[classification];
|
|
|
|
if (!list) {
|
|
|
|
list = [];
|
|
|
|
itemsBy[classification] = list;
|
|
|
|
}
|
2021-05-07 01:47:51 +02:00
|
|
|
list.push(item);
|
2021-02-05 01:38:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-12 01:11:03 +01:00
|
|
|
static distinct(array) {
|
|
|
|
return [...new Set(array)];
|
|
|
|
}
|
|
|
|
|
2021-03-22 20:10:37 +01:00
|
|
|
static data(it) {
|
2021-03-31 01:15:55 +02:00
|
|
|
if (it instanceof Actor || it instanceof Item || it instanceof Combatant) {
|
2021-03-22 20:10:37 +01:00
|
|
|
return it.data;
|
|
|
|
}
|
|
|
|
return it;
|
|
|
|
}
|
|
|
|
|
|
|
|
static templateData(it) {
|
|
|
|
return Misc.data(it)?.data ?? {}
|
|
|
|
}
|
2021-04-15 00:13:50 +02:00
|
|
|
|
2021-05-24 00:43:46 +02:00
|
|
|
static getEntityTypeLabel(entity) {
|
|
|
|
const documentName = entity?.documentName;
|
|
|
|
const type = entity?.data.type;
|
|
|
|
if (documentName === 'Actor' || documentName === 'Item') {
|
|
|
|
const label = CONFIG[documentName]?.typeLabels?.[type] ?? type;
|
|
|
|
return game.i18n.has(label) ? game.i18n.localize(label) : t;
|
|
|
|
}
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
static getActiveUser(id){
|
|
|
|
return game.users.entities.find(u => u.id == id && u.active);
|
|
|
|
}
|
|
|
|
|
|
|
|
static firstConnectedGM() {
|
|
|
|
return game.users.entities.sort(Misc.ascending(u => u.id)).find(u => u.isGM && u.active);
|
2021-04-15 00:13:50 +02:00
|
|
|
}
|
2021-10-08 23:25:50 +02:00
|
|
|
|
2021-11-26 00:45:21 +01:00
|
|
|
/**
|
|
|
|
* @returns true pour un seul utilisateur: le premier GM connecté par ordre d'id
|
|
|
|
*/
|
|
|
|
static isUniqueConnectedGM() {
|
|
|
|
return game.user.id == Misc.firstConnectedGM()?.id;
|
|
|
|
}
|
|
|
|
|
2021-10-08 23:28:55 +02:00
|
|
|
/* -------------------------------------------- */
|
|
|
|
static findPlayer(name) {
|
2021-11-20 01:04:22 +01: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 = {}) {
|
|
|
|
options = mergeObject({
|
|
|
|
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) {
|
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-20 01:04:22 +01:00
|
|
|
const choices = subset.map(it => options.mapper(it)).reduce((a, b) => `${a}<br>${b}`);
|
|
|
|
options.info(`Plusieurs choix de ${options.description}s possibles:<br>${choices}<br>Le premier sera choisi: ${mapToValue(single)}`);
|
2021-10-08 23:25:50 +02:00
|
|
|
}
|
|
|
|
return single;
|
|
|
|
}
|
2021-11-20 01:04:22 +01:00
|
|
|
|
|
|
|
static findAllLike(value, elements, options = {}) {
|
|
|
|
options = mergeObject({
|
|
|
|
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)
|
|
|
|
.filter(it => Grammar.toLowerCaseNoAccent(options.mapper(it)).includes(value));
|
|
|
|
if (subset.length == 0) {
|
|
|
|
options.onMessage(`Pas de ${options.description} correspondant à ${value}`);
|
|
|
|
}
|
|
|
|
return subset;
|
|
|
|
}
|
2020-11-14 03:16:03 +01:00
|
|
|
}
|