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

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

全站通知:

模块:版本导航

来自缺氧WIKI_BWIKI_哔哩哔哩
跳到导航 跳到搜索
-- Module:版本导航
local p = {}
local getArgs = require('Module:Dev/Arguments').getArgs
local fstr = mw.ustring.format -- shortcut for formattig a string
local vData = require("Module:Data/版本")
local vInfo = require("Module:Data/版本信息")
local navbox = require("Module:导航栏")

function f_and(p1, p2) return function(x) return p1(x) and p2(x) end end

-- of a version code is for the extension 1 (Space Out!)
function is_ex(is)
    return function(id) return is == (vData[id].prefix == 'EX1') end
end

-- Release / Hotfix
function is_rh(id) return vData[id].type ~= "Test" end
function is_t(id) return vData[id].type == "Test" end
function is_r(id) return vData[id].type == "Release" end

function has_name(id) return vData[id].name ~= nil and vData[id].name ~= "" end

-- Get the max key of a table
function tbl_max(tbl, predicate)
    local mk = -math.huge
    for k, _ in pairs(tbl) do if predicate(k) and k > mk then mk = k end end
    return mk
end

function get_first(tbl, predicate)
    for k, v in pairs(tbl) do if predicate(k) then return {k, v} end end
end

function getVarient(prefix) return prefix == "EX1" and "EX1" or "BASE" end

-- test by: mw.logObject(p.getInfo({"BASE"}))
-- test by: mw.logObject(p.getInfo({"EX1"}))
function p.getInfo(args)
    local out = {}

    -- sort versions according to their release date
    local sortedBuilds = {}
    for k, v in pairs(vData) do table.insert(sortedBuilds, k) end
    table.sort(sortedBuilds)

    local sortedPrefix = {}
    local groupedVersions = {} -- grouped by sortedPrefix
    for i, b in pairs(sortedBuilds) do
        local currVer = vData[b]
        local currPrefix = currVer.prefix or "default"
        if groupedVersions[currPrefix] == nil then
            table.insert(sortedPrefix, currPrefix)
            groupedVersions[currPrefix] = {}
        end
        table.insert(groupedVersions[currPrefix], currVer)
    end

    for i, currPrefix in ipairs(sortedPrefix) do
        if getVarient(currPrefix) == args[1] then end
    end

    local dlcCode = args[1] and args[1]:upper() or "BASE"
    local varientName = vInfo.versionVarients[dlcCode]
    if varientName == nil then
        return {"模板参数错误,请联系管理员。"}
    end
    if dlcCode == "BASE" then
        varientName = fstr("%s版本历史", varientName)
    else
        varientName = fstr("<span>'''[[%s]]版本历史'''</span>", varientName)
    end
    table.insert(out, varientName)

    for i = #sortedPrefix, 1, -1 do
        local prefix = sortedPrefix[i]
        if getVarient(prefix) == dlcCode then
            table.insert(out, vInfo.prefixName[prefix] or prefix)
            local versionLinks = {}
            for _, version in ipairs(groupedVersions[prefix]) do
                local link = fstr("[[%s|%s]]", version.page,
                                  version.page:match("/.+"):sub(2))
                if version.name ~= nil then
                    link = "'''" .. link .. "'''"
                end
                table.insert(versionLinks, link)
            end
            table.insert(out, table.concat(versionLinks, " ! "))
        end
    end
    out.collapse = "yes"
    return out
end

-- test by: = p.main()
function p.main(frame)
    return navbox.main(p.getInfo(getArgs(frame)))
    
    --mw.getCurrentFrame():expandTemplate{
    --    title = "PortableNavbox",
    --    args = p.getInfo(getArgs(frame))
    --}
end

return p