此处为全站公告,通常对读者进行申明或对该WIKI某些规则进行公告,请在确认后修改本通告。本WIKI编辑权限开放,欢迎收藏起来防止迷路,也希望有爱的小伙伴和我们一起编辑哟~

全站通知:

模块:SkillDesc

来自星塔旅人WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:SkillDesc/doc创建

local p = {}

local nameToId = {
    ["琥珀"] = "103",
    ["缇莉娅"] = "107",
    ["卡西米拉"] = "108",
    ["鸢尾"] = "111",
    ["尘沙"] = "112",
    ["师渺"] = "113",
    ["岭川"] = "116",
    ["璟麟"] = "117",
    ["科洛妮丝"] = "118",
    ["花原"] = "119",
    ["卡娜丝"] = "120",
    ["杏子"] = "123",
    ["苍兰"] = "125",
    ["紫槿"] = "126",
    ["特丽莎"] = "127",
    ["密涅瓦"] = "132",
    ["雾语"] = "135",
    ["赤霞"] = "141",
    ["珂赛特"] = "142",
    ["千都世"] = "144",
    ["焦糖"] = "147",
    ["格芮"] = "149",
    ["菈露"] = "150",
    ["希娅"] = "155",
    ["小禾"] = "156"
}

function p.main(frame)
    local args = frame.args
    local char = args[1]
    local skillType = args[2]

    local skillTypeToKey = {
        ["普通攻击"] = "Skill01",
        ["主控技能"] = "Skill02", 
        ["援助技能"] = "Skill03",
        ["绝招"] = "Skill04"
    }

    local IdTitle = mw.title.new('Mediawiki:Char.json')
    local itemcontent = IdTitle and IdTitle:getContent() or nil
    local IdData = mw.text.jsonDecode(itemcontent)
    local id = tostring(IdData[char])
    if not id then
        return "角色不存在"
    end

    local skillKey = skillTypeToKey[skillType]
    if not skillKey then
        return "未知技能类型:" .. (skillType or "nil") .. "(支持:普通攻击/主控技能/援助技能/绝招)"
    end

    local title = mw.title.new('Mediawiki:char/' .. id .. '.json')
    local content = title and title:getContent() or nil
    local data = mw.text.jsonDecode(content or '{}')

    local entry = data
    if type(entry) ~= 'table' or not entry.charId then
        return "未找到角色数据:" .. id
    end

    if tostring(entry.charId) ~= tostring(id) then
        return "角色ID不匹配:" .. id
    end

    if type(entry.skills) ~= "table"
        or not entry.skills.Skill01
        or not entry.skills.Skill02
        or not entry.skills.Skill03
        or not entry.skills.Skill04 then
        return "技能数据不完整(需要 4 个技能:Skill01~Skill04)"
    end

    local function formatSkillDesc(desc)
        if not desc then return "" end
        desc = tostring(desc)
        desc = desc:gsub("\\u000b", "<br>")
        desc = desc:gsub(string.char(11), "<br>")
        desc = desc:gsub("##([^#]+)#%d+#", "%1")
        desc = desc:gsub("<[Cc]olor=[^>]+>", '<span style="color:#fb8037;">')
        desc = desc:gsub("</[Cc]olor>", "</span>")
        return desc
    end

    local function replaceParamsWithLevels(desc, key, skillEntry, skillsRoot)
        if not desc then return "" end
        desc = tostring(desc)

        local function toPercent(val, hasSeconds)
            local n = tonumber(val) or 0
            local p = n / 10000
            if hasSeconds then
                return string.format("%g", p)
            else
                return string.format("%g%%", p)
            end
        end

        local paramMap = {}
        for k, v in pairs(skillEntry) do
            local paramNum = k:match("^" .. key .. "_Param(%d+)$")
            if paramNum then
                paramMap[tonumber(paramNum)] = v
            end
        end

        local out = {}
        for lvl = 1, 10 do
            local paramCount = 0
            local s = desc:gsub("&Param(%d+)&([^&<]*)", function(paramNumStr, afterText)
                local paramNum = tonumber(paramNumStr)
                paramCount = paramCount + 1
                
                local paramArray = paramMap[paramNum]
                if not paramArray then
                    return "&Param" .. paramNumStr .. "&" .. afterText
                end
                
                local value
                if type(paramArray) == "table" then
                    if #paramArray >= 10 then
                        value = paramArray[lvl]
                    else
                        value = paramArray[#paramArray]
                    end
                else
                    value = paramArray
                end

                if value == nil then return "&Param" .. paramNumStr .. "&" .. afterText end
                
                local numValue = tonumber(value)
                if numValue then
                    local hasSeconds = afterText:match("^秒")
                    return toPercent(numValue, hasSeconds) .. afterText
                else
                    return tostring(value) .. afterText
                end
            end)

            if lvl == 1 then
                table.insert(out,
                    '<div class="sora_sora_skill_txt_content" data-level-txt="' .. lvl .. '">' .. s .. '</div>')
            else
                table.insert(out,
                    '<div class="sora_sora_skill_txt_content" data-level-txt="' ..
                    lvl .. '" style="display:none;">' .. s .. '</div>')
            end
        end

        return table.concat(out)
    end

    local skillEntry = entry.skills[skillKey]
    if type(skillEntry) ~= "table" then
        return "未找到技能数据:" .. skillType
    end

    local nameField = skillKey .. "_name"
    local descField = skillKey .. "_desc"
    
    local rawDesc = skillEntry[descField] or ""
    rawDesc = formatSkillDesc(rawDesc)
    return replaceParamsWithLevels(rawDesc, skillKey, skillEntry, entry.skills)
end

return p