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

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

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

<script lang="ts">
    import { weightedRandom } 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, getWeightedData, 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 modules: ModularSet[] = [];
    // eslint-disable-next-line import/no-mutable-exports
    export let scenarios: Scenario[] = [];

    let scenariosWinEncounters: Map<string, Hero[]>;
    let randomEncounters: Partial<Encounter>[];

    $: {
        // eslint-disable-next-line prefer-const
        scenariosWinEncounters = encounters
            .filter((encounter) => encounter.result === ResultEnum.WON)
            .reduce((accumulator, e) => {
                if (!accumulator.has(e.scenario.name)) {
                    accumulator.set(e.scenario.name, []);
                }

                const newHeroes = accumulator.get(e.scenario.name);
                for (const hero of [e]
                    .flatMap((encounter) => encounter.heroes)
                    .filter((hero) => hero != undefined)
                    .map((hero) => hero.name)
                    .filter((hero, index, array) => array.indexOf(hero) === index))
                    newHeroes.push(hero);

                accumulator.set(e.scenario.name, newHeroes.sort());

                return accumulator;
            }, new Map());

        // eslint-disable-next-line prefer-const
        randomEncounters = [];

        if (scenarios.length > 0 && modules.length > 0 && heroes.length > 0) {
            // Add 5 random scenario
            [1, 2, 3, 4, 5].forEach(() => {
                const [weightedScenarios, weightedModularSets, weightedHeroes] = getWeightedData(
                    encounters,
                    scenarios,
                    modules,
                    heroes,
                );

                const randomScenario = weightedRandom(scenarios, weightedScenarios);
                const randomModularSet = weightedRandom(modules, weightedModularSets);
                const randomHero = weightedRandom(heroes, weightedHeroes);

                const encounter = {
                    scenario: randomScenario,
                    heroes: [randomHero],
                    modularSets: [randomModularSet],
                };

                randomEncounters.push(encounter);
            });
        }
    }

    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 resultElement = parentNode.children[5];

        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);
        }

        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,
        };

        // console.log(encounter);

        const encountersService = new EncountersService();
        encountersService.save(encounter).then(() => window.location.reload());
    }
</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>Result</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        <!-- Add 5 random encounters -->
        {#each randomEncounters as encounter}
            <tr class="random">
                <td>
                    <Select
                        defaultValue="{encounter.scenario.name}"
                        source="{scenarios.map((scenario) => scenario.name)}"
                    />
                </td>
                <td>
                    <Select
                        defaultValue="{encounter.modularSets[0].name}"
                        source="{modules.map((module) => module.name)}"
                        multiple="{true}"
                    />
                </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="{Object.values(ResultEnum)}" />
                </td>
                <td>
                    <Button label="Add" on:buttonClicked="{add}" />
                </td>
            </tr>
        {/each}
    </tbody>
</table>