Amélioration de la recherche dans un milieu

- 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
This commit is contained in:
Vincent Vandemeulebrouck 2022-12-03 01:24:56 +01:00
parent b07cea40e2
commit bdd3802e72
4 changed files with 69 additions and 20 deletions

View File

@ -1,7 +1,7 @@
import { SYSTEM_RDD } from "./constants.js"; import { SYSTEM_RDD } from "./constants.js";
import { Grammar } from "./grammar.js"; import { Grammar } from "./grammar.js";
import { Misc } from "./misc.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 = [ const RARETES = [
{ name: 'Commune', frequence: 54, min: 27, max: 108 }, { name: 'Commune', frequence: 54, min: 27, max: 108 },
@ -54,25 +54,45 @@ export class Environnement {
} }
async milieux() { async milieux() {
const milieux = new Set(this.getMilieuxSettings()); return Object.values(await this.mapMilieux());
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); 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() { 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) { async findEnvironnementsLike(search) {
const milieux = (await this.milieux()).filter(it => Grammar.includesLowerCaseNoAccent(it, search)); const milieux = await this.mapMilieux();
if (milieux.length > 1) { const searchLower = Grammar.toLowerCaseNoAccent(search);
const milieuExact = milieux.find(it => Grammar.equalsInsensitive(it, search)); const keys = Object.keys(milieux).filter(it => it.includes(searchLower));
if (keys.length > 1) {
const milieuExact = milieux[searchLower];
if (milieuExact) { if (milieuExact) {
return [milieuExact]; return [milieuExact];
} }
} }
return milieux; return keys.map(k => milieux[k]);
} }
async searchToChatMessage(milieux, typeName) { async searchToChatMessage(milieux, typeName) {
@ -131,10 +151,8 @@ export class EnvironmentSheetHelper {
} }
/* -------------------------------------------- */ /* -------------------------------------------- */
static async getData(sheet) { static async getData(sheet, formData) {
const formData = duplicate(sheet.item); const autresMilieux = await game.system.rdd.environnement.autresMilieux(sheet.item);
const milieux = await game.system.rdd.environnement.milieux();
const milieuxDisponibles = milieux.filter(it => !sheet.item.system.environnement.find(e => e.milieu == it));
mergeObject(formData, { mergeObject(formData, {
title: formData.name, title: formData.name,
isGM: game.user.isGM, isGM: game.user.isGM,
@ -143,7 +161,7 @@ export class EnvironmentSheetHelper {
actorId: sheet.actor?.id, actorId: sheet.actor?.id,
editable: sheet.isEditable, editable: sheet.isEditable,
cssClass: sheet.isEditable ? "editable" : "locked", cssClass: sheet.isEditable ? "editable" : "locked",
milieux: milieuxDisponibles milieux: autresMilieux
}); });
return formData; return formData;
} }
@ -172,9 +190,10 @@ export class EnvironmentSheetHelper {
const name = $(event.currentTarget).val(); const name = $(event.currentTarget).val();
const rarete = Environnement.getRarete(name); const rarete = Environnement.getRarete(name);
updated.rarete = rarete.name; updated.rarete = rarete.name;
updated.frequence = Math.min( updated.frequence = rarete.frequence;
Math.max(rarete.min, updated.frequence ?? rarete.frequence), // updated.frequence = Math.min(
rarete.max); // Math.max(rarete.min, updated.frequence ?? rarete.frequence),
// rarete.max);
} }
static async onAddMilieu(html, sheet, event) { static async onAddMilieu(html, sheet, event) {

View File

@ -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) { static classify(items, classifier = it => it.type) {
let itemsBy = {} let itemsBy = {}
Misc.classifyInto(itemsBy, items, classifier) Misc.classifyInto(itemsBy, items, classifier)

View File

@ -403,11 +403,14 @@ export class RdDCommands {
const search = Misc.join(params, ' '); const search = Misc.join(params, ' ');
const milieux = await game.system.rdd.environnement.findEnvironnementsLike(search); const milieux = await game.system.rdd.environnement.findEnvironnementsLike(search);
if (milieux.length == 0) { 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, `<strong>Aucun milieu correspondant à '${search}'.</strong>
<br>Milieux disponibles:
<br><ul class="chat-list"><li>${tous.reduce(Misc.joining('</li><li>'))}</li></ul>`);
} }
if (milieux.length > 1) { if (milieux.length > 1) {
ui.notifications.warn(`<strong>Plusieurs milieux correspondent à '${search}'</strong>: ui.notifications.warn(`<strong>Plusieurs milieux correspondent à '${search}'</strong>:
<br><ul><li>${milieux.reduce(Misc.joining('</li><li>'))}</li></ul>`); <br><ul class="chat-list"><li>${milieux.reduce(Misc.joining('</li><li>'))}</li></ul>`);
} }
const tableName = `ressources en ${milieux.reduce(Misc.joining(', '))}`; const tableName = `ressources en ${milieux.reduce(Misc.joining(', '))}`;
if (toChat == 'liste') { if (toChat == 'liste') {

View File

@ -873,6 +873,20 @@ ul, li {
.alterne-list > .list-item:nth-child(odd) { .alterne-list > .list-item:nth-child(odd) {
background: rgb(160, 130, 100, 0.05); 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 { .xp-level-up {
margin: 0.1rem; margin: 0.1rem;
box-shadow: inset 0px 0px 1px #00000096; box-shadow: inset 0px 0px 1px #00000096;