Editing Module:NpcDataGive feedback
Jump to navigation
Jump to search
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 | |||
return nil | |||
end | |||
return npcs_data[name] | |||
end | end | ||
-- 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| | --{{#invoke:NpcData|get_npc_var|NPC_KEY|STAT_KEY|SIG_FIGS}}-- | ||
p.get_npc_var = function(frame) | p.get_npc_var = function(frame) | ||
-- Get arguments from the #invoke call and trim whitespace | |||
local args = frame.args | |||
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 | |||
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 | |||
-- Retrieve the NPC data table | |||
local npc = npcs_data[npc_name] | |||
if not npc then | |||
return '<span class="error">Error: NPC "' .. npc_name .. '" not found.</span>' | |||
end | |||
-- Get the specific stat value | |||
local var_value = npc[npc_stat_key] | |||
if var_value == nil then | |||
return '<span class="error">Error: Stat "' .. npc_stat_key .. '" not found for NPC "' .. npc_name .. '".</span>' | |||
end | |||
-- round if a number is provided | |||
if sig_figs and tonumber(sig_figs) then | |||
var_value = util_module.round_to_sig_fig(var_value, sig_figs) | |||
if var_value == nil then | |||
return '<span class="error">Error: Rounding failed for value.</span>' | |||
end | |||
end | |||
return var_value | |||
end | |||
-- Retrieves an element from a list (array) within an NPC's data. | |||
--{{#invoke:NpcData|get_list_elem|NPC_KEY|LIST_KEY|INDEX}}-- | |||
p.get_list_elem = function(frame) | |||
local npc_name = frame.args[1] and mw.text.trim(frame.args[1]) or nil | |||
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 | |||
-- 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 | |||
local npc_data = npcs_data[npc_name] | |||
if not npc_data then return '<span class="error">Error: NPC "'..npc_name.. '" not found.</span>' end | |||
local bound_abilities_data = npc_data["BoundAbilities"] | |||
if not bound_abilities_data or type(bound_abilities_data) ~= "table" then | |||
return '<span class="error">Error: NPC "' .. npc_name .. '" has no \'BoundAbilities\' data.</span>' | |||
end | |||
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 | |||
-- 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 | ||