Editing Module:NpcData

Warning: You are not logged in. Once you make an edit, a temporary account will be created for you. Learn more. Log in or create an account to continue receiving notifications after this account expires, and to access other features.
The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then publish the changes below to finish undoing the edit.
Latest revision Your text
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
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


-- Retrieves a specific stat value for a given NPC. Handles both simple and nested calls.
-- Retrieves a specific stat value for a given NPC.
-- {{#invoke:NpcData|get_npc_var|NPC_KEY|STAT_KEY_1|STAT_KEY_2|...|SIG_FIGS}}
--{{#invoke:NpcData|get_npc_var|NPC_KEY|STAT_KEY|OPTIONS}}--
p.get_npc_var = function(frame)
p.get_npc_var = function(frame)
    -- Safer argument handling for both live and debug environments
-- Get arguments from the #invoke call and trim whitespace
    local args = {}
local args = frame.args
    if frame.getParent then
local npc_name = args[1] and mw.text.trim(args[1]) or nil
        local parent_args = frame:getParent().args
local npc_stat_key = args[2] and mw.text.trim(args[2]) or nil
        for k, v in pairs(parent_args) do args[k] = v end
local sig_figs_or_localize = args[3] and mw.text.trim(args[3]) or nil
    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
-- 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
end
if not npc_stat_key or npc_stat_key == '' then
return '<span class="error">Error: Stat key not provided.</span>'
end


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


    local current_value = npc
-- Get the specific stat value
    local last_key_index = 1
local var_value = npc[npc_stat_key]
    local path = {npc_name} -- Track the path for better error messages
if var_value == nil then
return '<span class="error">Error: Stat "' .. npc_stat_key .. '" not found for NPC "' .. npc_name .. '".</span>'
end


    -- Traverse through the arguments to find the nested value
-- round
    for i = 2, #args do
if sig_figs_or_localize and tonumber(sig_figs_or_localize) then
        local key = args[i] and mw.text.trim(args[i]) or nil
var_value = util_module.round_to_sig_fig(var_value, sig_figs_or_localize)
        if not key or key == '' then break end
if var_value == nil then
return '<span class="error">Error: Rounding failed for value.</span>'
end
end


        -- Check if this might be the sig_figs argument (numeric on a non-table)
-- localize
        if tonumber(key) and type(current_value) ~= "table" then
if sig_figs_or_localize == "true" then
            break
return lang_module.get_string(var_value)
        end
end


        if type(current_value) ~= "table" then
return var_value
            return '<span class="error">Error: Invalid path at "' .. table.concat(path, '.') .. '". Tried to index a ' .. type(current_value) .. ' value.</span>'
end
        end
 
       
-- Retrieves an element from a list (array) within an NPC's data.
        -- Try string key first
--{{#invoke:NpcData|get_list_elem|NPC_KEY|LIST_KEY|INDEX|localize}}--
        local next_value = current_value[key]
p.get_list_elem = function(frame)
       
local npc_name = frame.args[1] and mw.text.trim(frame.args[1]) or nil
        -- If nil, try numeric key
local var = frame.args[2] and mw.text.trim(frame.args[2]) or nil
        if next_value == nil then
local number_str = frame.args[3] and mw.text.trim(frame.args[3]) or nil
            local num_key = tonumber(key)
local localize = frame.args[4] and mw.text.trim(frame.args[4]) or nil
            if num_key then
                next_value = current_value[num_key]
if not npc_name or npc_name == '' then return '<span class="error">Error: NPC name not provided.</span>' end
            end
if not var or var == '' then return '<span class="error">Error: List variable name not provided.</span>' end
        end
if not number_str or number_str == '' then return '<span class="error">Error: Index not provided.</span>' end
       
        if next_value == nil then
local number_int = tonumber(number_str)
            return '<span class="error">Error: Key "' .. key .. '" not found in path "' .. table.concat(path, '.') .. '".</span>'
if not number_int then return '<span class="error">Error: Invalid index provided. Must be a number.</span>' end
        end
 
       
local npc = npcs_data[npc_name]
        current_value = next_value
if not npc then return '<span class="error">Error: NPC "' .. npc_name .. '" not found.</span>' end
        table.insert(path, key)
        last_key_index = i
local list = npc[var]
    end
if list == nil or type(list) ~= "table" then return "" end
local element = list[number_int]
if element == nil then return "" end
if localize == "true" then  
element = lang_module.get_string(element)
end
return element
end
 
-- Retrieves an ability key from an NPC's "BoundAbilities" map.
--{{#invoke:NpcData|get_ability_key|NPC_KEY|SLOT_KEY}}--
p.get_ability_key = function(frame)
local npc_name = frame.args[1] and mw.text.trim(frame.args[1]) or nil
local slot_name = frame.args[2] and mw.text.trim(frame.args[2]) or nil
 
if not npc_name or npc_name == '' then return '<span class="error">Error: NPC name not provided.</span>' end
    if not slot_name or slot_name == '' then return '<span class="error">Error: Slot name not provided.</span>' end


    -- Handle sig_figs if provided
local npc_data = npcs_data[npc_name]
    local sig_figs_arg = args[last_key_index + 1]
if not npc_data then return '<span class="error">Error: NPC "'..npc_name.. '" not found.</span>' end
    local sig_figs = sig_figs_arg and tonumber(mw.text.trim(sig_figs_arg)) or nil
   
    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)
        if current_value == nil then
            return '<span class="error">Error: Rounding failed for value at "' .. table.concat(path, '.') .. '".</span>'
        end
    end


    -- Convert tables/booleans to strings for display
local bound_abilities_data = npc_data["BoundAbilities"]
    if type(current_value) == "table" then
if not bound_abilities_data or type(bound_abilities_data) ~= "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: NPC "' .. npc_name .. '" has no \'BoundAbilities\' data.</span>'
    elseif type(current_value) == "boolean" then
end
        return tostring(current_value)
    end


     return current_value
local ability_key = bound_abilities_data[slot_name]
     if not ability_key then
return '<span class="error">Error: Slot "' .. slot_name .. '" not found for NPC "' .. npc_name .. '".</span>'
end
   
return ability_key
end
end


return p
return p
Please note that all contributions to The Deadlock Wiki are considered to be released under the Creative Commons Attribution-NonCommercial-ShareAlike (see Deadlock:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!
Cancel Editing help (opens in new window)
Preview page with this template

Pages included on this page: