跳转到内容

模块:物品数据:修订间差异

来自Wymbot 共创站
NotBot​(留言 | 贡献)
将超市可购买信息独立存储于模板空间,保留物品来源查询;机制页不修改
NotBot​(留言 | 贡献)
旅行、种子掉落表支持可选最小/最大掉落数、备注和数值排序;未提供数量保持空白
第34行: 第34行:
function p.getItem(name)
function p.getItem(name)
     return state('pending', { name = p.resolve(name) })
     return state('pending', { name = p.resolve(name) })
end
local function optionalQuantity(value)
    return value == nil or value == '' or
        (type(value) == 'number' and value >= 0 and value < math.huge and value % 1 == 0)
end
end


第51行: 第56行:
             if type(drop.item) ~= 'string' or mw.text.trim(drop.item) == '' then
             if type(drop.item) ~= 'string' or mw.text.trim(drop.item) == '' then
                 return state('error', { code = 'invalid-drop-item' })
                 return state('error', { code = 'invalid-drop-item' })
            end
            if not optionalQuantity(drop.minQuantity) or not optionalQuantity(drop.maxQuantity) then
                return state('error', { code = 'invalid-drop-quantity' })
            end
            if type(drop.minQuantity) == 'number' and type(drop.maxQuantity) == 'number' and
                drop.minQuantity > drop.maxQuantity then
                return state('error', { code = 'invalid-drop-quantity-range' })
            end
            if drop.note ~= nil and type(drop.note) ~= 'string' then
                return state('error', { code = 'invalid-drop-note' })
             end
             end
             if drop.probabilityStatus == 'known' then
             if drop.probabilityStatus == 'known' then

2026年10月24日 (六) 12:43的版本

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

local config = require('模块:物品配置')
local p = {}
local cache = {}

local function state(status, extra)
    local result = extra or {}
    result.status = status
    return result
end

local function load(kind)
    if cache[kind] then return cache[kind] end
    local spec = config[kind]
    if not spec or not spec.enabled then return state('pending') end
    local ok, value = pcall(mw.loadJsonData, spec.page)
    if not ok or type(value) ~= 'table' then
        cache[kind] = state('error', { code = 'data-load-failed', page = spec.page })
    else
        local records = kind ~= 'sources' and (value.records or value) or value
        if type(records) ~= 'table' then
            cache[kind] = state('error', { code = 'invalid-data-envelope', page = spec.page })
        else
            cache[kind] = state('known', { data = records, complete = spec.complete == true })
        end
    end
    return cache[kind]
end

function p.resolve(name)
    -- One explicit alias step; do not lowercase or strip disambiguation suffixes.
    return config.aliases[name] or name
end

function p.getItem(name)
    return state('pending', { name = p.resolve(name) })
end

local function optionalQuantity(value)
    return value == nil or value == '' or
        (type(value) == 'number' and value >= 0 and value < math.huge and value % 1 == 0)
end

local function pools()
    local source = load('sources')
    if source.status ~= 'known' then return source end
    if source.data.schemaVersion ~= 1 or type(source.data.pools) ~= 'table' then
        return state('error', { code = 'invalid-drop-schema' })
    end
    local seen = {}
    for _, pool in ipairs(source.data.pools) do
        if type(pool.name) ~= 'string' or mw.text.trim(pool.name) == '' or seen[pool.name] or
            (pool.kind ~= 'travel' and pool.kind ~= 'planting' and pool.kind ~= 'shop' and pool.kind ~= 'other') or
            type(pool.drops) ~= 'table' then return state('error', { code = 'invalid-drop-pool' }) end
        seen[pool.name] = true
        for _, drop in ipairs(pool.drops) do
            if type(drop.item) ~= 'string' or mw.text.trim(drop.item) == '' then
                return state('error', { code = 'invalid-drop-item' })
            end
            if not optionalQuantity(drop.minQuantity) or not optionalQuantity(drop.maxQuantity) then
                return state('error', { code = 'invalid-drop-quantity' })
            end
            if type(drop.minQuantity) == 'number' and type(drop.maxQuantity) == 'number' and
                drop.minQuantity > drop.maxQuantity then
                return state('error', { code = 'invalid-drop-quantity-range' })
            end
            if drop.note ~= nil and type(drop.note) ~= 'string' then
                return state('error', { code = 'invalid-drop-note' })
            end
            if drop.probabilityStatus == 'known' then
                if type(drop.probability) ~= 'number' or drop.probability < 0 or drop.probability > 1 then
                    return state('error', { code = 'invalid-drop-probability' })
                end
            elseif drop.probabilityStatus ~= 'unknown' or drop.probability ~= nil then
                return state('error', { code = 'invalid-drop-probability-status' })
            end
        end
    end
    return source
end

function p.getShop(name)
    local source = load('shops')
    if source.status ~= 'known' then return source end
    local value = source.data[p.resolve(name)]
    if value == nil or value == '' then return state('none') end
    if type(value) ~= 'string' then return state('error', { code = 'invalid-shop-availability' }) end
    return state('known', { availability = value })
end

function p.getShopItems()
    local source = load('shops')
    if source.status ~= 'known' then return source end
    local names, entries = {}, {}
    for name in pairs(source.data) do names[#names + 1] = name end
    table.sort(names)
    for _, name in ipairs(names) do
        local result = p.getShop(name)
        if result.status == 'error' then return result end
        if result.status == 'known' then entries[#entries + 1] = { item = name, availability = result.availability } end
    end
    return state('known', { entries = entries, kind = 'shop' })
end

function p.getSources(name)
    local source = pools()
    if source.status == 'error' then return source end
    name = p.resolve(name)
    local entries = {}
    for _, pool in ipairs(source.status == 'known' and source.data.pools or {}) do
        for _, drop in ipairs(pool.drops) do
            if p.resolve(drop.item) == name then
                entries[#entries + 1] = { kind = pool.kind, label = pool.name, target = pool.name,
                    probability = drop.probability, probabilityStatus = drop.probabilityStatus }
            end
        end
    end
    local shop = p.getShop(name)
    if shop.status == 'error' then return shop end
    if shop.status == 'known' then
        entries[#entries + 1] = { kind = 'shop', label = 'SB洪超市', target = 'SB洪超市', availability = shop.availability }
    end
    return state(#entries > 0 and 'known' or (source.complete and 'none' or 'pending'), { entries = entries })
end

function p.getOutputs(name)
    name = p.resolve(name)
    -- Compatibility for the already-created page awaiting manual deletion.
    if name == 'SB洪超市' then return p.getShopItems() end
    local source = pools()
    if source.status ~= 'known' then return source end
    name = p.resolve(name)
    for _, pool in ipairs(source.data.pools) do
        if p.resolve(pool.name) == name then return state('known', { entries = pool.drops, kind = pool.kind }) end
    end
    return state('pending', { entries = {} })
end

function p.getCollection(name, requested)
    local source = load('collection')
    if source.status ~= 'known' then return source end
    local record = source.data[p.resolve(name)]
    if record == nil then return state(source.complete and 'none' or 'pending', { entries = {} }) end
    if type(record) ~= 'table' then return state('error', { code = 'invalid-collection-record' }) end
    local levels, seen = {}, {}
    local function add(level)
        local number = tonumber(level)
        if not number or number <= 0 or number % 1 ~= 0 then return false end
        if not seen[number] then levels[#levels + 1], seen[number] = number, true end
        return true
    end
    if requested and requested ~= '' then
        local count = 0
        for level in (requested .. ','):gmatch('(.-),') do
            count = count + 1
            if not add(mw.text.trim(level)) then return state('error', { code = 'invalid-collection-levels' }) end
        end
        if count == 0 then return state('error', { code = 'invalid-collection-levels' }) end
    else
        add(100); add(300); add(500)
        for level in pairs(record) do
            if not add(level) then return state('error', { code = 'invalid-collection-level' }) end
        end
    end
    table.sort(levels)
    local entries, known, pending = {}, false, false
    for _, level in ipairs(levels) do
        local value = record[tostring(level)]
        if value == nil then value = record[level] end
        if value ~= nil and type(value) ~= 'string' then
            return state('error', { code = 'invalid-collection-text' })
        end
        local status = value == '无' and 'none' or (value and mw.text.trim(value) ~= '' and 'known' or 'pending')
        entries[#entries + 1] = { level = level, status = status, text = value }
        known, pending = known or status == 'known', pending or status == 'pending'
    end
    return state(known and 'known' or (pending and 'pending' or 'none'), { entries = entries })
end

local function recipe(name, record)
    if type(record) ~= 'table' or type(record['配方']) ~= 'table' then
        return state('error', { code = 'invalid-recipe-record' })
    end
    local raw = record['配方']
    local quantity = raw[1]
    if type(quantity) ~= 'number' or quantity <= 0 or quantity % 1 ~= 0 then
        return state('error', { code = 'invalid-recipe-output' })
    end
    local materials = {}
    for index, entry in ipairs(raw) do
        if index > 1 then
            if type(entry) ~= 'table' or type(entry[1]) ~= 'string' or mw.text.trim(entry[1]) == '' or
                type(entry[2]) ~= 'number' or entry[2] <= 0 or entry[2] % 1 ~= 0 then
                return state('error', { code = 'invalid-recipe-material' })
            end
            materials[#materials + 1] = { name = entry[1], quantity = entry[2] }
        end
    end
    if #materials == 0 then return state('error', { code = 'empty-recipe-materials' }) end
    local level = record['等级']
    if level ~= nil and (type(level) ~= 'number' or level < 0) then
        return state('error', { code = 'invalid-recipe-level' })
    end
    local info = record['信息']
    if info ~= nil and type(info) ~= 'string' then return state('error', { code = 'invalid-recipe-info' }) end
    return state('known', { name = name, quantity = quantity, materials = materials, level = level, info = info })
end

function p.getRecipes(name)
    local source = load('recipes')
    if source.status ~= 'known' then return source end
    name = p.resolve(name)
    local record = source.data[name]
    if record == nil then return state(source.complete and 'none' or 'pending', { recipes = {} }) end
    local result = recipe(name, record)
    if result.status == 'error' then return result end
    return state('known', { recipes = { result } })
end

function p.getUses(name)
    local source = load('recipes')
    if source.status ~= 'known' then return source end
    name = p.resolve(name)
    local products = {}
    for product, record in pairs(source.data) do
        local result = recipe(product, record)
        if result.status == 'error' then return result end
        for _, material in ipairs(result.materials) do
            if p.resolve(material.name) == name then products[#products + 1] = product; break end
        end
    end
    table.sort(products)
    return state(#products > 0 and 'known' or (source.complete and 'none' or 'pending'), { products = products })
end

return p