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

86.14% Statements 87/101
37.5% Branches 6/16
64.71% Functions 11/17
89.69% Lines 87/97

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  6x 6x 6x 6x 6x 6x 5x 2x   2x 2x   2x 2x 2x 8x 8x 4x 2x 2x   5x 5x                   2x 4x   2x 2x     2x 2x           2x             5x 5x 5x 5x 5x 5x         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 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                         2x 2x     2x 2x 2x 2x 2x 2x 2x     5x   6x                                                                  
import { DifficultyEnum, Encounter, Hero, HeroEnum, ResultEnum, Scenario, ScenarioEnum } from "../@models";
import { createSelectElement, createTitle } from "../helper";
import { EncountersService } from "../services";
 
export const loadProgressionEncounters = (
    body: HTMLBodyElement,
    encounters: Encounter[],
    heroes: Hero[],
    scenarios: Scenario[],
): void => {
    consEt scenariosWinEncounters = encounters
        .filter((encounter) => encounter.result === ResultEnum.WON)
        .reduce((acc, e) => {
            if (!acc.has(e.scenario)) {
                acc.set(e.scenario, []);
            }
 
            const newHeroes = acc.get(e.scenario);
            [e]
                .flatMap((encounter) => [encounter.hero1, encounter.hero2, encounter.hero3, encounter.hero4])
                .filter((hero) => null !== hero)
                .filter((hero, i, array) => array.indexOf(hero) === i)
                .forEach((hero) => newHeroes.push(hero));
 
            acc.set(e.scenario, newHeroes.sort());
 
            return acc;
        }, new Map());
 
    const progressionsEncounters: Map<Scenario, Partial<Encounter>[]> = new Map();
    scenarios.forEach((scenario) => {
        // const allies = heroes
        //     .map((hero) => hero.name)
        //     .filter(
        //         (hero) =>
        //             (scenariosWinEncounters.has(scenario.name)
        //      E           ? scenariosWinEncounters.get(scenario.name)
        //                 : []
        //             ).indexOf(hero) < 0,
        //     );
 
        heroes.forEach((hero) => {
            if (
                !scenariosWinEncounters.has(scenario.name) ||
                !scenariosWinEncounters.get(scenario.name).includes(hero.name)
            ) {
                if (!progressionsEncounters.has(scenario)) {
                    progressionsEncounters.set(scenario, []);
                }
 
                // const ally = allies[random(0, allies.length - 1)];
 
                const tryHeroes = progressionsEncounters.get(scenario) as Partial<Encounter>[];
                tryHeroes.push({
                    scenario: scenario.name as ScenarioEnum,
                    modularSets: scenario.defaultModularSets,
                    difficulty: DifficultyEnum.STANDARD,
                    hero1: hero.name,
                });
 
                progressionsEncounters.set(scenario, tryHeroes);
            }
        });
    });
 
    // console.table(scenariosLostEncounters);
    // 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) => {
        scenario.forEach((encounter) => {
            const row = document.createElement("tr");
 
            const scenario = document.createElement("td");
            scenario.innerText = encounter.scenario 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 as string;

            const hero2 = document.createElement("td");
            const hero2Element = createSelectElement(null, Object.values(HeroEnum));
            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 = () => {
                encounter.id = null;
                encounter.hero2 = Object.values(HeroEnum).find((x) => x === 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(scenario);
            // 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);
};