Disney Magic Kingdoms Wiki

Cinderella Part 4 Update has arrived! ✨
Visit this page to learn all about what's coming up in Disney Magic Kingdoms!

READ MORE

Disney Magic Kingdoms Wiki
Disney Magic Kingdoms Wiki

Description

This module is the Lua back-end for {{UpdateNo}}, to display information about game Updates.


Usage

{{#invoke:UpdateNumber|getVersion|<parameter list>}}
For parameter details see Template:UpdateNo/doc

For direct invocation or from other modules

{{#invoke:UpdateNumber|getLastVer|<count>}}
{{#invoke:UpdateNumber|getVars|<item name>}}


--[[<nowiki>
Get Update Name for version
getVersion
    Parameters
    1. version number
        If blank or one of the options below,
        the current page name is used
        "P" = previous update
        "N" = next update
        "T" = infobox title (version number) text only
        "V" = infobox version (version + name) text only
        "R" = release number
    2. include event date after name
    3. text only (no link)
--]]

local p = {}

---[==[ For testing purposes
local versionData = mw.loadData( 'Module:UpdateNumber/data' )
--[===[]==]
local versionData = {
    ["8.5.0"] = {126, "The Black Cauldron Event", "2023/10/10", 36, 75, "8.5.0j"},
    ["8.5.1"] = {127, "The Black Cauldron Event Hotfix", "2023/10/31", nil, nil, "8.5.1a"},
}
--]===]
local util = require('Module:Utility')
local date = require('Dev:Date')

local function prevUpdate(curVersion)
    local curDate = nil

    if versionData[curVersion] then
        curDate = versionData[curVersion][3]
    end

    local prevDate = ""
    local prevVer = ""

    if curDate then
        for k, v in pairs(versionData) do
            if v[3] and v[3] < curDate and v[3] > prevDate then
                prevDate = v[3]
                prevVer = k
            end
        end
    end
    
    return prevVer
end

local function nextUpdate(curVersion)
    local curDate = nil

    if versionData[curVersion] then
        curDate = versionData[curVersion][3]
    end

    local nextDate = "9"
    local nextVer = ""

    if curDate then
        for k, v in pairs(versionData) do
            if v[3] and v[3] > curDate and v[3] < nextDate then
                nextDate = v[3]
                nextVer = k
            end
        end
    end
    
    return nextVer
end

-- return information for a single version/update
local function oneVersion(versionNo, inclDate, txtOnly)
    local outType = ""

    if versionNo:match("^[NPTVR]$") then
        outType = versionNo
        versionNo = ""
    end

    if versionNo == "" then
        local pageName = mw.title.getCurrentTitle().text

        if versionData[pageName] then
            versionNo = pageName
        else
            local updateNo = tonumber(pageName:match("^Update (%d+)"))

            if updateNo then
                for k, v in pairs(versionData) do
                    if v[5] and updateNo == v[5] then
                        versionNo = k
                        break
                    end
                end
            end
        end
    end

    if not versionData[versionNo] and inclDate == "X" then
         return "2001/01/01"
    elseif not versionData[versionNo] then
         return "[[?|Unknown Version]]"
    end

    if outType == "P" then
        -- output previous version
        versionNo = prevUpdate(versionNo)
        if versionNo == "" then return "Unknown" end
    elseif outType == "N" then
        -- output next version
        versionNo = nextUpdate(versionNo)
        if versionNo == "" then return "--" end
    end

    local versionText = ""

    if outType == "T" then
        if versionData[versionNo][6] and versionData[versionNo][6] ~= "" then
            versionText = versionData[versionNo][6]
        else
            versionText = versionNo
        end
        txtOnly = true
    elseif outType == "R" then
        versionText = versionData[versionNo][1]
        txtOnly = true
    else
        local upNo = versionData[versionNo][5]
        
        if upNo and upNo == 0 then
            versionText = versionData[versionNo][2]
        else
            versionText = versionData[versionNo][2] .. " Update"
        end

        if outType == "V" then
            if versionData[versionNo][6] 
                    and versionData[versionNo][6] ~= "" then
                versionText = versionData[versionNo][6] .. "<br />("
                                .. versionText .. ")"
            else
                versionText = versionNo .. "<br />("
                                .. versionText .. ")"
            end
            txtOnly = true
        end
    end

    if inclDate == "S" and versionData[versionNo][5] then
        versionText = "Update " .. versionData[versionNo][5]
    end

    if not txtOnly then
        versionText = "[[" .. versionNo .. "|" .. versionText .. "]]"
    end

    if inclDate == "U" then
        versionText = ""
        if versionData[versionNo][5] then versionText = " and the " .. util.wordinal(versionData[versionNo][5]) .. " major update" end
        versionText = "On '''"
                      .. util.strDate(versionData[versionNo][3], 0)
                      .. "''', the "
                      .. util.wordinal(versionData[versionNo][1])
                      .. " update"
                      .. versionText
                      .. " ''("
                      .. versionNo
                      .. ")'' was released for the game."
    elseif inclDate == "D" then
        versionText = versionText .. " on "
                    .. util.strDate(versionData[versionNo][3])
    elseif inclDate == "X" then
        return versionData[versionNo][3]
    elseif inclDate == "B" then
        versionText = versionText .. " somewhere "
                    .. util.durDate(versionData[versionNo][3], versionData[versionNo][4], nil, nil, true)
    end

    return versionText
end

-- return a list of versions for year
local function multiVersion()
	local navbox = require('Module:Navbox').main
	local allVersions = {}

	for n, v in pairs(versionData) do
		local year = string.sub(v[3], 1, 4)
		if not allVersions[year] then allVersions[year] = {} end
		table.insert(allVersions[year], {v[1], n, v[3], v[6]})
	end

	local paramTable = {"child"}
	local group = 1
	local currYear = nil
	local list = {}

	table.sort(allVersions, function(a,b) return a[1] < b[1] end)

	for key, values in pairs(allVersions) do
		if key ~= currYear then
			if currYear then
				paramTable["group" .. key] = key
				paramTable["list" .. key] = table.concat(list, " '''·''' ")
				group = group + 1
	    			list = {}
	    		end 
			currYear = key
		end

		table.sort(values, function(a,b) return a[3] < b[3] end)

		for i, tData in pairs(values) do
			table.insert(list, table.concat({"[[", tData[2], "|", (tData[4] or tData[2]), "]]"}))
		end

		paramTable["group" .. key] = key
		paramTable["list" .. key] = table.concat(list, " '''·''' ")
	end

	return navbox(paramTable)
end

-- return last X versions formatted for Main Page
local function lastVersions(count)
    local tIndex  = {}
    local today = date():fmt("%Y/%m/%d")

    for k, v in pairs(versionData) do
        if today >= v[3] then
            table.insert(tIndex, k)
        end
    end

    table.sort(tIndex, function(a,b) return a > b end)

    local outText = ""

    for i, ver in ipairs(tIndex) do
        local dateText = util.strDate(versionData[ver][3], nil, nil, true)
        local verText = mw.text.truncate(versionData[ver][2] .. " Update",
                        43 - mw.ustring.len(dateText), nil, true)

        outText = outText 
                .. "* [["
                .. ver
                .. "|"
                .. verText
                .. "]] <small>"
                .. dateText
                .. "</small>\n"

        if i >= count then break end
    end

    return outText
end

-- function get Version Name
function p.getVersion(frame)
    local tArgs = util.getArgs(frame)

    local versionNo = tArgs[1] or ""
    local inclDate  = tArgs[2] or ""
    local txtOnly   = (tArgs[3] and tArgs[3] ~= "")

    if versionNo == "multi" then
        return multiVersion()
    else
        return oneVersion(versionNo, inclDate, txtOnly)
    end
end

-- function get Last X Versions formatted for Main Page
function p.getLastVer(frame)
    local tArgs = util.getArgs(frame)

    local count = tArgs[1] or 5
    count = tonumber(count) or 5

    return lastVersions(count)
end

function p.getVars(frame)
	local tArgs = util.getArgs(frame)

	local curVer = tArgs[1] or ""

	if curVer == "" then
        local pageName = mw.title.getCurrentTitle().text
        if versionData[pageName] then
            curVer = pageName
        else
            local updateNo = tonumber(pageName:match("^Update (%d+)"))
            if updateNo then
                for k, v in pairs(versionData) do
                    if v[5] and updateNo == v[5] then
                        curVer = k
                        break
                    end
                end
            end
        end
    end

	local curData  = versionData[curVer]

    if not curData then
    	curData = {0, "Unknown  Version", nil, 0, 0}
    	curVer = "Unkown Version"
    end

	local prevVer  = nil
    local prevData = {}
    local nextVer  = nil
    local nextData = {}

    if curData[3] then
        for k, v in pairs(versionData) do
            if v[3] then 
            	if v[3] < curData[3] and v[3] > (prevData[3] or "") then
	                prevData = v
	                prevVer = k
	            end
	            if v[3] > curData[3] and v[3] < (nextData[3] or "9") then
	                nextData = v
	                nextVer = k
	            end
            end
        end
    end

	if curData[6] and curData[6] ~= "" then
		curVer = curData[6]
    end

	local VL = mw.ext.VariablesLua

	VL.vardefine("pnTitle", curVer)
	VL.vardefine("pnUpNo", curData[1])

	local tVer = {curVer, "<br />(", curData[2], "", ")"}
	if not curData[5] or curData[5] ~= 0 then
		tVer[4] = " Update"
	end
	
	VL.vardefine("pnVer", table.concat(tVer))
	if curData[3] then
		VL.vardefine("pnDate", util.strDate(curData[3] ) .. " (iOS)<br/>" .. util.strDate(curData[3]) .. " (Android)<br/>" .. util.strDate(curData[3]) .. " (Windows)")
		VL.vardefine("pnDur", (curData[4] and (curData[4] .. " Days") or nil))
	end

	local function prevnext(ver, data)
	    if ver then
	    	local tLink = {"[[", ver, "|", data[2], " Update", "]]"}
	    	if data[5] and data[5] ~= 0 then
		    		tLink[5] = ""
		    end
	    	return table.concat(tLink)
		else
			return "--"
		end
	end

	VL.vardefine("pnPrev", prevnext(prevVer, prevData))
	VL.vardefine("pnNext", prevnext(nextVer, nextData))
end

return p