All files / games/mc/src/sections stats.ts

44.74% Statements 51/114
0% Branches 0/28
6.12% Functions 3/49
44.74% Lines 51/114

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  6x 6x 6x 6x 6x 2x   2x 2x 2x 2x   2x 2x       2x         2x 2x             2x   2x 2x     2x       2x 8x   2x   2x 2x       2x         2x 8x   2x   2x 2x     2x       2x 2x         2x   2x 2x         2x           2x 2x                 2x   2x 2x         2x 2x                   2x   2x 2x       2x 2x                     2x   6x                                                                                                                    
import { createTitle, createStatsTable, createSubTitle } from "../helper";
 
import { AspectEnum, DifficultyEnum, Encounter, Hero, ModularSet, ResultEnum, Scenario } from "../@models";
 
export const loadStats = (
    body: HTMLBodyElement,
    encounters: Encounter[],
    heroes: Hero[],
    scenarios: Scenario[],
    modularSets: ModularSet[],
    // eslint-disable-next-line max-params
): void => {
    createTitle("Stats", body);
 
    // games
    createSubTitle("Games", body);
    const games = document.createElement("div");
    games.innerText = encounters.length.toString();
 
    body.appendChild(games);

    // games by hero
    createSubTitle("Games / Heroes / Win %", body);
    const gamesHeroes = encounters
        .flatMap((encounter) => encounter.heroes)
        .map((hero) => hero.name)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

    const gamesHeroesWins = encounters
        .filter((encounter) => encounter.result === ResultEnum.WON)
        .flatMap((encounter) => encounter.heroes)
        .map((hero) => hero.name)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
 
    const gamesHeroesLastDate = new Map<string, string | undefined>();
    encounters
        .filter((encounter) => null != encounter.date)
        .forEach((encounter) => {
            encounter.heroes.forEach((hero) => {
                gamesHeroesLastDate.set(hero.name, encounter.date);
            });
        });
    createStatsTable(gamesHeroes, heroes, gamesHeroesWins, "Games / Heroes / Win %", body, gamesHeroesLastDate);
 
    // games by difficulty
    createSubTitle("Games / Difficulties / Win %", body);
    const gamesDifficulties = encounters
        .map((encounter) => encounter.difficulty)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

    const gamesDifficultiesWins = encounters
        .filter((encounter) => encounter.result === ResultEnum.WON)
        .map((encounter) => encounter.difficulty)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

    const difficulties = Object.values(DifficultyEnum).map((difficulty) => {
        return { name: difficulty };
    });
 
    createStatsTable(gamesDifficulties, difficulties, gamesDifficultiesWins, "Games / Difficulties / Win %", body);
 
    // games by aspects
    createSubTitle("Games / Aspects / Win %", body);
    const gamesAspects = encounters
        .flatMap((encounter) => encounter.heroes)
        .flatMap((hero) => hero.aspects)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

    const gamesAspectsWins = encounters
        .filter((encounter) => encounter.result === ResultEnum.WON)
        .flatMap((encounter) => encounter.heroes)
        .flatMap((hero) => hero.aspects)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

    const aspects = Object.values(AspectEnum).map((aspect) => {
        return { name: aspect };
    });
 
    createStatsTable(gamesAspects, aspects, gamesAspectsWins, "Games / Aspects / Win %", body);

    // games by scenario
    createSubTitle("Games / Scenarios / Win %", body);
    const gamesScenarios = encounters
        .map((encounter) => encounter.scenario.name)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

    const gamesScenariosWins = encounters
        .filter((encounter) => encounter.result === ResultEnum.WON)
        .map((encounter) => encounter.scenario.name)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
 
    const gamesScenariosLastDate = new Map<string, string | undefined>();
    encounters
        .filter((encounter) => null != encounter.date)
        .forEach((encounter) => {
            gamesScenariosLastDate.set(encounter.scenario.name, encounter.date);
        });
 
    createStatsTable(
        gamesScenarios,
        scenarios,
        gamesScenariosWins,
        "Games / Scenarios / Win %",
        body,
        gamesScenariosLastDate,
    );
 
    // games by modular sets
    createSubTitle("Games / Modular Sets / Win %", body);

    const gamesModularSets = encounters
        .filter((encounter) => null != encounter.modularSets)
        .flatMap((encounter) => encounter.modularSets)
        .map((modularSet) => modularSet?.name)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

    const gamesModularSetsWins = encounters
        .filter((encounter) => encounter.result === ResultEnum.WON)
        .filter((encounter) => null != encounter.modularSets)
        .flatMap((encounter) => encounter.modularSets)
        .map((modularSet) => modularSet?.name)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

    const gamesModularSetsLastDate = new Map<string, string | undefined>();
    encounters
        .filter((encounter) => null != encounter.date)
        .forEach((encounter) => {
            if (null != encounter.modularSets) {
                encounter.modularSets.forEach((modularSet) => {
                    gamesModularSetsLastDate.set(modularSet.name, encounter.date);
                });
            }
        });

    createStatsTable(
        gamesModularSets,
        modularSets,
        gamesModularSetsWins,
        "Games / Modular Sets / Win %",
        body,
        gamesModularSetsLastDate,
    );
 
    // number of heroes by games
    createSubTitle("Number of heroes / Games", body);
    const nbHeroesGames = encounters.reduce((acc, e) => {
        const nbHeroes = e.heroes.length;
 
        acc.set(nbHeroes, (acc.get(nbHeroes) || 0) + 1);
 
        return acc;
    }, new Map());
 
    const tableHeroesGames = document.createElement("table");
 
    new Map([...nbHeroesGames].sort()).forEach((value, key) => {
        const itemLine = document.createElement("tr");
        const itemName = document.createElement("td");
        const itemCount = document.createElement("td");
 
        itemName.innerText = key;
        itemCount.innerText = `${value} (${((value / encounters.length) * 100).toFixed(0)} %)`;
 
        itemLine.appendChild(itemName);
        itemLine.appendChild(itemCount);
 
        tableHeroesGames.appendChild(itemLine);
    });
 
    body.appendChild(tableHeroesGames);
 
    // results
    createSubTitle("Results", body);
    const gamesResults = encounters
        .map((encounter) => encounter.result)
        .sort()
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());
 
    const tableResults = document.createElement("table");
 
    gamesResults.forEach((value, key) => {
        const itemLine = document.createElement("tr");
        const itemName = document.createElement("td");
        const itemCount = document.createElement("td");
 
        itemName.innerText = key;
        itemCount.innerText =
            key === ResultEnum.WON ? `${value} (${((value / encounters.length) * 100).toFixed(0)} %)` : value;
 
        itemLine.appendChild(itemName);
        itemLine.appendChild(itemCount);
 
        tableResults.appendChild(itemLine);
    });
 
    body.appendChild(tableResults);
};