aboutsummaryrefslogtreecommitdiff
path: root/utils.mjs
blob: afe200ca365426755ff1d2f0a323ecbab4fa30bc (plain) (blame)
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
export class Manager {
    constructor(description, listData) {
        this.description = description;
        this.listData = listData;
        this.count = listData.length;
    }
    store() {
        let stringData = JSON.stringify(this.listData);
        localStorage.setItem(this.description, stringData);
    }
    render() {
        let renderedHTML = this.listData.map((i) => {return i.render();}).join('');
        let elementId = this.description + '_list';
        document.getElementById(elementId).innerHTML = renderedHTML;
        this.addToggleBehaviour();
    }
    addToggleBehaviour() {
        for (let idx = 0; idx < this.listData.length; ++idx) {
            const elementId = this.description + "_" + this.listData[idx].id;
            const p = document.getElementById(elementId);
            p.addEventListener("click", () => {
                this.listData[idx].toggle();
                this.store();
                p.classList.toggle('is-success');
                p.classList.toggle('is-danger');
            });
        }
    }
}

export function getMatchCount(regulars, courts) {
    const activeCourts = courts.filter(function (c) { return c.status });
    console.log("There are a total of " + activeCourts.length + " courts available.");
    const activePlayers = regulars.filter(function (p) { return p.status;});
    console.log("There are a total of " + activePlayers.length + " players available.");
    var possibleMatches = Math.floor(activePlayers.length / 4);
    possibleMatches = possibleMatches > activeCourts.length ? activeCourts.length : possibleMatches;
    return possibleMatches;
}

export function shuffle(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
    return array;
}