Module:NpcData: Difference between revisions

LVL (talk | contribs)
Update to handle refactored NPC data structure
LVL (talk | contribs)
Removed dead code
 
Line 7: Line 7:
-- returns the entire data table for a specific NPC, used by other Lua modules
-- returns the entire data table for a specific NPC, used by other Lua modules
function p.get_npc_data(name)
function p.get_npc_data(name)
if not name or name == '' then
    if not name or name == '' then
return nil
        return nil
end
    end
return npcs_data[name]
    return npcs_data[name]
end
end


Line 25: Line 25:
     for k, v in pairs(frame.args) do args[k] = v end
     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
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
    end


local npc = npcs_data[npc_name]
    local npc = npcs_data[npc_name]
if not npc then
    if not npc then
return '<span class="error">Error: NPC "' .. npc_name .. '" not found.</span>'
        return '<span class="error">Error: NPC "' .. npc_name .. '" not found.</span>'
end
    end


local current_value = npc
    local current_value = npc
local last_key_index = 1
    local last_key_index = 1
local path = {npc_name} -- Track the path for better error messages
    local path = {npc_name} -- Track the path for better error messages


-- Traverse through the arguments to find the nested value
    -- Traverse through the arguments to find the nested value
for i = 2, #args do
    for i = 2, #args do
local key = args[i] and mw.text.trim(args[i]) or nil
        local key = args[i] and mw.text.trim(args[i]) or nil
if not key or key == '' then break end
        if not key or key == '' then break end


-- Check if this might be the sig_figs argument (numeric on a non-table)
        -- Check if this might be the sig_figs argument (numeric on a non-table)
if tonumber(key) and type(current_value) ~= "table" then
        if tonumber(key) and type(current_value) ~= "table" then
break
            break
end
        end


if type(current_value) ~= "table" then
        if type(current_value) ~= "table" then
return '<span class="error">Error: Invalid path at "' .. table.concat(path, '.') .. '". Tried to index a ' .. type(current_value) .. ' value.</span>'
            return '<span class="error">Error: Invalid path at "' .. table.concat(path, '.') .. '". Tried to index a ' .. type(current_value) .. ' value.</span>'
end
        end
       
-- Try string key first
        -- Try string key first
local next_value = current_value[key]
        local next_value = current_value[key]
       
-- If nil, try numeric key
        -- If nil, try numeric key
if next_value == nil then
        if next_value == nil then
local num_key = tonumber(key)
            local num_key = tonumber(key)
if num_key then
            if num_key then
next_value = current_value[num_key]
                next_value = current_value[num_key]
end
            end
end
        end
       
if next_value == nil then
        if next_value == nil then
return '<span class="error">Error: Key "' .. key .. '" not found in path "' .. table.concat(path, '.') .. '".</span>'
            return '<span class="error">Error: Key "' .. key .. '" not found in path "' .. table.concat(path, '.') .. '".</span>'
end
        end
       
current_value = next_value
        current_value = next_value
table.insert(path, key)
        table.insert(path, key)
last_key_index = i
        last_key_index = i
end
    end


-- Handle sig_figs if provided
    -- Handle sig_figs if 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
   
if sig_figs then
    if sig_figs then
if type(current_value) ~= "number" then
        if type(current_value) ~= "number" then
return '<span class="error">Error: Cannot apply sig_figs to non-numeric value at "' .. table.concat(path, '.') .. '" (type: ' .. type(current_value) .. ').</span>'
            return '<span class="error">Error: Cannot apply sig_figs to non-numeric value at "' .. table.concat(path, '.') .. '" (type: ' .. type(current_value) .. ').</span>'
end
        end
current_value = util_module.round_to_sig_fig(current_value, sig_figs)
        current_value = util_module.round_to_sig_fig(current_value, sig_figs)
if current_value == nil then
        if current_value == nil then
return '<span class="error">Error: Rounding failed for value at "' .. table.concat(path, '.') .. '".</span>'
            return '<span class="error">Error: Rounding failed for value at "' .. table.concat(path, '.') .. '".</span>'
end
        end
end
    end


-- Convert tables/booleans to strings for display
    -- Convert tables/booleans to strings for display
if type(current_value) == "table" then
    if type(current_value) == "table" then
return '<span class="error">Error: Path "' .. table.concat(path, '.') .. '" points to a table/object. Specify a deeper path to get a value.</span>'
        return '<span class="error">Error: Path "' .. table.concat(path, '.') .. '" points to a table/object. Specify a deeper path to get a value.</span>'
elseif type(current_value) == "boolean" then
    elseif type(current_value) == "boolean" then
return tostring(current_value)
        return tostring(current_value)
end
    end
 
return current_value
end


-- Legacy function for backwards compatibility
    return current_value
-- Now just redirects to get_npc_var since it handles numeric indices
p.get_list_elem = function(frame)
return p.get_npc_var(frame)
end
end


return p
return p