Module:Abilities: Difference between revisionsGive feedback
Fixed get_ability_prop to return element.Value for table props |
get_ability_prop can now lookup abilities by their name or by their key |
||
| Line 4: | Line 4: | ||
local p = {} | local p = {} | ||
-- returns the table of a specific ability | -- returns the table of a specific ability from Data:AbilityCards.json | ||
function get_ability(hero_key, ability_num) | function get_ability(hero_key, ability_num) | ||
local ui_data = data[hero_key] | local ui_data = data[hero_key] | ||
| Line 11: | Line 11: | ||
end | end | ||
-- Returns the table of a specific ability from Data:AbilityData | -- Returns the table of a specific ability from Data:AbilityData.json by its Key or Name | ||
-- @function | -- It first attempts a direct lookup by key for efficiency. If that fails, it iterates | ||
-- @param {string} | -- through the data to find a matching Name property. | ||
-- @return {table} | -- @function find_json_ability | ||
local function | -- @param {string} identifier - The ability's key or name | ||
-- @return {table} or nil | |||
local function find_json_ability(identifier) | |||
-- First, try a direct lookup by key (most efficient method) | |||
if raw_ability_data[identifier] then | |||
return raw_ability_data[identifier] | |||
end | |||
-- If not found by key, loop through and search by the "Name" property as a fallback | |||
for _, v in pairs(raw_ability_data) do | for _, v in pairs(raw_ability_data) do | ||
if (v["Name"] == | if (v["Name"] == identifier) then | ||
return v | return v | ||
end | end | ||
end | end | ||
-- If not found by either method, return nil | |||
return nil | return nil | ||
end | end | ||
| Line 34: | Line 44: | ||
end | end | ||
--{{#invoke: | --{{#invoke:AbilityData|get_ability_prop|ability_key_or_name|prop}}-- | ||
p.get_ability_prop = function(frame) | p.get_ability_prop = function(frame) | ||
local | local ability_identifier = frame.args[1] | ||
local prop = frame.args[2] | |||
local | |||
-- Use the new, more flexible lookup function | |||
local ability = find_json_ability(ability_identifier) | |||
-- | |||
local ability = | |||
if(ability == nil) then return "Ability Not Found" end | if(ability == nil) then return "Ability Not Found" end | ||
local element = ability[ | local element = ability[prop] | ||
if element == nil then return "" end | if element == nil then return "" end | ||