Module:NpcData: Difference between revisions

Jump to navigation Jump to search
LVL (talk | contribs)
mNo edit summary
LVL (talk | contribs)
Update to handle refactored NPC data structure
Line 4: Line 4:
local npcs_data = mw.loadJsonData("Data:NpcData.json")
local npcs_data = mw.loadJsonData("Data:NpcData.json")
local util_module = require('Module:Utilities')
local util_module = require('Module:Utilities')
local lang_module = require('Module:Lang')


-- 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
Line 15: Line 14:


-- 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}}
p.get_npc_var = function(frame)
p.get_npc_var = function(frame)
     -- Safer argument handling for both live and debug environments
     -- Safer argument handling for both live and debug environments
Line 38: Line 37:
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


-- Traverse through the arguments to find the nested value
-- Traverse through the arguments to find the nested value
Line 44: Line 44:
if not key or key == '' then break end
if not key or key == '' then break end


if tonumber(key) and type(current_value) == "number" then
-- Check if this might be the sig_figs argument (numeric on a non-table)
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. Tried to index a non-table 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
local next_value = current_value[key]
local next_value = current_value[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)
Line 58: Line 62:
next_value = current_value[num_key]
next_value = current_value[num_key]
end
end
end
if next_value == nil then
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)
if current_value == nil then
return '<span class="error">Error: Key "' .. key .. '" not found.</span>'
end
last_key_index = i
last_key_index = i
end
end


-- 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
return '<span class="error">Error: Cannot apply sig_figs to non-numeric value at "' .. table.concat(path, '.') .. '" (type: ' .. type(current_value) .. ').</span>'
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.</span>'
return '<span class="error">Error: Rounding failed for value at "' .. table.concat(path, '.') .. '".</span>'
end
end
end
-- Convert tables/booleans to strings for display
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>'
elseif type(current_value) == "boolean" then
return tostring(current_value)
end
end


Line 81: Line 97:
end
end


-- Retrieves an element from a list (array) within an NPC's data.
-- Legacy function for backwards compatibility
--{{#invoke:NpcData|get_list_elem|NPC_KEY|LIST_KEY|INDEX}}--
-- Now just redirects to get_npc_var since it handles numeric indices
p.get_list_elem = function(frame)
p.get_list_elem = function(frame)
local npc_name = frame.args[1] and mw.text.trim(frame.args[1]) or nil
return p.get_npc_var(frame)
local var = frame.args[2] and mw.text.trim(frame.args[2]) or nil
local number_str = frame.args[3] and mw.text.trim(frame.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 var or var == '' then return '<span class="error">Error: List variable name not provided.</span>' end
if not number_str or number_str == '' then return '<span class="error">Error: Index not provided.</span>' end
local number_int = tonumber(number_str)
if not number_int then return '<span class="error">Error: Invalid index provided. Must be a number.</span>' end
 
local npc = npcs_data[npc_name]
if not npc then return '<span class="error">Error: NPC "' .. npc_name .. '" not found.</span>' end
local list = npc[var]
if list == nil or type(list) ~= "table" then return "" end
local element = list[number_int]
if element == nil then return "" end
return element
end
end


return p
return p