Module:MaroScale: Difference between revisions

From MTG Wiki
Jump to navigation Jump to search
>Corveroth
No edit summary
>Corveroth
No edit summary
Line 21: Line 21:
local type = args.type
local type = args.type
local tbl = mw.html.create("table"):addClass("wikitable"):addClass("sortable")
local tbl = mw.html.create("table"):addClass("wikitable"):addClass("sortable")
local head = tbl:tag("th")
local head = tbl:tag("thead")
head:tag("td"):wikitext(type)
head:tag("td"):wikitext(type)
head:tag("td"):wikitext("Latest ranking")
head:tag("td"):wikitext("Latest ranking")

Revision as of 03:38, 3 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 be 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 = {}

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, 100 do
        _ = args["entry" .. tostring(i)]
        _ = args["ratings" .. tostring(i)]
    end
	
	-- Table type
	local type = args.type
	local tbl = mw.html.create("table"):addClass("wikitable"):addClass("sortable")
	local head = tbl:tag("thead")
	head:tag("td"):wikitext(type)
	head:tag("td"):wikitext("Latest ranking")
	head:tag("td"):wikitext("History of rankings")
	head: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)
	
	for i, listnum in ipairs(listnums) do
		local newestDate, newestRating = 0, 0
		local rating, refDate, refString
		local ratings = {}
		string.gsub(args["ratings"..i], "({.-})", function(a)
			rating, refDate, refString = string.match(a, "(%d+),([%d%-]+),(.+)")
			rating = tonumber(rating)
			table.insert(ratings, {rating = rating, refDate = refDate, refString = refString})
			-- Dates MUST be yyyy-mm-dd
			local 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
		end)
		-- maybe later, it's not worth the effort right now
		-- table.sort(ratings, function(a,b) return a.refDate < b.refDate end)
		
		local row = tbl:tag("tr")
		row:tag("td"):wikitext(name or "Name not given")
		row:tag("td"):wikitext(newestRating or "Rating not found")
		local list = row:tag("td"):tag("ul")
		for _, rating in pairs(ratings) do
			list:tag("li"):wikitext(refDate .. rating.refString)
		end
	end
	return tbl
end

return MaroScale