From bdd3802e7271f74b1a42a876b444e9e0b5aa03e0 Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Sat, 3 Dec 2022 01:24:56 +0100 Subject: [PATCH] =?UTF-8?q?Am=C3=A9lioration=20de=20la=20recherche=20dans?= =?UTF-8?q?=20un=20milieu?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - gestion correcte de la case - recherche insensitive - correction de la liste des milieux à ajouter dans la feuille - si aucun milieu ne correspond à la recherche, affiche la liste des milieux disponibles --- module/environnement.js | 55 +++++++++++++++++++++++++++-------------- module/misc.js | 13 ++++++++++ module/rdd-commands.js | 7 ++++-- styles/simple.css | 14 +++++++++++ 4 files changed, 69 insertions(+), 20 deletions(-) diff --git a/module/environnement.js b/module/environnement.js index efb8925d..508f4da7 100644 --- a/module/environnement.js +++ b/module/environnement.js @@ -1,7 +1,7 @@ import { SYSTEM_RDD } from "./constants.js"; import { Grammar } from "./grammar.js"; import { Misc } from "./misc.js"; -import { CompendiumTableHelpers, SystemCompendiums, CompendiumTable } from "./settings/system-compendiums.js"; +import { CompendiumTableHelpers, CompendiumTable } from "./settings/system-compendiums.js"; const RARETES = [ { name: 'Commune', frequence: 54, min: 27, max: 108 }, @@ -54,25 +54,45 @@ export class Environnement { } async milieux() { - const milieux = new Set(this.getMilieuxSettings()); - const elements = await this.getElements(it => 1, it => ITEM_ENVIRONNEMENT_TYPES.includes(it.type)); - elements.forEach(it => it.system.environnement.forEach(env => milieux.add(env.milieu))) - return [...milieux].filter(env => env); + return Object.values(await this.mapMilieux()); + } + + async mapMilieux() { + const compendiumItems = await this.getElements(it => 1, it => ITEM_ENVIRONNEMENT_TYPES.includes(it.type)); + return Misc.indexLowercase(this.getMilieuxSettings().concat(Environnement.listMilieux(compendiumItems))); + } + + static listMilieux(items) { + return Misc.concat(items.map(it => Environnement.$itemToMilieux(it).filter(m => m))); + } + + async autresMilieux(item) { + const mapMilieux = await this.mapMilieux(); + const milieuxExistants = Environnement.$itemToMilieux(item).map(it => Grammar.toLowerCaseNoAccent(it)); + return Object.keys(mapMilieux) + .filter(it => !milieuxExistants.includes(it)) + .map(it => mapMilieux[it]); + } + + static $itemToMilieux(item) { + return item.system.environnement.map(env => env.milieu); } getMilieuxSettings() { - return game.settings.get(SYSTEM_RDD, SETTINGS_LISTE_MILIEUX).split(','); + return game.settings.get(SYSTEM_RDD, SETTINGS_LISTE_MILIEUX).split(',').map(it => it.trim()).filter(it => it != ''); } async findEnvironnementsLike(search) { - const milieux = (await this.milieux()).filter(it => Grammar.includesLowerCaseNoAccent(it, search)); - if (milieux.length > 1) { - const milieuExact = milieux.find(it => Grammar.equalsInsensitive(it, search)); + const milieux = await this.mapMilieux(); + const searchLower = Grammar.toLowerCaseNoAccent(search); + const keys = Object.keys(milieux).filter(it => it.includes(searchLower)); + if (keys.length > 1) { + const milieuExact = milieux[searchLower]; if (milieuExact) { return [milieuExact]; } } - return milieux; + return keys.map(k => milieux[k]); } async searchToChatMessage(milieux, typeName) { @@ -131,10 +151,8 @@ export class EnvironmentSheetHelper { } /* -------------------------------------------- */ - static async getData(sheet) { - const formData = duplicate(sheet.item); - const milieux = await game.system.rdd.environnement.milieux(); - const milieuxDisponibles = milieux.filter(it => !sheet.item.system.environnement.find(e => e.milieu == it)); + static async getData(sheet, formData) { + const autresMilieux = await game.system.rdd.environnement.autresMilieux(sheet.item); mergeObject(formData, { title: formData.name, isGM: game.user.isGM, @@ -143,7 +161,7 @@ export class EnvironmentSheetHelper { actorId: sheet.actor?.id, editable: sheet.isEditable, cssClass: sheet.isEditable ? "editable" : "locked", - milieux: milieuxDisponibles + milieux: autresMilieux }); return formData; } @@ -172,9 +190,10 @@ export class EnvironmentSheetHelper { const name = $(event.currentTarget).val(); const rarete = Environnement.getRarete(name); updated.rarete = rarete.name; - updated.frequence = Math.min( - Math.max(rarete.min, updated.frequence ?? rarete.frequence), - rarete.max); + updated.frequence = rarete.frequence; + // updated.frequence = Math.min( + // Math.max(rarete.min, updated.frequence ?? rarete.frequence), + // rarete.max); } static async onAddMilieu(html, sheet, event) { diff --git a/module/misc.js b/module/misc.js index 67298074..849c0d52 100644 --- a/module/misc.js +++ b/module/misc.js @@ -72,6 +72,19 @@ export class Misc { } } + 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; + } + static concat(lists) { + return lists.reduce((a, b) => a.concat(b), []); + } + static classify(items, classifier = it => it.type) { let itemsBy = {} Misc.classifyInto(itemsBy, items, classifier) diff --git a/module/rdd-commands.js b/module/rdd-commands.js index 9b21f0ea..7b3deebe 100644 --- a/module/rdd-commands.js +++ b/module/rdd-commands.js @@ -403,11 +403,14 @@ export class RdDCommands { const search = Misc.join(params, ' '); const milieux = await game.system.rdd.environnement.findEnvironnementsLike(search); if (milieux.length == 0) { - return RdDCommands._chatAnswer(msg, 'Aucun milieu correspondant à ' + search); + const tous = Object.values(await game.system.rdd.environnement.milieux()); + return RdDCommands._chatAnswer(msg, `Aucun milieu correspondant à '${search}'. +
Milieux disponibles: +
`); } if (milieux.length > 1) { ui.notifications.warn(`Plusieurs milieux correspondent à '${search}': -
`); +
`); } const tableName = `ressources en ${milieux.reduce(Misc.joining(', '))}`; if (toChat == 'liste') { diff --git a/styles/simple.css b/styles/simple.css index a068c96a..d20c17f8 100644 --- a/styles/simple.css +++ b/styles/simple.css @@ -873,6 +873,20 @@ ul, li { .alterne-list > .list-item:nth-child(odd) { background: rgb(160, 130, 100, 0.05); } + +ul.chat-list { + margin-left: 0.8rem; + list-style: inside; +} +ul.chat-list li:nth-child(even) { + background: rgba(80, 60, 0, 0.10); + list-style-type: disc; +} +ul.chat-list li:nth-child(odd) { + background: rgb(160, 130, 100, 0.05); + list-style-type: disc; +} + .xp-level-up { margin: 0.1rem; box-shadow: inset 0px 0px 1px #00000096;