跳转到内容

模块:地点解锁

来自Wymbot 共创站
NotBot​(留言 | 贡献)2026年11月12日 (四) 09:59的版本 (VisualJSON:地点解锁迁移为 and/or 条件树,支持 use;校正太阳持有条件)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

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

-- Offline implementation of the proposed 模块:地点解锁.
-- Uses Lua 5.1-compatible syntax and the existing 模块:物品通用 contract.
-- No network, page edits, game state, or mutation of the input record.
local C = require('模块:物品通用')
local p = { MAX_DEPTH = 8, MAX_NODES = 200 }

local function fail(code, path, message)
    error({ code = code, path = path, message = message }, 0)
end

local function protect(callback)
    local ok, value = pcall(callback)
    if ok then return value end
    if type(value) == 'table' and value.code then
        return { status = 'error', code = value.code, path = value.path, message = value.message }
    end
    return { status = 'error', code = 'internal-error', path = '$', message = '解锁解析发生内部错误' }
end

local function finite(value)
    return type(value) == 'number' and value == value and value > -math.huge and value < math.huge
end

local function nonempty(value)
    return type(value) == 'string' and C.trim(value) ~= ''
end

local function requireName(value, path)
    if not nonempty(value) then fail('invalid-item', path, '物品名称必须是非空文本') end
end

local function requireCount(value, path)
    if not finite(value) or value < 1 or value % 1 ~= 0 then
        fail('invalid-count', path, '持有数量必须是正整数')
    end
end

local function requirePercent(value, path)
    if not finite(value) or value < 0 then fail('invalid-percent', path, '收集度必须是有限的非负百分比数值') end
end

local function fields(value, allowed, path)
    for key in pairs(value) do
        if type(key) ~= 'string' or not allowed[key] then
            fail('unknown-field', path .. '.' .. tostring(key), '条件包含未知或不适用的字段')
        end
    end
end

local function list(value, path)
    if type(value) ~= 'table' then fail('invalid-list', path, '条件组必须是列表') end
    local count = 0
    for key in pairs(value) do
        if type(key) ~= 'number' or key < 1 or key % 1 ~= 0 then
            fail('invalid-list', path, '列表不能包含命名字段')
        end
        count = count + 1
    end
    if count > p.MAX_NODES then fail('node-limit', path, '条件节点过多') end
    for index = 1, count do
        if value[index] == nil then fail('invalid-list', path, '列表不能包含空洞') end
    end
    if count == 0 then fail('empty-group', path, '条件组至少需要一条条件') end
    return count
end

local function validate(condition)
    if condition == nil then return end
    local seen, count = {}, 0
    local function walk(node, path, depth)
        count = count + 1
        if count > p.MAX_NODES then fail('node-limit', path, '条件节点过多') end
        if depth > p.MAX_DEPTH then fail('depth-limit', path, '条件嵌套过深') end
        if type(node) ~= 'table' then fail('invalid-node', path, '每条条件必须是对象') end
        if seen[node] then fail('cyclic-condition', path, '条件不能循环引用') end
        seen[node] = true
        local shapes = (node['and'] ~= nil and 1 or 0) + (node['or'] ~= nil and 1 or 0) + (node.type ~= nil and 1 or 0)
        if shapes > 1 then fail('conflicting-shapes', path, '一个节点只能使用 and、or 或一种基本条件') end
        for _, name in ipairs({ 'text', 'addition' }) do
            if node[name] ~= nil and type(node[name]) ~= 'string' then
                fail('invalid-text', path .. '.' .. name, '说明字段必须是文本')
            end
        end
        local allowed = { text = true, addition = true }
        if node['and'] ~= nil or node['or'] ~= nil then
            local operator = node['and'] ~= nil and 'and' or 'or'
            allowed[operator] = true
            fields(node, allowed, path)
            local size = list(node[operator], path .. '.' .. operator)
            for index = 1, size do walk(node[operator][index], path .. '.' .. operator .. '[' .. index .. ']', depth + 1) end
        elseif node.type ~= nil then
            allowed.type, allowed.item = true, true
            if node.type == 'have' then
                allowed.count = true
                if node.count ~= nil then requireCount(node.count, path .. '.count') end
            elseif node.type == 'collection' then
                allowed.percent = true
                requirePercent(node.percent, path .. '.percent')
            elseif node.type ~= 'equipment' and node.type ~= 'use' then
                fail('unknown-type', path .. '.type', '不支持的解锁条件类型')
            end
            fields(node, allowed, path)
            requireName(node.item, path .. '.item')
        else
            fields(node, allowed, path)
            if not nonempty(node.text) then fail('empty-node', path, '节点必须有结构条件或非空 text') end
        end
        seen[node] = nil
    end
    walk(condition, '$', 1)
end

local function numberText(value)
    return string.format('%.15g', value)
end

local function bracket(text)
    return '(' .. text .. ')'
end

local function complexText(text)
    return text:find('且', 1, true) ~= nil or text:find('或', 1, true) ~= nil or text:find('\n', 1, true) ~= nil
end

local function render(condition)
    if condition == nil then return '无需解锁' end
    local function walk(node)
        local result
        if nonempty(node.text) then
            result = { text = C.literal(node.text), kind = 'atom', opaque = complexText(node.text) }
        elseif node.type then
            local item = C.link(node.item)
            if node.type == 'have' then
                local count = node.count or 1
                result = { text = count == 1 and ('持有' .. item) or ('持有至少' .. C.literal(numberText(count)) .. '个' .. item), kind = 'atom', simple = count == 1 and 'have' or nil, item = item }
            elseif node.type == 'collection' then
                result = { text = item .. '收集度达到' .. C.literal(numberText(node.percent)) .. '%', kind = 'atom' }
            elseif node.type == 'use' then
                result = { text = '使用' .. item, kind = 'atom', simple = 'use', item = item }
            else
                result = { text = '装备有' .. item, kind = 'atom', simple = 'equipment', item = item }
            end
        else
            local operator = node['and'] ~= nil and 'and' or 'or'
            local children, simple, sameSimple = {}, nil, true
            for index, child in ipairs(node[operator]) do
                children[index] = walk(child)
                if index == 1 then simple = children[index].simple end
                if simple == nil or children[index].simple ~= simple then sameSimple = false end
            end
            if #children == 1 then
                result = children[1]
            elseif sameSimple then
                local names = {}
                for index, child in ipairs(children) do names[index] = child.item end
                local verbs = { have = '持有', equipment = '装备有', use = '使用' }
                result = { text = verbs[simple] .. table.concat(names, operator == 'and' and '和' or '或'), kind = operator }
            else
                local phrases = {}
                for index, child in ipairs(children) do
                    phrases[index] = (child.kind ~= 'atom' or child.opaque) and bracket(child.text) or child.text
                end
                result = { text = table.concat(phrases, operator == 'and' and '且' or '或'), kind = operator }
            end
        end
        if nonempty(node.addition) then
            local base = (result.kind == 'or' or result.opaque) and bracket(result.text) or result.text
            local addition = C.literal(node.addition)
            if complexText(node.addition) then addition = bracket(addition) end
            result = { text = base .. '且' .. addition, kind = 'and' }
        end
        return result
    end
    return walk(condition).text
end

function p.validate(condition)
    return protect(function()
        validate(condition)
        return { status = condition == nil and 'none' or 'known' }
    end)
end

function p.render(condition)
    return protect(function()
        validate(condition)
        return { status = condition == nil and 'none' or 'known', text = render(condition) }
    end)
end

function p.describe(record, version)
    return protect(function()
        if type(record) ~= 'table' then fail('invalid-record', '$', '地点记录必须是对象') end
        if version ~= 2 then fail('unsupported-version', 'schemaVersion', '仅支持地点数据版本 2,旧数据应在部署前转换') end
        for _, key in ipairs({ 'entryEquipment', 'entryDisabled' }) do
            if record[key] ~= nil then fail('obsolete-field', '$.' .. key, '进入条件必须统一放在 unlock 中,不接受旧字段') end
        end
        validate(record.unlock)
        return { status = record.unlock == nil and 'none' or 'known', condition = record.unlock, text = render(record.unlock) }
    end)
end

return p