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

85.22% Statements 173/203
70.45% Branches 31/44
72.09% Functions 31/43
87.77% Lines 165/188

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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361  5x 5x 5x 5x 5x 5x 5x   5x 2x   2x 5x 2x 2x   5x     5x 2x   2x 2x   2x 2x 2x 2x 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   5x 5x     5x 5x 5x 5x   5x   2x 2x   2x 2x 2x 2x 2x       2x 4x 2x 3x 2x 2x 4x 2x             2x     2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                     2x 2x 2x   2x 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 5x 5x 5x 5x 5x 5x                                     5x 5x 5x 5x 5x 5x 5x 5x 5x   5x 5x 5x 5x 5x 5x 2x 2x 5x 2x 2x 2x 5x 2x 2x 5x           5x 2x 2x 9x     5x 4x 4x 19x     5x                                                                                                                                                                                                                  
import { random } from "jga-algorithms-random";
import { DifficultyEnum, Encounter, Hero, ModularSet, ResultEnum, Scenario } from "../@models";
import { convertRatingToStars, createSelectElement, createTitle } from "../helper";
import { EncountersService, HeroesService } from "../services";
 
export const loadProgressionEncounters = (
    body: HTMLBodyElement,
    encounters: Encounter[],
    heroes: Hero[],
    scenarios: Scenario[],
    modules: ModularSet[],
): 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]));
 
    // eslint-disable-next-line compat/compat
    // 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.heroes)
                .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,
                    heroes: [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 rating = document.createElement("th");
    rating.innerText = "Rating";
    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(rating);
    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.map((modularSet) => modularSet.name).join(", ") : "";
 
        // const difficulty = document.createElement("td");
        // difficulty.innerText = encounter.difficulty as string;
 
        const allreadyWonHeroes = scenariosWinEncounters.get(encounter.scenario?.name) || [];
        const excludedHeroes = heroes.filter((hero) => !allreadyWonHeroes.includes(hero.name));

        const hero1 = document.createElement("td");
        const hero1Element = createSelectElement(
            encounter.heroes[0],
            excludedHeroes.map((hero) => hero.name),
        );
        hero1.appendChild(hero1Element);

        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 modularSetsRating = encounter.modularSets
            ?.map((modularSet) => modularSet.rating)
            .reduce((acc, rating) => (acc += rating), 0);
 
        const ratingScore = (modularSetsRating || 0) + encounter.scenario.rating_standard_multi;
        const rating = document.createElement("td");
        rating.innerText = convertRatingToStars(ratingScore);
 
        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;
 
            if ("" !== hero2Element.value) {
                encounter.heroes.push(await heroesService.find(hero2Element.value));
            }
 
            encounter.result = Object.values(ResultEnum).find((x) => x === resultElement.value) || undefined;
            console.log(encounter);
 
            const encountersService = new EncountersService();
            encountersService.save(encounter).then(() => 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(rating);
        row.appendChild(result);
        row.appendChild(add);

        table.appendChild(row);
        // });
    });
 
    // Add random encounters
    table.appendChild(addRandomEncounter(encounters, scenarios, modules, heroes));
 
    body.appendChild(table);
};

const addRandomEncounter = (
    encounters: Encounter[],
    scenarios: Scenario[],
    modularsets: ModularSet[],
    heroes: Hero[],
): HTMLTableRowElement => {
    const row = document.createElement("tr");
 
    const [weightedScenarios, weightedModularSets, weightedHeroes] = getWeightedData(
        encounters,
        scenarios,
        modularsets,
        heroes,
    );
 
    const randomScenario = weightedScenarios[random(0, weightedScenarios.length - 1)];
    const randomModularSet = weightedModularSets[random(0, weightedModularSets.length - 1)];
    const randomHero = weightedHeroes[random(0, weightedHeroes.length - 1)];
 
    const scenario = document.createElement("td");
    const secnarioElement = createSelectElement(
        randomScenario,
        scenarios.map((scenario) => scenario.name),
    );
    scenario.appendChild(secnarioElement);
 
    const modularSet = document.createElement("td");
    const modularSetElement = createSelectElement(
        randomModularSet,
        modularsets.map((modularSet) => modularSet.name),
    );
    modularSet.appendChild(modularSetElement);
 
    const hero1 = document.createElement("td");
    const hero1Element = createSelectElement(
        randomHero,
        heroes.map((hero) => hero.name),
    );
    hero1.appendChild(hero1Element);
 
    const hero2 = document.createElement("td");
    const hero2Element = createSelectElement(
        null,
        heroes.map((hero) => hero.name),
    );
    hero2.appendChild(hero2Element);
 
    const ratingScore =
        (null != randomModularSet ? randomModularSet.rating : 0) +
        (null != randomScenario ? randomScenario.rating_standard_multi : 0);
    const rating = document.createElement("td");
    rating.innerText = convertRatingToStars(ratingScore);
 
    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();
 
        const heroes = [randomHero];
 
        if ("" !== hero2Element.value) {
            heroes.push(await heroesService.find(hero2Element.value));
        }
 
        const result = Object.values(ResultEnum).find((x) => x === resultElement.value) || undefined;
 
        const encounter: Encounter = new Encounter({
            id: null,
            difficulty: DifficultyEnum.STANDARD,
            scenario: randomScenario,
            modularSets: [randomModularSet],
            heroes,
            result,
        });
 
        console.log(encounter);
 
        const encountersService = new EncountersService();
        encountersService.save(encounter).then(() => window.location.reload());
    };
    add.appendChild(button);
 
    row.appendChild(scenario);
    row.appendChild(modularSet);
    row.appendChild(hero1);
    row.appendChild(hero2);
    row.appendChild(rating);
    row.appendChild(result);
    row.appendChild(add);
 
    return row;
};
 
const getWeightedData = (
    encounters: Encounter[],
    scenarios: Scenario[],
    modularsets: ModularSet[],
    heroes: Hero[],
): [Scenario[], ModularSet[], Hero[]] => {
    const base = encounters.length * 5;
    const weightedScenarios: Scenario[] = [];
    const weightedModularSets: ModularSet[] = [];
    const weightedHeroes: Hero[] = [];
 
    const gamesHeroes = encounters
        .flatMap((encounter) => encounter.heroes)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
 
    const gamesModularSets = encounters
        .flatMap((encounter) => encounter.modularSets)
        .map((module) => module)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
 
    const gamesScenarios = encounters
        .map((encounter) => encounter.scenario)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
 
    modularsets.forEach((module) => {
        const pastOccurrences = gamesModularSets.has(module.name) ? gamesModularSets.get(module.name) : 0;
 
        for (let i = 0; i < base - pastOccurrences; i++) {
            weightedModularSets.push(module);
        }
    });
 
    scenarios.forEach((scenario) => {
        const pastOccurrences = gamesScenarios.has(scenario.name) ? gamesScenarios.get(scenario.name) : 0;
 
        for (let i = 0; i < base - pastOccurrences; i++) {
            weightedScenarios.push(scenario);
        }
    });
 
    heroes.forEach((hero) => {
        const pastOccurrences = gamesHeroes.has(hero.name) ? gamesHeroes.get(hero.name) : 0;
 
        for (let i = 0; i < base - pastOccurrences; i++) {
            weightedHeroes.push(hero);
        }
    });
 
    return [weightedScenarios, weightedModularSets, weightedHeroes];
};