All files / games/mc/src/sections progression-encounters.ts

88.54% Statements 85/96
72.22% Branches 13/18
78.95% Functions 15/19
90.32% Lines 84/93

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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190  6x 6x 6x 6x 6x 6x 6x   5x 2x   2x 5x 2x 2x   5x 5x 5x 2x   2x 2x   2x 2x 2x 8x 2x 2x 2x 2x 2x   5x 5x 2x 4x   3x 2x   3x 3x           3x               5x 5x 5x 5x 5x 5x         5x 5x 5x 5x         5x 5x 5x 5x     5x 5x     5x 5x 5x   5x   2x 2x   2x 2x 2x         2x 2x 2x 4x 2x             2x 2x 2x 2x 2x 2x 2x 2x                           2x 2x     2x 2x     2x 2x 2x     5x   6x                                                                                
import { random } from "jga-algorithms-random";
import { DifficultyEnum, Encounter, Hero, ResultEnum, Scenario } from "../@models";
import { createSelectElement, createTitle } from "../helper";
import { EncountersService, HeroesService } from "../services";
 
export const loadProgressionEncounters = (
    body: HTMLBodyElement,
    encounters: Encounter[],
    heroes: Hero[],
    scenarios: Scenario[],
): void => {
    // order scenarios by play count
    const gamesScenarios = encounters
        .map((encounter) => encounter.scenario.name)
        // .map((scenario) => scenario)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
    const unorderedScenarios = scenarios.reduce((acc, e) => {
        acc.set(e, (acc.get(e) || 0) + (gamesScenarios.get(e.name) || 0));
 
        return acc;
    }, new Map());
 
    consEt orderedScenarios = new Map([...unorderedScenarios.entries()].sort((a, b) => a[1] - b[1]));
 
    console.table(orderedScenarios);
 
    const scenariosWinEncounters = encounters
        .filter((encounter) => encounter.result === ResultEnum.WON)
        .reduce((acc, e) => {
            if (!acc.has(e.scenario.name)) {
                acc.set(e.scenario.name, []);
            }
 
            const newHeroes = acc.get(e.scenario.name);
            [e]
                .flatMap((encounter) => [encounter.hero1, encounter.hero2, encounter.hero3, encounter.hero4])
                .filter((hero) => null != hero)
                .map((hero) => hero.name)
                .filter((hero, i, array) => array.indexOf(hero) === i)
                .forEach((hero) => newHeroes.push(hero));
 
            acc.set(e.scenario.name, newHeroes.sort());
 
            return acc;
        }, new Map());
 
    const progressionsEncounters: Map<Scenario, Partial<Encounter>[]> = new Map();
    Array.from(orderedScenarios.keys()).forEach((scenario) => {
        heroes.forEach((hero) => {
            if (
                !scenariosWinEncounters.has(scenario.name) ||
                !scenariosWinEncounters.get(scenario.name).includes(hero.name)
            ) {
                if (!progressionsEncounters.has(scenario)) {
                    progressionsEncounters.set(scenario, []);
                }
 
                const tryHeroes = progressionsEncounters.get(scenario) as Partial<Encounter>[];
                tryHeroes.push({
                    scenario: scenario,
                    modularSets: scenario.modularSets,
                    difficulty: DifficultyEnum.STANDARD,
                    hero1: hero,
                });
 
                progressionsEncounters.set(scenario, tryHeroes);
            }
        });
    });
 
    // console.table(scenariosLostEncounters);
    // eslint-disable-next-line compat/compat
    // console.table(scenariosWinEncounters);
    // eslint-disable-next-line compat/compat
    console.table(progressionsEncounters);
 
    createTitle("Suggested encounters", body);
 
    const table = document.createElement("table");
    const firstRow = document.createElement("tr");
    const scenario = document.createElement("th");
    scenario.innerText = "Scenario";
    // const modularsets = document.createElement("th");
    // modularsets.innerText = "Modular Sets";
    // const difficulty = document.createElement("th");
    // difficulty.innerText = "Difficulty";
    const hero1 = document.createElement("th");
    hero1.innerText = "Hero 1";
    const hero2 = document.createElement("th");
    hero2.innerText = "Hero 2";
    // const hero3 = document.createElement("th");
    // hero3.innerText = "Hero 3";
    // const hero4 = document.createElement("th");
    // hero4.innerText = "Hero 4";
    const result = document.createElement("th");
    result.innerText = "Result";
    const add = document.createElement("th");
 
    firstRow.appendChild(scenario);
    // firstRow.appendChild(modularsets);
    // firstRow.appendChild(difficulty);
    firstRow.appendChild(hero1);
    firstRow.appendChild(hero2);
    // firstRow.appendChild(hero3);
    // firstRow.appendChild(hero4);
    firstRow.appendChild(result);
    firstRow.appendChild(add);
 
    table.appendChild(firstRow);
 
    // load encounters
    progressionsEncounters.forEach((scenario) => {
        // pick a random one
        const encounters = Array.from(scenario);
        const encounter = encounters[random(0, encounters.length - 1)];
 
        // scenario.forEach((encounter) => {
        const row = document.createElement("tr");
 
        const scenarioElement = document.createElement("td");
        scenarioElement.innerText = encounter.scenario?.name as string;

        // const modularsets = document.createElement("td");
        // modularsets.innerText = null != encounter.modularSets ? encounter.modularSets.join(", ") : "";
 
        // const difficulty = document.createElement("td");
        // difficulty.innerText = encounter.difficulty as string;

        const hero1 = document.createElement("td");
        hero1.innerText = encounter.hero1?.name as string;

        const hero2 = document.createElement("td");
        const hero2Element = createSelectElement(
            null,
            heroes.map((hero) => hero.name),
        );
        hero2.appendChild(hero2Element);
 
        // const hero3 = document.createElement("td");
        // const hero3Element = createSelectElement(null, Object.values(HeroEnum));
        // hero3.appendChild(hero3Element);
 
        // const hero4 = document.createElement("td");
        // const hero4Element = createSelectElement(null, Object.values(HeroEnum));
        // hero4.appendChild(hero4Element);
 
        const result = document.createElement("td");
        const resultElement = createSelectElement(null, Object.values(ResultEnum));
        result.appendChild(resultElement);
 
        const add = document.createElement("td");
        const button = document.createElement("button");
        button.type = "button";
        button.innerText = "Add !";
        button.onclick = async () => {
            const heroesService = new HeroesService();
 
            encounter.id = null;
            encounter.hero2 = "" !== hero2Element.value ? await heroesService.find(hero2Element.value) : undefined;
            // encounter.hero3 = Object.values(HeroEnum).find((x) => x === hero3Element.value) || undefined;
            // encounter.hero4 = Object.values(HeroEnum).find((x) => x === hero4Element.value) || undefined;
            encounter.result = Object.values(ResultEnum).find((x) => x === resultElement.value) || undefined;
            console.log(encounter);
 
            const encountersService = new EncountersService();
            encountersService.save(encounter).then((result) => {
                console.log(result);
 
                window.location.reload();
            });
        };
        add.appendChild(button);
 
        row.appendChild(scenarioElement);
        // row.appendChild(modularsets);
        // row.appendChild(difficulty);
        row.appendChild(hero1);
        row.appendChild(hero2);
        // row.appendChild(hero3);
        // row.appendChild(hero4);
        row.appendChild(result);
        row.appendChild(add);
 
        table.appendChild(row);
        // });
    });
 
    body.appendChild(table);
};