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:
parent
b07cea40e2
commit
bdd3802e72
@ -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) {
|
||||
|
@ -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)
|
||||
|
@ -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, `<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) {
|
||||
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(', '))}`;
|
||||
if (toChat == 'liste') {
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user