From f91dfd04a2ecbb4fc7634f6015ae2605236bc45b Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Sat, 20 Nov 2021 01:04:22 +0100 Subject: [PATCH] Fix recherche competence MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit La recherche de compétence cherchait dans tous les items (y compris les armes), ce qui affichait des messages ui --- module/actor-sheet.js | 3 +-- module/actor.js | 26 +++++++++--------- module/item-competence.js | 28 +++++++++++++------- module/misc.js | 56 ++++++++++++++++++++++++++++----------- 4 files changed, 73 insertions(+), 40 deletions(-) diff --git a/module/actor-sheet.js b/module/actor-sheet.js index 9080d850..d8dd83e2 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -11,7 +11,6 @@ import { RdDBonus } from "./rdd-bonus.js"; import { Misc } from "./misc.js"; import { RdDCombatManager } from "./rdd-combat.js"; import { RdDCarac } from "./rdd-carac.js"; -import { RdDItem } from "./item.js"; import { DialogSplitItem } from "./dialog-split-item.js"; import { ReglesOptionelles } from "./regles-optionelles.js"; import { DialogRepos } from "./dialog-repos.js"; @@ -93,7 +92,7 @@ export class RdDActorSheet extends ActorSheet { formData.combat = duplicate(formData.armes ?? []); RdDItemArme.computeNiveauArmes(formData.combat, formData.competences); RdDItemArme.ajoutCorpsACorps(formData.combat, formData.competences, formData.data.carac); - formData.esquive = RdDItemCompetence.getEsquive(formData.competences); + formData.esquives = this.actor.getCompetences("Esquive").map(i => foundry.utils.deepClone(i.data)); formData.combat = RdDCombatManager.finalizeArmeList(formData.combat, formData.competences, formData.data.carac); this.armesList = formData.combat; diff --git a/module/actor.js b/module/actor.js index 7ec70482..faf7aaf4 100644 --- a/module/actor.js +++ b/module/actor.js @@ -313,13 +313,19 @@ export class RdDActor extends Actor { return Misc.templateData(this).compteurs.surenc?.value ?? 0; } /* -------------------------------------------- */ - getCompetence(name) { - return RdDItemCompetence.findCompetence(this.data.items, name); + getCompetence(idOrName, options={}) { + return RdDItemCompetence.findCompetence(this.data.items, idOrName, options); } + + getCompetences(name) { + return RdDItemCompetence.findCompetences(this.data.items, name); + } + /* -------------------------------------------- */ getObjet(id) { return id ? this.data.items.find(it => it.id == id) : undefined; } + listItemsData(type) { return this.filterItemsData(it => it.type == type); } @@ -329,16 +335,12 @@ export class RdDActor extends Actor { filterItems(filter) { return this.data.items.filter(it => filter(Misc.data(it))); } - getItemOfType(id, type) { - if (id && type) { - let itemById = this.data.items.find(it => it.id == id); - const itemData = Misc.data(itemById); - if (itemData.type == type) { - return itemById; - } - } - return undefined; + + getItemOfType(idOrName, type) { + return this.data.items.find(it => it.id == idOrName && it.type == type) + ?? Misc.findFirstLike(idOrName, this.data.items,{filter: it => it.type == type, description: type}); } + getMonnaie(id) { return this.getItemOfType(id, 'monnaie'); } @@ -3077,7 +3079,7 @@ export class RdDActor extends Actor { case 'chance-actuelle': case 'chance actuelle': return carac.chance; } - let entry = Misc.findFirstLike(name, Object.entries(carac), it => it[1].label, 'caractéristiques'); + let entry = Misc.findFirstLike(name, Object.entries(carac), {mapper:it => it[1].label, description: 'caractéristique'}); return entry.length>0 ? carac[entry[0]] : undefined; } diff --git a/module/item-competence.js b/module/item-competence.js index c2355ab1..4a7c246c 100644 --- a/module/item-competence.js +++ b/module/item-competence.js @@ -79,18 +79,12 @@ export class RdDItemCompetence extends Item { return Misc.data(competence)?.data.categorie; } static isDraconic(competence) { - return Misc.data(competence).data.categorie == 'draconic'; + return Misc.data(competence)?.data.categorie == 'draconic'; } /* -------------------------------------------- */ static getVoieDraconic(competences, voie) { - voie = Grammar.toLowerCaseNoAccent(voie); - return competences.find(it => RdDItemCompetence.isDraconic(it) && Grammar.toLowerCaseNoAccent(Misc.data(it).name).includes(voie)); - } - - /* -------------------------------------------- */ - static getEsquive(competences) { - return { name: 'Esquive', niveau: RdDItemCompetence.findCompetence(competences, 'Esquive')?.data.niveau ?? -6 }; + return RdDItemCompetence.findCompetence(competences.filter(it => RdDItemCompetence.isDraconic(it) ), voie); } /* -------------------------------------------- */ @@ -208,8 +202,22 @@ export class RdDItemCompetence extends Item { } /* -------------------------------------------- */ - static findCompetence(list, name) { - return Misc.findFirstLike(name, list, it => it.name, 'compétences'); + static findCompetence(list, idOrName, options = {}) { + options = mergeObject(options, { + filter: it => RdDItemCompetence.isCompetence(it), + description: 'compétence', + }); + return list.find(it => it.id == idOrName && RdDItemCompetence.isCompetence(it)) + ?? Misc.findFirstLike(idOrName, list, options); + } + + /* -------------------------------------------- */ + static findCompetences(list, name) { + return Misc.findAllLike(name, list, { filter: it => RdDItemCompetence.isCompetence(it), description: 'compétence' }); + } + + static isCompetence(item) { + return item.type == 'competence' || item.type == 'competencecreature'; } /* -------------------------------------------- */ diff --git a/module/misc.js b/module/misc.js index 2f4b106d..f6e0c7f1 100644 --- a/module/misc.js +++ b/module/misc.js @@ -134,33 +134,57 @@ export class Misc { /* -------------------------------------------- */ static findPlayer(name) { - return Misc.findFirstLike(name, game.users, it=>it.name,'joueurs'); + return Misc.findFirstLike(name, game.users, { description: 'joueur' }); } /* -------------------------------------------- */ - static findActor(name, actors = game.actors, description= 'acteurs') { - return Misc.findFirstLike(name, actors, it=>it.name, description); + static findActor(name, actors = game.actors) { + return Misc.findFirstLike(name, actors, { description: 'acteur' }); } /* -------------------------------------------- */ - static findFirstLike(value, elements, mapper = it=>it.name, description = 'valeurs') { - if (!value) { - return undefined; - } - value = Grammar.toLowerCaseNoAccent(value); - const subset = elements.filter(it => Grammar.toLowerCaseNoAccent(mapper(it)).includes(value)); + static findFirstLike(value, elements, options = {}) { + options = mergeObject({ + mapper: it => it.name, + preFilter: it => true, + description: 'valeur', + onMessage: m => ui.notifications.info(m) + }, options); + + const subset = this.findAllLike(value, elements, options); if (subset.length == 0) { - ui.notifications.info(`Pas de ${description} correspondant à ${value}`); - return undefined; + return undefined } - let single = subset.find(it => Grammar.toLowerCaseNoAccent(mapper(it)) == value); + if (subset.length == 1) { + return subset[0] + } + let single = subset.find(it => Grammar.toLowerCaseNoAccent(options.mapper(it)) == Grammar.toLowerCaseNoAccent(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)}`); - } + const choices = subset.map(it => options.mapper(it)).reduce((a, b) => `${a}
${b}`); + options.info(`Plusieurs choix de ${options.description}s possibles:
${choices}
Le premier sera choisi: ${mapToValue(single)}`); } return single; } + + static findAllLike(value, elements, options = {}) { + options = mergeObject({ + mapper: it => it.name, + preFilter: it => true, + description: 'valeur', + onMessage: m => ui.notifications.info(m) + }, options); + + if (!value) { + options.onMessage(`Pas de ${options.description} correspondant à une valeur vide`); + return []; + } + value = Grammar.toLowerCaseNoAccent(value); + const subset = elements.filter(options.preFilter) + .filter(it => Grammar.toLowerCaseNoAccent(options.mapper(it)).includes(value)); + if (subset.length == 0) { + options.onMessage(`Pas de ${options.description} correspondant à ${value}`); + } + return subset; + } } \ No newline at end of file