Module:MaroScale

From MTG Wiki
Revision as of 03:39, 28 December 2016 by >Corveroth
Jump to navigation Jump to search

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(target, displayText)
	if dispalyText then
		return "[["..target.."|"..displayText.."]]"
	else
		return "[["..target.."]]"
	end
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)]
        _ = args["subnolink" .. 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, subnolink = args["name"..listnum], args["entry"..listnum], args["ratings"..listnum], args["subname"..listnum], args["subentry"..listnum], args["subratings"..listnum], args["subnolink"..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, entry) else cellText = link(entry) end
				local cellList = row:tag("td"):tag("ul"):cssText("margin-top:0; margin-left:0; list-style-type: none;"):tag("li"):cssText("display:block;"):wikitext(cellText):done()
				if hasSubentry then
					if subnolink then
						cellText = subentry
					else
						if subname then cellText = link(subname) else cellText = link(subentry) end
					end
					cellList:tag("li"):cssText("display:block; margin-left: 1.0em;"):wikitext(cellText)
				end
			end
			-- Second cell
			do
				local cellText = sortedRatings[#sortedRatings].rating
				local cellList = row:tag("td"):tag("ul"):cssText("margin-top:0; margin-left:0; list-style-type: none;"):tag("li"):cssText("display:block;"):wikitext(cellText):done()
				if hasSubentry then
					cellText = sortedSubRatings[#sortedSubRatings].rating
					cellList:tag("li"):cssText("display:block; margin-left: 1.0em;"):wikitext(cellText)
				end
			end
			
			-- Third cell
			do
				local cell = row:tag("td"):addClass("hlist")
				local cellList = cell:tag("ul"):cssText("margin-top:0; margin-left:0; list-style-type: none;")
				local firstItem = cellList:tag("li"):cssText("display:block;")
					local list = firstItem:tag("ul"):addClass("barelist"):cssText("display:block;")
					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 = firstItem:done():tag("li"):cssText("display:block;")
					local list = firstItem:tag("ul"):addClass("barelist"):cssText("display:block; margin-left: 1.6em;")
					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