UNPKG

step-sequence-generator

Version:

A step sequence generator for figure skating programs

57 lines (56 loc) 2.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.WeightCalculator = void 0; const round2_1 = require("../../../utils/round2"); /** * @name WeightCalculator * @param S тип для selection * @param M тип для ChanceRatioMap * * */ class WeightCalculator { /** * count * @param args * @arg args.selection элементы * @arg args.chanceRatioMap шансы на выпадение элемента в selection в процентах * @arg args.itemKeyExtractor callback для опредления ключа для weightMap */ count(args) { const { selection, itemKeyExtractor, chanceRatioMap } = args; const groupItemCounted = this.groupAndCountItems(selection, itemKeyExtractor); return this.calcWeights(groupItemCounted, chanceRatioMap); } calcWeights(groupItemCounted, chanceRatioMap) { const totalItems = Array.from(groupItemCounted.values()).reduce((acc, cur) => acc + cur, 0); const weightMap = new Map(); for (let [key, currentItemAmount] of groupItemCounted.entries()) { const desirePercent = chanceRatioMap.get(key) || 0; const weight = this.calcItemWeight({ currentItemAmount, desirePercent, totalItems }); weightMap.set(key, weight); } return weightMap; } calcItemWeight(args) { const { currentItemAmount, desirePercent, totalItems } = args; const desireItemAmount = this.calcDesireItemAmount(totalItems, desirePercent); return this.calcWeight(currentItemAmount, desireItemAmount); } calcDesireItemAmount(totalItems, desirePercent) { const HUNDRED_PERCENT = 100; return (0, round2_1.round2)((totalItems / HUNDRED_PERCENT) * desirePercent); } calcWeight(currentItemAmount, desireItemAmount) { return (0, round2_1.round2)(desireItemAmount / currentItemAmount); } groupAndCountItems(selection, keyExtractor) { const map = new Map(); for (let item of selection) { const key = keyExtractor(item); const value = map.get(key) || 0; map.set(key, value + 1); } return map; } } exports.WeightCalculator = WeightCalculator;