Module:NpcData: Difference between revisions

Jump to navigation Jump to search
LVL (talk | contribs)
mNo edit summary
LVL (talk | contribs)
mNo edit summary
Line 17: Line 17:
--{{#invoke:NpcData|get_npc_var|NPC_KEY|STAT_KEY_1|STAT_KEY_2|...|SIG_FIGS}}--
--{{#invoke:NpcData|get_npc_var|NPC_KEY|STAT_KEY_1|STAT_KEY_2|...|SIG_FIGS}}--
p.get_npc_var = function(frame)
p.get_npc_var = function(frame)
-- Use pcall to handle both template and module calls gracefully
    -- Safer argument handling for both live and debug environments
local parent_args = frame:getParent() and frame:getParent().args or {}
    local args = {}
local direct_args = frame.args
    if frame.getParent then
local args = {}
        local parent_args = frame:getParent().args
-- Merge args, giving direct #invoke args priority
        for k, v in pairs(parent_args) do args[k] = v end
for k, v in pairs(parent_args) do args[k] = v end
    end
for k, v in pairs(direct_args) do args[k] = v end
    -- Direct args from #invoke or the debug table overwrite parent args
    for k, v in pairs(frame.args) do args[k] = v end


local npc_name = args[1] and mw.text.trim(args[1]) or nil
local npc_name = args[1] and mw.text.trim(args[1]) or nil
Line 43: Line 44:
if not key or key == '' then break end
if not key or key == '' then break end


-- Check if the current argument is the rounding value
if tonumber(key) and type(current_value) == "number" then
if tonumber(key) and type(current_value) == "number" then
break
break
Line 52: Line 52:
end
end
current_value = current_value[key]
local next_value = current_value[key]
if next_value == nil then
local num_key = tonumber(key)
if num_key then
next_value = current_value[num_key]
end
end
current_value = next_value
 
if current_value == nil then
if current_value == nil then
return '<span class="error">Error: Key "' .. key .. '" not found.</span>'
return '<span class="error">Error: Key "' .. key .. '" not found.</span>'
Line 59: Line 68:
end
end


-- Determine if a rounding value was provided
local sig_figs_arg = args[last_key_index + 1]
local sig_figs_arg = args[last_key_index + 1]
local sig_figs = sig_figs_arg and tonumber(mw.text.trim(sig_figs_arg)) or nil
local sig_figs = sig_figs_arg and tonumber(mw.text.trim(sig_figs_arg)) or nil