模块:物品数据:修订间差异
外观
建立模块化物品展示及用户提供的测试数据(不迁移旧掉落) |
按旧Wiki恢复完整来源表、合成样式和缺省文案;物品来源不显示概率 |
||
| 第59行: | 第59行: | ||
return state('error', { code = 'invalid-drop-probability-status' }) | return state('error', { code = 'invalid-drop-probability-status' }) | ||
end | end | ||
end | |||
end | |||
for _, shop in ipairs(source.data.shops or {}) do | |||
if type(shop.name) ~= 'string' or mw.text.trim(shop.name) == '' or type(shop.items) ~= 'table' then | |||
return state('error', { code = 'invalid-shop' }) | |||
end | |||
for _, item in ipairs(shop.items) do | |||
if type(item) ~= 'string' or mw.text.trim(item) == '' then return state('error', { code = 'invalid-shop-item' }) end | |||
end | end | ||
end | end | ||
| 第75行: | 第83行: | ||
probability = drop.probability, probabilityStatus = drop.probabilityStatus } | probability = drop.probability, probabilityStatus = drop.probabilityStatus } | ||
end | end | ||
end | |||
end | |||
for _, shop in ipairs(source.data.shops or {}) do | |||
for _, item in ipairs(shop.items) do | |||
if p.resolve(item) == name then entries[#entries + 1] = { kind = 'shop', label = shop.name, target = shop.name } end | |||
end | end | ||
end | end | ||
| 第86行: | 第99行: | ||
for _, pool in ipairs(source.data.pools) do | 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 | if p.resolve(pool.name) == name then return state('known', { entries = pool.drops, kind = pool.kind }) end | ||
end | |||
for _, shop in ipairs(source.data.shops or {}) do | |||
if p.resolve(shop.name) == name then | |||
local entries = {} | |||
for _, item in ipairs(shop.items) do entries[#entries + 1] = { item = item } end | |||
return state('known', { entries = entries, kind = 'shop' }) | |||
end | |||
end | end | ||
return state('pending', { entries = {} }) | return state('pending', { entries = {} }) | ||
2026年10月24日 (六) 12:18的版本
此模块的文档可以在模块:物品数据/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 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 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
for _, shop in ipairs(source.data.shops or {}) do
if type(shop.name) ~= 'string' or mw.text.trim(shop.name) == '' or type(shop.items) ~= 'table' then
return state('error', { code = 'invalid-shop' })
end
for _, item in ipairs(shop.items) do
if type(item) ~= 'string' or mw.text.trim(item) == '' then return state('error', { code = 'invalid-shop-item' }) end
end
end
return source
end
function p.getSources(name)
local source = pools()
if source.status ~= 'known' then return source end
name = p.resolve(name)
local entries = {}
for _, pool in ipairs(source.data.pools) 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
for _, shop in ipairs(source.data.shops or {}) do
for _, item in ipairs(shop.items) do
if p.resolve(item) == name then entries[#entries + 1] = { kind = 'shop', label = shop.name, target = shop.name } end
end
end
return state(#entries > 0 and 'known' or (source.complete and 'none' or 'pending'), { entries = entries })
end
function p.getOutputs(name)
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
for _, shop in ipairs(source.data.shops or {}) do
if p.resolve(shop.name) == name then
local entries = {}
for _, item in ipairs(shop.items) do entries[#entries + 1] = { item = item } end
return state('known', { entries = entries, kind = 'shop' })
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