Module:MaroScale: Difference between revisions

From MTG Wiki
Jump to navigation Jump to search
>Corveroth
No edit summary
>Corveroth
No edit summary
Line 51: Line 51:
         _ = args["ratings" .. tostring(i)]
         _ = args["ratings" .. tostring(i)]
         _ = args["subratings" .. tostring(i)]
         _ = args["subratings" .. tostring(i)]
        _ = args["subnolink" .. tostring(i)]
     end
     end
Line 72: Line 73:
local name, entry, ratings, subname, subentry, subratings
local name, entry, ratings, subname, subentry, subratings
for i, listnum in ipairs(listnums) do
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]
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 sortedRatings = parseRatings(rawRatings)
Line 91: Line 92:
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()
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 hasSubentry then
if subname then cellText = link(subname) else cellText = link(subentry) end
if subnolink then
cellList:tag("li"):cssText("display:block; margin-left: 1.6em;"):wikitext(cellText)
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
end
end
Line 101: Line 106:
if hasSubentry then
if hasSubentry then
cellText = sortedSubRatings[#sortedSubRatings].rating
cellText = sortedSubRatings[#sortedSubRatings].rating
cellList:tag("li"):cssText("display:block; margin-left: 1.6em;"):wikitext(cellText)
cellList:tag("li"):cssText("display:block; margin-left: 1.0em;"):wikitext(cellText)
end
end
end
end
Line 119: Line 124:
end
end
if hasSubentry then
if hasSubentry then
local secondItem = cellList:tag("li"):cssText("display:block; margin-left: 1.6em;")
local secondItem = cellList:tag("li"):cssText("display:block;")
local list = firstItem:tag("ul"):addClass("barelist"):cssText("display:block;")
local list = firstItem:tag("ul"):addClass("barelist"):cssText("display:block; margin-left: 1.6em;")
for n, rating in pairs(sortedSubRatings) do
for n, rating in pairs(sortedSubRatings) do
if n ~= #sortedSubRatings then
if n ~= #sortedSubRatings then

Revision as of 03:36, 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(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 = cellList: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