Модуль:ChemistryAPI: различия между версиями
Kirus59 (обсуждение | вклад) Нет описания правки |
Kirus59 (обсуждение | вклад) fix reaction temp desc |
||
| Строка 181: | Строка 181: | ||
local tempString = "" | local tempString = "" | ||
if hasMax then | if hasMax then | ||
tempString = string.format(" | tempString = string.format("ниже %sK", reactPrototype.maxTemp) | ||
end | end | ||
| Строка 189: | Строка 189: | ||
end | end | ||
tempString = tempString .. string.format(" | tempString = tempString .. string.format("выше %sК", reactPrototype.minTemp) | ||
end | end | ||
Текущая версия от 10:37, 6 декабря 2025
Для документации этого модуля может быть создана страница Модуль:ChemistryAPI/doc
local prototypes = mw.loadData("Module:ChemistryAPI/prototypes")
local images = mw.loadData("Module:ChemistryAPI/images")
local chemicals = prototypes.chemicals
local reactions = prototypes.reactions
local p = {}
function p.getChemicalField(frame)
return mw.text.nowiki(chemicals[frame.args.id][frame.args.field])
end
function p.buildChemicalsTable(frame)
local result = ""
local group = frame.args.group
local groups
if group ~= nil and group ~= "" then
groups = mw.text.split(group, ",")
end
local additional = frame.args.additional
if additional ~= nil and additional ~= "" then
local additionalChemPrototypeIds = mw.text.split(additional, ",")
for _, chemPrototypeId in pairs(additionalChemPrototypeIds) do
result = result .. buildChemicalRow(chemicals[chemPrototypeId], frame)
end
end
for _, chemPrototype in pairs(chemicals) do
if group == nil or group == "" or arrayContains(groups, chemPrototype.group) then
result = result .. buildChemicalRow(chemPrototype, frame)
end
end
return result
end
function p.buildReactionsTable(frame)
local result = ""
for _, reactPrototype in pairs(reactions) 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 = chemicals[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 = chemicals[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 = "Chemistry/ReactionRow"
result = result .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
return result
end
-- region Local API
function buildChemicalRow(chemicalPrototype, frame)
local result = ""
local templateArgs = {}
templateArgs.id = chemicalPrototype.id
templateArgs.name = chemicalPrototype.name
templateArgs.description = chemicalPrototype.desc .. "<br />''На вид " .. chemicalPrototype.physicalDesc .. ".''"
templateArgs.color = chemicalPrototype.color
templateArgs.textColor = chemicalPrototype.textColor
templateArgs.effects = getEffects(chemicalPrototype)
templateArgs.recipes_count = arrayLength(chemicalPrototype.recipes)
templateArgs.reactants = ""
templateArgs.products = ""
templateArgs.action = ""
local chemicalTemplate = "Chemistry/ChemicalsTable/ChemicalRow"
if templateArgs.recipes_count == 0 then
templateArgs.recipes_count = 1 -- Для заполнения параметра rowspan строки
result = result .. frame:expandTemplate{ title = chemicalTemplate, args = templateArgs}
return result
end
local firstReaction = true
for _, reactId in pairs(chemicalPrototype.recipes) do
local reactPrototype = reactions[reactId]
local reactants = {}
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
local reactantChemical = chemicals[reactantId]
local reactantText = string.format("%s [[#chem_%s|%s]]", reactantValue.amount, reactantChemical.id, reactantChemical.name)
if reactantValue.catalyst then
reactantText = reactantText .. " (катализатор)"
end
table.insert(reactants, reactantText)
end
templateArgs.reactants = table.concat(reactants, "<br>")
local products = {}
for productId, productAmount in pairs(reactPrototype.products) do
local productChemical = chemicals[productId]
local productText = string.format("%s [[#chem_%s|%s]]", productAmount, productChemical.id, productChemical.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 = "Chemistry/ReactionRow"
if firstReaction then
template = chemicalTemplate
firstReaction = false
end
result = result .. frame:expandTemplate{ title = template, args = templateArgs}
end
return result
end
function getEffects(chemicalPrototype)
local result = ""
if chemicalPrototype.metabolisms == nil then
return result
end
for metabolismGroupKey, metabolismGroup in pairs(chemicalPrototype.metabolisms) do
result = addNewLine(result, string.format("* %s (%s единиц в секунду)", metabolismGroupKey, metabolismGroup.rate))
for _, effect in pairs(metabolismGroup.effects) do
if effect.description ~= "" then
result = addNewLine(result, "** "..effect.description)
end
end
end
return result
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(array)
local count = 0
for _ in pairs(array) do
count = count + 1
end
return count
end
function arrayContains(array, T)
for _, element in pairs(array) do
if element == T then
return true
end
end
return false
end
function addNewLine(original, new)
return original .. "\n" .. new
end
-- endregion
return p