All files / games/mc/src helper.ts

100% Statements 58/58
77.78% Branches 14/18
100% Functions 8/8
100% Lines 58/58

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  7x 7x 7x 22x 22x 22x   7x 7x 35x 35x 35x   7x 7x 15x 15x 15x 15x 5x 5x 5x   15x 15x   7x 7x 6x 6x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   6x   7x 7x 14x 14x 14x 1x   14x 14x 27x 27x 27x 27x     1x   27x   14x   7x   7x 16x                                                                    
import { DifficultyEnum, Hero, ModularSet, ResultEnum, Scenario } from "./@models";
 
export const createTitle = (title: string, body: HTMLBodyElement): void => {
    const header = document.createElement("h2");
    header.innerText = title;
 
    body.appendChild(header);
};
 
export const createSubTitle = (title: string, body: HTMLBodyElement): void => {
    const header = document.createElement("h3");
    header.innerText = title;
 
    body.appendChild(header);
};
 
export const createSection = (title: string, data: Hero[] | Scenario[] | ModularSet[], body: HTMLBodyElement): void => {
    createSubTitle(title, body);
 
    const section = document.createElement("section");
 
    const list = document.createElement("ul");
 
    data.forEach((item: Hero | Scenario | ModularSet) => {
        const element = document.createElement("li");
        element.innerText = `${item.name}`;
 
        list.appendChild(element);
    });
 
    section.appendChild(list);
 
    body.appendChild(section);
};
 
export const createStatsTable = (
    data: Map<string, number>,
    source: Hero[] | Scenario[],
    wins: Map<string, number>,
    body: HTMLBodyElement,
): void => {
    const table = document.createElement("table");
 
    source.forEach((item: Hero | Scenario) => {
        const itemLine = document.createElement("tr");
        const itemName = document.createElement("td");
        const itemCount = document.createElement("td");
        const itemRate = document.createElement("td");
 
        const gameNumber = (data.has(item.name) ? data.get(item.name) : 0) as number;
        const winRate = (
            gameNumber !== 0 ? ((wins.has(item.name) ? (wins.get(item.name) as number) : 0) / gameNumber) * 100 : 0
        ) as number;
 
        itemName.innerText = item.name;
        itemCount.innerText = gameNumber.toString();
        itemRate.innerText = ` (${winRate.toFixed(0).toString()} %) `;
 
        itemLine.appendChild(itemName);
        itemLine.appendChild(itemCount);
        itemLine.appendChild(itemRate);
 
        table.appendChild(itemLine);
    });
 
    body.appendChild(table);
};
 
export const createSelectElement = (
    defaultValue: DifficultyEnum | Hero | ModularSet | ResultEnum | Scenario | null,
    source: DifficultyEnum[] | ResultEnum[] | string[],
    multiple = false,
): HTMLSelectElement => {
    const selectElement = document.createElement("select");
    const selectOption = document.createElement("option");
 
    if (true === multiple) {
        selectElement.setAttribute("multiple", "true");
    }
 
    selectElement.appendChild(selectOption);
 
    for (const item in source) {
        const option = document.createElement("option");
        option.value = source[item];
        option.innerText = source[item];
 
        if (
            null !== defaultValue &&
            ((isHeroOrModularSetOrScenario(defaultValue) && defaultValue.name === source[item]) ||
                (!isHeroOrModularSetOrScenario(defaultValue) && defaultValue === source[item]))
        ) {
            option.selected = true;
        }
 
        selectElement.appendChild(option);
    }
 
    return selectElement;
};
 
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const isHeroOrModularSetOrScenario = (value: any): value is Hero | ModularSet | Scenario => {
    return null != value && value.name !== undefined;
};