Module:房间信息框

来自缺氧 Wiki
跳转到导航 跳转到搜索

用于模板:Template:房间信息框。 视图模块:Module:信息框/房间。 数据模块:Module:Data/RoomTypes



local p = {}
local fstr = mw.ustring.format

local utils = require("Module:Utils")
local k0 = utils.K0
local po = require([[Module:Po]]).po
local i18ncr = require([[Module:I18n]]).loadMessages([[Module:I18n/Rooms]])
local infobox = require([[Module:信息框/房间]])
local getArgs = require("Dev:Arguments").getArgs
local fDataPath = [[Module:Data/RoomTypes]]
local fData = mw.loadData(fDataPath)

function p._main(roomData, roomCode)
    local out = {}
    out["ID"] = fstr("<code>%s</code>", roomData.Id)
    out["名称"] = po(roomCode)
    out["图片说明"] = po(roomCode:sub(1, -6) .. ".DESCRIPTION")
    if roomData.buildings ~= nil then
        local frame = mw.getCurrentFrame()
        out["图片"] = {}
        for _, buildId in ipairs(roomData.buildings) do
            local photo = po("STRINGS.BUILDINGS.PREFABS."..buildId:upper()..".NAME")
            table.insert(out['图片'], tostring(frame:expandTemplate{ title = '图', args = {'64x64px', photo}}))
        end
        out['图片'] = #out["图片"] ~= 0 and table.concat(out["图片"]) or nil
    end

    out['解锁建筑使用'] = roomData.priority_building_use == true and "✔" or nil
    if roomData.category ~= nil then
        out['类型'] = fstr("[[Category:%s的页面]]", po("STRINGS.ROOMS.CATEGORY."..roomData.category:upper()..".NAME"))
    end
    if roomData.priority ~= nil then
        out['优先级'] = tostring(roomData.priority)
    end

    if roomData.constraints ~= nil then
        out["房间条件"] = {}
        for _, name in ipairs(roomData.constraints) do
            if string.find(name, "STRINGS.ROOMS.CRITERIA.") ~= nil then
                table.insert(out["房间条件"], fstr("* %s", po(name)))
            else
                table.insert(out["房间条件"], fstr("* %s", name))
            end
        end
        out['房间条件'] = #out["房间条件"] ~= 0 and table.concat(out["房间条件"], "\n") or nil
    end
    if roomData.effects ~= nil then
        out["效果"] = {}
        for _, effect in ipairs(roomData.effects) do
            table.insert(out['效果'], fstr(
                "[[%s]] %s",
                po("STRINGS.DUPLICANTS.ATTRIBUTES."..effect.Attribute:upper()..".NAME"),
                utils.float2str(effect.Value, nil, true)
            ))
        end
        out['效果'] = #out["效果"] ~= 0 and table.concat(out["效果"], "<br/>") or nil
    end
    if roomData.prePaths ~= nil then
        out["上一类型"] = {}
        for _, type in ipairs(roomData.prePaths) do
            for k, v in pairs(fData) do
                if k == type then
                    table.insert(out['上一类型'], fstr("[[%s]]",po(v.Name)))
                    break
                end
            end
        end
        out['上一类型'] = #out["上一类型"] ~= 0 and table.concat(out["上一类型"], "<br/>") or nil
    end
    if roomData.nextPaths ~= nil then
        out["下一类型"] = {}
        for _, type in ipairs(roomData.nextPaths) do
            for k, v in pairs(fData) do
                if k == type then
                    table.insert(out['下一类型'], fstr("[[%s]]",po(v.Name)))
                    break
                end
            end
        end
        out['下一类型'] = #out["下一类型"] ~= 0 and table.concat(out["下一类型"], "<br/>") or nil
    end

    return out
end

-- test by: = p.main(require("Module:debug").frame({},{debug=1, pagename="公共厕所"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, "Kitchen"}))
-- test by: = p.main(require("Module:debug").frame({},{debug=1, "Private Bedroom"}))

function p.main(frame)
    local args = getArgs(frame)
    local rooms = {}
    local infos = {}

    if args[1] ~= nil then
        for _, roomId in ipairs(args) do
            local roomCode = nil
            for k, v in pairs(fData) do
                if roomId:upper() == k:upper() then
                    roomCode = v.Name
                    local curr, cat = p._main(v, roomCode)
                    table.insert(infos, {
                        label = curr["名称"],
                        data = curr
                    })
                    break
                end
            end
            if roomCode == nil then
                return {
                    ["名称"] = fstr("找不到房间类型 '%s',请使用参数1或检查 [[%s]]。", foodId, fDataPath)
                }
            end
            table.insert(rooms, {
                id = roomId,
                code = roomCode
            })
        end
    else
        local roomCode = i18ncr:msgRev({
            key = args.pagename,
            args = {
                prefix = "STRINGS.ROOMS.TYPES."
            }
        } or "")
        if roomCode == nil then
            error(fstr("找不到房间类型 '%s',请使用参数1或检查 [[%s]]。", roomCode, fDataPath))
        end
        for k, v in pairs(fData) do
            if roomCode == v.Name then
                local curr = p._main(v, roomCode)
                table.insert(infos, {
                    label = curr["名称"],
                    data = curr
                })
                break
            end
        end
    end

    if args.debug then
        mw.logObject(infos)
    end

    local infoboxTitle = #infos == 1 and infos[1].label or args.pagename
    return infobox.main(infoboxTitle, infos)
end

return p