Модуль:ChemistryAPI
Для документации этого модуля может быть создана страница Модуль:ChemistryAPI/doc
local prototypes = mw.loadData("Module:ChemistryAPI/prototypes")
local images = ms.loadData("Module:ChemistryAPI/images")
local p = {}
p.chem = prototypes.chem
p.react = prototypes.react
function p.buildReactionsTable(frame)
local out = ""
for _, reactPrototype in pairs(p.react) do
if arrayLength(reactPrototype.effects) ~= 0 then
local reactants = {}
local reactantTemplate = "%s [[#chem_%s|%s]]"
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
local reactantChemData = p.chem[reactantId]
local reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id, reactantChemData.name)
if reactantValue.catalyst then
reactantText = reactantText .. " (катализатор)"
end
table.insert(reactants, reactantText)
end
local templateArgs = {}
templateArgs.reactants = table.concat(reactants, "<br>")
local products = {}
local productTemplate = "%s [[#chem_%s|%s]]"
for productId, productAmount in pairs(reactPrototype.products) do
local productChemData = p.chem[productId]
local productText = string.format(productTemplate, productAmount, productChemData.id, productChemData.name)
table.insert(products, productText)
end
if arraylength(reactPrototype.effects) then
for _, effect in pairs(reactPrototype.effects) do
if effect.description ~= "" then
table.insert(products, effect.description)
end
end
end
templateArgs.products = table.concat(products, "<br>")
templateArgs.actions = getActions(reactPrototype)
local template = "Строка_реакции"
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
return out
end
function getActions(reactPrototype)
local actions = {}
local hasMin = reactPrototype.minTemp ~= 0
local hasMax = reactPrototype.hasMax
local tempString = ""
if hasMax then
tempString = string.format("выше %sK", reactPrototype.maxTemp)
end
if hasMin then
if tempString ~= "" then
tempString = tempString .. " и "
end
tempString = tempString .. string.Format("ниже %sК", reactPrototype.minTemp)
end
for _, mixingCategory in pairs(reactPrototype.mixingCategories) do
local image = images[mixingCategory.id]
if image ~= nil then
table.insert(actions, string.format("[[File:%s|32px|link=]]", image)) -- Картинка
end
table.insert(actions, mixingCategory.name .. " " .. tempString) -- Название
end
return table.concat(actions, "<br>")
end
function arrayLength(T)
local count = 0
for _ in pairs(T) do
count = count + 1
end
return count
end
return p