Module:ItemData: Difference between revisions

Jump to navigation Jump to search
added get_prop_meta
LVL (talk | contribs)
Add get_builds_into functions to lookup which items use the current item as a component (reverses the Components array).
Line 451: Line 451:


return meta
return meta
end
-- Builds Into functions
-- Helper: accepts item key or display name, returns the key used in data table
local function get_codename_from_input(input)
if data[input] then return input end
for k, v in pairs(data) do
if v["Name"] == input and (v["IsDisabled"] == false or v["IsDisabled"] == nil) then
return k
end
end
for k, v in pairs(data) do
if v["Name"] == input then return k end
end
return nil
end
-- Core logic: returns table of keys that have this item in their Components
local function get_component_of(item_input)
local target_key = get_codename_from_input(item_input)
if not target_key then return {} end
local parents = {}
for key, item in pairs(data) do
if item.Components and type(item.Components) == "table" then
for _, comp in ipairs(item.Components) do
if comp == target_key then
table.insert(parents, key)
break
end
end
end
end
table.sort(parents)
return parents
end
-- Template: {{#invoke:ItemData|get_builds_into_count|ITEM_NAME}}
p.get_builds_into_count = function(frame)
local item = frame.args[1]
return #get_component_of(item)
end
-- Template: {{#invoke:ItemData|get_builds_into_name|ITEM_NAME|INDEX}}
p.get_builds_into_name = function(frame)
local item = frame.args[1]
local idx = tonumber(frame.args[2]) or 1
local parents = get_component_of(item)
local key = parents[idx]
return key and data[key] and data[key]["Name"] or ""
end
-- Template: {{#invoke:ItemData|get_builds_into|ITEM_NAME}}
-- Returns: {{ItemIcon|Parent1}}, {{ItemIcon|Parent2}}, and {{ItemIcon|Parent3}}
p.get_builds_into = function(frame)
local item = frame.args[1]
local parents = get_component_of(item)
if #parents == 0 then return "" end
local parts = {}
for _, key in ipairs(parents) do
local name = data[key] and data[key]["Name"] or key
table.insert(parts, string.format("{{ItemIcon|%s}}", name))
end
if #parts == 1 then return parts[1] end
if #parts == 2 then return parts[1] .. " and " .. parts[2] end
local last = table.remove(parts)
return table.concat(parts, ", ") .. ", and " .. last
end
end


return p
return p