Module:MaroScale: Difference between revisions

From MTG Wiki
Jump to navigation Jump to search
>Corveroth
mNo edit summary
>Corveroth
(This probably breaks something)
Line 4: Line 4:


local MaroScale = {}
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)
function MaroScale.TableBuilder(frame)
Line 14: Line 41:
     _ = args.above
     _ = args.above
     for i = 1, 200 do
     for i = 1, 200 do
        _ = args["name" .. tostring(i)]
         _ = args["entry" .. tostring(i)]
         _ = args["entry" .. tostring(i)]
        _ = args["subname" .. tostring(i)]
        _ = args["subentry" .. tostring(i)]
         _ = args["ratings" .. tostring(i)]
         _ = args["ratings" .. tostring(i)]
        _ = args["subratings" .. tostring(i)]
     end
     end
Line 30: Line 61:
local listnums = {}
local listnums = {}
for k, v in pairs(args) do
for k, v in pairs(args) do
        local listnum = ('' .. k):match('^ratings(%d+)$')
local listnum = ('' .. k):match('^ratings(%d+)$')
        if listnum then table.insert(listnums, tonumber(listnum)) end
if listnum then table.insert(listnums, tonumber(listnum)) end
    end
end
    table.sort(listnums)
table.sort(listnums)
 
local name, entry, ratings, subname, subentry, subratings
for i, listnum in ipairs(listnums) do
for i, listnum in ipairs(listnums) do
local newestDate, newestRating = 0, 0
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 rating, refDate, refString, t
local sortedRatings = {}
local ratings = {}
local sortedSubratings
string.gsub(args["ratings"..listnum], "{(.-)}", 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(ratings, {rating = rating, refDate = refDate, refString = refString, t = t})
end)
-- maybe later, it's not worth the effort right now
local hasSubentry = subentry and rawSubratings -- subname not necessary
table.sort(ratings, function(a,b) return a.t < b.t end)
-- Only init the table if the subentry exists
if hasSubentry then
sortedSubratings = {}
end
local entryLink = "[["..args["entry"..listnum].."]]"
-- Parse the ratings
local sortedRatings = parseRatings(rawRatings)
local sortedSubRatings = parseRatings(rawSubratings)
-- Make the row
local row = tbl:tag("tr")
local row = tbl:tag("tr")
row:tag("td"):wikitext(entryLink or "Name not given")
do
row:tag("td"):wikitext(newestRating or "Rating not found")
-- First cell
local cell = row:tag("td"):addClass("hlist")
do
local list = cell:tag("ul"):addClass("barelist")
local cellText
 
if name then cellText = link(name) else cellText = link(entry) end
for n, rating in pairs(ratings) do
local cellList = row:tag("td"):tag("ul"):tag("li"):cssText("list-style-type: none; padding-left: 0;"):wikitext(cellText):done()
if n ~= #ratings then
if hasSubentry then
list:tag("li"):wikitext(rating.rating .. rating.refString .. ", ")
if subname then cellText = link(subname) else cellText = link(subentry) end
else
cellList:tag("li"):cssText("list-style-type: none;"):wikiText(cellText)
list:tag("li"):wikitext(rating.rating .. rating.refString)
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
end
end
end
return tbl
return tbl

Revision as of 03:03, 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 = {}
		local sortedSubratings
		
		local hasSubentry = subentry and rawSubratings -- subname not necessary
		-- Only init the table if the subentry exists
		if hasSubentry then
			sortedSubratings = {}
		end
		
		-- Parse the ratings
		local sortedRatings = parseRatings(rawRatings)
		local sortedSubRatings = parseRatings(rawSubratings)
		
		-- 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"):tag("li"):cssText("list-style-type: none; padding-left: 0;"):wikitext(cellText):done()
				if hasSubentry then
					if subname then cellText = link(subname) else cellText = link(subentry) end
					cellList:tag("li"):cssText("list-style-type: none;"):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