Модуль:ChemistryAPI: различия между версиями

Нет описания правки
fix reaction temp desc
 
(не показано 46 промежуточных версий этого же участника)
Строка 2: Строка 2:
local images = mw.loadData("Module:ChemistryAPI/images")
local images = mw.loadData("Module:ChemistryAPI/images")


local chemicals = prototypes.chemicals
local reactions = prototypes.reactions
local p = {}
local p = {}
p.chem = prototypes.chem
 
p.react = prototypes.react
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)
function p.buildReactionsTable(frame)
local out = ""
local result = ""
for _, reactPrototype in pairs(p.react) do
for _, reactPrototype in pairs(reactions) do
if arrayLength(reactPrototype.effects) ~= 0 then
if arrayLength(reactPrototype.effects) ~= 0 then
local reactants = {}
local reactants = {}
Строка 15: Строка 44:
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
local reactantChemData = p.chem[reactantId]
local reactantChemData = chemicals[reactantId]
local reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id,  reactantChemData.name)
local reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id,  reactantChemData.name)
if reactantValue.catalyst then
if reactantValue.catalyst then
Строка 30: Строка 59:
for productId, productAmount in pairs(reactPrototype.products) do
for productId, productAmount in pairs(reactPrototype.products) do
local productChemData = p.chem[productId]
local productChemData = chemicals[productId]
local productText = string.format(productTemplate, productAmount, productChemData.id,  productChemData.name)
local productText = string.format(productTemplate, productAmount, productChemData.id,  productChemData.name)
table.insert(products, productText)
table.insert(products, productText)
Строка 46: Строка 75:
templateArgs.actions = getActions(reactPrototype)
templateArgs.actions = getActions(reactPrototype)
local template = "Строка_реакции"
local template = "Chemistry/ReactionRow"
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
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
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
end
return out
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
end


Строка 63: Строка 181:
local tempString = ""
local tempString = ""
if hasMax then
if hasMax then
tempString = string.format("выше %sK", reactPrototype.maxTemp)
tempString = string.format("ниже %sK", reactPrototype.maxTemp)
end
end
Строка 71: Строка 189:
end
end
tempString = tempString .. string.format("ниже %sК", reactPrototype.minTemp)
tempString = tempString .. string.format("выше %sК", reactPrototype.minTemp)
end
end
Строка 86: Строка 204:
end
end


function arrayLength(T)
function arrayLength(array)
local count = 0
local count = 0
for _ in pairs(T) do  
for _ in pairs(array) do  
count = count + 1  
count = count + 1  
end
end
return count
return count
end
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
return p

Текущая версия от 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