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

Нет описания правки
Нет описания правки
Строка 2: Строка 2:
local images = mw.loadData("Module:ChemistryAPI/images")
local images = mw.loadData("Module:ChemistryAPI/images")


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


function p.buildReactionsTable(frame)
function out.buildChemicalsTable(frame)
local out = ""
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 .. getChemistryRow(chemicals[chemPrototypeId], frame)
end
end
for _, reactPrototype in pairs(p.react) do
for _, chemPrototype in pairs(chemicals) do
if group == nil or group == "" or arrayContains(groups, chemPrototype.group) then
result = result .. getChemistryRow(chemPrototype, frame)
end
end
return result
end
 
function out.buildReactionsTable(frame)
local result = ""
for _, reactPrototype in pairs(reactions) do
if arrayLength(reactPrototype.effects) ~= 0 then
if arrayLength(reactPrototype.effects) ~= 0 then
local reactants = {}
local reactants = {}
Строка 15: Строка 40:
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: Строка 55:
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)
Строка 48: Строка 73:
local template = "Chemistry/ReactionRow"
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
 
function getChemistryRow(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.id)
templateArgs.recipes_count = arrayLength(chemicalPrototype.recipes)
templateArgs.reactants = ""
templateArgs.products = ""
templateArgs.action = ""
if templateArgs.recipes_count == 0 then
templateArgs.recipes_count = 1 -- Для заполнения параметра rowspan строки
result = result .. frame:expandTemplate{ title = "Первая_строка_химического_вещества", args = templateArgs}
else
local firstReact = 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 = "Строка_химического_вещества"
if firstReact then
template = "Первая_строка_химического_вещества"
firstReact = false
end
result = result .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
end
end
return out
return result
end
end


Строка 86: Строка 178:
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
Строка 94: Строка 186:
end
end


return p
function arrayContains(array, T)
for _, element in pairs(array) do
if element == T then
return true
end
end
return false
end
 
return out

Версия от 05:59, 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 out = {}

function out.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 .. getChemistryRow(chemicals[chemPrototypeId], frame) 
		end
	end
	
	for _, chemPrototype in pairs(chemicals) do
		if group == nil or group == "" or arrayContains(groups, chemPrototype.group) then
			result = result .. getChemistryRow(chemPrototype, frame) 
		end
	end
	
	return result
end

function out.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

function getChemistryRow(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.id)
	templateArgs.recipes_count = arrayLength(chemicalPrototype.recipes)
	
	templateArgs.reactants = ""
	templateArgs.products = ""
	templateArgs.action = ""
	
	if templateArgs.recipes_count == 0 then
		templateArgs.recipes_count = 1 -- Для заполнения параметра rowspan строки
		result = result .. frame:expandTemplate{ title = "Первая_строка_химического_вещества", args = templateArgs}
	else
		local firstReact = 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 = "Строка_химического_вещества"
			if firstReact then
				template = "Первая_строка_химического_вещества"
				firstReact = false
			end
			
			result = result .. frame:expandTemplate{ title = template, args = templateArgs}
		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

return out