All files / games/mc/src helper.ts

86.81% Statements 79/91
61.9% Branches 26/42
100% Functions 9/9
86.81% Lines 79/91

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  6x 6x 6x 6x 21x 21x 21x   6x 6x 26x 26x 26x   6x 6x 15x 15x 15x 15x 5x 5x 5x 5x 5x     5x     5x 5x 5x   15x 15x   6x 6x 6x 6x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x   6x   6x 6x 33x 33x 33x     33x 33x 47x 47x 47x 47x     6x   47x   33x   6x   6x 14x   6x 8x 8x     8x     8x     8x     8x     8x     8x     8x     8x 3x   5x     8x   6x                                                            
import { DifficultyEnum, Hero, isScenario, isModularSet, 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");
        Iconst data = document.createElement("data");
        data.innerText = `${item.name}`;
 
        Ilet dataAttribute = `Pack : ${item.pack.name}`;

        if (isModularSet(item)) {
            dataAttribute = `${dataAttribute}, rating: ${item.rating}`;
        }
 
        if (isScenario(item)) {
            dataAttribute = `${dataAttribute}, rating standard solo: ${item.rating_standard_solo}, rating standard multi: ${item.rating_standard_multi}`;
        }
 
        data.setAttribute("value", dataAttribute);
 
        element.appendChild(data);
 
        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");
    I    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[],
    Imultiple = false,
): HTMLSelectElement => {
    const selectElement = document.createElement("select");
    constI selectOption = document.createElement("option");

    if (true === multiple) {
        sIelectElement.setAttribute("multiple", "true");
    }
 
    selecItElement.appendChild(selectOption);

    for (const item in source) {
        cIonst option = document.createElement("option");
        option.value = source[item];
        option.innerText = source[item];
I
        if (
            null !== defaultValue &&
         I   ((isHeroOrModularSetOrScenario(defaultValue) && defaultValue.name === source[item]) ||
                (!isHeroOrModularSetOrScenario(defaultValue) && defaultValue === source[item]))
        ) {
         I   option.selected = true;
        }
 
        selectElement.appendChild(option);
    }
 
    returIn 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;
};
 
export const convertRatingToStars = (rating: number): string => {
    let stars = 0;
 
    if (28 <= rating) {
        stars = 5;
    } else if (24 <= rating) {
        stars = 4.5;
    } else if (20 <= rating) {
        stars = 4;
    } else if (16 <= rating) {
        stars = 3.5;
    } else if (12 <= rating) {
        stars = 3;
    } else if (9 <= rating) {
        stars = 2.5;
    } else if (5 <= rating) {
        stars = 2;
    } else if (1 <= rating) {
        stars = 1.5;
    } else if (-4 <= rating) {
        stars = 1;
    } else if (-7 <= rating) {
        stars = 0.5;
    }
 
    return `⭐`.repeat(stars * 2);
};