All files / projects-dev/apps/mc/src/lib functions.ts

0% Statements 0/52
0% Branches 0/1
0% Functions 0/1
0% Lines 0/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53                                                                                                         
import type { Encounter, Hero, ModularSet, Scenario } from "$models";

export const notEmpty = <T>(value: T | null | undefined): value is T => value !== null && value !== undefined;

export const excludeHeroes = (heroes: Hero[], scenarioName: string, encounters: Map<string, Hero[]>): Hero[] =>
    heroes.filter((hero) => !(encounters.get(scenarioName) || []).includes(hero.name));

// eslint-disable-next-line max-params
export function getWeightedData(
    encounters: Encounter[],
    scenarios: Scenario[],
    modularsets: ModularSet[],
    heroes: Hero[],
): [number[], number[], number[]] {
    const base = encounters.length * 5;
    const weightedScenarios: number[] = [];
    const weightedModularSets: number[] = [];
    const weightedHeroes: number[] = [];

    const gamesHeroes = encounters
        .flatMap((encounter) => encounter.heroes)
        .reduce((accumulator, e) => accumulator.set(e, (accumulator.get(e) || 0) + 1), new Map());

    const gamesModularSets = encounters
        .flatMap((encounter) => encounter.modularSets)
        .map((module) => module)
        .reduce((accumulator, e) => accumulator.set(e, (accumulator.get(e) || 0) + 1), new Map());

    const gamesScenarios = encounters
        .map((encounter) => encounter.scenario)
        .reduce((accumulator, e) => accumulator.set(e, (accumulator.get(e) || 0) + 1), new Map());

    for (const module of modularsets) {
        const pastOccurrences = gamesModularSets.has(module.name) ? gamesModularSets.get(module.name) : 0;

        weightedModularSets.push(base - pastOccurrences);
    }

    for (const scenario of scenarios) {
        const pastOccurrences = gamesScenarios.has(scenario.name) ? gamesScenarios.get(scenario.name) : 0;

        weightedScenarios.push(base - pastOccurrences);
    }

    for (const hero of heroes) {
        const pastOccurrences = gamesHeroes.has(hero.name) ? gamesHeroes.get(hero.name) : 0;

        weightedHeroes.push(base - pastOccurrences);
    }

    return [weightedScenarios, weightedModularSets, weightedHeroes];
}