Module:UpdateNumber

-- 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 = {} local versionData = mw.loadData( 'Module:UpdateNumber/data' ) 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[4] and updateNo == v[4] then versionNo = k                       break end end end end end

if 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][5] and versionData[versionNo][5] ~= "" then versionText = versionData[versionNo][5] else versionText = versionNo end txtOnly = true elseif outType == "R" then versionText = versionData[versionNo][1] txtOnly = true else local upNo = versionData[versionNo][4] if upNo and upNo == 0 then versionText = versionData[versionNo][2] else versionText = versionData[versionNo][2] .. " Update" end

if outType == "V" then if versionData[versionNo][5] and versionData[versionNo][5] ~= "" then versionText = versionData[versionNo][5] .. " ("                               .. versionText .. ")" else versionText = versionNo .. " ("                               .. versionText .. ")" end txtOnly = true end end

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

if not txtOnly then if versionData[versionNo][4] and versionData[versionNo][4] ~= 0 then versionText = "" .. versionText .. "" else versionText = "" .. versionText .. "" end end

if inclDate == "U" then versionText = "" if versionData[versionNo][4] then versionText = " and the " .. util.wordinal(versionData[versionNo][4]) .. " 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]) end

return versionText end

-- return a list of versions for year local function multiVersion(year, vertical) local tIndex = {}

if year and year ~= 0 then local patt = "^" .. tostring(year) for k, v in pairs(versionData) do           if tostring(v[3]):match(patt) then table.insert(tIndex, k)           end end else for k, v in pairs(versionData) do           table.insert(tIndex, k)        end end

if vertical then table.sort(tIndex, function(a,b) return a > b end) else table.sort(tIndex) end

local outText = "" local sDot   = ""

for _, ver in ipairs(tIndex) do       local verText = ""

if versionData[ver][5] and versionData[ver][5] ~= "" then verText = versionData[ver][5] end

if vertical or not versionData[ver][5] or versionData[ver][5] ~= "" then if versionData[ver][4] and versionData[ver][4] ~= 0 then outText = outText .. sDot .. "" .. verText .. "" else outText = outText .. "|" .. ver .. "]]"               end else outText = outText .. sDot .. "" .. verText               end                outText = outText .. "" end sDot = vertical and " " or " · " end if vertical then outText = outText .. " - "                   ..  string.gsub(tostring(versionData[ver][3]),                            "(%d%d%d%d)/?(%d%d)/?(%d%d)", "%3/%2/%1") .. " - "                   .. versionData[ver][2]

if not versionData[ver][4] or versionData[ver][4] ~= 0 then outText = outText .. " Update" end end end

return outText 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] and v[4] and v[4] ~= 0 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 .. "* "               .. verText                .. " " .. dateText .. " \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] ~= "")

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

if multi then return multiVersion(tonumber(year), (tArgs[2] and tArgs[2] ~= "")) 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[4] and updateNo == v[4] then curVer = k                       break end end end end end

local curData = versionData[curVer]

if not curData then curData = {0, "Unknown Version", nil, 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[5] and curData[5] ~= "" then curVer = curData[5] end

local VL = mw.ext.VariablesLua

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

local tVer = {curVer, " (", curData[2], "", ")"} if not curData[4] or curData[4] ~= 0 then tVer[4] = " Update" end VL.vardefine("pnVer", table.concat(tVer)) VL.vardefine("pnDate", util.strDate(curData[3] or nil))

local function prevnext(ver, data) if ver then local tLink = {"", data[2], " Update", ""} if data[4] then if data[4] ~= 0 then tLink[2] = ":Category:Update " tLink[3] = tostring(data[4]) else tLink[6] = "" end end return table.concat(tLink) else return "--" end end

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

return p