All files / games/mc/src helper.ts

54.81% Statements 74/135
31.67% Branches 19/60
64.29% Functions 9/14
56.49% Lines 74/131

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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240  7x 7x 7x 7x 23x 23x 23x 23x   7x 7x 36x 36x 36x   7x 7x 21x 21x 21x 21x 7x 7x 7x 7x 7x                   7x       7x 7x 7x                 7x   21x 21x   7x 7x 10x 10x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x   10x   7x 7x 202x 202x 202x 75x   202x 202x 526x 526x 526x 526x     25x   526x   202x   7x   7x 457x   7x                                                                   7x 7x 4x 4x                                           7x 7x                                                                                                                                                          
import {
    Deck,
    DifficultyEnum,
    Hero,
    isDeck,
    isHero,
    isModularSet,
    isPack,
    isScenario,
    ModularSet,
    Pack,
    ResultEnum,
    Scenario,
} from "./@models";
 
export interface SourceItemInterface {
    name: string;
}
 
export const createTitle = (title: string, body: HTMLBodyElement): void => {
    const header = document.createElement("h2");
    header.innerText = title;
    header.className = "center";
 
    body.appendChild(header);
};
I
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: HeIro[] | Scenario[] | ModularSet[] | Pack[] | Deck[],
    body: HTMLBodyElement,
): void => {
    createSubTitle(title, body);
 
    const section = document.createElement("section");
I
    const list = document.createElement("ul");

    data.forEach((item: Hero | Scenario | ModularSet | Pack) => {
        const element = document.createElement("li");
        const data = document.createElement("data");
        data.innerText = `${item.name}`;

        let dataAttribute = "";
 
        if (isHeroOrModularSetOrScenario(item)) {
            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}`;
            }
        } else {
            if (isPack(item)) {
                dataAttribute = item.releaseDate;
            }
        }
 
        data.setAttribute("value", dataAttribute);
 
        element.appendChild(data);
 
        if (isDeck(item)) {
            const heroesList = document.createElement("ul");
 
            item.heroes.forEach((hero) => {
                const heroItem = document.createElement("li");
                heroItem.innerText = `${hero.name}`;
 
                heroesList.appendChild(heroItem);
            });
 
            element.appendChild(heroesList);
        }
 
        list.appendChild(element);
    });
 
    section.appendChild(list);

    body.appendChild(section);
};
 
export const createStatsTable = (
    data: Map<string, number>,
    source: SourceItemInterface[],
    wins: Map<string, number>,
    body: HTMLBodyElement,
): void => {
    const table = document.createElement("table");
 
    source.forEach((item: SourceItemInterface) => {
        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);
I
    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 isHero(value) || isModularSet(value) || isScenario(value);
};

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);
};
 
export const paginateTable = (selector: string, rowsPerPage = 10): void => {
    const table = document.querySelector(selector) as HTMLTableElement;
 
    if (null != table) {
        const tRows = table.querySelectorAll("tbody tr");
        const numPages = Math.ceil(tRows.length / rowsPerPage);
 
        const colCount = [].slice
            .call(table.querySelector("tr")?.cells)
            .reduce((a: any, b: any) => a + parseInt(b.colSpan), 0);
 
        table.createTFoot().insertRow().innerHTML = `<td colspan=${colCount}><div class="nav"></div></td>`;
 
        if (numPages == 1) {
            return;
        }
 
        for (let i = 0; i < numPages; i++) {
            const pageNum = i + 1;
 
            table
                .querySelector(".nav")
                ?.insertAdjacentHTML("beforeend", `<a href="#${selector}" rel="${i}">${pageNum}</a> `);
        }
 
        changeToPage(table, 1, rowsPerPage);
 
        for (const navA of Array.from(table.querySelectorAll(".nav a"))) {
            navA.addEventListener("click", (e: any) => changeToPage(table, parseInt(e.target.innerHTML), rowsPerPage));
        }
    }
};
 
const changeToPage = (table: HTMLTableElement, page: number, rowsPerPage: number): void => {
    const startItem = (page - 1) * rowsPerPage;
    const endItem = startItem + rowsPerPage;
    const navAs = table.querySelectorAll(".nav a");
    const tRows = table.querySelectorAll("tbody tr");
 
    for (let nix = 0; nix < navAs.length; nix++) {
        if (nix == page - 1) navAs[nix].classList.add("active");
        else navAs[nix].classList.remove("active");
 
        for (let trix = 0; trix < tRows.length; trix++)
            tRows[trix].style.display = trix >= startItem && trix < endItem ? "table-row" : "none";
    }
};