Untitled

 avatar
unknown
javascript
3 years ago
8.7 kB
3
Indexable
const rolePointsOptions = [
    { points: -100, role: '976610552874930196' },
    { points: 100, role: '976610550882660362' },
    { points: 200, role: '976610548735160320' },
    { points: 300, role: '976610547862757456' },
    { points: 400, role: '976610545715253249' },
    { points: 500, role: '976610543580373043' },
    { points: 600, role: '976766034495279124' },
    { points: 700, role: '976766194759639040' },
    { points: 800, role: '976766270882058249' }
];

module.exports = {
    generateRandomHex(size) {
        if (isNaN(Number(size))) return null;
        const genRanHex = [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
        return genRanHex;
    },
    addPoints(curr_points) {
        let pointsToAdd = 0;

        if (curr_points < 100) pointsToAdd += 50;
        else if (curr_points < 200) pointsToAdd += 40;
        else if (curr_points < 300) pointsToAdd += 30;
        else if (curr_points < 400) pointsToAdd += 25;
        else if (curr_points < 500) pointsToAdd += 20;
        else if (curr_points < 700) pointsToAdd += 15;
        else if (curr_points >= 700) pointsToAdd += 10;

        return pointsToAdd;
    },
    winRemovePoints(curr_points) {
        let pointsToAdd = 0;

        if (curr_points < 100) pointsToAdd -= 50;
        else if (curr_points < 200) pointsToAdd -= 40;
        else if (curr_points < 300) pointsToAdd -= 30;
        else if (curr_points < 400) pointsToAdd -= 25;
        else if (curr_points < 500) pointsToAdd -= 20;
        else if (curr_points < 700) pointsToAdd -= 15;
        else if (curr_points >= 700) pointsToAdd -= 10;

        return pointsToAdd;
    },
    removePoints(curr_points) {
        let pointsToRemove = 0;

        if (curr_points < 100) pointsToRemove -= 0;
        else if (curr_points < 400) pointsToRemove -= 10;
        else if (curr_points < 600) pointsToRemove -= 15;
        else if (curr_points < 700) pointsToRemove -= 20;
        else if (curr_points < 800) pointsToRemove -= 25;
        else if (curr_points >= 800) pointsToRemove -= 30;

        return pointsToRemove;
    },
    loseAddPoints(curr_points) {
        let pointsToRemove = 0;

        if (curr_points < 100) pointsToRemove += 0;
        else if (curr_points < 400) pointsToRemove += 10;
        else if (curr_points < 600) pointsToRemove += 15;
        else if (curr_points < 700) pointsToRemove += 20;
        else if (curr_points < 800) pointsToRemove += 25;
        else if (curr_points >= 800) pointsToRemove += 30;

        return pointsToRemove;
    },
    RolePointChecker(old_points, new_points) {
        let old_role;
        for (const role of rolePointsOptions) {
            if (role.points <= old_points) {
                old_role = role.role;
            } else {
                break;
            };
        };

        let new_role;
        for (const role of rolePointsOptions) {
            if (role.points <= new_points) {
                new_role = role.role;
            } else {
                break;
            };
        };

        if (old_role !== new_role) return new_role;
        else return old_role;
    },
    convertMSToDate(ms) {
        let days = Math.floor(ms / 86400000);
        ms -= days * 86400000;
        let hours = Math.floor(ms / 3600000);
        ms -= hours * 3600000;
        let minutes = Math.floor(ms / 60000);
        ms -= minutes * 60000;
        let seconds = Math.floor(ms / 1000);
        return { days, hours, minutes, seconds };
    },
    convertDateToUTC(date) {
        date.setHours(date.getHours() - 5);
        date.setMinutes(date.getMinutes() - 30);
        return date;
    },
    getRandomizedArray(arr = [], n = arr.length) {
        var result = new Array(n),
            len = arr.length,
            taken = new Array(len);
        if (n > len)
            throw new RangeError("getRandom: more elements taken than available");
        while (n--) {
            var x = Math.floor(Math.random() * len);
            result[n] = arr[x in taken ? taken[x] : x];
            taken[x] = --len in taken ? taken[len] : len;
        }
        return result;
    },
    parseTime(time) {
        const regex = /\d+\.*\d*\D+/g;
        time = time.split(/\s+/).join("");
        let res;
        let duration = 0;
        while ((res = regex.exec(time)) !== null) {
            if (res.index === regex.lastIndex) {
                regex.lastIndex++;
            }
            const local = res[0].toLowerCase();
            if (local.endsWith("seconds") || local.endsWith("second") || (local.endsWith("s") && local.match(/\D+/)[0].length === 1)) {
                duration += parseInt(local.match(/\d+\.*\d*/)[0], 10) * 1000;
            }
            else if (local.endsWith("minutes") || local.endsWith("minute") || (local.endsWith("m") && local.match(/\D+/)[0].length === 1)) {
                duration += parseInt(local.match(/\d+\.*\d*/)[0], 10) * 60000;
            }
            else if (local.endsWith("hours") || local.endsWith("hour") || (local.endsWith("h") && local.match(/\D+/)[0].length === 1)) {
                duration += parseInt(local.match(/\d+\.*\d*/)[0], 10) * 3600000;
            }
            else if (local.endsWith("days") || local.endsWith("day") || (local.endsWith("d") && local.match(/\D+/)[0].length === 1)) {
                duration += parseInt(local.match(/\d+\.*\d*/)[0], 10) * 86400000;
            }
            else if (local.endsWith("weeks") || local.endsWith("week") || (local.endsWith("w") && local.match(/\D+/)[0].length === 1)) {
                duration += parseInt(local.match(/\d+\.*\d*/)[0], 10) * 604800000;
            }
            else if (local.endsWith("months") || local.endsWith("month")) {
                duration += parseInt(local.match(/\d+\.*\d*/)[0], 10) * 2628000000;
            }
            else if (local.endsWith("years") || local.endsWith("year") || (local.endsWith("y") && local.match(/\D+/)[0].length === 1)) {
                duration += parseInt(local.match(/\d+\.*\d*/)[0], 10) * 31557600000;
            }
        }
        if (duration === 0) {
            return null;
        }
        return duration;
    },
    formatTime(milliseconds, minimal = false) {
        if (!milliseconds || isNaN(milliseconds) || milliseconds <= 0) {
            throw new RangeError("Utils#formatTime(milliseconds: number) Milliseconds must be a number greater than 0");
        }
        if (typeof minimal !== "boolean") {
            throw new RangeError("Utils#formatTime(milliseconds: number, minimal: boolean) Minimal must be a boolean");
        }
        const times = {
            years: 0,
            months: 0,
            weeks: 0,
            days: 0,
            hours: 0,
            minutes: 0,
            seconds: 0,
        };
        while (milliseconds > 0) {
            if (milliseconds - 31557600000 >= 0) {
                milliseconds -= 31557600000;
                times.years++;
            }
            else if (milliseconds - 2628000000 >= 0) {
                milliseconds -= 2628000000;
                times.months++;
            }
            else if (milliseconds - 604800000 >= 0) {
                milliseconds -= 604800000;
                times.weeks += 7;
            }
            else if (milliseconds - 86400000 >= 0) {
                milliseconds -= 86400000;
                times.days++;
            }
            else if (milliseconds - 3600000 >= 0) {
                milliseconds -= 3600000;
                times.hours++;
            }
            else if (milliseconds - 60000 >= 0) {
                milliseconds -= 60000;
                times.minutes++;
            }
            else {
                times.seconds = Math.round(milliseconds / 1000);
                milliseconds = 0;
            }
        }
        const finalTime = [];
        let first = false;
        for (const [k, v] of Object.entries(times)) {
            if (minimal) {
                if (v === 0 && !first) {
                    continue;
                }
                finalTime.push(v < 10 ? `0${v}` : `${v}`);
                first = true;
                continue;
            }
            if (v > 0) {
                finalTime.push(`${v} ${v > 1 ? k : k.slice(0, -1)}`);
            }
        }
        let time = finalTime.join(minimal ? ":" : ", ");
        if (time.includes(",")) {
            const pos = time.lastIndexOf(",");
            time = `${time.slice(0, pos)} and ${time.slice(pos + 1)}`;
        }
        return time;
    }
};