Модуль:TableOfChemicals

Материал из Space Station 14 Вики

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

p = {}
p.chem = mw.text.jsonDecode(mw.title.new("User:KrasnoshchekovPavel/chem_prototypes.json"):getContent())
p.react = mw.text.jsonDecode(mw.title.new("User:KrasnoshchekovPavel/react_prototypes.json"):getContent())

p.fillTable = function(frame)
	
	local out = ""
	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
			out = out .. fillChemistryRow(chemPrototypeId, frame) 
		end
	end
	
	for _, chemPrototype in pairs(p.chem) do
		
		if group == nil or group == "" or contains(groups, chemPrototype.group) then
			out = out .. fillChemistryRow(chemPrototype.id, frame) 
		end
	end
	
	return out
end

function contains(list, x)
	for _, v in ipairs(list) do
		if v == x then return true end
	end
	return false
end

p.fillChemistryRow = function(frame)
	
	return fillChemistryRow(frame.args.id, frame)
	
end

fillChemistryRow = function(chemPrototypeId, frame)
	
	local out = ""
	local chemPrototype = p.chem[chemPrototypeId]
	local templateArgs = {}
	templateArgs.id = chemPrototype.id
	templateArgs.name = chemPrototype.name
	templateArgs.description = chemPrototype.desc .. " На вид " .. chemPrototype.physicalDesc .. "."
	templateArgs.color = chemPrototype.color
	templateArgs.textColor = chemPrototype.textColor
	templateArgs.effects = getEffects(chemPrototype.id)
	templateArgs.recipes_count = tablelength(chemPrototype.recipes)
	
	templateArgs.reactants = ""
	templateArgs.products = ""
	templateArgs.action = ""
	
	if templateArgs.recipes_count == 0 then
		templateArgs.recipes_count = 1 -- Для заполнения параметра rowspan строки
		out = out .. frame:expandTemplate{ title = "Первая_строка_химического_вещества", args = templateArgs}
	else
		local firstReact = true
		for _, reactId in pairs(chemPrototype.recipes) do
			
			local reactPrototype = p.react[reactId]
			local reactants = {}
			local reactantTemplate = "%s [[#chem_%s|%s]]"
			
			for reactantId, reactantValue in pairs(reactPrototype.reactants) do
				
				local reactantChemData = p.chem[reactantId]
				local reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id,  reactantChemData.name)
				table.insert(reactants, reactantText)
			end
			templateArgs.reactants = table.concat(reactants, "<br>")
			
			local products = {}
			local productTemplate = "%s [[#chem_%s|%s]]"
			
			for productId, productAmount in pairs(reactPrototype.products) do
				
				local productChemData = p.chem[productId]
				local productText = string.format(productTemplate, productAmount, productChemData.id,  productChemData.name)
				table.insert(products, productText)
			end
			templateArgs.products = table.concat(products, "<br>")
			
			templateArgs.actions = getActions(reactPrototype)
			
			local template = "Строка_химического_вещества"
			if firstReact then
				template = "Первая_строка_химического_вещества"
				firstReact = false
			end
			
			out = out .. frame:expandTemplate{ title = template, args = templateArgs}
		end
	end
	return out
end

getActions = function(reactPrototype)
	
	local actions = {}
	
	-- Температура
	local minMaxTemplate = "выше %sК и ниже %sК"
	local minTemplate = "выше %sК"
	local maxTemplate = "ниже %sК"
	
	local hasMin = reactPrototype.minTemp ~= 0
	local hasMax = reactPrototype.hasMax
	
	local tempString = ""
	if hasMax and hasMin then
		tempString = string.format(minMaxTemplate, reactPrototype.minTemp, reactPrototype.maxTemp)
	elseif hasMin then
		tempString = string.format(minTemplate, reactPrototype.minTemp)
	elseif hasMax then
		tempString = string.format(maxTemplate, reactPrototype.maxTemp)
	end
	
	for _, mixingCategory in pairs(reactPrototype.mixingCategories) do
		local image = getMixingImage(mixingCategory.id)
		if image ~= nil then
			table.insert(actions, string.format("[[File:%s|32px]]", image)) -- Картинка
		end
		
		table.insert(actions, mixingCategory.name .. " " .. tempString) -- Название
		
	end
	
	return table.concat(actions, "<br>")
	
end

getMixingImage = function(mixingId)
	
	local images = {
		["DummyMix"] = "Beaker.png"
	}
	
	return images[mixingId]
	
end

function getEffects(chemPrototypeId)
	
	local descriptions = {}
	
	local chemPrototype = p.chem[chemPrototypeId]
	if chemPrototype.metabolisms == nil then
		return ""
	end
	
	for _, metabolismGroup in pairs(chemPrototype.metabolisms) do
		for _, effect in pairs(metabolismGroup) do
			if effect.description ~= "" then
				local description = (string.gsub(effect.description, "%[", "\""))
				description = (string.gsub(effect.description, "%]", "\""))
				table.insert(descriptions, description)
			end
		end
	end
	return table.concat(descriptions, "<br>")
  
end

function tablelength(T)
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end

function attachAsNewLine(originalStr, joinStr)
	return originalStr .. "\n" .. joinStr 
end

return p