Module:InfoboxItem
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 _, 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
if name and name ~= "" then
table.insert(result, {name, count})
end
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 = recipes[1]
if not firstRecipe then
for _, v in pairs(recipes) do
firstRecipe = v
break
end
end
if type(firstRecipe) ~= "table" then
return ""
end
local inputs = normalizeArray(firstRecipe.input)
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')
if #inputs == 0 then
table.insert(out, '|-')
table.insert(out, '| colspan="2" style="text-align:center;" | —')
else
for _, ing in ipairs(inputs) do
table.insert(out, '|-')
table.insert(out, string.format('| [[File:T_%s.png|22px]] || [[%s]] × %s',
ing[1],
splitCamelCase(ing[1]),
tostring(ing[2])
))
end
end
table.insert(out, '|}')
return table.concat(out, '\n')
end
return p