Fix recherche competence

La recherche de compétence cherchait dans tous les items (y compris les
armes), ce qui affichait des messages ui
This commit is contained in:
Vincent Vandemeulebrouck 2021-11-20 01:04:22 +01:00
parent 0f655bd89d
commit f91dfd04a2
4 changed files with 73 additions and 40 deletions

View File

@ -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;

View File

@ -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;
}

View File

@ -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';
}
/* -------------------------------------------- */

View File

@ -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}<br>${b}`);
ui.notifications.info(`Plusieurs choix de ${description} possibles:<br>${choices}<br>Le premier sera choisi: ${mapper(single)}`);
}
const choices = subset.map(it => options.mapper(it)).reduce((a, b) => `${a}<br>${b}`);
options.info(`Plusieurs choix de ${options.description}s possibles:<br>${choices}<br>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;
}
}