Module:LTEvent

--[[ LUA script to get Limited Time Event information

getLTE: get Limited Time Event based on input parameters Parameters: 1 = short name : Event / IP short name for multiple set to "MULTIyyyy" where yyyy (optional) is the year will list all events for type for year in order specified by sequence no in data 2 = type      : event type N = normal limited time event (default) T = Tower Challenge 3 = output    : output type N = Full Event Name (default) S = Short Name Only E = Event Name without year D = Event Name with start date R = Only Event start and end date C = Collection Name (=S if no Collection Name) A = Alternate Text (parameter 4) T = Text only Event Name without year P = Event Name with Event Period (start-end dates) 4 = alt text  : alternate text

Unknown Event is returned if Event is not in data.

getNavi: get Previous and Next event from current Event Page Parameter: none

getLastLTE: get Last X Events (formatted for main page) Parameter: 1 = Count  : return Last Count Events (default 5) --]]

local p = {}

local lteData = mw.loadData( 'Module:LTEvent/data' ) local date = require('Dev:Date')

Local Functions local function getArgs(frame) local tArgs

if frame == mw.getCurrentFrame then fParent = frame:getParent tArgs = fParent.args

if (fParent.args[1]) then tArgs = fParent.args else tArgs = frame.args end else tArgs = frame end return tArgs end

-- Date Functions local function ordinal(n) local numb = tonumber(n) or 0 local idd = math.fmod(numb,10) return (idd==1 and numb~=11 and "st") or (idd==2 and numb~=12 and "nd") or (idd==3 and numb~=13 and "rd") or (numb~=0 and "th") or "" end

local function strDate(nYear, nMonDay, days, cardinal, whenTo) local theDate = date(nYear,                        math.floor(nMonDay/100),                        math.fmod(nMonDay, 100)) local nDays = tonumber(days) or 0

local day = "" local retText = {}

if whenTo then if theDate < date then table.insert(retText, "started on ") else table.insert(retText, "will start on ") end end if nDays > 0 then local start = theDate:copy theDate:adddays(nDays) local day = string.gsub(start:fmt("%d"), "^0", "") table.insert(retText, day)

if not cardinal then table.insert(retText, ordinal(day)) end

if whenTo or start:getmonth ~= theDate:getmonth then table.insert(retText, start:fmt(" %B")) end

if whenTo or (not noYear and start:getyear ~= theDate:getyear) then table.insert(retText, start:fmt(" %Y")) end

if whenTo then if theDate < date then table.insert(retText, " and ended on ") else table.insert(retText, " and will end on ") end else table.insert(retText, " - ") end end

day = string.gsub(theDate:fmt("%d"), "^0", "") table.insert(retText, day) if not cardinal then table.insert(retText, ordinal(day)) end

table.insert(retText, theDate:fmt(" %B"))

if not noYear then table.insert(retText, theDate:fmt(" %Y")) end

if whenTo and nDays > 0 then table.insert(retText, ", lasting ") table.insert(retText, tostring(nDays)) if nDays > 1 then table.insert(retText, " days") else table.insert(retText, " day") end end return table.concat(retText) end

local function singleEvent(sName, eType, outType, altText) local eventName = "Work In Progress" local collection = "Unknown Event" local eventYear = nil local eventDate = 0 local eventDur  = 0

local outText = eventName local dispText = "" local dateText = ""

if lteData[eType] and lteData[eType][sName] then eventName = lteData[eType][sName][2] eventYear = lteData[eType][sName][3] eventDate = lteData[eType][sName][4] collection = lteData[eType][sName][5] or eventName eventDur  = lteData[eType][sName][6] or 0

if eType == "T" then outText = "The Tower Challenge ("                       .. eventName                        .. ") Event" else outText = eventName .. " Event" end end

if outType:match("^[ACDERUS]$") then if outType == "A" then dispText = altText elseif outType == "C" then dispText = collection elseif outType == "S" then dispText = eventName elseif outType == "E" then if eType == "N" then dispText = eventName .. " Event" else dispText = "The Tower Challenge Event" end elseif outType == "U" then local eventNumb = lteData[eType][sName][1] or 0 local ttcNumb = lteData[eType][sName][7] or 0 outText =	"'''" .. outText .. "''' is the " .. eventNumb .. ordinal(eventNumb) .. " major event of Disney Magic Kingdoms" if eType == "T" then outText =	outText .. ", and the " .. ttcNumb .. ordinal(ttcNumb) .. " Tower Challenge event" end if eType == "C" then outText =	outText .. ", and the " .. ttcNumb .. ordinal(eventNumb) .. " Cascading Critter event" end outText =	outText .. ". It " .. strDate(eventYear, eventDate, eventDur, nil, true) .. "."       elseif outType == "R" then outText = strDate(eventYear, eventDate, eventDur) elseif outType == "D" then if altText ~= "" then dispText = altText elseif eType == "N" then dispText = eventName .. " Event" else dispText = eventName .. "'s Tower Challenge Event" end dateText = " on " .. strDate(eventYear, eventDate, 0) end end

if outType ~= "T" and outType ~= "R" and outType ~= "U" then if eventYear then outText = outText .. " " .. eventYear end if dispText and dispText ~="" then outText = outText .. "|" .. dispText end outText = "" .. outText .. "" if dateText and dateText ~="" then outText = outText .. dateText end end

return outText end

local function multiEvent(eType, outType, year) local tIndex = {} local tOutput = {}

if lteData[eType] then if year and year ~= 0 then for k, v in pairs(lteData[eType]) do               if year == v[3] then table.insert(tIndex,v[1]) tOutput[v[1]] = v               end end else for k, v in pairs(lteData[eType]) do               table.insert(tIndex,v[1]) tOutput[v[1]] = v           end end end

table.sort(tIndex)

local outText = "" local sDot   = ""

for _, idx in ipairs(tIndex) do

if eType == "T" then outText = outText .. sDot .. "[[The Tower Challenge ("                       .. tOutput[idx][2]                        .. ") Event "                        .. tOutput[idx][3]        else            outText = outText .. sDot                         .. "[["                        .. tOutput[idx][2]                        .. " Event "                        .. tOutput[idx][3]        end

if outType == "S" then outText = outText .. "|" .. tOutput[idx][2] elseif outType == "C" then if tOutput[idx][5] then outText = outText .. "|" .. tOutput[idx][5] else outText = outText .. "|" .. tOutput[idx][2] end end outText = outText .. "]]"

sDot = " · " end return outText end

-- function get Limited Time Event function p.getLTE(frame)

local tArgs = getArgs(frame)

local sName   = tArgs[1] or "" local eType   = tArgs[2] local outType = tArgs[3] or "N" local altText = tArgs[4] or ""

sName = sName:lower:gsub("^%s*(.-)%s*$", "%1") or ''

if not eType or eType == "" then eType = "N" end

eType = eType:upper outType = outType:upper

local multi, year = sName:match("^(multi)(%d*)$")

if multi then return multiEvent(eType, outType, tonumber(year)) else return singleEvent(sName, eType, outType, altText) end end

-- function get previous, next navigation links function p.getNavi(frame) local tArgs = getArgs(frame)

--local curName = tArgs[1] or mw.title.getCurrentTitle.text local curName = mw.title.getCurrentTitle.text

local eType = "N" local eName = "" local eYear = ""

if curName:match("^The Tower Challenge %(.+%) Event %d%d%d%d$") then eName, eYear = curName:match("^The Tower Challenge %((.+)%) Event (%d+)$") eType = "T" elseif curName:match("^.+ Event %d%d%d%d$") then eName, eYear = curName:match("^(.+) Event (%d+)$") eType = "N" end

eYear = tonumber(eYear)

local curDate = 0

if lteData[eType] then for k, v in pairs(lteData[eType]) do           if eYear == v[3] and eName == v[2] then curDate = (v[3] * 10000) + v[4] break end end end

local prevDate = 0 local prevEvent = "" local prevType = "" local nextDate = 99999999 local nextEvent = "" local nextType = ""

if curDate ~= 0 then for k, v in pairs(lteData["N"]) do           local xDate = (v[3] * 10000) + v[4] if xDate < curDate and xDate > prevDate then prevDate = xDate prevEvent = v               prevType = "N" end if xDate > curDate and xDate < nextDate then nextDate = xDate nextEvent = v               nextType = "N" end end for k, v in pairs(lteData["T"]) do           local xDate = (v[3] * 10000) + v[4] if xDate < curDate and xDate > prevDate then prevDate = xDate prevEvent = v               prevType = "T" end if xDate > curDate and xDate < nextDate then nextDate = xDate nextEvent = v               nextType = "T" end end end

local prevName = "Main Storyline Walkthrough" local nextName = ""

if prevDate ~= 0 then if prevType == "T" then prevName = "The Tower Challenge ("                       .. prevEvent[2]                        .. ") Event " .. prevEvent[3] .. "|Tower Challenge ("                       .. prevEvent[2]                        .. ")" else prevName = prevEvent[2] .. " Event " .. prevEvent[3] end end if nextDate ~= 99999999 then if nextType == "T" then nextName = "The Tower Challenge ("                       .. nextEvent[2]                        .. ") Event " .. nextEvent[3] .. "|Tower Challenge ("                       .. nextEvent[2]                        .. ")" else nextName = nextEvent[2] .. " Event " .. nextEvent[3] end nextName = "" .. nextName .. " »" end return "{| class=\"article-table\" style=\"width: 100%;\"" .. "\n| nowrap width=\"50%\"|« "           .. prevName            .. "\n| nowrap align=\"right\"|" .. nextName .. "\n|}" end

-- function get Last X events function p.getLastLTE(frame) local tArgs = getArgs(frame)

if frame == mw.getCurrentFrame then fParent = frame:getParent tArgs = fParent.args

if (fParent.args[1]) then tArgs = fParent.args else tArgs = frame.args end else tArgs = frame end

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

for k, t in pairs(lteData) do       for _, v in pairs(t) do            local event = {} --event[1] = (v[3] * 10000) + v[4] event[1] = os.time({year = v[3],                       month= math.floor(v[4]/100),                        day= math.fmod(v[4], 100)}) event[2] = k           event[3] = v[2] event[4] = v[3] if (event[1] <= os.time) then table.insert(allEvents, event) end end end

table.sort(allEvents, function(a,b) return a[1] > b[1] end)

local outText = ""

for i, v in ipairs(allEvents) do       local linkText  = "" local dateText = os.date("%e %B %Y", v[1]) dateText = dateText:gsub("^%s*(.-)%s*$", "%1") or ''

if v[2] == "T" then linkText = "* "                       .. mw.text.truncate(v[3] .. "'s Tower Challenge", 43 - mw.ustring.len(dateText), nil, true)                        .. " " .. dateText .. " \n" else linkText = "* "                       .. mw.text.truncate(v[3], 43 - mw.ustring.len(dateText), nil, true)                        .. " " .. dateText .. " \n" end

outText = outText .. linkText if i >= count then break end end return outText end

return p

-- --