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

全站通知:

模块:ItemDesc

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

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

local p = {}

local nameToId = {
    ["噜噗遗魂"] = "21082",
}

local rareMap = {
    [1] = "彩",
    [2] = "金",
    [3] = "蓝",
    [4] = "绿",
    [5] = "灰",
}

local function normalize_vtab_to_newline(desc)
    if not desc then return desc end
    desc = desc:gsub("\\u000b", "<br>")
    desc = desc:gsub(string.char(11), "<br>")
    return desc
end

local function build_jumpto_list(data)
    if not data or not data.jumpto then
        return {}
    end

    local list = {}
    local j = data.jumpto

    if j.jumpto1 and j.jumpto1 ~= "" then
        table.insert(list, tostring(j.jumpto1))
    end
    if j.jumpto2 and j.jumpto2 ~= "" then
        table.insert(list, tostring(j.jumpto2))
    end

    return list
end

local function icon_key_from_source(s)
    if not s then return "" end
    local key = tostring(s):match("([^%-]+)") or tostring(s)
    key = key:gsub("%s+", "")
    key = key:gsub("[/\\:%%]", "_")
    return key
end

function p.main(frame)
    local args = frame.args or {}
    local item = args[1] or ""
    local itemtype = args[2] or ""
    
    local IdTitle = mw.title.new('Mediawiki:Item.json')
    local itemcontent = IdTitle and IdTitle:getContent() or nil
    local IdData = mw.text.jsonDecode(itemcontent)
    local id = tostring(IdData[item])
    if not id then
        return "道具不存在"
    end    
    
    local title = mw.title.new('Mediawiki:Item/' .. id .. '.json')
    local content = title and title:getContent() or nil
    if not content or content == "" then
        return "JSON文件未找到"
    end

    local data = mw.text.jsonDecode(content)

    if itemtype == "描述" then
        local desc = data.desc
        if not desc then
            return "描述不存在"
        end
        return data.desc or ""
    end

    if itemtype == "背景" then
        local bg = data.Literary
        if not bg then
            return "背景不存在"
        end
        bg = normalize_vtab_to_newline(bg)
        return bg or ""
    end

    if itemtype == "稀有度" then
        local rare = rareMap[data.rare]
        if not rare then
            return "未知稀有度"
        end
        return rare
    end

    if itemtype == "来源" then
        local items = build_jumpto_list(data)
        if #items == 0 then
            return ""
        end

        local out = '<div class="sora_item_desc_box">'
                  .. '<div class="sora_item_desc_title"><span>来源</span></div>'

        for _, v in ipairs(items) do
            local key = icon_key_from_source(v)
            local icon = ""
            if key ~= "" then
                if tostring(v) == "材料合成" then
                    icon = '[[文件:道具_icon_' .. key .. '.png|link=|class=sora_item_source_icon]]'
                else
                    icon = '[[文件:道具_icon_' .. key .. '.png|link=]]'
                end
            end
            out = out .. '<div class="sora_item_source">' .. icon .. '<span>' .. tostring(v) .. '</span></div>'
        end

        out = out .. '</div>'
        return out
    end

    return ""
end

return p