From 74ddc8893a7dc10d04311938394a60e48a44fb2d Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Fri, 9 Apr 2021 01:03:51 +0200 Subject: [PATCH] =?UTF-8?q?Fix=20xp=20des=20comp=C3=A9tences=20tronc=20#18?= =?UTF-8?q?2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit La méthode splice retourne les éléments supprimées, et non pas le tableau après suppression # Conflicts: # module/item-competence.js # module/misc.js --- module/item-competence.js | 12 ++++++------ module/misc.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/module/item-competence.js b/module/item-competence.js index 7f234e77..5d480b57 100644 --- a/module/item-competence.js +++ b/module/item-competence.js @@ -130,12 +130,12 @@ export class RdDItemCompetence extends Item { static computeEconomieXPTronc(competences) { return competenceTroncs.map( list => list.map(name => RdDItemCompetence.findCompetence(competences, name)) - // calcul du coût xp jusqu'au niveau 0 maximum - .map(it => RdDItemCompetence.computeDeltaXP(it?.data.base ?? -11, Math.min(it?.data.niveau ?? -11, 0))) - .sort((a, b) => b - a) // tri descendant - .splice(0, 1) // ignorer le coût xp le plus élevé - .reduce((a, b) => a + b, 0) - ).reduce((a, b) => a + b, 0); + // calcul du coût xp jusqu'au niveau 0 maximum + .map(it => RdDItemCompetence.computeDeltaXP(it?.data.base ?? -11, Math.min(it?.data.niveau ?? -11, 0))) + .sort(Misc.ascending()) + .splice(0, list.length-1) // prendre toutes les valeurs sauf l'une des plus élevées + .reduce(Misc.sum(), 0) + ).reduce(Misc.sum(), 0); } /* -------------------------------------------- */ diff --git a/module/misc.js b/module/misc.js index fcc89b22..538ea47f 100644 --- a/module/misc.js +++ b/module/misc.js @@ -18,6 +18,24 @@ export class Misc { return isPositiveNumber ? "+" + number : number } + static sum() { + return (a, b) => a + b; + } + + static ascending(orderFunction = x=>x) { + return (a, b) => Misc.sortingBy(orderFunction(a), orderFunction(b)); + } + + static descending(orderFunction = x=>x) { + return (a, b) => Misc.sortingBy(orderFunction(b), orderFunction(a)); + } + + static sortingBy(a, b) { + if (a > b) return 1; + if (a < b) return -1; + return 0; + } + /** * Converts the value to an integer, or to 0 if undefined/null/not representing integer * @param {*} value value to convert to an integer using parseInt