Module:LatestUpdate: Difference between revisions

From The Deadlock Wiki
Jump to navigation Jump to search
created
 
No edit summary
Line 3: Line 3:
-- Load JSON content from a page
-- Load JSON content from a page
local data = mw.loadJsonData("Data:LatestUpdate.json")
local data = mw.loadJsonData("Data:LatestUpdate.json")
local update = data.latest_update
local update = data.latest_update or {}
 
-- Month name → number mapping
local month_map = {
    January = 1,
    February = 2,
    March = 3,
    April = 4,
    May = 5,
    June = 6,
    July = 7,
    August = 8,
    September = 9,
    October = 10,
    November = 11,
    December = 12
}


function p.month()
function p.month()
     return update.month
     return update.month or ""
end
end


function p.day()
function p.day()
     return update.day
     return update.day or ""
end
end


function p.year()
function p.year()
     return update.year
     return update.year or ""
end
 
local function getMonthNumber(monthName)
    return month_map[monthName]
end
 
local function getCurrentTime()
    local frame = mw.getCurrentFrame()
    local now_str = frame:callParserFunction("CURRENTTIMESTAMP")
 
    return os.time({
        year = tonumber(string.sub(now_str, 1, 4)),
        month = tonumber(string.sub(now_str, 5, 6)),
        day = tonumber(string.sub(now_str, 7, 8)),
        hour = 0
    })
end
 
-- Calculate days since last update
function p.days_since()
    local month_number = getMonthNumber(update.month)
 
    if not month_number or not update.day or not update.year then
        return 0
    end
 
    local update_time = os.time({
        year = tonumber(update.year),
        month = month_number,
        day = tonumber(update.day),
        hour = 0
    })
 
    local now = getCurrentTime()
 
    local diff_seconds = os.difftime(now, update_time)
    local days = math.floor(diff_seconds / 86400)
 
    if days < 0 then
        return 0
    end
 
    return days
end
 
-- Return formatted text (e.g. "1 day ago", "5 days ago")
function p.days_since_text()
    local days = p.days_since()
 
    return days .. (days == 1 and " day ago" or " days ago")
end
end


return p
return p

Revision as of 13:25, 10 April 2026

Overview

This module calculates the days since the last update stored in Data:LatestUpdate.json and sends to Module:UpdateExcerpt to be displayed by Template:Deadlock Wiki/UpdateExcerpt. It can also be called to calculate the time since any given date, in days.

Examples

days_since_date

This function can be called to calculate the days passed from a given date, using the format "Month/Day/Year". "Month" has to be in numbers.

For days since January 30 2026:

{{#invoke:LatestUpdate|days_since_date|1|30|2026}}

Script error: The function "days_since_date" does not exist.

days_since

This is an automated function that calculates the days since the latest update stored in Data:LatestUpdate.json.

days_since_text

This is an automated function that wraps the "days_since" data in a localized text template to be used by Module:UpdateExcerpt.


local p = {}

-- Load JSON content from a page
local data = mw.loadJsonData("Data:LatestUpdate.json")
local update = data.latest_update or {}

-- Month name → number mapping
local month_map = {
    January = 1,
    February = 2,
    March = 3,
    April = 4,
    May = 5,
    June = 6,
    July = 7,
    August = 8,
    September = 9,
    October = 10,
    November = 11,
    December = 12
}

function p.month()
    return update.month or ""
end

function p.day()
    return update.day or ""
end

function p.year()
    return update.year or ""
end

local function getMonthNumber(monthName)
    return month_map[monthName]
end

local function getCurrentTime()
    local frame = mw.getCurrentFrame()
    local now_str = frame:callParserFunction("CURRENTTIMESTAMP")

    return os.time({
        year = tonumber(string.sub(now_str, 1, 4)),
        month = tonumber(string.sub(now_str, 5, 6)),
        day = tonumber(string.sub(now_str, 7, 8)),
        hour = 0
    })
end

-- Calculate days since last update
function p.days_since()
    local month_number = getMonthNumber(update.month)

    if not month_number or not update.day or not update.year then
        return 0
    end

    local update_time = os.time({
        year = tonumber(update.year),
        month = month_number,
        day = tonumber(update.day),
        hour = 0
    })

    local now = getCurrentTime()

    local diff_seconds = os.difftime(now, update_time)
    local days = math.floor(diff_seconds / 86400)

    if days < 0 then
        return 0
    end

    return days
end

-- Return formatted text (e.g. "1 day ago", "5 days ago")
function p.days_since_text()
    local days = p.days_since()

    return days .. (days == 1 and " day ago" or " days ago")
end

return p