All files / projects-dev/apps/mc/src/lib suggested.svelte

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

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
<svelte:options customElement="mc-suggested" />

<script lang="ts">
    import { random } from "@jga/algorithms";

    import type { Encounter, Hero, ModularSet, Scenario } from "$models";
    import { DifficultyEnum, ResultEnum } from "$models";

    import { EncountersService, HeroesService, ModularSetsService, ScenariosService } from "$services";

    import { excludeHeroes, notEmpty } from "./functions";

    import Button from "$ui/button/src/button.svelte";
    import Select from "$lib/select.svelte";
    import Title from "$lib/title.svelte";

    // eslint-disable-next-line import/no-mutable-exports
    export let heroes: Hero[] = [];
    // eslint-disable-next-line import/no-mutable-exports
    export let encounters: Encounter[] = [];
    // eslint-disable-next-line import/no-mutable-exports
    export let scenarios: Scenario[] = [];

    let gamesScenarios: Map<string, number>;
    let unorderedScenarios: Map<string, number>;
    let orderedScenarios: Map<string, number>;

    let scenariosWinEncounters: Map<string, Hero[]>;
    let progressionsEncounters: Map<Scenario, Partial<Encounter>[]>;
    let suggestedEncounters: Partial<Encounter>[];

    $: {
        // order scenarios by play count
        // eslint-disable-next-line prefer-const
        gamesScenarios = encounters
            .map((encounter) => encounter.scenario.name)
            .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

        // eslint-disable-next-line prefer-const
        unorderedScenarios = scenarios.reduce((acc, e) => {
            acc.set(e, (acc.get(e) || 0) + (gamesScenarios.get(e.name) || 0));

            return acc;
        }, new Map());

        // eslint-disable-next-line prefer-const
        orderedScenarios = new Map(Array.from(unorderedScenarios.entries()).sort((a, b) => a[1] - b[1]));

        // eslint-disable-next-line prefer-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) => hero != null)
                    .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());

        // eslint-disable-next-line prefer-const
        progressionsEncounters = 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,
                        modularSets: scenario.modularSets,
                        difficulty: DifficultyEnum.STANDARD,
                        heroes: [hero],
                    });

                    progressionsEncounters.set(scenario, tryHeroes);
                }
            });
        });

        // eslint-disable-next-line prefer-const
        suggestedEncounters = [];
        // Pick a random one for each progression scenario
        progressionsEncounters.forEach((scenario) => {
            const scenarioEncounters = Array.from(scenario);
            const scenarioEncounter = scenarioEncounters[random(0, scenarioEncounters.length - 1)];

            suggestedEncounters.push(scenarioEncounter);
        });
    }

    async function add(event) {
        const { parentNode } = event.detail.parentNode;

        const isRandom = parentNode.classList.contains("random");

        const scenarioElement = parentNode.children[0];
        const modulesElement = parentNode.children[1];
        const difficultyElement = parentNode.children[2];
        const hero1Element = parentNode.children[3];
        const hero2Element = parentNode.children[4];
        const hero3Element = parentNode.children[5];
        const hero4Element = parentNode.children[6];
        const resultElement = parentNode.children[7];

        const scenariosService = new ScenariosService();
        const heroesService = new HeroesService();
        const modularSetsService = new ModularSetsService();

        const encounterHeroes = [];

        const scenarioValue = isRandom ? scenarioElement.children[0].value : scenarioElement.innerHTML;

        const scenario = await scenariosService.find(scenarioValue);
        const hero1 = await heroesService.find(hero1Element.children[0].value);
        encounterHeroes.push(hero1);

        if (hero2Element.children[0].value !== "") {
            const hero2 = await heroesService.find(hero2Element.children[0].value);
            encounterHeroes.push(hero2);
        }

        if (hero3Element.children[0].value !== "") {
            const hero3 = await heroesService.find(hero3Element.children[0].value);
            encounterHeroes.push(hero3);
        }

        if (hero4Element.children[0].value !== "") {
            const hero4 = await heroesService.find(hero4Element.children[0].value);
            encounterHeroes.push(hero4);
        }

        const difficulty =
            Object.values(DifficultyEnum).find((x) => x === difficultyElement.children[0].value) ||
            DifficultyEnum.STANDARD;

        const result =
            Object.values(ResultEnum).find((x) => x === resultElement.children[0].value) || ResultEnum.FORFEIT;

        // Retrieve modular sets
        const encounterModularSetsValues = isRandom
            ? Array.from(modulesElement.children[0].selectedOptions, (option) => option.value)
            : modulesElement.innerHTML.split(", ");

        const rawEncounterModularSets = await Promise.all(
            encounterModularSetsValues.map((modularSet) => modularSetsService.find(modularSet)),
        );
        const encounterModules: ModularSet[] = rawEncounterModularSets.filter(notEmpty);

        const encounter = {
            id: null,
            scenario,
            heroes: encounterHeroes,
            modularSets: encounterModules,
            difficulty,
            result,
        };

        // eslint-disable-next-line no-console
        console.log(encounter);

        const encountersService = new EncountersService();
        encountersService
            .save(encounter)
            .then(() => window.location.reload())
            // eslint-disable-next-line no-console
            .catch((error) => console.error(error));
    }
</script>

<Title title="Suggested encounters" />

<table>
    <thead>
        <tr>
            <th>Scenario</th>
            <th>Modular Sets</th>
            <th>Difficulty</th>
            <th>Hero 1</th>
            <th>Hero 2</th>
            <th>Hero 3</th>
            <th>Hero 4</th>
            <th>Result</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <!-- Add progression encounters -->
        {#each suggestedEncounters as encounter}
            <tr class="suggested">
                <td>{encounter.scenario.name}</td>
                <td>{encounter.modularSets?.map((module) => module.name).join(", ") || ""}</td>
                <td>
                    <Select defaultValue="{DifficultyEnum.STANDARD}" source="{Object.values(DifficultyEnum)}" />
                </td>
                <td>
                    <Select
                        defaultValue="{encounter.heroes[0].name}"
                        source="{excludeHeroes(heroes, encounter.scenario.name, scenariosWinEncounters).map(
                            (hero) => hero.name,
                        )}"
                    />
                </td>
                <td>
                    <Select
                        source="{excludeHeroes(heroes, encounter.scenario.name, scenariosWinEncounters).map(
                            (hero) => hero.name,
                        )}"
                    />
                </td>
                <td>
                    <Select
                        source="{excludeHeroes(heroes, encounter.scenario.name, scenariosWinEncounters).map(
                            (hero) => hero.name,
                        )}"
                    />
                </td>
                <td>
                    <Select
                        source="{excludeHeroes(heroes, encounter.scenario.name, scenariosWinEncounters).map(
                            (hero) => hero.name,
                        )}"
                    />
                </td>
                <td>
                    <Select source="{Object.values(ResultEnum)}" />
                </td>
                <td>
                    <Button label="Add" on:buttonClicked="{add}" />
                </td>
            </tr>
        {/each}
    </tbody>
</table>