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

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


local chemicals = prototypes.chem
local chemicals = prototypes.chemicals
local reactions = prototypes.react
local reactions = prototypes.reactions
local p = {}
local p = {}
function p.getChemicalField(frame)
return mw.text.nowiki(chemicals[frame.args.id][frame.args.field])
end


function p.buildChemicalsTable(frame)
function p.buildChemicalsTable(frame)
Строка 33: Строка 37:
function p.buildReactionsTable(frame)
function p.buildReactionsTable(frame)
local result = ""
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
for _, reactPrototype in pairs(reactions) do
if whitelist == nil or arrayLength(whitelist) == 0 or arrayContains(whitelist, reactPrototype.id) then
if arrayLength(reactPrototype.effects) ~= 0 then
local reactants = {}
local reactants = {}
local reactantTemplate = "%s [[#chem_%s|%s]]"
local reactantTemplate = "%s [[#chem_%s|%s]]"
Строка 81: Строка 81:
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
else
whitelist = whitelist .. "," .. reactProto.id
end
end
end
frame.args.whitelist = whitelist
result = result .. p.buildReactionsTable(frame)
return result
return result
end
end
Строка 110: Строка 90:
templateArgs.id = chemicalPrototype.id
templateArgs.id = chemicalPrototype.id
templateArgs.name = chemicalPrototype.name
templateArgs.name = chemicalPrototype.name
templateArgs.description = chemicalPrototype.desc .. " На вид " .. chemicalPrototype.physicalDesc .. "."
templateArgs.description = chemicalPrototype.desc .. "<br />''На вид " .. chemicalPrototype.physicalDesc .. ".''"
templateArgs.color = chemicalPrototype.color
templateArgs.color = chemicalPrototype.color
templateArgs.textColor = chemicalPrototype.textColor
templateArgs.textColor = chemicalPrototype.textColor
Строка 201: Строка 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
Строка 209: Строка 189:
end
end
tempString = tempString .. string.format("ниже %sК", reactPrototype.minTemp)
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