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

Нет описания правки
Нет описания правки
Строка 52: Строка 52:
end
end
else
else
reactantText = string.format("%s %s", reactantAmount, reactantId)
reactantText = string.format("%s %s", reactantValue.amount, reactantId)
end
end
table.insert(reactants, reactantText)
table.insert(reactants, reactantText)

Версия от 09:44, 8 июля 2025

Для документации этого модуля может быть создана страница Модуль:ChemistryAPI/doc

local prototypes = mw.loadData("Module:ChemistryAPI/prototypes")
local images = mw.loadData("Module:ChemistryAPI/images")

local chemicals = prototypes.chem
local reactions = prototypes.react
local p = {}

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 = ""
	local whitelist = nil
	if frame.args.whitelist ~= nil and frame.args.whitelist ~= "" then
		whitelist = mw.text.split(frame.args.whitelist, ",")
	end
	
	for _, reactPrototype in pairs(reactions) do
		if whitelist == nil or arrayLength(whitelist) == 0 or arrayContains(whitelist, reactPrototype.id) then
			local reactants = {}
			local reactantTemplate = "%s [[#chem_%s|%s]]"
			
			for reactantId, reactantValue in pairs(reactPrototype.reactants) do
				local reactantChemData = chemicals[reactantId]
				local reacteantText = ""
				if reactantChemData ~= nil then
					reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id,  reactantChemData.name)
					if reactantValue.catalyst then
						reactantText = reactantText .. " (катализатор)"
					end
				else
					reactantText = string.format("%s %s", reactantValue.amount, reactantId)
				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 productText = ""
				local productChemData = chemicals[productId]
				if productChemData ~= nil then
					productText = string.format(productTemplate, productAmount, productChemData.id,  productChemData.name)
				else
					productText = string.format("%s %s", productAmount, productId)
				end
				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

function p.buildReactionsEffectsTable(frame)
	local result = ""
	local whitelist = ""
	
	local first = true
	for _, reactProto in pairs(reactions) do 
		if arrayLength(reactProto.effects) ~= 0 then 
			if first then
				whitelist = reactProto.id
				first = false
			else
				whitelist = whitelist .. "," .. reactProto.id
			end
		end
	end
	frame.args.whitelist = whitelist
	
	result = result .. p.buildReactionsTable(frame)
	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 .. " На вид " .. 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 = ""
	
	if templateArgs.recipes_count == 0 then
		templateArgs.recipes_count = 1 -- Для заполнения параметра rowspan строки
	end	
	
	local whitelist = ""
	local first = true
	for _, reactId in pairs(chemicalPrototype.recipes) do 
		if arrayLength(reactProto.effects) ~= 0 then 
			if first then
				whitelist = reactId
				first = false
			else
				whitelist = whitelist .. "," .. reactId
			end
		end
	end
	templateArgs.reactionsWhitelist = whitelist
	
	result = result .. frame:expandTemplate{ title = "Chemistry/ChemicalRow", args = templateArgs}
	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