Module:MaroScale: Difference between revisions
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+)$') | |||
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 | 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 | |||
local | -- 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( | 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 | 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