Модуль:TableOfChemicals

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

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

p = {}

local prototypes = mw.loadData("Module:Chemistry Lookup/data")
p.chem = prototypes.chem
p.react = prototypes.react

p.test = function()
	
	mw.log(tablelength(p.chem["Bicaridine"].recipes))
	
end

p.fillTable = function(frame)
	
	local out = ""
	local group = frame.args.group
	local templateArgs = {}
	for _, chemPrototype in pairs(p.chem) do
		
		if group == nil or group == "all" or chemPrototype.group == group then
			
			templateArgs.id = chemPrototype.id
			templateArgs.name = chemPrototype.name
			templateArgs.description = chemPrototype.desc .. " На вид " .. chemPrototype.physicalDesc .. "."
			
			templateArgs.recipes_count = tablelength(chemPrototype.recipes)
			
			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 reactantTemplateWithLink = "%s [[#%s|%s]]"
					local reactantTemplate = "%s %s"
					for reactantId, reactantValue in pairs(reactPrototype.reactants) do
						
						local reactantChemData = p.chem[reactantId]
						local reactantText 
						if makeALink(group, reactantChemData.id) then
							reactantText = string.format(reactantTemplateWithLink, reactantValue.amount, reactantChemData.id,  reactantChemData.name)
						else
							reactantText = string.format(reactantTemplate, reactantValue.amount,  reactantChemData.name)
						end
						table.insert(reactants, reactantText)
					end
					templateArgs.reactants = table.concat(reactants, "<br>")
					
					local products = {}
					for productId, productAmount in pairs(reactPrototype.products) do
						
						local productChemData = p.chem[productId]
						local productText = productAmount .. " " .. productChemData.name 
						table.insert(products, productText)
					end
					templateArgs.products = table.concat(products, "<br>")
					
					templateArgs.action = "смешать"
					
					local template = "Строка_химического_вещества"
					if firstReact then
						template = "Первая_строка_химического_вещества"
						firstReact = false
					end
					
					out = out .. frame:expandTemplate{ title = template, args = templateArgs}
				end
			end
			
			
			
		end
	end
	
	return out
	
end

p.fillEffectsTable = function(frame)
	
	local chemPrototypeId = frame.args.id

	local out = "{|" -- Объявление таблицы (Условие | Эффект)
	local rowTemplate = "|-\n| %s || %s" -- Шаблон строки таблицы
	
	local effects = getEffects(chemPrototypeId)
	
	for _, effect in pairs(effects) do
		out = attachAsNewLine(out, string.format(rowTemplate, effect.condition, effect.effect))
	end
	
	out = attachAsNewLine(out, "|}") -- Конец таблицы
	mw.log(out)
	return out
	
end

function makeALink(group, linkId)
	
	chemPrototype = p.chem[linkId]
	if chemPrototype == nil then -- Не на что ссылаться
		return false
	end
	
	return true
end

function getEffects(chemPrototypeId)
	
	local chemPrototype = p.chem[chemPrototypeId]
	
	local effects = {}
	local effect = {}
	effect.condition = ""
	effect.effect = p.geneffects(chemPrototypeId)
	table.insert(effects, effect)
	
	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

-- НЕ МОЕ
function p.geneffects(chemPrototypeId)
	local frame = mw.getCurrentFrame()
	local met = p.chem[chemPrototypeId].metabolisms
	if met == nil then
		return ""
	end
	local out = ""
	for k, v in pairs(met) do
		out = out .. "<b>" .. k .. "</b> (" .. v.rate .. " единиц в секунду)\n" .. p.geneffectlist(v.effects, frame, v.rate)
	end
	return out
end

function p.geneffectlist(effects, frame, rate)
	local out = ""
	for l, w in pairs(effects) do
		-- Popup Message is ignored on purpose
		if w.id == "HealthChange" then
			out = out .. ":" .. p.genhealthchange(w, rate, frame) .. "\n"
		elseif w.id == "AdjustReagent" then
			out = out .. ":" .. p.genadjustreagent(w, rate, frame) .. "\n"
		elseif w.id == "FlammableReaction" then
			out = out .. ":" .. p.genflammablereaction(w, frame) .. "\n"
		elseif w.id == "AdjustTemperature" then
			out = out .. ":" .. p.genadjusttemperature(w, frame) .. "\n"
		elseif w.id == "GenericStatusEffect" then
			out = out .. ":" .. p.gengenericstatuseffect(w, frame) .. "\n"
		elseif w.id == "ExplosionReactionEffect" then
			out = out .. ":" .. p.genexplosionreactioneffect(w, frame) .. "\n"
		elseif w.id == "FoamAreaReactionEffect" then
			out = out .. ":" .. p.genfoamareareactioneffect(w, frame) .. "\n"
		elseif w.id == "SmokeAreaReactionEffect" then
			out = out .. ":" .. p.gensmokeareareactioneffect(w, frame) .. "\n"
		elseif w.id == "ModifyBleedAmount" then
			out = out .. ":" .. p.genmodifybleedamount(w, frame) .. "\n"
		end
	end
	return out
end

function p.gensmokeareareactioneffect(r, frame)
	if r.conditions ~= nil then 
		conds = p.genconds(r.conditions, frame)
	end
	return frame:expandTemplate{ title = "SmokeAreaReactionEffect", args = { when = conds, prob = r.probability }}
end

function p.genfoamareareactioneffect(r, frame)
	if r.conditions ~= nil then 
		conds = p.genconds(r.conditions, frame)
	end
	return frame:expandTemplate{ title = "FoamAreaReactionEffect", args = { when = conds, prob = r.probability }}
end

function p.genexplosionreactioneffect(r, frame)
	if r.conditions ~= nil then 
		conds = p.genconds(r.conditions, frame)
	end
	return frame:expandTemplate{ title = "ExplosionReactionEffect", args = { when = conds, prob = r.probability }}
end

function p.gengenericstatuseffect(r, frame)
	if r.conditions ~= nil then 
		conds = p.genconds(r.conditions, frame)
	end
	return frame:expandTemplate{ title = "GenericStatusEffect", args = { key = r.Key, type = r.Type, time = r.Time, refresh = r.Refresh, when = conds, prob = r.probability }}
end

function p.genadjusttemperature(r, frame)
	if r.conditions ~= nil then 
		conds = p.genconds(r.conditions, frame)
	end
	return frame:expandTemplate{ title = "AdjustTemperature", args = { amount = r.Amount, when = conds, prob = r.probability }}
end

function p.genflammablereaction(r, frame)
	if r.conditions ~= nil then 
		conds = p.genconds(r.conditions, frame)
	end
	return frame:expandTemplate{ title = "FlammableReaction", args = { multiplier = r.Multiplier, when = conds, prob = r.probability }}
end

function p.genflammablereaction(r, frame)
	if r.conditions ~= nil then 
		conds = p.genconds(r.conditions, frame)
	end
	return frame:expandTemplate{ title = "FlammableReaction", args = { multiplier = r.Multiplier, when = conds, prob = r.probability }}
end

function p.genadjustreagent(r, rate, frame)
	if r.conditions ~= nil then 
		conds = p.genconds(r.conditions, frame)
	end
	return frame:expandTemplate{ title = "AdjustReagent", args = { amount = r.Amount, reagent = r.Reagent, when = conds, prob = r.probability }}
end

function p.genmodifybleedamount(r, frame)
	if r.conditions ~= nil then 
		conds = p.genconds(r.conditions, frame)
	end
	return frame:expandTemplate{ title = "ModifyBleedAmount", args = { amount = r.Amount, when = conds, prob = r.probability }}
end

function p.genhealthchange(h, rate, frame)
	local healst = {}
	local dealst = {}
	local r = 1.0 / rate
	if h.damage.types ~= nil then
	for k, v in pairs(h.damage.types) do
		if v < 0 then
			healst[k] = v * r
		else
			dealst[k] = v * r
		end
	end
	end
	
	if h.damage.groups ~= nil then
	for k, v in pairs(h.damage.groups) do
		if v < 0 then
			healst[k] = v * r
		else
			dealst[k] = v * r
		end
	end
	end
	
	local heals = hchangelist(healst, frame)
	local deals = hchangelist(dealst, frame)
	local conds = nil
	if h.conditions ~= nil then 
		conds = p.genconds(h.conditions, frame)
	end
	return frame:expandTemplate{ title = "HealthChange", args = { heals = heals, deals = deals, when = conds, prob = h.probability }}
end

function hchangelist(l, frame)
	out = ""
	local len = tablelength(l)
	local i = 0
	for k, v in pairs(l) do
		i = i + 1
		if len == i and i ~= 1 then
			out = out .. ", and "
		elseif i ~= 1 then
			out = out .. ", "
		end
		out = out .. hchange(k, v, frame)
	end
	return out
end

-- So we can make it fancy later
function hchange(ty, amnt, frame)
	return frame:expandTemplate{ title = "HealthModifier", args = { adj = amnt, kind = ty } }
end

function p.genconds(conds, frame)
	out = ""
	local len = tablelength(conds)
	local i = 0
	for k, v in pairs(conds) do
		i = i + 1
		if len == i and i ~= 1 then
			out = out .. ", and "
		elseif i ~= 1 then
			out = out .. ", "
		end
		out = out .. p.gencond(v, frame)
	end
	return out
end

function p.gencond(c, frame)
	if c.id == "TotalDamage" then
		return frame:expandTemplate{ title = "TotalDamage", args = { min = c.Min, max = c.Max } }
	elseif c.id == "ReagentThreshold" then
		return frame:expandTemplate{ title = "ReagentThreshold", args = { min = c.Min, max = c.Max, reagent = c.Reagent } }
	elseif c.id == "OrganType" then
		return frame:expandTemplate{ title = "OrganType", args = { shouldhave = c.ShouldHave, type = c.Type } }
	elseif c.id == "Temperature" then
		return frame:expandTemplate{ title = "Temperature", args = {  min = c.Min, max = c.Max } }
	end
	return ""
end
-- НЕ МОЕ

return p