Module:RecipeLoader: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
Line 23: | Line 23: | ||
local tableNode = mw.html.create("table") | local tableNode = mw.html.create("table") | ||
:addClass("wikitable recipe-table") | :addClass("wikitable recipe-table") | ||
:css("text-align", " | :css("margin", "5px 0") | ||
:css(" | :css("border-collapse", "collapse") | ||
:css(" | :css("font-size", "90%") | ||
:css("width", "280px") | |||
local function sectionHeader(text) | |||
tableNode:tag("tr") | |||
:tag("th"):attr("colspan", 3) | |||
:css("background-color", "#ececec") | |||
:css("text-align", "center") | |||
:css("padding", "3px") | |||
:wikitext(text) | |||
end | |||
local function addRow(name, count) | |||
local tr = tableNode:tag("tr") | |||
tr:tag("td") | |||
:css("width", "36px") | |||
:css("text-align", "center") | |||
:wikitext(string.format("[[File:T_%s.png|28px]]", name)) | |||
tr:tag("td") | |||
:css("padding-left", "5px") | |||
:wikitext(string.format("[[%s]]", name)) | |||
tr:tag("td") | |||
:css("width", "30px") | |||
:css("text-align", "center") | |||
:wikitext("× " .. tostring(count)) | |||
end | |||
-- Input | -- Input | ||
sectionHeader("Input") | |||
if #inputs > 0 then | if #inputs > 0 then | ||
for _, ing in ipairs(inputs) do | for _, ing in ipairs(inputs) do | ||
addRow(ing[1], ing[2]) | |||
end | end | ||
else | else | ||
tableNode:tag("tr"):tag("td"):attr("colspan", 3):wikitext("—") | tableNode:tag("tr"):tag("td"):attr("colspan", 3):css("text-align", "center"):wikitext("—") | ||
end | end | ||
-- Output | -- Output | ||
sectionHeader("Output") | |||
if #outputs > 0 then | if #outputs > 0 then | ||
for _, res in ipairs(outputs) do | for _, res in ipairs(outputs) do | ||
addRow(res[1], res[2]) | |||
end | end | ||
else | else | ||
tableNode:tag("tr"):tag("td"):attr("colspan", 3):wikitext("—") | tableNode:tag("tr"):tag("td"):attr("colspan", 3):css("text-align", "center"):wikitext("—") | ||
end | end | ||
-- Time | -- Time | ||
tableNode:tag("tr"):tag(" | sectionHeader("Time") | ||
tableNode:tag("tr") | |||
:tag("td"):attr("colspan", 3) | |||
:css("text-align", "center") | |||
:css("padding", "4px") | |||
:wikitext(tostring(ticks) .. " ticks") | |||
return tableNode | return tableNode |
Revision as of 12:49, 31 July 2025
Documentation for this module may be created at Module:RecipeLoader/doc
local p = {}
local function normalizeArray(tbl)
local result = {}
if not tbl then return result end
for k, v in pairs(tbl) do
-- Каждая строка тоже может быть ассоциативной таблицей
if type(v) == "table" then
local name = v[1] or v.name or v["1"]
local count = v[2] or v.count or v["2"] or 1
table.insert(result, {name, count})
end
end
table.sort(result, function(a,b) return tostring(a[1]) < tostring(b[1]) end)
return result
end
local function renderRecipe(recipe)
local inputs = normalizeArray(recipe.input)
local outputs = normalizeArray(recipe.output)
local ticks = recipe.ticks or recipe.time or ""
local tableNode = mw.html.create("table")
:addClass("wikitable recipe-table")
:css("margin", "5px 0")
:css("border-collapse", "collapse")
:css("font-size", "90%")
:css("width", "280px")
local function sectionHeader(text)
tableNode:tag("tr")
:tag("th"):attr("colspan", 3)
:css("background-color", "#ececec")
:css("text-align", "center")
:css("padding", "3px")
:wikitext(text)
end
local function addRow(name, count)
local tr = tableNode:tag("tr")
tr:tag("td")
:css("width", "36px")
:css("text-align", "center")
:wikitext(string.format("[[File:T_%s.png|28px]]", name))
tr:tag("td")
:css("padding-left", "5px")
:wikitext(string.format("[[%s]]", name))
tr:tag("td")
:css("width", "30px")
:css("text-align", "center")
:wikitext("× " .. tostring(count))
end
-- Input
sectionHeader("Input")
if #inputs > 0 then
for _, ing in ipairs(inputs) do
addRow(ing[1], ing[2])
end
else
tableNode:tag("tr"):tag("td"):attr("colspan", 3):css("text-align", "center"):wikitext("—")
end
-- Output
sectionHeader("Output")
if #outputs > 0 then
for _, res in ipairs(outputs) do
addRow(res[1], res[2])
end
else
tableNode:tag("tr"):tag("td"):attr("colspan", 3):css("text-align", "center"):wikitext("—")
end
-- Time
sectionHeader("Time")
tableNode:tag("tr")
:tag("td"):attr("colspan", 3)
:css("text-align", "center")
:css("padding", "4px")
:wikitext(tostring(ticks) .. " ticks")
return tableNode
end
-- Вывод одного конкретного рецепта
function p.get(frame)
local dictName = frame.args[1]
local recipeName = frame.args[2]
if not dictName or not recipeName then
return "❌ Missing parameters: {{Recipe|DictionaryName|RecipeName}}"
end
local success, recipes = pcall(mw.loadData, "Module:" .. dictName)
if not success or type(recipes) ~= "table" then
return "❌ Recipe dictionary not found: " .. tostring(dictName)
end
local html = mw.html.create()
for _, recipe in ipairs(recipes) do
if recipe.name == recipeName then
html:node(renderRecipe(recipe))
end
end
if tostring(html) == "" then
return "⚠️ Recipe not found: " .. tostring(recipeName)
end
return tostring(html)
end
-- Вывод всех рецептов из словаря
function p.getAll(frame)
local dictName = frame.args[1]
if not dictName then
return "❌ Missing parameter: {{AllRecipes|DictionaryName}}"
end
local success, recipes = pcall(mw.loadData, "Module:" .. dictName)
if not success or type(recipes) ~= "table" then
return "❌ Recipe dictionary not found: " .. tostring(dictName)
end
local html = mw.html.create()
for _, recipe in ipairs(recipes) do
html:tag("h4"):wikitext(recipe.name or "(no name)"):done()
html:node(renderRecipe(recipe))
end
return tostring(html)
end
return p