Модуль:Prototypes/Химия/Растения: различия между версиями
Pok (обсуждение | вклад) мНет описания правки |
Pok (обсуждение | вклад) Нет описания правки |
||
| Строка 1: | Строка 1: | ||
local p = {} | local p = {} | ||
local plantData = mw.loadData(" | -- Загрузка данных | ||
local chemData = mw.loadData(" | 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 function formatEffect(effect) | |||
local rawType = effect['!type'] | local rawType = effect['!type'] | ||
if not rawType then return "<li>Ошибка: не найден тип эффекта.</li>" end | if not rawType then | ||
return "<li>Ошибка: не найден тип эффекта.</li>" | |||
end | |||
local effectType = rawType:gsub("%s*%b{}", "") | local effectType = rawType:gsub("%s*%b{}", "") | ||
-- Форматируем значение, если есть | |||
local | 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 | end | ||
-- Вызываем шаблон для основного описания | |||
local tpl = string.format("{{Prototypes/Химия/Растения/base|%s|%s}}", effectType, formattedAmount) | |||
local description = frame:preprocess(tpl) | |||
-- Обрабатываем вероятность | |||
if effect.probability and effect.probability < 1 then | if effect.probability and effect.probability < 1 then | ||
local pct = tostring(effect.probability * 100) | local pct = tostring(effect.probability * 100) | ||
| Строка 61: | Строка 35: | ||
end | end | ||
-- Основная функция | -- Основная функция | ||
function p.reagentRow(frame) | function p.reagentRow(frame) | ||
local args | local args = frame.args | ||
local reagentId | local reagentId = args.id or "" | ||
if reagentId == "" then | if reagentId == "" then | ||
return [[<tr><td colspan="2">'''Ошибка: не указан ID реагента.'''</td></tr>]] | return [[<tr><td colspan="2">'''Ошибка: не указан ID реагента.'''</td></tr>]] | ||
end | end | ||
-- Поиск записей в данных | |||
local reagentEntry | local reagentEntry | ||
for _, r in ipairs(plantData) do | for _, r in ipairs(plantData) do | ||
if r.id == reagentId then reagentEntry = r; break end | if r.id == reagentId then reagentEntry = r; break end | ||
end | end | ||
local chemEntry = chemData[reagentId] | |||
local | local reagentName = chemEntry and chemEntry.name or reagentId | ||
local reagentCell = string.format("! [[Химия#chem_%s|%s]]", reagentId, reagentName) | local reagentCell = string.format("! [[Химия#chem_%s|%s]]", reagentId, reagentName) | ||
local fragments = {} | local fragments = {} | ||
if reagentEntry and type(reagentEntry.plantMetabolism) == 'table' then | if reagentEntry and type(reagentEntry.plantMetabolism) == 'table' then | ||
for _, eff in ipairs(reagentEntry.plantMetabolism) do | for _, eff in ipairs(reagentEntry.plantMetabolism) do | ||
table.insert(fragments, formatEffect(eff)) | table.insert(fragments, formatEffect(eff, frame)) | ||
end | end | ||
end | end | ||
if args.manualEffects and args.manualEffects ~= "" then | if args.manualEffects and args.manualEffects ~= "" then | ||
table.insert(fragments, args.manualEffects) | table.insert(fragments, args.manualEffects) | ||
| Строка 100: | Строка 68: | ||
end | end | ||
local effectsCell = string.format("| <ul>%s</ul>", table.concat(fragments)) | |||
local effectsCell = string.format("| <ul>%s</ul>", | |||
return frame:preprocess(string.format("|-\n%s\n%s", reagentCell, effectsCell)) | return frame:preprocess(string.format("|-\n%s\n%s", reagentCell, effectsCell)) | ||
end | end | ||
return p | return p | ||