模块:制作配方
注意
由于模块使用的lua语言编辑,没有编程基础的请不要对模块内代码进行编辑
如需修改样式和逻辑调整请联系站长或者我。
请仔细学习下方 示例 后再进行使用,否则容易造成各种奇怪的问题
smw
因为set属性值分割了之后会把重复的数据合并 所以我建议set的时候把属性和数据分成两个,一个用于查询筛选,一个用于生成展示
|制作配方_属性={{{制作配方|}}}|+sep=, <!--用于查询,获取到的属性值,因为是用逗号隔开的所以要“|+sep=,”进行分隔符声明才能把属性值分开-->
|制作配方_数据={{{制作配方|}}} <!--用于生成-->
示例
func_2使用效果示例
func_2使用代码示例
基本格式:[①,②]
①:物品名(③⑤同理)
②:数量( ④⑥同理)
①,②,③,④,⑤,⑥ 以此类推
所以下方演示的格式可以看成
黏菌柄,4,
大叶藻条,4,
睡莲蜡,4
演示:
{{#if:配方数组
|{{#invoke: 制作配方|func_2|黏菌柄,4,大叶藻条,4,睡莲蜡,4}}
|暂无合成配方}}
func_5使用效果示例
演示:
工作台
2
真菌滋生物
1
红蚂蚁卵
4
干草块
香肠机
3
真菌滋生物
2
干草块
func_5使用代码示例
基本格式:[①,②,③,④,⑤]
①:方式
②:总数(用了几个物品就填几,纯数字)
③:备注(没有就填无)
④:物品名(⑤同理)
⑤:数量( ⑦同理)
ps:如果总数是1则④,⑤,如果总数是2,则④,⑤,⑥,⑦ 以此类推
所以下方演示的格式可以看成
工作站,2,备注,物品1,数量1,物品2,数量2,
手工,1,备注,物品3,数量3,
工作站,1,无,物品4,数量4,
锻造站,1,无,物品4,数量4
演示:
{{#invoke: 制作配方|func_5|工作台,3,无,真菌滋生物,2,红蚂蚁卵,1,干草块,4,香肠机,2,无,真菌滋生物,3,干草块,2}}
local p = {}
function p.func_5(frame) --{方式,总数(纯数字),备注(没有填无),物品,数量}
local inputStr = frame.args[1] or "" -- 获取原始参数
-- 分割字符串为数组
local arr = {}
for item in string.gmatch(inputStr, "[^,]+") do
item = item:match("^%s*(.-)%s*$") -- 去除前后空格
table.insert(arr, item)
end
-- 调试:打印分割后的数组
--for i, v in ipairs(arr) do
-- mw.log("arr[" .. i .. "] = " .. tostring(v))
--end
local result = {}
local i = 1
local totalElements = #arr
while i <= totalElements do
local blockStart = arr[i] --方式
local itemCount = tonumber(arr[i + 1]) --总数
local remark = arr[i + 2] --备注
local items = {}
local requiredLength = i + 2 + itemCount * 2 -- 计算数据完整性
if requiredLength > totalElements then -- 数据不完整,返回错误信息
return "错误:数据不完整,期望长度: " .. requiredLength .. ",实际长度: " .. totalElements
end
-- 读取物品和数量
for j = 1, itemCount do
local itemIndex = i + 3 + (j - 1) * 2
if itemIndex <= totalElements then
local itemName = arr[itemIndex]
local itemQuantity = arr[itemIndex + 1]
if itemName and itemQuantity then
table.insert(items, {
name = itemName,
quantity = itemQuantity
})
end
end
end
-- 保存到结果中
if not result[blockStart] then
result[blockStart] = {}
end
table.insert(result[blockStart], {
remark = remark,
items = items
})
i = i + 3 + itemCount * 2 -- 移动到下一个区块
end
return p.formatOutput_5(result) -- 格式化输出
end
function p.formatOutput_5(result)
local output = ""
-- 按区块类型排序,使输出更稳定
local sortedTypes = {}
for blockType in pairs(result) do
table.insert(sortedTypes, blockType)
end
table.sort(sortedTypes)
for _, blockType in ipairs(sortedTypes) do
local blocks = result[blockType]
output = output .."[[File:"..blockType..".png|40px]] ".."'''[[" ..blockType .. "]]'''".. "<br>\n"
for idx, block in ipairs(blocks) do
if idx > 1 then
output = output .. "或者<br>\n"
end
if block.remark ~= "无" then --如果备注为无
output = output .. "(" .. (block.remark or "") .. ")<br>\n"
end
for _, item in ipairs(block.items) do
output = output .. item.quantity.."[[File:"..item.name..".png|30px]] ".."[[" ..item.name .. "]]".. "<br>\n"
end
end
end
return output
end
--
function p.func_2(frame) --{物品,数量}
local inputStr = frame.args[1] or "" -- 获取原始参数
-- 分割字符串为数组
local arr = {}
for item in string.gmatch(inputStr, "[^,]+") do
item = item:match("^%s*(.-)%s*$") -- 去除前后空格
table.insert(arr, item)
end
-- 调试:打印分割后的数组
--for i, v in ipairs(arr) do
-- mw.log("arr[" .. i .. "] = " .. tostring(v))
--end
local result = {}
local i = 1
local totalElements = #arr
while i <= totalElements do
local itemName = arr[i] --物品
local itemQuantity = arr[i + 1] --数量
local items = {}
local requiredLength = i + 1 -- 计算数据完整性
if requiredLength > totalElements then -- 数据不完整,返回错误信息
return "错误:数据不完整,期望长度: " .. requiredLength .. ",实际长度: " .. totalElements
end
-- 读取物品和数量
local itemName = arr[i]
local itemQuantity = arr[i + 1]
if itemName and itemQuantity then
table.insert(items, {
name = itemName,
quantity = itemQuantity
})
end
-- 保存到结果中
if not result then
result = {}
end
table.insert(result, {
name = itemName,
quantity = itemQuantity
})
i = i + 2 -- 移动到下一个区块
end
return p.formatOutput_2(result) -- 格式化输出
end
function p.formatOutput_2(result)
local output = ""
for _, item in ipairs(result) do
output = output .. item.quantity.."[[File:"..item.name..".png|30px]] ".."[[" ..item.name .. "]]".. "<br>\n"
end
return output
end
--
return p
-- DevControl: mw.log(p['func_5']({ args={"工作站1,2,备注,物品1,数量1,物品2,数量2,手工,1,备注,物品3,数量3,工作站,1,无,物品4,数量4,捡到,1,备注,物品4,数量4"}}))
-- DevControl: mw.log(p['func_2']({ args={"物品1,数量1,物品2,数量2,物品3,数量3,物品4,数量4"}}))

沪公网安备 31011002002714 号