七日内新公告:全站维护公告
  • 你好~!欢迎来到萌娘百科镜像站!如需查看或编辑,请联系本站管理员注册账号。
  • 本镜像站和其他萌娘百科的镜像站无关,请注意分别。

Module:ImasIcon/SideM

贴贴♀百科,万娘皆可贴的百科全书!转载请标注来源页面的网页链接,并声明引自贴贴百科。内容不可商用。
跳转到导航 跳转到搜索
模块文档  [创建] [刷新]
local getArgs = require('Module:Arguments').getArgs
local data = require('Module:ImasIcon/SideM/Data')  -- 引入角色数据

local p = {}

-- 预建小写名称到ID的映射表
local nameToId = {}
for name, id in pairs(data) do
    nameToId[name:lower()] = id
end

-- 常量定义
local DEFAULT_SIZE = 150
local MIN_SIZE = 20  -- 允许生成的最小尺寸
local MAX_SIZE = 300  -- 允许生成的最大尺寸
local DEFAULT_ID = 0
local SPRITE_COLS = 8
local SPRITE_ROWS = 7

-- 根据输入获取角色编号,支持编号或名称
local function getCharacterId(input)
    if not input or input == "" then
        return DEFAULT_ID, "未提供角色参数"
    end
    
    -- 尝试直接转换为数字
    local num = tonumber(input)
    if num then
        return num > 0 and num or DEFAULT_ID, 
               num <= 0 and "无效角色ID:" .. input or nil
    end
    
    -- 从预建索引中查找名称
    local id = nameToId[input:lower()]
    if id then
        return id, nil
    end
    
    return DEFAULT_ID, "无效角色名称:" .. input
end

-- 类型参数转换
local function getTypeClass(typeParam)
    if not typeParam then
        return ""
    end
    
    local typeMap = {
        P = "physical",
        I = "intelli",
        M = "mental"
    }
    
    return typeMap[typeParam] or typeParam:lower()
end

function p.main(frame)
    local args = getArgs(frame)
    local id, errorMsg = getCharacterId(args[1])
    local isValidId = id > 0
    
    -- 处理尺寸参数并限制范围
    local size = tonumber(args[2]) or DEFAULT_SIZE
    size = math.max(MIN_SIZE, math.min(size, MAX_SIZE))
    
    -- 构建类名集合
    local classes = {"imas-sm-sprite"}
    if args.border then
        table.insert(classes, "sm-sprite-bordered")
    end
    if not isValidId then
        table.insert(classes, "imas-sm-error")
    end
    if args.class then
        table.insert(classes, args.class)
    end
    
    -- 计算背景位置
    local bgSize, bgPos
    if isValidId then
        local col = (id - 1) % SPRITE_COLS
        local row = math.floor((id - 1) / SPRITE_COLS)
        bgSize = string.format("%dpx %dpx", 
            SPRITE_COLS * size, 
            SPRITE_ROWS * size)
        bgPos = string.format("%dpx %dpx", 
            -(col * size + 4), 
            -(row * size + 4))
    else
        bgSize = "0 0"
        bgPos = "0 0"
    end
    
    -- 构建样式
    local styles = {
        string.format("height:%dpx;", size - 8),
        string.format("width:%dpx;", size - 8),
        string.format("background-size:%s;", bgSize),
        string.format("background-position:%s;", bgPos),
        args.style or ""
    }
    
    -- 处理类型图标
    local typeClass = getTypeClass(args.type)
    local typeHtml = ""
    if typeClass ~= "" then
        typeHtml = string.format(
            '<span class="imas-sm-type sm-type-%s"></span>',
            typeClass
        )
    end
    
    -- 生成HTML
    return string.format(
        '<span class="%s" style="%s" title="%s">%s</span>',
        table.concat(classes, " "),
        table.concat(styles),
        errorMsg or "",
        typeHtml
    )
end

return p