All files / games/mc/src helper.ts

58.99% Statements 82/139
33.93% Branches 19/56
64.29% Functions 9/14
60.74% Lines 82/135

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 241 242 243 244 245 246 247 248  7x 7x 7x 7x 7x 23x 23x 23x 23x   7x 7x 38x 38x 38x   7x 7x 21x 21x 21x 21x 7x 7x 7x 7x 7x       7x       7x 7x 7x                 7x   21x 21x   7x 7x 12x 12x 12x 12x 12x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x       21x   12x 12x 12x 12x   7x 7x 232x 232x 232x 85x   232x 232x 646x 646x 646x 646x     55x   646x   232x   7x   7x 697x   7x                                                                   7x 7x 4x 4x                                           7x 7x                                                                                                                                                                
import { sortBy } from "jga-algorithms-sort";
 
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.iInnerText = title;

    body.appendChild(header);
};
 
export const createSection = (
    titlIe: string,
    data: Hero[] | Scenario[] | ModularSet[] | Pack[] | Deck[],
    body: HTMLBodyElement,
): void => {
    createSubTitle(title, body);

    const section = document.createElement("section");

    const list = document.createElement("ul");
 
    data.forEach((item: Hero | Scenario | ModularSet | Pack| Deck) => {
        const element = document.createElement("li");
        const data = document.createElement("data");
        data.innerText = `${item.name}`;
 
        let dataAttribute = "";
 
        if (isHeroOrModularSetOrScenario(item)) {
            dataAttribute = `Pack : ${item.pack.name}`;
        } 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>,
    title,
    body: HTMLBodyElement,
): void => {
    const table = document.createElement("table");
    const leaderboard = document.createElement("jga-ui-leaderboard");
    leaderboard.setAttribute("title", title);
 
    const sourceRate: unknown[] = [];
 
    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);

        sourceRate.push({
            name: item.name,
            value: winRate,
        });
 
        table.appendChild(itemLine);
    });
 
    const leaderboardData = sortBy(sourceRate, "value").reverse().slice(0, 5);
    leaderboard.setAttribute("data", JSON.stringify(leaderboardData));
 
    body.appendChild(table);
    body.appendChild(leaderboard);
};

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");
    I}

    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 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";
    }
};