米斯特利亚Wiki正在建设中,本WIKI编辑权限开放!欢迎参与~!
全站通知:
模块:Fish
刷
历
编
跳到导航
跳到搜索
此模块的文档可以在模块:Fish/doc创建
-- 模块:fish
-- 说明:本模块用于查询 fish.lua 中的鱼类数据。
-- 数据请在“模块:fish/Data”子页面维护,其结构可参考仓库中的 ./data_lua/fish.lua
local fishData = require('模块:fish/Data')
local Utils = require('模块:Utils')
local Custom = require('模块:custom')
local Perks = require('模块:Perks')
local Items = require('模块:Items')
local p = {}
-- 数据预处理:剔除rarity="junk"和is_chest=true的项目
local function preprocessData(dataArg)
local filteredData = {}
-- 保留default项
if dataArg.default then
filteredData.default = dataArg.default
end
-- 遍历所有鱼类,只保留符合条件的项目
for id, itemData in pairs(dataArg) do
if id ~= 'default' then -- 跳过default项,已经单独处理
-- 检查是否有rarity="junk"或is_chest=true的条件
local shouldKeep = true
-- 处理rarity="junk"或文物的情况
if itemData.rarity and
(itemData.rarity == "junk" or itemData.rarity == "artifact_divespots" or itemData.rarity ==
"artifact_fishing") then
shouldKeep = false
end
-- 处理is_chest=true的情况
if itemData.is_chest and itemData.is_chest == true then
shouldKeep = false
end
-- 如果符合条件,保留该项
if shouldKeep then
filteredData[id] = itemData
end
end
end
return filteredData
end
-- 执行数据预处理并更新本地data引用
local data = preprocessData(fishData)
-- 合并默认值:以 data.default 为基底,显式合并所有默认字段到结果表中
local function mergeWithDefault(id)
if not id or type(id) ~= 'string' then
return nil
end
id = Utils.trim(id)
local raw = data and data[id]
if not raw then
return nil
end
local base = data.default or {}
local merged = {}
-- 首先将所有默认字段复制到merged表中
for k, v in pairs(base) do
merged[k] = v
end
-- 然后用具体鱼类的字段覆盖默认字段
for k, v in pairs(raw) do
merged[k] = v
end
merged.id = id
-- 特殊处理seasons字段:当seasons=false时,将其设置为包含四季的数组
if merged.seasons == false then
merged.seasons = {"spring", "summer", "fall", "winter"}
end
return merged
end
-- 获取指定鱼类的单个属性值
-- @param frame.args[1] string 鱼类ID
-- @param frame.args[2] string 属性名
-- @return any 找到的属性值(已合并默认值),找不到则返回 nil。
function p.get(frame)
local id = frame and frame.args and frame.args[1]
local property = frame and frame.args and frame.args[2]
if not id or not property or id == '' or property == '' then
return nil
end
local info = mergeWithDefault(id)
if info and info[property] ~= nil then
return info[property]
end
return nil
end
-- 获取指定鱼类的所有属性(已合并默认值,并附带 id 字段)
-- @param frame.args[1] string 鱼类ID
-- @return table or nil 找到的完整数据表,找不到则返回 nil。
function p.getAll(frame)
local id = frame and frame.args and frame.args[1]
return mergeWithDefault(id)
end
-- 获取类型
function p._legendary(id)
local info = mergeWithDefault(id)
if info and info.legendary then
return "传说"
end
return "普通"
end
function p.legendary(frame)
local id = frame and frame.args and frame.args[1]
return p._legendary(id)
end
-- 获取稀有度
function p._rarity(id)
local info = mergeWithDefault(id)
if info and info.rarity then
return Custom._rarity(info.rarity)
end
return "未知稀有度"
end
function p.rarity(frame)
local id = frame and frame.args and frame.args[1]
return p._rarity(id)
end
-- 获取季节
function p._season(id)
local info = mergeWithDefault(id)
local seasons = info and info.seasons or nil
if type(seasons) == 'table' then
return Custom._nameTable2Str(seasons)
elseif type(seasons) == 'string' then
return Custom._nameStr(seasons)
end
return "未知季节"
end
function p.season(frame)
local id = frame and frame.args and frame.args[1]
return p._season(id)
end
-- 获取天气
function p._weather(id)
local info = mergeWithDefault(id)
local weather = info and info.weather or {}
if type(weather) == 'table' then
return Custom._nameTable2Str(weather)
elseif type(weather) == 'string' then
return Custom._nameStr(weather)
end
return "未知天气"
end
function p.weather(frame)
local id = frame and frame.args and frame.args[1]
return p._weather(id)
end
-- 获取时间
function p._hours(id)
local info = mergeWithDefault(id)
if info and info.hours then
return table.concat(hours, ",")
end
return "全天"
end
function p.hours(frame)
local id = frame and frame.args and frame.args[1]
return p._hours(id)
end
-- 获取水流区域
local water_type_map = {
river = "河流",
ocean = "海洋",
pond = "池塘",
mines = "矿井"
}
function p._water_type(id)
local info = mergeWithDefault(id)
local s = info and info.water_type
if not s then
return ""
end
local rStr = Utils.mapST2table(water_type_map, s)
return rStr
end
function p.water_type(frame)
local id = frame and frame.args and frame.args[1]
return p._water_type(id)
end
-- 获取位置限制
function p._location(id)
local info = mergeWithDefault(id)
if info and info.locations then
return Custom._locationByTable(info.locations)
end
return "无"
end
function p.location(frame)
local id = frame and frame.args and frame.args[1]
return p._location(id)
end
-- 获取鱼影
local size_map = {
small = "小",
medium = "中",
large = "大",
giant = "巨大"
}
function p._size(id)
local info = mergeWithDefault(id)
if info and info.size then
return size_map[info.size] or info.size
end
return "未知"
end
function p.size(frame)
local id = frame and frame.args and frame.args[1]
return p._size(id)
end
-- get获取方式
local retrieval_map = {
fishing = "钓鱼",
divespot = "潜水"
}
function p._retrieval(id)
local info = mergeWithDefault(id)
if info and info.retrieval then
local rStr = Utils.mapST2table(retrieval_map, info.retrieval)
return rStr or "fish.retrieval error"
end
return "未知"
end
function p.retrieval(frame)
local id = frame and frame.args and frame.args[1]
return p._retrieval(id)
end
-- 获取文物技能需求
function p._perk_artifact(id)
local info = mergeWithDefault(id)
if info and info.perk_artifact then
local perk_name = Perks._name(info.perk_artifact)
return perk_name ~= "" and perk_name or info.perk_artifact
end
return "无"
end
-- 获取钓鱼技能需求
function p._has_perk(id)
local info = mergeWithDefault(id)
if info and info.has_perk then
local perk_name = Perks._name(info.has_perk)
return perk_name ~= "" and perk_name or info.has_perk
end
return "无"
end
-- 获取鱼类的中文名
function p._name(id)
if not id or type(id) ~= 'string' then
return id
end
-- 先获取完整的鱼类信息,查看是否有item字段
local info = mergeWithDefault(id)
local itemId = id -- 默认使用鱼类ID
if info and info.item and type(info.item) == 'string' and info.item ~= '<..>' then
itemId = info.item -- 如果有item字段且不是'<..>',使用item字段作为物品ID
end
local name = Items._name(itemId)
return name or itemId
end
function p.name(frame)
local id = frame and frame.args and frame.args[1]
return p._name(id)
end
-- 获取鱼类的图标
function p._icon(id)
if not id or type(id) ~= 'string' then
return id
end
-- 先获取完整的鱼类信息,查看是否有item字段
local info = mergeWithDefault(id)
local itemId = id -- 默认使用鱼类ID
if info and info.item and type(info.item) == 'string' and info.item ~= '<..>' then
itemId = info.item -- 如果有item字段且不是'<..>',使用item字段作为物品ID
end
local icon = Items._icon(itemId)
return icon or itemId
end
function p.icon(frame)
local id = frame and frame.args and frame.args[1]
return p._icon(id)
end
-- 根据指定筛选条件筛选并返回鱼类ID列表
-- @param filters table 筛选条件配置表
-- 筛选条件支持两种配置格式:
-- 1. 简单格式:直接指定属性值进行匹配,例如 {water_type="河流", rarity="少见"}
-- 2. 复杂格式:使用操作符指定匹配逻辑,例如 {seasons={op="contains", value="spring"}}
-- 支持的操作符及说明:
-- - equals: 精确匹配值(适用于所有数据类型)
-- - contains: 表中包含指定元素(仅适用于表类型属性)
-- - exact: 表中恰好只包含指定元素(仅适用于表类型属性)
-- - not_contains: 表中不包含指定元素(仅适用于表类型属性)
-- - contains_but_not_exact: 表中包含指定元素但元素数量大于1(仅适用于表类型属性)
-- 常用可筛选属性:legendary(是否传说), rarity(稀有度), seasons(季节), weather(天气), hours(时间),
-- water_type(水域类型), locations(地点), size(鱼影大小), retrieval(获取方式)等
-- @return table 符合所有筛选条件的鱼类ID数组
function p._filterFish(filters)
local result = {}
-- 如果没有提供筛选条件,则返回所有鱼类id
if not filters or type(filters) ~= 'table' then
for id, _ in pairs(data) do
if id ~= 'default' then -- 排除默认数据
table.insert(result, id)
end
end
return result
end
-- 遍历所有鱼类数据
for id, _ in pairs(data) do
if id ~= 'default' then -- 排除默认数据
local info = mergeWithDefault(id)
local match = true
-- 检查每个筛选条件
for key, filterValue in pairs(filters) do
if info[key] ~= nil then
-- 判断是简单筛选还是复杂筛选
if type(filterValue) == 'table' and filterValue.op and filterValue.value then
-- 复杂筛选条件
local op = filterValue.op
local value = filterValue.value
if op == 'equals' then
-- 值相等(适用于所有类型)
if info[key] ~= value then
match = false
break
end
elseif op == 'contains' and type(info[key]) == 'table' then
-- 表中包含指定值
local found = false
for _, v in ipairs(info[key]) do
if v == value then
found = true
break
end
end
if not found then
match = false
break
end
elseif op == 'exact' then
-- 精确匹配
if type(info[key]) == 'table' then
-- 表类型:恰好只有指定值
if #info[key] ~= 1 or info[key][1] ~= value then
match = false
break
end
else
-- 字符串类型:值完全相等
if info[key] ~= value then
match = false
break
end
end
elseif op == 'not_contains' then
-- 不包含指定值(同时支持表类型和字符串类型)
if type(info[key]) == 'table' then
-- 表类型:检查表中是否不包含指定值
for _, v in ipairs(info[key]) do
if v == value then
match = false
break
end
end
else
-- 字符串类型:检查字符串是否不包含指定子串
if tostring(info[key]):find(value, 1, true) ~= nil then
match = false
end
end
if not match then
break
end
elseif op == 'contains_but_not_exact' and type(info[key]) == 'table' then
-- 表中包含指定元素但元素数量大于1
local found = false
for _, v in ipairs(info[key]) do
if v == value then
found = true
break
end
end
if not found or #info[key] <= 1 then
match = false
break
end
end
else
-- 简单筛选条件
if type(info[key]) == 'table' then
-- 表类型属性,检查是否包含指定值
local found = false
for _, v in ipairs(info[key]) do
if v == filterValue then
found = true
break
end
end
if not found then
match = false
break
end
else
-- 基本类型属性,直接比较
if info[key] ~= filterValue then
match = false
break
end
end
end
end
end
if match then
table.insert(result, id)
end
end
end
return result
end
-- 传入frame,调用_filterFish函数获得鱼类id列表,为每个鱼类id调用模板渲染
-- @param frame 参数frame对象
-- @return string 所有鱼类模板渲染结果的拼接
function p.filteredFish(frame)
local frame = frame or mw.getCurrentFrame()
-- 从frame.args中获取筛选条件
local filters = {}
for key, value in pairs(frame.args) do
if key ~= 'args' and key ~= 1 and type(key) ~= 'number' then
filters[key] = value
end
end
-- 获取符合条件的鱼类id列表
local fishIds = p._filterFish(filters)
-- 为每个鱼类id调用模板渲染
local results = {}
for _, id in ipairs(fishIds) do
-- 获取鱼类的中文名和图标
local name = p._name(id)
local icon = p._icon(id)
-- 调用模板渲染
local rendered = frame:expandTemplate{
title = '物品',
args = {name, -- 中文名
icon -- 图标名
}
}
table.insert(results, rendered)
end
-- 返回所有渲染结果的拼接
return table.concat(results, '')
end
-- 根据季节和筛选类型筛选鱼类
-- @param frame 参数frame对象
-- 从frame参数中获取:
-- 参数1: 季节,如spring
-- 参数2: 类型,如contains, exact, not_contains, contains_but_not_exact
-- @return string 符合条件的鱼类模板渲染结果的拼接
function p.filteredFishBySeason(frame)
local frame = frame or mw.getCurrentFrame()
-- 从frame.args中获取季节和筛选类型参数
local season = frame.args[1] or '' -- 第一个参数: 季节
local filterType = frame.args[2] or 'contains' -- 第二个参数: 筛选类型,默认为contains
-- 构建筛选条件
local filters = {
seasons = {
op = filterType,
value = season
},
-- 排除retrieval = "mines"的鱼类
retrieval = {
op = "not_contains",
value = "mines"
}
}
-- 获取符合条件的鱼类id列表
local fishIds = p._filterFish(filters)
mw.logObject(fishIds)
-- 为每个鱼类id调用模板渲染
local results = {}
for _, id in ipairs(fishIds) do
-- 获取鱼类的中文名和图标
local name = p._name(id)
local icon = p._icon(id)
-- 调用模板渲染
local rendered = frame:expandTemplate{
title = '物品',
args = {name, -- 中文名
icon -- 图标名
}
}
table.insert(results, rendered)
end
-- 返回所有渲染结果的拼接
return table.concat(results, '')
end
-- 根据季节和水域类型筛选鱼类
-- @param frame 参数frame对象
-- 从frame参数中获取:
-- 参数1: 季节,如spring
-- 参数2: 筛选类型,如contains, exact, not_contains, contains_but_not_exact
-- 参数3: 水域类型或地点,如river、ocean、pond或特定地点
-- @return string 符合条件的鱼类模板渲染结果的拼接
function p.filteredFishBySeasonWater(frame)
local frame = frame or mw.getCurrentFrame()
-- 从frame.args中获取季节、筛选类型和水域类型/地点参数
local season = frame.args[1] or '' -- 第一个参数: 季节
local filterType = frame.args[2] or 'contains' -- 第二个参数: 筛选类型,默认为contains
local waterTypeOrLocation = frame.args[3] or '' -- 第三个参数: 水域类型或地点
local water_type_Type = frame.args[4] or 'contains' -- 第四个参数: 水域类型筛选操作符
local legend = frame.args[5] or 'contains' -- 第五个参数: 传说类型筛选操作符
-- 构建筛选条件
local filters = {}
if season ~= '' then
filters.seasons = {
op = filterType,
value = season
}
end
-- 检查第三个参数是否为water_type_map中的值
if waterTypeOrLocation == '河流' then
filters.water_type = {
op = water_type_Type,
value = "river"
}
filters.locations = false
filters.retrieval = {
op = "exact",
value = "fishing"
}
end
if waterTypeOrLocation == "海洋" then
filters.water_type = {
op = water_type_Type,
value = "ocean"
}
filters.locations = false
filters.retrieval = {
op = "exact",
value = "fishing"
}
end
if waterTypeOrLocation == "池塘" then
filters.water_type = {
op = water_type_Type,
value = "pond"
}
filters.locations = false
filters.retrieval = {
op = "exact",
value = "fishing"
}
end
if waterTypeOrLocation == "深林" then
filters.locations = {
op = water_type_Type,
value = "deep_woods"
}
end
if waterTypeOrLocation == "矿井" then
filters.retrieval = {
op = water_type_Type,
value = "mines"
}
end
if legend == "传说" then
filters.legendary = {
op = "equals",
value = true
}
end
-- 获取符合条件的鱼类id列表
local fishIds = p._filterFish(filters)
mw.logObject(fishIds)
-- 为每个鱼类id调用模板渲染
local results = {}
for _, id in ipairs(fishIds) do
-- 获取鱼类的中文名和图标
local name = p._name(id)
local icon = p._icon(id)
-- 调用模板渲染
local rendered = frame:expandTemplate{
title = '物品',
args = {name, -- 中文名
icon -- 图标名
}
}
table.insert(results, rendered)
end
-- 返回所有渲染结果的拼接
return table.concat(results, '')
end
--[[
mw.logObject(p._legendary("lightning_fish")=="传说")
mw.logObject(p._rarity("lightning_fish")=="少见")
mw.logObject(p._season("lightning_fish")=="夏天")
mw.logObject(p._weather("lightning_fish")=="极端恶劣天气")
mw.logObject(p._hours("lightning_fish")=="全天")
mw.logObject(p._water_type("lightning_fish")=="河流")
mw.logObject(p._location("lightning_fish")=="无")
mw.logObject(p._size("lightning_fish")=="中")
mw.logObject(p._retrieval("lightning_fish")=="钓鱼")
mw.logObject(p._has_perk("lightning_fish")=="传说之鱼")
mw.logObject(p._perk_artifact("lightning_fish")=="无")
mw.logObject(p._legendary("salmon")=="普通")
mw.logObject(p._season("salmon")=="春天,秋天")
mw.logObject(p._water_type("shark")=="海洋")
mw.logObject(p._size("shark")=="巨大")
mw.logObject(p._location("shark")=="无")
mw.logObject(p._has_perk("snow_fish")=="传说之鱼")
mw.logObject(p._has_perk("leaf_fish")=="传说之鱼")
mw.logObject(p._has_perk("cherry_fish")=="传说之鱼")
mw.logObject(p._perk_artifact("treasure_box_gold")=="无")
local frame = mw.getCurrentFrame()
frame.args = {"spring","exact","河流","exact"}
p.filteredFishBySeasonWater(frame)
p.test("trout")
]] --
function p.test(id)
local info = mergeWithDefault(id)
mw.logObject(info)
mw.logObject({test=false})
end
return p