Для документации этого модуля может быть создана страница Модуль:Chemistry Lookup/doc
local p = {}
local JsonPageLoader = require('Module:JsonPageLoader')
-- Загружаем данные (json с рецептами)
local data = JsonPageLoader.load('react_prototypes.json')
-- Функция: превращает products/ reactants в массив
local function normalizeDict(dict)
if type(dict) ~= "table" then return {} end
local arr = {}
for id, val in pairs(dict) do
if type(val) == "table" then
table.insert(arr, { id = id, amount = val.amount or 1 })
else
-- если значение просто число (например Bruizine: 2)
table.insert(arr, { id = id, amount = val })
end
end
return arr
end
-- Функция: нормализует рецепт
local function normalizeRecipe(id, recipe)
-- гарантируем, что у рецепта есть id
if not recipe.id then
recipe.id = id
end
-- нормализуем продукты и реагенты
recipe.products = normalizeDict(recipe.products)
recipe.reactants = normalizeDict(recipe.reactants)
return recipe
end
-- Основной билдер
function p.buildreactboxes(frame)
local out = {}
for id, recipe in pairs(data) do
if recipe.type == "reaction" then
local r = normalizeRecipe(id, recipe)
-- строим красивую карточку рецепта
table.insert(out, string.format("== %s ==\n", r.id))
if #r.reactants > 0 then
table.insert(out, "'''Реагенты:''' ")
local reacts = {}
for _, v in ipairs(r.reactants) do
table.insert(reacts, string.format("%s × %s", v.id, v.amount))
end
table.insert(out, table.concat(reacts, ", ") .. "\n")
end
if #r.products > 0 then
table.insert(out, "'''Продукты:''' ")
local prods = {}
for _, v in ipairs(r.products) do
table.insert(prods, string.format("%s × %s", v.id, v.amount))
end
table.insert(out, table.concat(prods, ", ") .. "\n")
end
if r.impact then
table.insert(out, "'''Импакт:''' " .. r.impact .. "\n")
end
table.insert(out, "\n")
end
end
return table.concat(out, "")
end
return p