Module:InfoboxItem: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary Tag: Reverted |
||
Line 89: | Line 89: | ||
return table.concat(out, '\n') | return table.concat(out, '\n') | ||
end | end | ||
Line 117: | Line 102: | ||
end | end | ||
local dictName = "Data/"..itemModule.."/InOutput" | local dictName = "Data/" .. itemModule .. "/InOutput" | ||
local success, recipes = pcall(mw.loadData, "Module:" .. dictName) | |||
if not success or type(recipes) ~= "table" or next(recipes) == nil then | |||
return "" | |||
end | |||
-- Берём первый рецепт из таблицы (работает и для массивов, и для словарей) | |||
local firstRecipe | |||
if recipes[1] then | |||
firstRecipe = recipes[1] | |||
else | |||
for _, v in pairs(recipes) do | |||
firstRecipe = v | |||
break | |||
end | |||
end | |||
-- Если нет корректных данных – пропускаем | |||
if | if type(firstRecipe) ~= "table" then | ||
return "" | return "" | ||
end | |||
local function renderRecipeInput(recipe) | |||
local inputs = normalizeArray(recipe.input) | |||
if #inputs == 0 then | |||
return "—" | |||
end | |||
local parts = {} | |||
for _, ing in ipairs(inputs) do | |||
table.insert(parts, | |||
string.format("[[File:T_%s.png|22px]] [[%s]] × %s", | |||
ing[1], | |||
splitCamelCase(ing[1]), | |||
tostring(ing[2]) | |||
) | |||
) | |||
end | |||
return table.concat(parts, "<br/>") | |||
end | end | ||
Line 129: | Line 148: | ||
local out = {} | local out = {} | ||
table.insert(out, '{| class="infobox"') | table.insert(out, '{| class="infobox"') | ||
table.insert(out, '|-') | table.insert(out, '|-') | ||
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. | table.insert(out, '| colspan="2" style="text-align:center;" | ' .. | ||
Line 136: | Line 153: | ||
imageName, pageName, pageName)) | imageName, pageName, pageName)) | ||
table.insert(out, '|-') | table.insert(out, '|-') | ||
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. renderRecipeInput( | table.insert(out, '! colspan="2" | Recipe input') | ||
table.insert(out, '|-') | |||
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. renderRecipeInput(firstRecipe)) | |||
table.insert(out, '|}') | table.insert(out, '|}') | ||
Revision as of 10:12, 2 August 2025
Documentation for this module may be created at Module:InfoboxItem/doc
-- Module:InfoboxItem
local p = {}
local function safeLoadData(name)
local success, data = pcall(function()
return mw.loadData('Module:Data/' .. name)
end)
if success and type(data) == "table" then
return data
end
return nil
end
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 splitCamelCase(str)
str = str:gsub("_", " ")
str = str:gsub("(%l)(%u)", "%1 %2")
str = str:gsub("(%a)(%d)", "%1 %2")
str = str:gsub("(%d)(%a)", "%1 %2")
return str
end
function p.show_inline(frame)
local itemModule = frame.args[1]
if not itemModule or itemModule == "" then
return "❌ No data module provided"
end
local data = safeLoadData(itemModule)
if not data then
return ""
end
local pageName = splitCamelCase(itemModule)
local imageName = (data.Image or "") .. ".png"
return string.format("[[File:%s|32px|link=%s]] [[%s]]",
imageName, pageName, pageName);
end
function p.show(frame)
local itemModule = frame.args[1]
if not itemModule or itemModule == "" then
return "❌ No data module provided"
end
local data = safeLoadData(itemModule)
if not data then
return ""
end
local pageName = splitCamelCase(itemModule)
local imageName = (data.Image or "") .. ".png"
local out = {}
table.insert(out, '{| class="infobox"')
-- Первая строка: объединённая ячейка, иконка + название со ссылками
table.insert(out, '|-')
table.insert(out, '| colspan="2" style="text-align:center;" | ' ..
string.format('[[File:%s|64px|link=%s]] [[%s]]',
imageName, pageName, pageName))
-- Остальные поля
table.insert(out, '|-')
table.insert(out, '! Category')
table.insert(out, '| ' .. (data.Category or ''))
table.insert(out, '|-')
table.insert(out, '! Tier')
table.insert(out, '| ' .. (data.Tier or ''))
table.insert(out, '|-')
table.insert(out, '! Stack size')
table.insert(out, '| ' .. (data.StackSize or ''))
table.insert(out, '|}')
return table.concat(out, '\n')
end
function p.show_recipe(frame)
local itemModule = frame.args[1]
if not itemModule or itemModule == "" then
return "❌ No data module provided"
end
local data = safeLoadData(itemModule)
if not data then
return ""
end
local dictName = "Data/" .. itemModule .. "/InOutput"
local success, recipes = pcall(mw.loadData, "Module:" .. dictName)
if not success or type(recipes) ~= "table" or next(recipes) == nil then
return ""
end
-- Берём первый рецепт из таблицы (работает и для массивов, и для словарей)
local firstRecipe
if recipes[1] then
firstRecipe = recipes[1]
else
for _, v in pairs(recipes) do
firstRecipe = v
break
end
end
-- Если нет корректных данных – пропускаем
if type(firstRecipe) ~= "table" then
return ""
end
local function renderRecipeInput(recipe)
local inputs = normalizeArray(recipe.input)
if #inputs == 0 then
return "—"
end
local parts = {}
for _, ing in ipairs(inputs) do
table.insert(parts,
string.format("[[File:T_%s.png|22px]] [[%s]] × %s",
ing[1],
splitCamelCase(ing[1]),
tostring(ing[2])
)
)
end
return table.concat(parts, "<br/>")
end
local pageName = splitCamelCase(itemModule)
local imageName = (data.Image or "") .. ".png"
local out = {}
table.insert(out, '{| class="infobox"')
table.insert(out, '|-')
table.insert(out, '| colspan="2" style="text-align:center;" | ' ..
string.format('[[File:%s|64px|link=%s]] [[%s]]',
imageName, pageName, pageName))
table.insert(out, '|-')
table.insert(out, '! colspan="2" | Recipe input')
table.insert(out, '|-')
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. renderRecipeInput(firstRecipe))
table.insert(out, '|}')
return table.concat(out, '\n')
end
return p