All files / games/mc/src helper.ts

60.14% Statements 86/143
36.67% Branches 22/60
64.29% Functions 9/14
61.87% Lines 86/139

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 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270  7x 7x 7x 7x 7x 23x 23x 23x 23x   7x 7x 34x 34x 34x   7x 7x 17x 17x 17x 17x 7x 7x 7x 7x 7x       7x       7x 7x 7x                 7x   17x 17x   7x 7x 12x 12x 12x 12x 12x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 21x 4x 4x 4x   21x       21x   12x 12x 12x 12x   7x 7x 172x 172x 172x 25x   172x 172x 406x 406x 406x 406x     30x   406x   172x   7x   7x 457x   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 interface StatSourceInterface {
    name: string;
    value: number;
}
 
export const createTitle = (title: string, body: HTMLBodyElement): void => {
    consIt header = document.createElement("h2");
    header.innerText = title;
    header.className = "center";
 
    body.appIendChild(header);
};
 
export const createSubTitle = (title: string, body: HTMLBodyElement): void => {
    const header = document.createElement("h3");
    header.innerText = title;
I
    body.appendChild(header);
};

export const createSection = (
    title: 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: string,
    body: HTMLBodyElement,
    dates?: Map<string, string | undefined>,
): void => {
    const table = document.createElement("table");
    const leaderboard = document.createElement("jga-ui-leaderboard");
    leaderboard.setAttribute("title", title);
 
    const sourceRate: StatSourceInterface[] = [];
 
    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);

        if (null != dates) {
            const itemLastDate = document.createElement("td");

            itemLastDate.innerText = (dates.has(item.name) ? dates.get(item.name) : "") as string;

            itemLine.appendChild(itemLastDate);
        }

        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 = (
    IdefaultValue: 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 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: number, b: HTMLTableCellElement) => a + b.colSpan, 0);
 
        // eslint-disable-next-line no-unsanitized/property
        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;
 
            // eslint-disable-next-line no-unsanitized/method
            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: Event) =>
                changeToPage(table, parseInt((e.target as HTMLElement)?.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") as NodeListOf<HTMLElement>;
 
    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";
        }
    }
};