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

Материал из Space Station 14 Вики
мНет описания правки
Нет описания правки
 
(не показаны 73 промежуточные версии этого же участника)
Строка 1: Строка 1:
p = {}
p = {}
p.chem = mw.text.jsonDecode(mw.title.new("User:IanComradeBot/chem_prototypes.json"):getContent())
p.react = mw.text.jsonDecode(mw.title.new("User:IanComradeBot/react_prototypes.json"):getContent())


local prototypes = mw.loadData("Module:Chemistry Lookup/data")
p.fillReactTable = function(frame)
p.chem = prototypes.chem
p.react = prototypes.react
local out = ""
for _, reactPrototype in pairs(p.react) do
if tablelength(reactPrototype.effects) ~= 0 then
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)
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 = p.chem[productId]
local productText = string.format(productTemplate, productAmount, productChemData.id,  productChemData.name)
table.insert(products, productText)
end
-- Эффекты реакции
if tablelength(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 = "Строка_реакции"
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
return out
end


p.fillTable = function(frame)
p.fillChemTable = function(frame)
local out = {}
local out = ""
local out = ""
local group = frame.args.group
local group = frame.args.group
local templateArgs = {}
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
for _, chemPrototype in pairs(p.chem) do
if group == nil or chemPrototype.group == group then
if group == nil or group == "" or contains(groups, chemPrototype.group) then
out = out .. fillChemistryRow(chemPrototype.id, frame)
end
end
return out
end
 
p.fillContentOfDispencer = function(frame)
local dispencer = frame.args.dispencer
local out = ""
for _, chemId in pairs(dispencersContent()[dispencer]) do
out = out .. frame:expandTemplate{ title = "Кнопка_реагента", args = {chemId, "y"}}
end
return out
end
 
function dispencersContent()
local content = {
["Химикатов"]={
"Aluminium","Carbon","Chlorine","Copper","Ethanol","Fluorine","Hydrogen","Iodine",
"Iron","Lithium","Mercury","Nitrogen","Oxygen","Phosphorus","Potassium","Radium",
"Silicon","Sodium","Sugar","Sulfur","SulfuricAcid"
},
["Соды"]={
"Water","Ice","Coffee","Cream","Tea","GreenTea","IcedTea","IcedGreenTea",
"Cola","SpaceMountainWind","DrGibb","SpaceUp","TonicWater","SodaWater","LemonLime","Sugar",
"JuiceOrange","JuiceLime","JuiceWatermelon"
},
["Алкоголя"]={
"LemonLime","Sugar","JuiceOrange","JuiceLime","SodaWater","TonicWater","Beer","CoffeeLiqueur",
"Whiskey","Wine","Vodka","Gin","Rum","Tequila","Vermouth","Cognac",
"Ale","Mead"
}
}
return content
end
 
 
p.readArg = function(frame)
return mw.text.nowiki(p.chem[frame.args[1]][frame.args[2]])
end
 
 
function contains(list, x)
for _, v in ipairs(list) do
if v == x then return true end
end
return false
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
templateArgs.id = chemPrototype.id
local reactPrototype = p.react[reactId]
templateArgs.name = chemPrototype.name
local reactants = {}
templateArgs.description = chemPrototype.desc .. " На вид " .. chemPrototype.physicalDesc .. "."
local reactantTemplate = "%s [[#chem_%s|%s]]"
templateArgs.recipes_count = tablelength(chemPrototype.recipes)
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
local firstReact = true
for _, reactId in pairs(chemPrototype.recipes) do
local reactPrototype = p.react[reactId]
local reactantChemData = p.chem[reactantId]
local reactants = {}
local reactantText = string.format(reactantTemplate, reactantValue.amount, reactantChemData.id,  reactantChemData.name)
for reactantId, reactantValue in pairs(reactPrototype.reactants) do
if reactantValue.catalyst then
reactantText = reactantText .. " (катализатор)"
local reactantChemData = p.chem[reactantId]
local reactantText = reactantValue.amount .. " " .. reactantChemData.name
table.insert(reactants, reactantText)
end
end
templateArgs.reactants = table.concat(reactants, "<br>")
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 products = {}
local productChemData = p.chem[productId]
for productId, productAmount in pairs(reactPrototype.products) do
local productText = string.format(productTemplate, productAmount, productChemData.id,  productChemData.name)
table.insert(products, productText)
local productChemData = p.chem[productId]
end
local productText = productAmount .. " " .. productChemData.name
-- Эффекты реакции
table.insert(products, productText)
if tablelength(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>")
end
templateArgs.products = table.concat(products, "<br>")
templateArgs.action = "смешать"
templateArgs.actions = getActions(reactPrototype)
local template = "Строка_химического_вещества"
if firstReact then
local template = "Строка_химического_вещества"
template = "Первая_строка_химического_вещества"
if firstReact then
firstReact = false
template = "Первая_строка_химического_вещества"
end
firstReact = false
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
out = out .. frame:expandTemplate{ title = template, args = templateArgs}
end
end
end
end
return out
return out
end
end


p.fillEffectsTable = function(frame)
getActions = function(reactPrototype)
local actions = {}
local chemPrototypeId = frame.args.id
-- Температура
 
local minMaxTemplate = "выше %sК и ниже %sК"
local out = "{|" -- Объявление таблицы (Условие | Эффект)
local minTemplate = "выше %sК"
local rowTemplate = "|-\n| %s || %s" -- Шаблон строки таблицы
local maxTemplate = "ниже %"
local effects = getEffects(chemPrototypeId)
local hasMin = reactPrototype.minTemp ~= 0
local hasMax = reactPrototype.hasMax
for _, effect in pairs(effects) do
local tempString = ""
out = attachAsNewLine(out, string.format(rowTemplate, effect.condition, effect.effect))
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
end
out = attachAsNewLine(out, "|}") -- Конец таблицы
for _, mixingCategory in pairs(reactPrototype.mixingCategories) do
mw.log(out)
local image = getMixingImage(mixingCategory.id)
return out
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
 
getMixingImage = function(mixingId)
local images = {
["DummyMix"] = "Beaker.png", -- Смешивание
["DummyGrind"] = "Blender.png", -- Измельчение
["DummyJuice"] = "Blender.png", -- Выжимка
["DummyCondense"] = "Конденсатор газа.png",
["Centrifuge"] = "Центрифуга.png",
["Electrolysis"] = "Электролизер.png",
["Holy"] = "Библия.png"
}
return images[mixingId]
end
end
Строка 81: Строка 249:
function getEffects(chemPrototypeId)
function getEffects(chemPrototypeId)
local effects = {}
local effects = ""
local effect = {}
effect.condition = "Быть в баньке"
local chemPrototype = p.chem[chemPrototypeId]
effect.effect = "+9 к хилке"
if chemPrototype.metabolisms == nil then
table.insert(effects, effect)
return effects
end
for metabolismGroupKey, metabolismGroup in pairs(chemPrototype.metabolisms) do
effects = attachAsNewLine(effects, string.format("* %s (%s единиц в секунду)", metabolismGroupKey, metabolismGroup.rate))
for _, effect in pairs(metabolismGroup.effects) do
if effect.description ~= "" then
effects = attachAsNewLine(effects, "** "..effect.description)
end
end
end
local effect = {}
effect.condition = "Счастье"
effect.effect = "+99 к недпоониманию окружающих"
table.insert(effects, effect)
return effects
return effects

Текущая версия от 13:30, 4 мая 2024

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

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

p.fillReactTable = function(frame)
	
	local out = ""
	
	for _, reactPrototype in pairs(p.react) do
		if tablelength(reactPrototype.effects) ~= 0 then
			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)
				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 = p.chem[productId]
				local productText = string.format(productTemplate, productAmount, productChemData.id,  productChemData.name)
				table.insert(products, productText)
			end
			-- Эффекты реакции
			if tablelength(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 = "Строка_реакции"
			
			out = out .. frame:expandTemplate{ title = template, args = templateArgs}
		end
	end
	return out
end

p.fillChemTable = 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

p.fillContentOfDispencer = function(frame)
	local dispencer = frame.args.dispencer
	local out = ""
	for _, chemId in pairs(dispencersContent()[dispencer]) do
		out = out .. frame:expandTemplate{ title = "Кнопка_реагента", args = {chemId, "y"}}
	end
	return out
end

function dispencersContent()
	local content = {
		["Химикатов"]={
			"Aluminium","Carbon","Chlorine","Copper","Ethanol","Fluorine","Hydrogen","Iodine",
			"Iron","Lithium","Mercury","Nitrogen","Oxygen","Phosphorus","Potassium","Radium",
			"Silicon","Sodium","Sugar","Sulfur","SulfuricAcid"
		},
		["Соды"]={
			"Water","Ice","Coffee","Cream","Tea","GreenTea","IcedTea","IcedGreenTea",
			"Cola","SpaceMountainWind","DrGibb","SpaceUp","TonicWater","SodaWater","LemonLime","Sugar",
			"JuiceOrange","JuiceLime","JuiceWatermelon"
		},
		["Алкоголя"]={
			"LemonLime","Sugar","JuiceOrange","JuiceLime","SodaWater","TonicWater","Beer","CoffeeLiqueur",
			"Whiskey","Wine","Vodka","Gin","Rum","Tequila","Vermouth","Cognac",
			"Ale","Mead"
		}
	}
	return content
end


p.readArg = function(frame)
	return mw.text.nowiki(p.chem[frame.args[1]][frame.args[2]])
end


function contains(list, x)
	for _, v in ipairs(list) do
		if v == x then return true end
	end
	return false
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)
				if reactantValue.catalyst then
					reactantText = reactantText .. " (катализатор)"
				end
				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
			-- Эффекты реакции
			if tablelength(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
			
			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|link=]]", image)) -- Картинка
		end
		
		table.insert(actions, mixingCategory.name .. " " .. tempString) -- Название
		
	end
	
	return table.concat(actions, "<br>")
	
end

getMixingImage = function(mixingId)
	
	local images = {
		["DummyMix"] = "Beaker.png", -- Смешивание
		["DummyGrind"] = "Blender.png", -- Измельчение
		["DummyJuice"] = "Blender.png", -- Выжимка
		["DummyCondense"] = "Конденсатор газа.png", 
		["Centrifuge"] = "Центрифуга.png",
		["Electrolysis"] = "Электролизер.png",
		["Holy"] = "Библия.png"
	}
	
	return images[mixingId]
	
end

function getEffects(chemPrototypeId)
	
	local effects = ""
	
	local chemPrototype = p.chem[chemPrototypeId]
	if chemPrototype.metabolisms == nil then
		return effects
	end
	
	for metabolismGroupKey, metabolismGroup in pairs(chemPrototype.metabolisms) do
		effects = attachAsNewLine(effects, string.format("* %s (%s единиц в секунду)", metabolismGroupKey, metabolismGroup.rate))
		for _, effect in pairs(metabolismGroup.effects) do
			if effect.description ~= "" then
				effects = attachAsNewLine(effects, "** "..effect.description)
			end
		end
	end
	
	
	return effects
  
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