模块:钓鱼数据
外观
此模块的文档可以在模块:钓鱼数据/doc创建
-- Fishing-only data reader. Lua 5.1 syntax; never reads player state or travel data.
local config = require('模块:物品配置')
local p = {}
local cached
local MAX_ENTRIES = 5000
local function state(status, extra)
local result = extra or {}
result.status = status
result.enabled = true
return result
end
local function fail(code, path) error({ code = code, path = path }, 0) end
local function protect(callback)
local ok, value = pcall(callback)
if ok then return value end
return state('error', { code = type(value) == 'table' and value.code or 'invalid-fishing-data',
path = type(value) == 'table' and value.path or '$', page = (config.fishing or {}).page })
end
local function finite(value)
return type(value) == 'number' and value == value and value > -math.huge and value < math.huge
end
local function validName(value)
return type(value) == 'string' and mw.text.trim(value) ~= '' and #value <= 800
end
local function fields(value, allowed, path)
if type(value) ~= 'table' then fail('invalid-fishing-object', path) end
for key in pairs(value) do
if type(key) ~= 'string' or not allowed[key] then fail('unknown-fishing-field', path .. '.' .. tostring(key)) end
end
end
local function list(value, path, empty)
if type(value) ~= 'table' then fail('invalid-fishing-list', path) end
local size = 0
for key in pairs(value) do
if type(key) ~= 'number' or key < 1 or key % 1 ~= 0 then fail('invalid-fishing-list', path) end
size = size + 1
if size > MAX_ENTRIES then fail('fishing-entry-limit', path) end
end
for index = 1, size do if value[index] == nil then fail('invalid-fishing-list', path) end end
if not empty and size == 0 then fail('empty-disassembly', path) end
return size
end
local function names(value, path)
if type(value) ~= 'table' then fail('invalid-fishing-index', path) end
local keys = {}
for key in pairs(value) do
if not validName(key) then fail('invalid-fishing-name', path) end
keys[#keys + 1] = key
if #keys > MAX_ENTRIES then fail('fishing-entry-limit', path) end
end
table.sort(keys)
return keys
end
local function resolve(name) return (config.aliases or {})[name] or name end
local function checkQuantity(value, path)
if value == nil or (type(value) == 'string' and #value <= 800) then return end
if not finite(value) or value < 0 or value % 1 ~= 0 then fail('invalid-disassembly-quantity', path) end
end
local function groundEntries(raw, path)
local count = list(raw, path, true)
local entries, seen = {}, {}
for index = 1, count do
local row, location = raw[index], path .. '[' .. index .. ']'
fields(row, { item = true, rewardType = true, smallToBigBaits = true, ordinaryCatch = true }, location)
if not validName(row.item) then fail('invalid-fishing-item', location .. '.item') end
local kind = row.rewardType or 'item'
if kind ~= 'item' and kind ~= 'wum' then fail('invalid-fishing-reward-type', location) end
if row.ordinaryCatch ~= nil and (row.ordinaryCatch ~= true or row.smallToBigBaits == nil) then
fail('invalid-fishing-ordinary-catch', location)
end
local baits = {}
if row.smallToBigBaits ~= nil then
local baitCount = list(row.smallToBigBaits, location .. '.smallToBigBaits', true)
if baitCount == 0 then fail('empty-fishing-baits', location) end
for baitIndex = 1, baitCount do
local bait = row.smallToBigBaits[baitIndex]
if not validName(bait) then fail('invalid-fishing-bait', location) end
baits[#baits + 1] = bait
end
end
local key = kind == 'item' and resolve(row.item) or row.item
local entry = seen[key]
if entry and entry.rewardType ~= kind then fail('conflicting-fishing-reward-type', location) end
if not entry then
entry = { item = row.item, rewardType = kind }
seen[key] = entry
entries[#entries + 1] = entry
end
-- Merge duplicate rows without losing any bait or either acquisition route.
if row.smallToBigBaits == nil or row.ordinaryCatch == true then entry.ordinaryCatch = true end
for _, bait in ipairs(baits) do
entry.smallToBigBaits = entry.smallToBigBaits or {}
local found = false
for _, existing in ipairs(entry.smallToBigBaits) do if resolve(existing) == resolve(bait) then found = true end end
if not found then entry.smallToBigBaits[#entry.smallToBigBaits + 1] = bait end
end
end
for _, entry in ipairs(entries) do if not entry.smallToBigBaits then entry.ordinaryCatch = nil end end
return state('known', { entries = entries })
end
local function decompositionEntries(raw, path)
local count = list(raw, path, false)
local entries, allKnown, total = {}, true, 0
for index = 1, count do
local row, location = raw[index], path .. '[' .. index .. ']'
fields(row, { item = true, rewardType = true, probabilityStatus = true, probability = true, minQuantity = true, maxQuantity = true }, location)
local kind = row.rewardType or 'item'
if kind ~= 'item' and kind ~= 'currency' and kind ~= 'none' then fail('invalid-disassembly-reward-type', location) end
if kind == 'none' then
if row.item ~= nil or row.minQuantity ~= nil or row.maxQuantity ~= nil then fail('invalid-no-output', location) end
elseif not validName(row.item) then fail('invalid-disassembly-item', location .. '.item') end
if row.probabilityStatus == 'known' then
if not finite(row.probability) or row.probability < 0 or row.probability > 1 then fail('invalid-disassembly-probability', location) end
total = total + row.probability
elseif row.probabilityStatus == 'unknown' and row.probability == nil then allKnown = false
else fail('invalid-disassembly-probability-status', location) end
checkQuantity(row.minQuantity, location .. '.minQuantity')
checkQuantity(row.maxQuantity, location .. '.maxQuantity')
if type(row.minQuantity) == 'number' and type(row.maxQuantity) == 'number' and row.minQuantity > row.maxQuantity then fail('invalid-disassembly-range', location) end
entries[#entries + 1] = { item = row.item, rewardType = kind, probabilityStatus = row.probabilityStatus,
probability = row.probability, minQuantity = row.minQuantity, maxQuantity = row.maxQuantity }
end
if total > 1 + 0.000001 or (allKnown and math.abs(total - 1) > 0.000001) then fail('invalid-disassembly-probability-total', path) end
return state('known', { entries = entries })
end
local function load()
local spec = config.fishing
if not spec or spec.enabled ~= true then return { status = 'pending', enabled = false } end
if cached then return cached end
cached = protect(function()
if not validName(spec.page) then fail('invalid-fishing-config', '$') end
local ok, data = pcall(mw.loadJsonData, spec.page)
if not ok then fail('fishing-data-load-failed', '$') end
fields(data, { schemaVersion = true, grounds = true, disassembly = true }, '$')
if data.schemaVersion ~= 1 then fail('invalid-fishing-schema', '$.schemaVersion') end
local groundNames = names(data.grounds, '$.grounds')
local parentNames = names(data.disassembly, '$.disassembly')
local result = state('known', { grounds = {}, disassembly = {}, groundNames = groundNames, parentNames = {} })
local count = 0
local function charge(raw)
if type(raw) ~= 'table' then return end
for _, row in pairs(raw) do
count = count + 1
if count > MAX_ENTRIES then fail('fishing-entry-limit', '$') end
if type(row) == 'table' and type(row.smallToBigBaits) == 'table' then
for _ in pairs(row.smallToBigBaits) do
count = count + 1
if count > MAX_ENTRIES then fail('fishing-entry-limit', '$') end
end
end
end
end
for _, name in ipairs(groundNames) do
charge(data.grounds[name])
result.grounds[name] = protect(function() return groundEntries(data.grounds[name], '$.grounds.' .. name) end)
end
for _, name in ipairs(parentNames) do
local canonical = resolve(name)
if result.disassembly[canonical] then fail('duplicate-disassembly-alias', '$.disassembly.' .. name) end
result.parentNames[#result.parentNames + 1] = canonical
charge(data.disassembly[name])
result.disassembly[canonical] = protect(function() return decompositionEntries(data.disassembly[name], '$.disassembly.' .. name) end)
end
if count > MAX_ENTRIES then fail('fishing-entry-limit', '$') end
return result
end)
return cached
end
function p.getGround(name)
local source = load()
if source.status ~= 'known' then return source end
return source.grounds[name] or state('none', { entries = {} })
end
function p.getDisassembly(name)
local source = load()
if source.status ~= 'known' then return source end
return source.disassembly[resolve(name)] or state('none', { entries = {} })
end
function p.getSources(name)
local source = load()
if source.status ~= 'known' then return source end
if not source.sourceIndex then
source.sourceIndex = protect(function()
local index = {}
local function add(item, entry)
item = resolve(item)
index[item] = index[item] or {}
index[item][#index[item] + 1] = entry
end
for _, ground in ipairs(source.groundNames) do
local result = source.grounds[ground]
if result.status == 'error' then return result end
for _, entry in ipairs(result.entries) do
if entry.rewardType == 'item' then
local note
if entry.smallToBigBaits then note = entry.ordinaryCatch and '普通钓获或以小钓大' or '以小钓大' end
add(entry.item, { kind = 'fishing', target = ground, label = ground, note = note })
end
end
end
for _, parent in ipairs(source.parentNames) do
local result, seen = source.disassembly[parent], {}
if result.status == 'error' then return result end
for _, entry in ipairs(result.entries) do
local key = entry.item and resolve(entry.item)
if entry.rewardType == 'item' and not seen[key] and (entry.probabilityStatus ~= 'known' or entry.probability > 0) then
add(entry.item, { kind = 'disassembly', target = parent, label = parent .. '分解', fragment = '有什么用?' }); seen[key] = true
end
end
end
return state('known', { index = index })
end)
end
if source.sourceIndex.status == 'error' then return source.sourceIndex end
local entries = source.sourceIndex.index[resolve(name)] or {}
return state(#entries > 0 and 'known' or 'pending', { entries = entries })
end
return p