Module:MiniEvent

--[[ LUA script to get Mini Event information

getME: get Mini Event based on input parameters Parameters: 1 = name    : Mini Event name 2 = number  : Event Number (multiple Numbers can be specified                    separated with ",") 3 = options : One or more Output Options Some Options are mutually exclusive --- Fixed Formats The priority is in the below order Only the first ME number will be displayed C = Fixed Text ME Count Till Today E = Event Page Format (Table Row: Columns= Icon, Short Name, Dates w/o year) T = Timeline Page Format (Table Columns: Icon + Short Name #, Dates with year) (Parameter 4 can be  for italic and ' for bold') K = toKen Page Format (Table Row: Columns= Icon, Full Name Sub Text) (sub text in parameter 4) H = Horizontal List Format (Short Name only for use in Template:Events) L = Latest Info Main Page Format (Short Name with small Date) --- Customizable Display Format Options F = Full ME Name 9 = Include ME Number S = Short ME Name D = Append ME Date(s) A = Alternate Text (parameter 4) 4 = alt text : Alternate Text / Sub Text / bold-italic

getLastME: get Last X Events (formatted for main page) Output Format is Fixed ("L") Parameter: 1 = Count : return Last Count Events (default 5)

getEventME: get Mini Events during LTE Output Format is Fixed ("E") Parameters: 1 = LTE name : Limited Time Event Name

getMEList: get a List of Mini Events in horizontal format Output Format is Fixed ("H") Parameters: None

getMETimeLine: get a Complete Time Line of Mini Events Output Format is Fixed ("T") Parameters: None --]]

local p = {}

local meData = mw.loadData( 'Module:MiniEvent/data' ) local npc = require('Module:NPC').getNPC 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(sDate, days, noYear, sNextDate, cardinal) local theDate = date(sDate)

local nextDate local chkDate = false

if sNextDate then nextDate = date(sNextDate) chkDate = true end

local nDays = tonumber(days) or 0

local retText = ""

if nDays > 0 then local start = theDate:copy theDate:adddays(nDays) retText = string.gsub(start:fmt("%d"), "^0", "") if not cardinal then retText = retText .. ordinal(start:getday) end

if start:getmonth ~= theDate:getmonth then retText = retText .. start:fmt(" %B") end

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

retText = retText .. " - "   end retText = retText .. string.gsub(theDate:fmt("%d"), "^0", "")

if not cardinal then retText = retText .. ordinal(theDate:getday) end retText = retText .. theDate:fmt(" %B")

if not noYear and (not chkDate or theDate:getyear ~= nextDate:getyear) then retText = retText .. theDate:fmt(" %Y") end

return retText end

-- function get single event local function singleEvent(sName, sNumber, sOption, altText) if sName == "ttc" then return "" end

local eventName = "Unknown Event" local eventNumb = {} local eventDate = "" local eventDays = nil local eventMain = nil local npcVer   = nil

local linkText = "Work In Progress" local linkHash = ""

local outText = ""

if meData[sName] then eventName = meData[sName]["Name"] or "Data Error" imageNPC = meData[sName]["Icon"] or "" nameNPC  = meData[sName]["NPC"] or "" linkText = eventName .. " Mini Event"

for n in string.gmatch(sNumber, "%d+") do           if meData[sName][tonumber(n)] then table.insert(eventNumb, tonumber(n)) end end

if meData[sName]["Disp"] then eventName = meData[sName]["Disp"] end

if eventNumb[1] then eventDate = meData[sName][eventNumb[1]][1] or "" eventDays = meData[sName][eventNumb[1]][2] or 0 eventMain = meData[sName][eventNumb[1]][3] npcVer   = meData[sName][eventNumb[1]][4] linkHash = "#" .. eventName .. " #"               .. tostring(eventNumb[1]) end end

if string.match(sOption, "[ETKHL]") then -- Fixed Formats if string.match(sOption, "E") then -- Event Page Format outText = "|-\n| align=\"center\" width=\"1\"|"

if nameNPC ~= "" then outText = outText .. npc({nameNPC, nil, nil, nil, eventMain or npcVer}) end

outText = outText .. "\n|"                   .. eventName                    .. "\n|"

if eventDate ~= "" then outText = outText .. strDate(eventDate, eventDays, true) end outText = outText .. "\n"

elseif string.match(sOption, "T") then -- TimeLine Page Format outText = "|"

if nameNPC ~= "" then outText = outText .. npc({nameNPC, nil, nil, nil, eventMain or npcVer}) end

outText = outText .. " "                   .. altText .. "[["                   .. linkText                    .. linkHash                    .. "|"                    .. eventName

if eventNumb[1] and eventDate ~= "" then outText = outText .. " #" .. eventNumb[1] end

outText = outText .. "]]"                   .. altText .. "\n|"

if eventDate ~= "" then outText = outText .. strDate(eventDate, eventDays) end outText = outText .. "\n"

elseif string.match(sOption, "K") then -- Token Page Activity Format outText = "|-\n|"

if nameNPC ~= "" then outText = outText .. npc({nameNPC, nil, nil, 50, eventMain or npcVer}) end

outText = outText .. "\n| colspan=\"4\" align=\"left\"|"                   .. eventName                    .. " Mini Event ''* " .. altText .. "'' "

elseif string.match(sOption, "H") then -- Horizontal List Format outText = ""                   .. eventName                    .. ""

elseif string.match(sOption, "L") then -- Latest Info Main Page Format outText = "* "                   .. eventName                    .. ""

if eventDate ~= "" then outText = outText .. " "                       .. strDate(eventDate, 0, nil, nil, true) .. " "           end outText = outText .. "\n" end else -- Normal Mode outText = "[[" .. linkText .. linkHash

if eventNumb[1] and eventDate ~= "" then outText = outText .. "|"           if string.match(sOption, "9") then outText = outText .. eventName if not string.match(sOption, "S") then outText = outText .. " Mini Event" end local sNo = "" local sep = false

for _, n in ipairs(eventNumb) do                   if sNo ~= "" then outText = outText .. (sep and "," or "") .. " #" .. sNo sep = true end sNo = tostring(n) end outText = outText .. (sep and " and" or "") .. " #" .. sNo

elseif string.match(sOption, "A") and altText ~= "" then outText = outText .. altText else outText = outText .. eventName if not string.match(sOption, "S") then outText = outText .. " Mini Event" end end else if string.match(sOption, "A") and altText ~= "" then outText = outText .. "|" .. altText elseif not string.match(sOption, "S") then outText = outText .. "|" .. eventName .. " Mini Event" end end

outText = outText .. "]]"       if string.match(sOption, "D") then outText = outText .. " on the "

local sNo = nil local sep = false

for _, n in ipairs(eventNumb) do               if sNo then outText = outText .. (sep and ", " or "") .. strDate(meData[sName][sNo][1],                                   nil, nil, meData[sName][n][1]) sep = true end sNo = n           end outText = outText .. (sep and ", " or "") .. (meData[sName][sNo] and strDate(meData[sName][sNo][1]) or "??") end end

return outText end

local function getCount(sName) local nCount = 0

local today = os.date("%Y/%m/%d") if meData[sName] then for idx, data in ipairs(meData[sName]) do           if today >= data[1] then nCount = nCount + 1 end end end

return "This mini event has occurred " .. nCount .. " time" .. ((nCount == 1) and "" or "s")

end

-- Public Functions --

-- function get Mini Event function p.getME(frame) local tArgs = getArgs(frame)

local sName   = tArgs[1] or "" local sNumber = tArgs[2] or "" local sOption = tArgs[3] or "" local altText = tArgs[4] or ""

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

sOption = tostring(sOption)

-- [==[   -- Temporary During Transition from Old Template to LUA based. if sOption and sOption ~= "" and string.match(sOption, "[^CETHLKF9SAD]") then if altText and altText ~= "" then altText = sOption sOption = "K" else altText = sOption sOption = "A" end end --]==]

sOption = sOption:upper

if sOption == "" then if sNumber ~= "" then sOption = "S" end end

if string.match(sOption, "C") then return getCount(sName) else return singleEvent(sName, sNumber, sOption, altText) end end

-- return last X versions formatted for Main Page function p.getLastME(frame) local tArgs = getArgs(frame)

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

local tIndex = {}

local today = os.date("%Y/%m/%d") for k, v in pairs(meData) do       if k ~= "ttc" then for idx, data in ipairs(v) do               if today >= data[1] then local t = {data[1], k, idx} table.insert(tIndex, t)               end end end end

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

local outText = ""

for i, ver in ipairs(tIndex) do       outText = outText .. singleEvent(ver[2], ver[3], "L", "") if i >= count then break end end

return outText end

-- return all ME during Limited Time Event formatted for Event Page function p.getEventME(frame) local tArgs = getArgs(frame)

local event = tArgs[1] or ""

-- Does not make sense if no Event specified so return blank if event == "" then return "" end

event = string.lower(event)

local tIndex = {}

for k, v in pairs(meData) do       if k ~= "ttc" then for idx, data in ipairs(v) do               if event == data[3] then local t = {data[1], k, idx} table.insert(tIndex, t)               end end end end

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

local outText = ""

for i, ver in ipairs(tIndex) do       outText = outText .. singleEvent(ver[2], ver[3], "E", "") end

return outText end

-- return all ME Types in horizontal format

function p.getMEList local tIndex = {}

for k, v in pairs(meData) do       if k ~= "ttc" then local t = {k, k, 0} table.insert(tIndex, t)       end end

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

local outText = ""

local space = ""

for i, ver in ipairs(tIndex) do       outText = outText .. space .. singleEvent(ver[2], 0, "H", "")

space = " · " end

return outText end

function p.getMETimeLine local tIndex = {} local tMainEvents = {} local nCount = 0 local nEventCount = 0

local lte = require("Module:LTEvent").getLTE

for k, v in pairs(meData) do       for idx, data in ipairs(v) do            local t = {data[1], k, idx, data[3], nil} table.insert(tIndex, t)

if k ~= "ttc" and data[3] then if not tMainEvents[data[3]] then tMainEvents[data[3]] = 0 end tMainEvents[data[3]] = tMainEvents[data[3]] + 1 end if k ~= "ttc" then nCount = nCount + 1 if data[3] then nEventCount = nEventCount + 1 end end end tIndex[#tIndex][5] = true end

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

local prevMain = ""

local outText = "{| class=\"article-table\" style=\"width: 100%;\"\n" .. "! width=\"27%\"|Major Event\n" .. "! width=\"33%\"|Mini Event\n" .. "! width=\"40%\"|Date(s)\n"

for i, ver in ipairs(tIndex) do       outText = outText .. "|-\n"

if ver[2] == "ttc" then outText = outText .. "| colspan=\"3\"|" .. lte({ver[4], "T", "P"}) .. "\n" prevMain = "" else if ver[4] then if ver[4] ~= prevMain and tMainEvents[ver[4]] then outText = outText .. "| rowspan=\""                           .. tMainEvents[ver[4]]                            .. "\" |" .. lte({ver[4], "N", "P"}) .. "\n" prevMain = ver[4] end else outText = outText .. "|\n" prevMain = "" end

if ver[5] then outText = outText .. singleEvent(ver[2], ver[3], "T", "'''") else outText = outText .. singleEvent(ver[2], ver[3], "T", "") end end end

outText = outText .. "|}\n\n==Trivia==\n" .. "* There has been a total of " .. nCount .. " mini events, out of which " .. nEventCount .. " occurred during a major event, while " .. (nCount - nEventCount) .. " occured separately.\n"

return outText end

return p

-- --