Module:METables

--[[ Lua script to get Mini Event Tables

getMilestones: get Milestones Table based on input parameters Parameters: 1 =        2 =         3 = --]]

local p = {}

local cData = mw.loadData("Module:ItemCollection/data") local meData = mw.loadData("Module:MiniEvent/data") local util = require("Module:Utility") local EC = require("Module:EC").getEC local TOK = require("Module:CharacterToken").getToken local ICON = require("Module:CharacterAvatar").getAvatar local NPC = require("Module:NPC")

local tArgs = {}

-- LB Prizes local prize = {} local lb   = {} local lbA  = {} local lbV  = {} -- alternate LB ranking table. (uses the same prizes/counts) local alb  = {} local ms   = {} local msA  = {} local msV  = {} local ch   = {}

local meName = "" local iconSize = "30" local altStr = "

If already unlocked: "

local function lbItems(inStr) local tIn = mw.text.split(inStr, "%s*%$%s*") local tOut = {} tOut.range = tonumber((string.gsub(tIn[1], "%,", ""))) or 0 for idx = 2, #tIn do		table.insert(tOut, tIn[idx]) end return tOut end

local function albItems(inStr) local tIn = mw.text.split(inStr, "%s*%$%s*") local tOut = {} for i, v in ipairs(tIn) do		tOut[i] = tonumber((string.gsub(tIn[i], "[%,]", ""))) or 0 end return tOut end

local function msItems(inStr) local tIn = mw.text.split(inStr, "%s*%$%s*") local tOut = {} tOut.count = tIn[1] for idx = 2, #tIn do		local tt = mw.text.split(tIn[idx], "%s*#%s*") if not tt[2] or tt[2] == "" then tt[2] = 1 end table.insert(tOut, tt) end return tOut end

local function chItems(inStr) local tIn = mw.text.split(inStr, "%s*%$%s*") local tOut = {tIn[1], tIn[2]} return tOut end

local function processArgs(frame) tArgs = util.getArgs(frame)

meName = tArgs.mename or mw.title.getCurrentTitle.baseText meName = mw.text.decode(meName, false) :gsub("^%s*(.-)%s*$", "%1") :lower :gsub("%s*mini event$", "")

-- Leaderboard Prizes if tArgs.prize then prize = mw.text.split(tArgs.prize, "%s*%$%s*") end

-- Loop over all the args to get tier/ms prize counts for arg, value in pairs(tArgs) do		-- Prize / Reward Counts local cat, n = tostring(arg):match('^(%a+)([1-9]%d*)$') local num = tonumber(n) if num then if cat == "lb" then lb[num] = lbItems(value) elseif cat == "ms" then ms[num] = msItems(value) elseif cat == "ch" then ch[num] = chItems(value) elseif cat == "alb" then -- alternate LB rankings alb[num] = albItems(value) end end -- Alternate Prize/Reward cat, n = tostring(arg):match('^(%a+)([1-9]%d*)A$') num = tonumber(n) if num then if cat == "lb" then lbA[num] = lbItems(value) elseif cat == "ms" then msA[num] = msItems(value) end end -- Valued At ?? cat, n = tostring(arg):match('^(%a+)([1-9]%d*)V$') num = tonumber(n) if num then if cat == "lb" then lbV[num] = lbItems(value) elseif cat == "ms" then msV[num] = msItems(value) end end end end

local function getCollection(coll) local collName = "" for _, data in pairs(cData.Collections) do		if (data[3] and data[3] == coll) or coll == data[1] then collName = data[1] end end return collName end

local function oneItem(item, count, withName) local nCount = tonumber((string.gsub(count or "", "[,]", ""))) or 0 --skip item if count = 0 if nCount == 0 then return "" end

local conv = { ["ch"] = "cp", ["cos"] = "cp", }

local tItem = {"", " " .. util.th_comma(nCount), (withName and " " or ""), "", "",			(withName and " " or ""), }

local coll = nil local iName = item local iData = cData.Items[iName] if not iData then coll = item:match("^Legendary Chests%s*%-%s*(.-)$") if coll then iName = "Legendary Chests" iData = cData.Items[iName] end end

if iData and iData[2] then local fname = iData.I or iName:lower:gsub("[':%,%.]", ""):gsub(" ", "_") if iData[2] == "ec" then fname = fname:gsub("_chests$", "") elseif iData[2] == "pf" then fname = fname:gsub("_float$", "") else fname = fname:gsub("^([^%(]-)_*%(([^%d]+%d*)%)$", "%1-%2")		end

tItem[1] = table.concat({								""							}) if nCount == 1 and iData[2] ~= "ec" then tItem[2] = "" end

if withName then local tLink = { "", iName,				"", "", "", "", "",				"", }			local sp = nil local spText = iName if nCount == 1 and iData.S then sp = true spText = iName:gsub("[Ss]$", "") elseif iData.P then sp = true spText = string.gsub(iData.P, "%%N", function return iName end) end if coll or sp then tLink[3] = "|" tLink[4] = coll and getCollection(coll) or "" tLink[5] = coll and " " or "" tLink[6] = spText end tItem[4] = table.concat(tLink) end elseif item:match("^EC") then tItem[1] = EC({item, nil, nil, nil, iconSize}) if withName then tItem[4] = EC({item, "L", S=(nCount == 1)}) end else tItem[1] = TOK({item, iconSize}) if withName then tItem[4] = TOK({item, "L"}) if nCount > 1 then tItem[5] = "s" end end end

return table.concat(tItem) end

local function msTable local tOut = {'|-|Milestones=\n{| class="article-table dmk-milestone" ', 'style="width: 100%;"\n|-\n!Milestone\n'} local tRew = {'|-\n!Rewards\n'}

local maxMS = 4 local befCount = "" local aftCount = ""

if meName:match("tower challenge") then maxMS = 5 aftCount = " Points" else if meData[meName] and meData[meName].NPC then befCount = NPC.getNPC({meData[meName].NPC, "", "", iconSize, tArgs.npcver}) end end

for i = 1, maxMS do		table.insert(tOut, "!") table.insert(tRew, "|") if ms[i] then table.insert(tOut, befCount) table.insert(tOut, util.th_comma(ms[i].count)) table.insert(tOut, aftCount)

for rNo, rData in ipairs(ms[i]) do				if rNo > 1 then table.insert(tRew, "

") end				table.insert(tRew, oneItem(rData[1], rData[2], true))			end			if msA[i] then				table.insert(tRew, altStr)				for rNo, rData in ipairs(msA[i]) do					if rNo > 1 then table.insert(tRew, "

") end					table.insert(tRew, oneItem(rData[1], rData[2], true))				end			elseif msV[i] then				table.insert(tRew, " (Valued at ")				local vData = msV[i][1]				local iSize = (tonumber(iconSize) or 30) * 0.6				table.insert(tRew, EC({vData[1], nil, nil, nil, iSize}))				table.insert(tRew, util.th_comma(vData[2]))				table.insert(tRew, ") ")			end		end		table.insert(tOut, "\n")		table.insert(tRew, "\n")	end	return table.concat(tOut) .. table.concat(tRew) .. "|}\n" end

local function lbTable(altRank) local tOut = {'|-|Leaderboards', , , , , '=\n', '{| class="article-table dmk-leaderboard" style="width: 100%;"\n', '! style="width: 1%"|Rank\n! colspan="4"|Rewards\n', }

local useAlt = nil if type(altRank) == "number" and alb[altRank] then useAlt = alb[altRank] tOut[2] = " (Alternate"		tOut[5] = ")" if #alb > 1 then tOut[3] = " " tOut[4] = tostring(altRank) end end

local prevRange = 0 local prevPrize = {}

for lbNo, lbData in ipairs(lb) do		table.insert(tOut, "|-\n| nowrap|") table.insert(tOut, util.th_comma(prevRange + 1)) table.insert(tOut, "-") if type(useAlt) == "table" then prevRange = useAlt[lbNo] else prevRange = lbData.range end table.insert(tOut, util.th_comma(prevRange)) table.insert(tOut, "\n") for idx= 1, 4 do			table.insert(tOut, "|") if lbData[idx] then local iName = prize[idx] local iCount = string.match(lbData[idx], "^[%d%,]*$") if not iCount then iName, iCount = string.match(lbData[idx], "^([^#]-)%s*#*%s*([%d%,]*)$") if iCount == "" then iCount = "1" end end if iCount == "" then iCount = "0" end if iName then table.insert(tOut, oneItem(iName, iCount, iName ~= prevPrize[idx])) end prize[idx]    = iName prevPrize[idx] = iName end if lbA[lbNo] and lbA[lbNo][idx] then local lbAData = lbA[lbNo] local iName, iCount = string.match(lbAData[idx], "^([^#]-)%s*#*%s*([%d%,]*)$") if iCount == "" then iCount = "1" end if iName and iName ~= "" then table.insert(tOut, altStr) table.insert(tOut, oneItem(iName, iCount, true)) end elseif lbV[lbNo] and lbV[lbNo][idx] then local lbVData = lbV[lbNo] local iName, iCount = string.match(lbVData[idx], "^([^#]-)%s*#*%s*([%d%,]*)$") if iCount == "" then iCount = "1" end if iName and iName ~= "" then table.insert(tOut, " (Valued at ")					local iSize = (tonumber(iconSize) or 30) * 0.6					table.insert(tOut, EC({iName, nil, nil, nil, iSize}))					table.insert(tOut, util.th_comma(iCount))					table.insert(tOut, ") ") end end table.insert(tOut, "\n") end end

return table.concat(tOut) .. "|}\n" end

local function chTable local tHead = {'|-|Character Tasks=\n', '{| class="article-table" style="width: 100%;"\n', }	local tRow = {"|-\n"}

for chNo, chData in ipairs(ch) do table.insert(tHead, "! width=\"10%\"|Character\n!Time\n") table.insert(tRow, "| align=\"center\"|") table.insert(tRow, ICON({chData[1]})) table.insert(tRow, "\n|") table.insert(tRow, chData[2]) table.insert(tRow, "\n") end

local npcName if meData[meName] then npcName = meData[meName].NPC end

if npcName and tArgs.TOK then local tokens = tArgs.TOK local t1Count, t2Count = tokens:match("^%s*(%d+)%s*%$%s*(%d+)%s*$") if t1Count then tokens = npcName .. ",1," .. t1Count .. "$" .. npcName .. ",2," .. t2Count end

table.insert(tHead, "! width=\"20%\"|Requirements\n") table.insert(tRow, "|") table.insert(tRow, TOK({tokens})) table.insert(tRow, "\n") end

return table.concat(tHead) .. table.concat(tRow) .. "|}\n" end

function p.getTables(frame) processArgs(frame)

iconSize = tArgs.iconsize or "30" local allTables = {}

if ch[1] then table.insert(allTables, chTable) end

if ms[1] then table.insert(allTables, msTable) end

if lb[1] then table.insert(allTables, lbTable) end

for idx, data in ipairs(alb) do		table.insert(allTables, lbTable(idx)) end

return table.concat(allTables) end

return p