User:MonkeysHK/common.js

/** FestiveEvents * Script for loading multiple Festive Events scripts at specific times of the year. * All script options are still fully customizable by the user. */

/* jshint esversion: 5, forin: true, immed: true, indent: 4, latedef: true, newcap: true, noarg: true, undef: true, undef: true, unused: true, browser: true, jquery: true, onevar: true, eqeqeq: true, multistr: true, maxerr: 999999, forin: false, -W082, -W084 /* global window */ $(function {    var thisdate = new Date,        thisyear = thisdate.getFullYear,        nextyear = thisyear + 1;

if (window.HSWEventsLoaded) return; window.HSWEventsLoaded = true;

function assignDefault(obj, defaults) { window[obj] = Object.assign(defaults, window[obj] || {}); }

function ajaxCall(name, url, def) { $.ajax({           cache: true,            dataType: "script",            url: url + "?action=raw&ctype=text/javascript"        }).done(function  {            if (def) {                def.resolve(window[name]);            }        }); }

function getDefAndAjax(name, straightToResolve, url) { return $.Deferred(function (def) {           if (straightToResolve) {                def.resolve(window[name]);            } else {                ajaxCall(name, url, def);            }        }); }

function during(date0, date1) { return date0.getFullYear === date1.getFullYear && date0.getMonth === date1.getMonth && date0.getDate === date1.getDate; }

function duringAny(date0, dates) { return dates.some(function (d) {           return during(date0, d);        }); }

function waitTill(func, mode, eventName) { // note: func expects an array or a function that receives a year and returns an array // "months" mode: expects array of numbers representing months (1-12) // "date" mode: expects array of dates function getIncludes(thisyearlist, nextyearlist, mode) { if (mode === "month") return thisyearlist.includes(thisdate.getMonth + 1); else return duringAny(thisdate, thisyearlist); }

function getTill(thisyearlist, nextyearlist, mode) { if (mode === "month") return Math.min.apply(this, thisyearlist.map(function (v, i) { var firstdayofmonththisyear = new Date(thisyear, v - 1, 1, 0, 0, 0, 0), firstdayofmonthnextyear = new Date(nextyear, nextyearlist[i] - 1, 1, 0, 0, 0, 0); return ((thisdate < firstdayofmonththisyear) && firstdayofmonththisyear || firstdayofmonthnextyear) - thisdate; }));           else return Math.min.apply(this, thisyearlist.map(function (v, i) { return ((thisdate < v) && v || nextyearlist[i]) - thisdate; }));       }        var promise = $.Deferred; var eventTag = "[FestiveEvents] [" + eventName + "] "; var thisyearlist, nextyearlist; if (typeof func === "function") { thisyearlist = func(thisyear); nextyearlist = func(nextyear); } else { thisyearlist = nextyearlist = func; }       if (getIncludes(thisyearlist, nextyearlist, mode)) { console.log(eventTag + "Starting celebration!"); promise.resolve(true); } else if (!thisyearlist.length) { promise.resolve(false); } else { var till = getTill(thisyearlist, nextyearlist, mode); if (till < 86400000) { // Celebration in less than 1 day console.log(eventTag + till + "ms till celebration."); setTimeout(function {                    console.log(eventTag + "Starting celebration!");                    promise.resolve(true);                }, till); } else console.log(eventTag + "More than one day till celebration. Days left: " + (till / 86400000).toFixed(2)); promise.resolve(false); }       return promise; }

function getFireworkDates(year) { var cny_lookup = { 2021: "12 Feb 2021", 2022: "1 Feb 2022", 2023: "22 Jan 2023", 2024: "10 Feb 2024", 2025: "29 Jan 2025", 2026: "17 Feb 2026", 2027: "6 Feb 2027", 2028: "26 Jan 2028", 2029: "13 Feb 2029", 2030: "3 Feb 2030", 2031: "23 Jan 2031", 2032: "11 Feb 2032", };       return [ new Date("1 January " + year), // new year (Gregorian calendar) new Date(cny_lookup[year]), // new year (Chinese calendar) ];   }

var deffers = [$.Deferred, $.Deferred];

assignDefault("fireworkShow", {       autoStart: false,        startOnEvent: true,        optionalDeferredRegister: deffers[0]    }); ajaxCall("fireworkShow", "https://hypixel-skyblock.fandom.com/wiki/User:MonkeysHK/common.js/1.js");

assignDefault("HalloweenEffectsConfig", {       autoStart: false,        startOnEvent: true,        optionalDeferredRegister: deffers[1]    }); ajaxCall("HalloweenEffects", "https://hypixel-skyblock.fandom.com/wiki/User:MonkeysHK/common.js/0.js");

assignDefault("snowStorm", {       autoStart: false,        startOnEvent: true    });

$.when.apply(       null,        [            getDefAndAjax("snowStorm", window.snowStorm && window.snowStorm.flakes, "https://dev.fandom.com/wiki/MediaWiki:SnowStorm.js")        ].concat(deffers)    ).then(function  {        if (!window.fireworkShow.autoStart && window.fireworkShow.startOnEvent) {            waitTill(getFireworkDates, "date", "FireworkEffects").then(function (toStart) { if (toStart) window.fireworkShow.start; });       }        if (!window.HalloweenEffectsConfig.autoStart && window.fireworkShow.startOnEvent) {            waitTill([10], "month", "HalloweenEffects").then(function (toStart) { if (toStart) window.HalloweenEffects.start; });       }        if (!window.snowStorm.autoStart && window.fireworkShow.startOnEvent) {            waitTill([12], "month", "SnowStorm").then(function (toStart) { if (toStart) window.snowStorm.start; });       }    }); });