Fixes mineurs #583

Merged
uberwald merged 4 commits from VincentVk/foundryvtt-reve-de-dragon:v10 into v10 2022-11-30 16:40:36 +01:00
2 changed files with 31 additions and 14 deletions
Showing only changes of commit 979a48e4d9 - Show all commits

View File

@ -58,7 +58,7 @@ export class Environnement {
const milieux = new Set(this.getMilieuxSettings()); const milieux = new Set(this.getMilieuxSettings());
const elements = await this.getElements(it => 1, it => ITEM_ENVIRONNEMENT_TYPES.includes(it.type)); 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))) elements.forEach(it => it.system.environnement.forEach(env => milieux.add(env.milieu)))
return [...milieux]; return [...milieux].filter(env => env);
} }
getMilieuxSettings() { getMilieuxSettings() {
@ -66,22 +66,34 @@ export class Environnement {
} }
async findEnvironnementsLike(search) { async findEnvironnementsLike(search) {
return (await this.milieux()).filter(it => Grammar.includesLowerCaseNoAccent(it, 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));
if (milieuExact) {
return [milieuExact];
}
}
return milieux;
} }
async searchToChatMessage(search) { async searchToChatMessage(milieux, typeName) {
const table = await this.buildEnvironnementTable(search); const table = await this.buildEnvironnementTable(milieux);
await CompendiumTableHelpers.tableToChatMessage(table, 'Item', ITEM_ENVIRONNEMENT_TYPES, `ressources en "${search}"`); await CompendiumTableHelpers.tableToChatMessage(table, 'Item', ITEM_ENVIRONNEMENT_TYPES, typeName);
return true return true
} }
async getRandom(search) { async getRandom(milieux, typeName) {
const table = await this.buildEnvironnementTable(search); const table = await this.buildEnvironnementTable(milieux);
return await CompendiumTableHelpers.getRandom(table, 'Item', ITEM_ENVIRONNEMENT_TYPES, undefined, `ressources en "${search}"`); return await CompendiumTableHelpers.getRandom(table, 'Item', ITEM_ENVIRONNEMENT_TYPES, undefined, typeName);
} }
async buildEnvironnementTable(search) { async buildEnvironnementTable(milieux) {
const itemRareteEnMilieu = item => item.system?.environnement.find(env => Grammar.includesLowerCaseNoAccent(env.milieu, search)); const filterMilieux = item => item.system?.environnement.filter(env => milieux.includes(env.milieu));
const itemRareteEnMilieu = item => {
const raretes = filterMilieux(item);
const frequenceMax = Math.max(raretes.map(env => env.frequence));
return raretes.find(env => env.frequence == frequenceMax);
}
const itemFrequenceEnMilieu = item => itemRareteEnMilieu(item)?.frequence ?? 0; const itemFrequenceEnMilieu = item => itemRareteEnMilieu(item)?.frequence ?? 0;
const isPresentEnMilieu = item => itemFrequenceEnMilieu(item) > 0; const isPresentEnMilieu = item => itemFrequenceEnMilieu(item) > 0;
return await this.table.buildTable(itemFrequenceEnMilieu, isPresentEnMilieu); return await this.table.buildTable(itemFrequenceEnMilieu, isPresentEnMilieu);

View File

@ -382,15 +382,20 @@ export class RdDCommands {
async tableMilieu(msg, params, toChat) { async tableMilieu(msg, params, toChat) {
if (params && params.length > 0) { if (params && params.length > 0) {
const search = Misc.join(params, ' '); const search = Misc.join(params, ' ');
const searches = game.system.rdd.environnement.findEnvironnementsLike(search); const milieux = await game.system.rdd.environnement.findEnvironnementsLike(search);
if (searches.length == 0) { if (milieux.length == 0) {
return RdDCommands._chatAnswer(msg, 'Aucun milieu correspondant à ' + search); return RdDCommands._chatAnswer(msg, 'Aucun milieu correspondant à ' + search);
} }
if (milieux.length > 1) {
ui.notifications.warn(`<strong>Plusieurs milieux correspondent à '${search}'</strong>:
<br><ul><li>${milieux.reduce(Misc.joining('</li><li>'))}</li></ul>`);
}
const tableName = `ressources en ${milieux.reduce(Misc.joining(', '))}`;
if (toChat == 'liste') { if (toChat == 'liste') {
return await game.system.rdd.environnement.searchToChatMessage(search); return await game.system.rdd.environnement.searchToChatMessage(milieux, tableName);
} }
else { else {
const row = await game.system.rdd.environnement.getRandom(search); const row = await game.system.rdd.environnement.getRandom(milieux, tableName);
await CompendiumTableHelpers.tableRowToChatMessage(row, 'Item'); await CompendiumTableHelpers.tableRowToChatMessage(row, 'Item');
return true; return true;
} }