User:Shahar/common.js: Difference between revisions

From Inkipedia, the Splatoon wiki
(testing challenge schedule)
Tag: Reverted
m (adding some functions from all.js for load order)
Tag: Reverted
Line 8: Line 8:
}
}


///////////////////////////////////////////////////////////////////////////////
//                        Schedule Utility Functions                        //
///////////////////////////////////////////////////////////////////////////////
// Advances a timestamp to the next multiple of 2 hours
function advanceDateTime(time) {
    var ret = new Date(time.getTime());
    ret.setMinutes(0);
    ret.setTime(ret.getTime() + 3600000 * (
        ret.getUTCHours() & 1 ? 1 : 2
    ));
    return ret;
}
// Formats a timestamp as a string in local time
function formatDateTime(time) {
    var ret =
        [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
        ][time.getMonth()] + " " +
        time.getDate() + " " +
        zeroPad(time.getHours(), 2) + ":" +
        zeroPad(time.getMinutes(), 2)
    ;
    return ret;
}
// Parses a UTC date string in the format "MMM dd hh:mm YYYY", the year at the end of the string is optional and replaces the year argument if provided
function parseDateTime(text, year) {
    text = text.split(/[\s:]+/);
    if(parseInt(text[4]) != NaN && parseInt(text[4]) < 9999 && parseInt(text[4]) >= 1970) year = text[4];
    return new Date(Date.UTC(
        year,
        { jan: 0, feb: 1, mar: 2, apr: 3, may:  4, jun:  5,
          jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11
        }[text[0].toLowerCase()],
        parseInt(text[1]), // Day
        parseInt(text[2]), // Hours
        parseInt(text[3]), // Minutes
        0, 0 // Seconds, milliseconds
    ));
}
// Parses a last-fetched string into a Date object
function parseFetched(now, text) {
    var ret = parseDateTime(text, now.getUTCFullYear());
    if (now < ret) // Accounts for year boundary
        ret.setUTCFullYear(ret.getUTCFullYear() - 1);
    return ret;
}
// Parses a schedule string into a Date object
function parseSchedule(fetched, text) {
    var ret = parseDateTime(text, fetched.getUTCFullYear());
    if (ret.getTime() < fetched.getTime() - 8640000000)
        ret.setUTCFullYear(ret.getUTCFullYear() + 1);
    return ret;
}
// Calculates the time remaining until a given timestamp, as a string
function timeUntil(now, target) {
    target      = target.getTime() - now.getTime();
    target      = Math.floor(target % 7200000 / 1000);
    var seconds = zeroPad(target % 60, 2);
    var minutes = zeroPad(Math.floor(target / 60) % 60, 2);
    var hours  = Math.floor(target / 3600);
    return hours + ":" + minutes + ":" + seconds;
}
// Pad a number with leading zeroes
function zeroPad(number, digits) {
    number = "" + number;
    while (number.length < digits)
        number = "0" + number;
    return number;
}


///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////

Revision as of 23:06, 5 December 2023

/* Counts all your edits and saves them to a page ( http://en.wikipedia.org/wiki/User:Kanegasi/editcounter ) */
if (mw.config.get('wgTitle') === mw.config.get('wgUserName') && mw.config.get('wgNamespaceNumber') === 2) {
/* begin options */

/* end options */

mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Kanegasi/editcounter.js&action=raw&ctype=text/javascript');
}

///////////////////////////////////////////////////////////////////////////////
//                        Schedule Utility Functions                         //
///////////////////////////////////////////////////////////////////////////////

// Advances a timestamp to the next multiple of 2 hours
function advanceDateTime(time) {
    var ret = new Date(time.getTime());
    ret.setMinutes(0);
    ret.setTime(ret.getTime() + 3600000 * (
        ret.getUTCHours() & 1 ? 1 : 2
    ));
    return ret;
}

// Formats a timestamp as a string in local time
function formatDateTime(time) {
    var ret =
        [ "Jan", "Feb", "Mar", "Apr", "May", "Jun",
          "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
        ][time.getMonth()] + " " +
        time.getDate() + " " +
        zeroPad(time.getHours(), 2) + ":" +
        zeroPad(time.getMinutes(), 2)
    ;
    return ret;
}

// Parses a UTC date string in the format "MMM dd hh:mm YYYY", the year at the end of the string is optional and replaces the year argument if provided
function parseDateTime(text, year) {
    text = text.split(/[\s:]+/);
    if(parseInt(text[4]) != NaN && parseInt(text[4]) < 9999 && parseInt(text[4]) >= 1970) year = text[4];
    return new Date(Date.UTC(
        year,
        { jan: 0, feb: 1, mar: 2, apr: 3, may:  4, jun:  5,
          jul: 6, aug: 7, sep: 8, oct: 9, nov: 10, dec: 11
        }[text[0].toLowerCase()],
        parseInt(text[1]), // Day
        parseInt(text[2]), // Hours
        parseInt(text[3]), // Minutes
        0, 0 // Seconds, milliseconds
    ));
}

// Parses a last-fetched string into a Date object
function parseFetched(now, text) {
    var ret = parseDateTime(text, now.getUTCFullYear());
    if (now < ret) // Accounts for year boundary
        ret.setUTCFullYear(ret.getUTCFullYear() - 1);
    return ret;
}

// Parses a schedule string into a Date object
function parseSchedule(fetched, text) {
    var ret = parseDateTime(text, fetched.getUTCFullYear());
    if (ret.getTime() < fetched.getTime() - 8640000000)
        ret.setUTCFullYear(ret.getUTCFullYear() + 1);
    return ret;
}

// Calculates the time remaining until a given timestamp, as a string
function timeUntil(now, target) {
    target      = target.getTime() - now.getTime();
    target      = Math.floor(target % 7200000 / 1000);
    var seconds = zeroPad(target % 60, 2);
    var minutes = zeroPad(Math.floor(target / 60) % 60, 2);
    var hours   = Math.floor(target / 3600);
    return hours + ":" + minutes + ":" + seconds;
}

// Pad a number with leading zeroes
function zeroPad(number, digits) {
    number = "" + number;
    while (number.length < digits)
        number = "0" + number;
    return number;
}

///////////////////////////////////////////////////////////////////////////////
//                           ChallengeSchedule Class                         //
///////////////////////////////////////////////////////////////////////////////

// Maintains auto-updating Challenge schedule elements

// Object constructor
var ChallengeSchedule = function() {

    // Get the current and last-fetched timestamps
    var lblFetched = document.getElementById("battleFetched");
    if (!lblFetched) return; // No schedule
    var now        = new Date();
    var fetched    = parseFetched(now, lblFetched.innerHTML);

    // Initialize instance fields
    this.slots = [
        this.parse(document.getElementById("challenge1_rotation1"), fetched),
        this.parse(document.getElementById("challenge1_rotation2"), fetched),
        this.parse(document.getElementById("challenge1_rotation3"), fetched),
        this.parse(document.getElementById("challenge1_rotation4"), fetched),
        this.parse(document.getElementById("challenge1_rotation5"), fetched),
        this.parse(document.getElementById("challenge1_rotation6"), fetched),
        this.parse(document.getElementById("challenge2_rotation1"), fetched),
        this.parse(document.getElementById("challenge2_rotation2"), fetched),
        this.parse(document.getElementById("challenge2_rotation3"), fetched),
        this.parse(document.getElementById("challenge2_rotation4"), fetched),
        this.parse(document.getElementById("challenge2_rotation5"), fetched),
        this.parse(document.getElementById("challenge2_rotation6"), fetched)
        
    ];

    // Update initial display
    this.onTick(now);
    lblFetched.innerHTML = formatDateTime(fetched);

    // Schedule periodic updates
    var that = this;
    this.timer = setInterval(function() { that.onTick(new Date()); }, 1000);
};

// Periodic update handler
ChallengeSchedule.prototype.onTick = function(now) {

    // Cycle through slots
    for (var x = 0; x < this.slots.length; x++) {
        var slot = this.slots[x];
        if (slot.prev) continue; // Skip this slot

        // Determine when this slot should stop updating
        slot.prev = now >= slot.end;

        // Update the element
        slot.element.innerHTML =
            now >= slot.end ?  "<s>" + formatDateTime(slot.start) + " - " + formatDateTime(slot.end) + "</s>" :
            now >= slot.start ? "Now - " + formatDateTime(slot.end) :
            formatDateTime(slot.start) + " - " + formatDateTime(slot.end)
        ;
    }

    // De-schedule the timer
    if (this.slots[this.slots.length - 1].prev)
        clearInterval(this.timer);
};

// Parse a single Challenge rotation slot
ChallengeSchedule.prototype.parse = function(element, fetched) {
    var text = element.innerHTML;
    return {
        element: element,
        start:   parseSchedule(fetched, text.substring( 0, 12)),
        end:     parseSchedule(fetched, text.substring(15, 27)),
        prev:    false
    };
}

new ChallengeSchedule();