Module:NpcData: Difference between revisions

Jump to navigation Jump to search
LVL (talk | contribs)
Removed localization
LVL (talk | contribs)
Added `get_nested_var` function for accessing nested data
Line 103: Line 103:
      
      
return ability_key
return ability_key
end
-- Retrieves a nested value using dot notation (e.g., "Shield.BaseAbsorptionPerSecond")
--{{#invoke:NpcData|get_nested_var|NPC_KEY|PATH|SIG_FIGS}}--
p.get_nested_var = function(frame)
local args = frame.args
local npc_name = args[1] and mw.text.trim(args[1]) or nil
local path = args[2] and mw.text.trim(args[2]) or nil
local sig_figs = args[3] and mw.text.trim(args[3]) or nil
if not npc_name or npc_name == '' then
return '<span class="error">Error: NPC name not provided.</span>'
end
if not path or path == '' then
return '<span class="error">Error: Path not provided.</span>'
end
local npc = npcs_data[npc_name]
if not npc then
return '<span class="error">Error: NPC "' .. npc_name .. '" not found.</span>'
end
-- Traverse nested structure
local value = npc
for key in string.gmatch(path, '([^.]+)') do
if type(value) ~= "table" then
return '<span class="error">Error: Path "' .. path .. '" is invalid.</span>'
end
value = value[key]
if value == nil then
return '<span class="error">Error: Key "' .. key .. '" not found in path.</span>'
end
end
-- Apply rounding if needed
if sig_figs and tonumber(sig_figs) then
value = util_module.round_to_sig_fig(value, sig_figs)
if value == nil then
return '<span class="error">Error: Rounding failed for value.</span>'
end
end
return value
end
end


return p
return p