跳转到内容

模块:地点数据

来自Wymbot 共创站
NotBot​(留言 | 贡献)2026年10月24日 (六) 20:40的版本 (地点hours/distance/experience改为可选,空值不显示对应概况;cost仍必填,同步使用文档)

此模块的文档可以在模块:地点数据/doc创建

local p = {}
local cached
local page = '模板:地点信息/json'

function p.get(name)
    if not cached then
        local ok, value = pcall(mw.loadJsonData, page)
        if not ok or type(value) ~= 'table' or value.schemaVersion ~= 1 or type(value.records) ~= 'table' then
            cached = { status = 'error', code = 'invalid-place-data', page = page }
        else
            cached = { status = 'known', records = value.records }
        end
    end
    if cached.status == 'error' then return cached end
    local record = cached.records[name]
    if record == nil then return { status = 'none' } end
    if type(record) ~= 'table' or type(record.descriptions) ~= 'table' then
        return { status = 'error', code = 'invalid-place-record', page = page }
    end
    if record.unlock ~= nil and type(record.unlock) ~= 'table' then
        return { status = 'error', code = 'invalid-place-unlock', page = page }
    end
    if record.unlock and record.unlock.text ~= nil and type(record.unlock.text) ~= 'string' then
        return { status = 'error', code = 'invalid-place-unlock-text', page = page }
    end
    for _, key in ipairs({ 'cost', 'hours', 'distance', 'experience' }) do
        local value = record[key]
        local empty = key ~= 'cost' and (value == nil or
            (type(value) == 'string' and mw.text.trim(value) == ''))
        if not empty and (type(value) ~= 'number' or value < 0 or value >= math.huge) then
            return { status = 'error', code = 'invalid-place-' .. key, page = page }
        end
    end
    for level, text in pairs(record.descriptions) do
        local n = tonumber(level)
        if not n or n <= 0 or n % 1 ~= 0 or type(text) ~= 'string' then
            return { status = 'error', code = 'invalid-place-description', page = page }
        end
    end
    return { status = 'known', record = record }
end

return p