|
|
| Line 14: |
Line 14: |
| end | | end |
|
| |
|
| -- Retrieves a specific stat value for a given NPC.
| |
| -- Retrieves a specific stat value for a given NPC. Handles both simple and nested calls. | | -- 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}}-- | | --{{#invoke:NpcData|get_npc_var|NPC_KEY|STAT_KEY_1|STAT_KEY_2|...|SIG_FIGS}}-- |
| Line 98: |
Line 97: |
| | | |
| return element | | return element |
| 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
| |
| -- try first with the raw key, then with a number conversion
| |
| local next = value[key] or value[tonumber(key)] or value[tostring(key)]
| |
| if next == nil then
| |
| return '<span class="error">Error: Key "' .. key .. '" not found in path.</span>'
| |
| end
| |
| value = next
| |
| 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 |