Module:InfoboxItem: Difference between revisions
Jump to navigation
Jump to search
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
Line 15: | Line 15: | ||
local result = {} | local result = {} | ||
if not tbl then return result end | if not tbl then return result end | ||
for | for _, v in pairs(tbl) do | ||
if type(v) == "table" then | if type(v) == "table" then | ||
local name = v[1] or v.name or v["1"] | local name = v[1] or v.name or v["1"] | ||
local count = v[2] or v.count or v["2"] or 1 | local count = v[2] or v.count or v["2"] or 1 | ||
table.insert(result, {name, count}) | if name and name ~= "" then | ||
table.insert(result, {name, count}) | |||
end | |||
end | end | ||
end | end | ||
Line 69: | Line 70: | ||
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;" | ' .. | ||
string.format('[[File:%s|64px|link=%s]] [[%s]]', | string.format('[[File:%s|64px|link=%s]] [[%s]]', imageName, pageName, pageName)) | ||
table.insert(out, '|-') | table.insert(out, '|-') | ||
table.insert(out, '! Category') | table.insert(out, '! Category') | ||
Line 87: | Line 83: | ||
table.insert(out, '| ' .. (data.StackSize or '')) | table.insert(out, '| ' .. (data.StackSize or '')) | ||
table.insert(out, '|}') | table.insert(out, '|}') | ||
return table.concat(out, '\n') | return table.concat(out, '\n') | ||
end | end | ||
Line 108: | Line 103: | ||
end | end | ||
-- Берём первый рецепт | -- Берём первый рецепт | ||
local | local firstRecipe = recipes[1] | ||
if not firstRecipe then | |||
for _, v in pairs(recipes) do | for _, v in pairs(recipes) do | ||
firstRecipe = v | firstRecipe = v | ||
Line 118: | Line 111: | ||
end | end | ||
end | end | ||
if type(firstRecipe) ~= "table" then | if type(firstRecipe) ~= "table" then | ||
return "" | return "" | ||
end | end | ||
local inputs = normalizeArray(firstRecipe.input) | |||
local pageName = splitCamelCase(itemModule) | local pageName = splitCamelCase(itemModule) | ||
Line 150: | Line 124: | ||
table.insert(out, '|-') | table.insert(out, '|-') | ||
table.insert(out, '| colspan="2" style="text-align:center;" | ' .. | table.insert(out, '| colspan="2" style="text-align:center;" | ' .. | ||
string.format('[[File:%s|64px|link=%s]] [[%s]]', | string.format('[[File:%s|64px|link=%s]] [[%s]]', imageName, pageName, pageName)) | ||
table.insert(out, '|-') | table.insert(out, '|-') | ||
table.insert(out, '! colspan="2" | Recipe input') | table.insert(out, '! colspan="2" | Recipe input') | ||
table.insert(out, '|-') | |||
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, '|}') | table.insert(out, '|}') | ||
return table.concat(out, '\n') | return table.concat(out, '\n') | ||
end | end | ||
return p | return p |
Revision as of 10:14, 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 _, 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