Модуль:ChemistryAPI: различия между версиями
Kirus59 (обсуждение | вклад) Новая страница: «p = {} function p.buildreactionstable(frame) end return p» |
Kirus59 (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local prototypes = mw.loadData("Module:ChemistryAPI/prototypes") | |||
local images = ms.loadData("Module:ChemistryAPI/images") | |||
function p. | 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>") | |||
end | |||
end | |||
end | |||
function getActions(reactPrototype) | |||
local actions = {} | |||
-- Температура | |||
local minMaxTemplate = "выше %sК и ниже %sК" | |||
local minTemplate = "выше %sК" | |||
local maxTemplate = "ниже %sК" | |||
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 = getMixingImage(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 | end | ||
return p | return p | ||
Версия от 11:17, 7 июля 2025
Для документации этого модуля может быть создана страница Модуль: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>")
end
end
end
function getActions(reactPrototype)
local actions = {}
-- Температура
local minMaxTemplate = "выше %sК и ниже %sК"
local minTemplate = "выше %sК"
local maxTemplate = "ниже %sК"
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 = getMixingImage(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