diff --git a/module/actor.js b/module/actor.js index c5014b77..132d668b 100644 --- a/module/actor.js +++ b/module/actor.js @@ -3020,17 +3020,8 @@ export class RdDActor extends Actor { case 'chance-actuelle': case 'chance actuelle': return carac.chance; } - const keys = Object.entries(carac) - .filter(it => it[0].includes(name) || Grammar.toLowerCaseNoAccent(it[1].label).includes(name)) - .map(it => it[0]); - if (keys.length > 1) { - const names = keys.reduce((a, b) => `${a}
${b}`); - ui.notifications.info(`Plusieurs caractéristiques possibles:
${names}
La première sera choisie.`); - } - if (keys.length > 0) { - return carac[keys[0]]; - } - return undefined; // Per default + let entry = Misc.findFirstLike(name, Object.entries(carac), it => it[1].label, 'caractéristiques'); + return entry.length>0 ? carac[entry[0]] : undefined; } /* -------------------------------------------- */ diff --git a/module/item-competence.js b/module/item-competence.js index 03939d03..c2355ab1 100644 --- a/module/item-competence.js +++ b/module/item-competence.js @@ -209,20 +209,7 @@ export class RdDItemCompetence extends Item { /* -------------------------------------------- */ static findCompetence(list, name) { - name = Grammar.toLowerCaseNoAccent(name); - const competences = list.filter(it => Grammar.toLowerCaseNoAccent(it.name).includes(name) && (it.type == "competence" || it.type == "competencecreature")); - if (competences.length == 0) { - return undefined; - } - let competence = competences.find(it => Grammar.toLowerCaseNoAccent(it.name) == name); - if (!competence) { - competence = competences[0]; - if (competences.length > 1) { - const names = competences.map(it => it.name).reduce((a, b) => `${a}
${b}`); - ui.notifications.info(`Plusieurs compétences possibles:
${names}
La première sera choisie: ${competence.name}`); - } - } - return competence; + return Misc.findFirstLike(name, list, it => it.name, 'compétences'); } /* -------------------------------------------- */ diff --git a/module/misc.js b/module/misc.js index da74c589..ea0d7aa1 100644 --- a/module/misc.js +++ b/module/misc.js @@ -1,3 +1,4 @@ +import { Grammar } from "./grammar.js"; import { RdDDice } from "./rdd-dice.js"; /** @@ -131,4 +132,22 @@ export class Misc { static isElectedUser() { return game.user.id == Misc.connectedGMOrUser(); } + + /* -------------------------------------------- */ + static findFirstLike(value, elements, mapper = it=>it.name, description = 'valeurs') { + value = Grammar.toLowerCaseNoAccent(value); + const subset = elements.filter(it => Grammar.toLowerCaseNoAccent(mapper(it)).includes(value)); + if (subset.length == 0) { + return undefined; + } + let single = subset.find(it => Grammar.toLowerCaseNoAccent(mapper(it)) == value); + if (!single) { + single = subset[0]; + if (subset.length > 1) { + const choices = subset.map(it => mapper(it)).reduce((a, b) => `${a}
${b}`); + ui.notifications.info(`Plusieurs choix de ${description} possibles:
${choices}
Le premier sera choisi: ${mapper(single)}`); + } + } + return single; + } } \ No newline at end of file