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

46.77% Statements 29/62
0% Branches 0/14
5.56% Functions 1/18
46.77% Lines 29/62

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  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   5x                                                        
import { createTitle, createStatsTable, createSubTitle } from "../helper";
 
import { Encounter, Hero, ResultEnum, Scenario } from "../@models";
 
export const loadStats = (
    body: HTMLBodyElement,
    encounters: Encounter[],
    heroes: Hero[],
    scenarios: Scenario[],
): 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());

    createStatsTable(gamesHeroes, heroes, gamesHeroesWins, body);
 
    // games by scenario
    createSubTitle("Games / Scenarios / Win %", body);
    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 gamesScenariosWins = encounters
        .filter((encounter) => encounter.result === ResultEnum.WON)
        .map((encounter) => encounter.scenario.name)
        // .map((scenario) => scenario)
        .reduce((acc, e) => acc.set(e, (acc.get(e) || 0) + 1), new Map());

    createStatsTable(gamesScenarios, scenarios, gamesScenariosWins, body);

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