缺氧 wiki 编辑团队提示:注册账号并登录后体验更佳,且可通过参数设置定制优化您的浏览体验!

该站点为镜像站点,如果你想帮助这个由玩家志愿编辑的 wiki 站点,请前往原站点参与编辑,
同时欢迎加入编辑讨论群 851803695 与其他编辑者一起参与建设!

全站通知:

模块:建筑目录

来自缺氧WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索

此模块的文档可以在模块:建筑目录/doc创建

-- Module:建筑目录
--- 本模板为建筑生成目录,样式为卡片集合。详见[[Template:建筑目录]]
local p = {}
local fstr = mw.ustring.format -- shortcut for formattig a string
local utils = require([[Module:Utils]])
local i18nb = require([[Module:I18n]]).loadMessages([[Module:i18n/Buildings]])
local getArgs = require('Module:Dev/Arguments').getArgs
local buData = mw.loadData([[Module:Data/Buildings]])

local specialArgs = {
    sort = true,
    width = true,
    debug = true
}
local argvMap = {
    ["true"] = true,
    ["false"] = false
}

local function isMatchFilter(args, itemData)
    -- 根据数据模块的字段筛选
    for argk, argv in pairs(args) do
        local valid = specialArgs[argk] ~= true
        local mappedArgv = argvMap[argv] or argv
        if valid and (itemData[argk] == nil or itemData[argk] ~= mappedArgv) then
            return false
        end
    end
    return true
end

local function getDlcIcon(dlcs)
    -- DLC图标
    local out = {}
    local isBase = false
    for _, dlc in ipairs(dlcs) do
        if "" == dlc then
            isBase = true
        end
    end
    for _, dlc in ipairs(dlcs) do
        if "DLC2_ID" == dlc then
            table.insert(out, utils.DLC_ICONS[dlc])
        elseif not isBase and "EXPANSION1_ID" == dlc then
            table.insert(out, utils.DLC_ICONS[dlc])
        end
    end
    return table.concat(out, "")
end

function p.getInfo(args)
    local infos = {}

    for k, itemData in pairs(buData) do
        if itemData.Deprecated then
            -- do nothing
        elseif isMatchFilter(args, itemData) then
            table.insert(infos, itemData)
        end
    end

    -- 排序类型
    if args.sort ~= nil and #infos > 0 then
        local sortField = args.sort
        if infos[1][sortField] == nil then
            error(fstr('"args.sort = %s" is not supported', sortField))
        end
        table.sort(infos, function(a, b)
            return a[sortField] < b[sortField]
        end)
    end

    return infos
end

-- test by: p.main(require("Module:debug").frame({},{debug=1}))
-- test by: p.main(require("Module:debug").frame({},{sort="category", debug=1}))
-- test by: p.main(require("Module:debug").frame({},{category="STRINGS.UI.BUILDCATEGORIES.FURNITURE.NAME", debug=1}))
-- test by: p.main(require("Module:debug").frame({},{category="STRINGS.UI.BUILDCATEGORIES.FURNITURE.NAME", sort="subCategory", debug=1}))
function p.main(frame)
    local args = getArgs(frame)

    -- 筛选数据
    local infos = p.getInfo(args)

    local out = {}
    for _, itemData in ipairs(infos) do
        local name = i18nb:msg("STRINGS.BUILDINGS.PREFABS." .. itemData.id:upper() .. ".NAME")
        table.insert(out, frame:expandTemplate{
            title = "Template:卡片",
            args = {
                name,
                dlc = getDlcIcon(itemData.RequiredDlcIds),
                width = args.width
            }
        })
    end

    if args.debug then
        mw.logObject(out, "out")
    end

    return mw.getCurrentFrame():preprocess(table.concat(out, ""))
end

return p