Module:Rank

local getArgs = require('Module:Arguments').getArgs local loader = require('Module:Loader')

local string, table, yesno = loader.require('String', 'Table', 'Yesno')

local rankData, rankAliases = loader.loadData('Rank/Data', 'Rank/Aliases')

local p = {}

-- Template:Rank function p.serverRank(frame) local args = getArgs(frame) local rankname = args.rankname or args[1] local opts = { size = (args.size or 12)..'px', bracket = yesno(args.brackets, true) and yesno(args.bracket, true) and yesno(args.br, true), }	return p._serverRank(rankname, opts) end

-- Template:RankName function p.playerName(frame) local args = getArgs(frame) local skyshiiyumoe = yesno(args.skyshiiyumoe) or yesno(args.ssm) local profile = args.profile or args.prof local rankname = args.rankname or args[1] local rank = yesno(args.rank, true) skyshiiyumoe = (skyshiiyumoe or profile) and ('https://sky.shiiyu.moe/%s'):format(profile and 'stats/'..profile or '') or nil local opts = { size = (args.size or 12)..'px', bracket = true, append = args.player or args[2], link = skyshiiyumoe, rankless = not rank, }	return p._serverRank(rankname, opts) end

-- Access Point function p._serverRank(name, opts) local function colorize(text, c)		if string.trim(text) ==  then return  end return string.wrapHtml(text, 'span', { class = ('format-%s'):format(c) }) end local function typeHandler(d) if type(d) == 'table' then d = table.deepCopy(d, true) d = #d < 2 and table.concat(d) or d		end return d	end local rank = rankData[rankAliases[string.upper(name)] or string.upper(name)] if not rank then return string._error('No such rank: '..name) end local bracket = yesno(opts.bracket, true) local append = opts.append and (' %s'):format(opts.append) or '' local rankdisp, colorcode = typeHandler(rank.display), typeHandler(rank.colorcode) local delta = type(colorcode) == 'table' and colorcode[1] or colorcode local gamma = rank.bracketcolor or delta delta = not bracket and (rank.bracketcolor or delta) or delta if opts.rankless then rankdisp = colorize(string.trim(append), gamma) else if type(colorcode) == 'table' then if type(rankdisp) == 'table' then rankdisp = table.concat(table.map(rankdisp, function(v, i)					return colorize(v, colorcode[i]) end)) else local r, s = string.match(rankdisp, '(%w+)(%++)') if r and s then rankdisp = ('%s%s'):format(colorize(r, colorcode[1]), colorize(s, colorcode[2])) else rankdisp = colorize(rankdisp, colorcode[1]) end end else if bracket and not rank.bracketcolor then -- special case: place everything together and colorize rankdisp = colorize(('&#91;%s&#93;%s'):format(rankdisp, append), delta) bracket = false -- Done else rankdisp = colorize(('%s%s'):format(rankdisp, not bracket and append or ''), delta) end end if bracket then rankdisp = ('%s%s%s%s'):format(				colorize('&#91;', gamma),				rankdisp,				colorize('&#93;', gamma),				colorize(append, gamma)				) -- Done end end local text = string.wrapHtml(rankdisp, 'span', {		class = 'mcui-text',		style = {			display = 'inline',			['flex-wrap'] = 'wrap',			['font-size'] = opts.size,		}	}) local ret if opts.link then ret = ('[%s %s]'):format(rank.index, opts.link, text) else ret = ('%s'):format(rank.index, rank.link, text) end return mw.getCurrentFrame:preprocess(ret) end

return p