模块:Caps

来自Rizline中文维基

可在模块:Caps/doc创建此模块的帮助文档

local getArgs = require('Module:Arguments').getArgs;

local p = setmetatable({},{
    __index = function (t, k)
        local rawFunc = t['_' .. k];
        if rawFunc then
            local rtnFunc = function(frame)
                return rawFunc(getArgs(frame))
            end
            rawset(t,k,rtnFunc)
            return rtnFunc;
        end
        return nil;
    end
})

local function getCap(txt,color,bgcolor,padding,margin)
    -- 其实可以考虑改成使用TemplateStyle。
    return string.format(
        '<span style="width: max-content;text-align:center;display:inline-block;border-radius:1em;color:%s;background-color:%s;padding:0 %.2fem;margin:0.1em %.2fem;">%s</span>',
        color or 'white',
        bgcolor or 'black',
        padding or 1,
        margin or 0.5,
        txt or '一串友好的文字'
    )
end

local toLowerCase = string.lower;
local function searchByList(table,str,i)
    i = i or 1;
    local lStr = toLowerCase(str)
    for _, t in ipairs(table) do
        for _i = i,#t do
            if toLowerCase(t[_i]) == lStr then return t;end
        end
    end
    return nil;
end

function p._rank(args)
    local CapList = {
        {'PERFECT','#FFD40B','Perfect','P'},
        {'CLEAR','#61D8FF','Clear','C'},
        {'NOT CLEAR','#CFCFCF','Not Clear','NC'},
        {'MOD PLAY','#CFCFCF','Mod Play','MP'}
    }
    local name = args[1]
    local l = searchByList(CapList,name,3);
    if l then
        return getCap(l[1],'white',l[2],1,1);
    else error('找不到Rank"' .. name ..'"对应的信息!');end
end

function p._diff(args)
    local CapList = {
        {'EZ','#57E4C4','EZ','Eazy','E'},
        {'HD','#FDBA61','HD','Hard','H'},
        {'IN','#FE8661','IN','Insane','I'},
        {'AT','#4C364B','AT','AnoTher','A'},
      --{'Def','#53D6FF'}
    }
    local name = args[1]
    local l = searchByList(CapList,name,3) or {name,'#53D6FF'};
    local diff = args[2];

    local str = l[1];
    if diff then str = str .. ' ' .. diff;end
    return getCap(str,'white',l[2],0.8,0.5);
end

return p;