Module:NpcData: Difference between revisions

Jump to navigation Jump to search
LVL (talk | contribs)
Removed obsolete get_ability_key; get_nested_var covers BoundAbilities access
LVL (talk | contribs)
fix nested calls
Line 15: Line 15:


-- Retrieves a specific stat value for a given NPC.
-- Retrieves a specific stat value for a given NPC.
--{{#invoke:NpcData|get_npc_var|NPC_KEY|STAT_KEY|SIG_FIGS}}--
-- Retrieves a specific stat value for a given NPC. Handles both simple and nested calls.
--{{#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)
-- Get arguments from the #invoke call and trim whitespace
-- Use pcall to handle both template and module calls gracefully
local args = frame.args
local parent_args = frame:getParent() and frame:getParent().args or {}
local direct_args = frame.args
local 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(direct_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
local npc_stat_key = args[2] and mw.text.trim(args[2]) or nil
local sig_figs = args[3] and mw.text.trim(args[3]) or nil
-- Validate required arguments
if not npc_name or npc_name == '' then
if not npc_name or npc_name == '' then
return '<span class="error">Error: NPC name not provided.</span>'
return '<span class="error">Error: NPC name not provided.</span>'
end
if not npc_stat_key or npc_stat_key == '' then
return '<span class="error">Error: Stat key not provided.</span>'
end
end


-- Retrieve the NPC data table
local npc = npcs_data[npc_name]
local npc = npcs_data[npc_name]
if not npc then
if not npc then
Line 37: Line 36:
end
end


-- Get the specific stat value
local current_value = npc
local var_value = npc[npc_stat_key]
local last_key_index = 1
if var_value == nil then
 
return '<span class="error">Error: Stat "' .. npc_stat_key .. '" not found for NPC "' .. npc_name .. '".</span>'
-- Traverse through the arguments to find the nested value
for i = 2, #args do
local key = args[i] and mw.text.trim(args[i]) or nil
if not key or key == '' then break end
 
-- Stop traversing if the next key looks like a number for rounding
if tonumber(key) then
break
end
 
if type(current_value) ~= "table" then
return '<span class="error">Error: Invalid path. Tried to index a non-table value.</span>'
end
current_value = current_value[key]
if current_value == nil then
return '<span class="error">Error: Key "' .. key .. '" not found.</span>'
end
last_key_index = i
end
end


-- round if a number is provided
-- Determine if a rounding value was provided
if sig_figs and tonumber(sig_figs) then
local sig_figs_arg = args[last_key_index + 1]
var_value = util_module.round_to_sig_fig(var_value, sig_figs)
local sig_figs = sig_figs_arg and tonumber(mw.text.trim(sig_figs_arg)) or nil
if var_value == nil then
if sig_figs then
current_value = util_module.round_to_sig_fig(current_value, sig_figs)
if current_value == nil then
return '<span class="error">Error: Rounding failed for value.</span>'
return '<span class="error">Error: Rounding failed for value.</span>'
end
end
end
end


return var_value
return current_value
end
end