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 = (not rank.noncanon) and yesno(opts.bracket, true) local append_ = string.trim(opts.append or '') -- text without space local append = opts.append and (' %s'):format(append_) or '' -- text added space local rankdisp, colorcode = typeHandler(rank.display), typeHandler(rank.colorcode) local rankless = opts.rankless or (append_ ~= '' and rank.noncanon) 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 rankless then append = append_ 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: wrap bracket early, colorize as a whole rankdisp = colorize(('&#91;%s&#93;'):format(rankdisp), delta) bracket = false -- Done else rankdisp = colorize(('%s'):format(rankdisp), delta) -- If no bracket, done here -- If needs bracket, this part only colorizes the rank part end end -- wrap brackets, colorize separately if bracket then rankdisp = ('%s%s%s'):format(				colorize('&#91;', gamma),				rankdisp,				colorize('&#93;', gamma)				) -- Done end end local text if rankless then text = ('%s'):format(rank.index, colorize(append, gamma)) elseif opts.link then text = ('[%s %s]%s'):format(rank.index, opts.link, rankdisp, colorize(append, gamma)) else text = ('%s%s'):format(rank.index, rank.link, rankdisp, colorize(append, gamma)) end local ret = string.wrapHtml(text, 'span', {		class = 'mcui-text',		style = {			display = 'inline',			['flex-wrap'] = 'wrap',			['font-size'] = opts.size,		}	}) return mw.getCurrentFrame:preprocess(ret) end

return p