Module:MaroScale: Difference between revisions

From MTG Wiki
Jump to navigation Jump to search
>Corveroth
No edit summary
>Corveroth
No edit summary
Line 85: Line 85:
local cellText
local cellText
if name then cellText = link(name) else cellText = link(entry) end
if name then cellText = link(name) else cellText = link(entry) end
local cellList = row:tag("td"):tag("ul"):cssText("margin-left:0; list-style-type: none;"):tag("li"):wikitext(cellText):done()
local cellList = row:tag("td"):tag("ul"):cssText("margin-left:0; list-style-type: none;"):tag("li"):cssText("display:block;"):wikitext(cellText):done()
if hasSubentry then
if hasSubentry then
if subname then cellText = link(subname) else cellText = link(subentry) end
if subname then cellText = link(subname) else cellText = link(subentry) end

Revision as of 03:08, 28 December 2016

Documentation for this module may be created at Module:MaroScale/doc

-- Little bit of Lua for building tables for the Storm and Rabiah scales.
-- Mechanics have been re-ranked in the past, and it's probably interesting to track their drift.
-- Rather than force editors to track the newest one, let's see if we can't do it in code.

local MaroScale = {}

local function link(text)
	return "[["..text.."]]"
end

local function parseRatings(raw)
	local out = {}
	local newestDate, newestRating = 0, 0
	local rating, refDate, refString, t
	string.gsub(raw, "{(.-)}", function(a)
		rating, refDate, refString = string.match(a, "(%d+),([%d%-]+),(.+)")
		rating = tonumber(rating)
		
		-- Dates MUST be yyyy-mm-dd
		t = os.time({year=string.sub(refDate, 1,4), month=string.sub(refDate, 6,7), day=string.sub(refDate, 9,10),})
		if t > newestDate then
			newestDate = t
			newestRating = rating
		end
		
		table.insert(out, {rating = rating, refDate = refDate, refString = refString, t = t})
	end)
	-- Sort ratings into chronological order
	table.sort(out, function(a,b) return a.t < b.t end)
	return out
end


function MaroScale.TableBuilder(frame)
	local args = frame:getParent().args -- coming from one layer up
	
	-- Imitating navbox, here. I trust they know what they're doing.
	-- Read the arguments in the order they'll be output in, to make references number in the right order.
    local _
    _ = args.type
    _ = args.above
    for i = 1, 200 do
        _ = args["name" .. tostring(i)]
        _ = args["entry" .. tostring(i)]
        _ = args["subname" .. tostring(i)]
        _ = args["subentry" .. tostring(i)]
        _ = args["ratings" .. tostring(i)]
        _ = args["subratings" .. tostring(i)]
    end
	
	-- Table type
	local type = args.type
	local tbl = mw.html.create("table"):addClass("wikitable"):addClass("sortable")
	tbl:tag("tr")
		:tag("th"):wikitext(type):done()
		:tag("th"):wikitext("Latest ranking"):done()
		:tag("th"):wikitext("History of rankings"):done()

	
	-- Seriously this loop is entirely too clever
	local listnums = {}
	for k, v in pairs(args) do
		local listnum = ('' .. k):match('^ratings(%d+)$')
		if listnum then table.insert(listnums, tonumber(listnum)) end
	end
	table.sort(listnums)

	local name, entry, ratings, subname, subentry, subratings
	for i, listnum in ipairs(listnums) do
		name, entry, rawRatings, subname, subentry, rawSubratings = args["name"..listnum], args["entry"..listnum], args["ratings"..listnum], args["subname"..listnum], args["subentry"..listnum], args["subratings"..listnum]
		
		local sortedRatings = parseRatings(rawRatings)
		local sortedSubratings
		
		local hasSubentry = subentry and rawSubratings -- subname not necessary
		if hasSubentry then
			sortedSubRatings = parseRatings(rawSubratings)
		end
		
		-- Make the row
		local row = tbl:tag("tr")
		do
			-- First cell
			do
				local cellText
				if name then cellText = link(name) else cellText = link(entry) end
				local cellList = row:tag("td"):tag("ul"):cssText("margin-left:0; list-style-type: none;"):tag("li"):cssText("display:block;"):wikitext(cellText):done()
				if hasSubentry then
					if subname then cellText = link(subname) else cellText = link(subentry) end
					cellList:tag("li"):cssText("margin-left: 1.6em;"):wikiText(cellText)
				end
			end
			-- Second cell
			do
				local cellText = sortedRatings[#sortedRatings].rating
				local cellList = row:tag("td"):tag("ul"):tag("li"):cssText("list-style-type: none; padding-left: 0;"):wikitext(cellText):done()
				if hasSubentry then
					cellText = sortedSubRatings[#sortedSubRatings].rating
					cellList:tag("li"):cssText("list-style-type: none;"):wikiText(cellText)
				end
			end
			
			-- Third cell
			do
				local cell = row:tag("td"):addClass("hlist")
				local cellList = cell:tag("ul")
				local firstItem = cellList:tag("li"):cssText("display: list-item !important;")
					local list = firstItem:tag("ul"):addClass("barelist")
					for n, rating in pairs(sortedRatings) do
						if n ~= #sortedRatings then
							list:tag("li"):wikitext(rating.rating .. rating.refString .. ", ")
						else
							list:tag("li"):wikitext(rating.rating .. rating.refString)
						end
					end
				if hasSubentry then
					local secondItem = cellList:tag("li"):cssText("display: list-item !important;")
					local list = firstItem:tag("ul"):addClass("barelist")
					for n, rating in pairs(sortedSubRatings) do
						if n ~= #sortedSubRatings then
							list:tag("li"):wikitext(rating.rating .. rating.refString .. ", ")
						else
							list:tag("li"):wikitext(rating.rating .. rating.refString)
						end
					end
				end
			end
		end
	end
	return tbl
end

return MaroScale