Модуль:Prototypes/Химия/Растения

Материал из Space Station 14 Вики
Версия от 18:24, 5 августа 2025; Pok (обсуждение | вклад)

(разн.) ← Предыдущая версия | Подтверждённая версия (разн.) | Текущая версия (разн.) | Следующая версия → (разн.)

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

local p = {}

-- Загрузка данных
local plantData = mw.loadData("Module:IanComradeBot/prototypes/chem/plant.json/data")
local chemData  = mw.loadData("Module:IanComradeBot/chem prototypes.json/data")

-- Вспомогательная функция
local function formatEffect(effect, frame)
	local rawType = effect['!type']
	if not rawType then
		return "<li>Ошибка: не найден тип эффекта.</li>"
	end
	local effectType = rawType:gsub("%s*%b{}", "")

	-- Форматируем значение, если есть
	local formattedAmount = ""
	if effect.amount then
		local num = tonumber(effect.amount)
		local color = num and (num > 0 and "good" or "bad") or "text"
		local amt   = num and ((num > 0) and ("+" .. num) or tostring(num)) or tostring(effect.amount)
		formattedAmount = string.format("{{цвет|text|%s|c='''%s'''}}", color, amt)
	end

	-- Вызываем шаблон для основного описания
	local tpl = string.format("{{Prototypes/Химия/Растения/base|%s|%s}}", effectType, formattedAmount)
	local description = frame:preprocess(tpl)

	-- Обрабатываем вероятность
	if effect.probability and effect.probability < 1 then
		local pct = tostring(effect.probability * 100)
		description = string.format("С шансом '''%s %%''' %s", pct, description:sub(1,1):lower() .. description:sub(2))
	end

	return "<li>{{ucfirst:" .. description .. "}}</li>"
end

-- Основная функция
function p.reagentRow(frame)
	local args	  = frame.args
	local reagentId = args.id or ""
	if reagentId == "" then
		return [[<tr><td colspan="2">'''Ошибка: не указан ID реагента.'''</td></tr>]]
	end

	-- Поиск записей в данных
	local reagentEntry
	for _, r in ipairs(plantData) do
		if r.id == reagentId then reagentEntry = r; break end
	end
	local chemEntry = chemData[reagentId]

	local reagentName = chemEntry and chemEntry.name or reagentId
	local reagentCell = string.format("! [[Химия#chem_%s|%s]]", reagentId, reagentName)

	local fragments = {}
	if reagentEntry and type(reagentEntry.plantMetabolism) == 'table' then
		for _, eff in ipairs(reagentEntry.plantMetabolism) do
			table.insert(fragments, formatEffect(eff, frame))
		end
	end

	if args.manualEffects and args.manualEffects ~= "" then
		table.insert(fragments, args.manualEffects)
	end

	if reagentEntry and reagentEntry.chemicals then
		table.insert(fragments, formatChemicals(reagentEntry))
	end

	local effectsCell = string.format("| <ul>%s</ul>", table.concat(fragments))
	return frame:preprocess(string.format("|-\n%s\n%s", reagentCell, effectsCell))
end

return p