Module:Sandbox/LVL: Difference between revisionsGive feedback
Jump to navigation
Jump to search
No edit summary Tag: Manual revert |
No edit summary |
||
| Line 1: | Line 1: | ||
local | local data = mw.loadJsonData("Data:AbilityCards.json") | ||
local raw_ability_data = mw.loadJsonData("Data:AbilityData.json") | |||
local | |||
local p = {} | |||
local | |||
} | |||
-- returns the table of a specific | -- returns the table of a specific ability from Data:AbilityCards.json | ||
function | function get_ability(hero_key, ability_num) | ||
local ui_data = data[hero_key] | |||
if(ui_data == nil) then return "Hero Not Found" end | |||
return ui_data[tonumber(ability_num)] | |||
return | |||
end | end | ||
-- Returns | -- 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 { | -- @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 | end | ||
-- If not found by key, loop through and search by the "Name" property as a fallback | |||
-- Skip any ability that is explicitly disabled | |||
-- | for _, v in pairs(raw_ability_data) do | ||
if v.Name == identifier and not v.IsDisabled then | |||
for | return v | ||
if | |||
return | |||
end | end | ||
end | end | ||
-- If not found by either method, return nil | |||
return nil | return nil | ||
end | end | ||
--{{#invoke:AbilityData|get_ability_name|HERO_NAME|ABILITY_NUM}}-- | |||
--{{#invoke: | p.get_ability_name = function(frame) | ||
p. | |||
local hero_name = frame.args[1] | local hero_name = frame.args[1] | ||
local | local ability_num = frame.args[2] | ||
local | local ability = get_ability(hero_name, ability_num) | ||
if(ability == nil) then return "Ability Not Found" end | |||
if( | return ability.Name | ||
end | end | ||
p. | --{{#invoke:AbilityData|get_ability_prop|ability_key_or_name|prop}}-- | ||
local | 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) | |||
if(ability == nil) then return "Ability Not Found" end | |||
end | |||
local element = ability[prop] | |||
local | if element == nil then return "" end | ||
if | |||
end | |||
-- | -- if the property is a table, return its Value field | ||
if type(element) == "table" then | |||
return element.Value or "" | |||
if( | |||
end | end | ||
-- | -- otherwise return the scalar value | ||
return element | |||
return | |||
end | end | ||
-- | --{{#invoke:AbilityData|get_ability_upgrade_prop|ability_key_or_name|upgrade_num|prop}}-- | ||
-- Gets a specific property from a specific upgrade of an ability. | |||
-- @param {string} frame.args[1] - The ability's key (e.g., "ability_afterburn") or name (e.g., "Afterburn"). | |||
-- @param {string|number} frame.args[2] - The upgrade number (1, 2, or 3). | |||
local | -- @param {string} frame.args[3] - The name of the property to retrieve. | ||
local | p.get_ability_upgrade_prop = function(frame) | ||
local | local ability_identifier = frame.args[1] | ||
local upgrade_num_str = frame.args[2] | |||
local prop = frame.args[3] | |||
-- Check for missing arguments | |||
if not (ability_identifier and upgrade_num_str and prop) then | |||
return "Missing Arguments" -- Or return "" | |||
end | end | ||
-- Find the base ability data | |||
local ability = find_json_ability(ability_identifier) | |||
if not ability then | |||
-- | return "Ability Not Found" | ||
end | end | ||
-- Get the list of upgrades for this ability | |||
local upgrades = ability.Upgrades | |||
if not upgrades or type(upgrades) ~= "table" then | |||
return "No Upgrades Found" | |||
-- | |||
local | |||
end | end | ||
-- Convert the upgrade number from a string to an integer | |||
local upgrade_num = tonumber(upgrade_num_str) | |||
if not upgrade_num then | |||
return "Invalid Upgrade Number" | |||
-- | |||
local | |||
if | |||
end | end | ||
-- | -- Get the specific upgrade table from the list (Lua tables are 1-indexed) | ||
local upgrade = upgrades[upgrade_num] | |||
if not upgrade or type(upgrade) ~= "table" then | |||
return "Upgrade Not Found" | |||
end | end | ||
-- Get the desired property from the specific upgrade | |||
-- | local element = upgrade[prop] | ||
if element == nil then | |||
return "" -- The property doesn't exist in this specific upgrade, which is a valid case. Return empty. | |||
local | |||
end | end | ||
-- If the property is a table (like a Damage object), return its Value field | |||
if type(element) == "table" then | |||
return element.Value or "" | |||
end | end | ||
-- Otherwise, return the direct value | |||
return element | |||
return | |||
end | end | ||
return p | return p | ||
Revision as of 23:03, 13 November 2025
Documentation for this module may be created at Module:Sandbox/LVL/doc
local data = mw.loadJsonData("Data:AbilityCards.json")
local raw_ability_data = mw.loadJsonData("Data:AbilityData.json")
local p = {}
-- returns the table of a specific ability from Data:AbilityCards.json
function get_ability(hero_key, ability_num)
local ui_data = data[hero_key]
if(ui_data == nil) then return "Hero Not Found" end
return ui_data[tonumber(ability_num)]
end
-- Returns the table of a specific ability from Data:AbilityData.json by its Key or Name
-- It first attempts a direct lookup by key for efficiency. If that fails, it iterates
-- through the data to find a matching Name property.
-- @function find_json_ability
-- @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
-- Skip any ability that is explicitly disabled
for _, v in pairs(raw_ability_data) do
if v.Name == identifier and not v.IsDisabled then
return v
end
end
-- If not found by either method, return nil
return nil
end
--{{#invoke:AbilityData|get_ability_name|HERO_NAME|ABILITY_NUM}}--
p.get_ability_name = function(frame)
local hero_name = frame.args[1]
local ability_num = frame.args[2]
local ability = get_ability(hero_name, ability_num)
if(ability == nil) then return "Ability Not Found" end
return ability.Name
end
--{{#invoke:AbilityData|get_ability_prop|ability_key_or_name|prop}}--
p.get_ability_prop = function(frame)
local ability_identifier = frame.args[1]
local prop = frame.args[2]
-- Use the new, more flexible lookup function
local ability = find_json_ability(ability_identifier)
if(ability == nil) then return "Ability Not Found" end
local element = ability[prop]
if element == nil then return "" end
-- if the property is a table, return its Value field
if type(element) == "table" then
return element.Value or ""
end
-- otherwise return the scalar value
return element
end
--{{#invoke:AbilityData|get_ability_upgrade_prop|ability_key_or_name|upgrade_num|prop}}--
-- Gets a specific property from a specific upgrade of an ability.
-- @param {string} frame.args[1] - The ability's key (e.g., "ability_afterburn") or name (e.g., "Afterburn").
-- @param {string|number} frame.args[2] - The upgrade number (1, 2, or 3).
-- @param {string} frame.args[3] - The name of the property to retrieve.
p.get_ability_upgrade_prop = function(frame)
local ability_identifier = frame.args[1]
local upgrade_num_str = frame.args[2]
local prop = frame.args[3]
-- Check for missing arguments
if not (ability_identifier and upgrade_num_str and prop) then
return "Missing Arguments" -- Or return ""
end
-- Find the base ability data
local ability = find_json_ability(ability_identifier)
if not ability then
return "Ability Not Found"
end
-- Get the list of upgrades for this ability
local upgrades = ability.Upgrades
if not upgrades or type(upgrades) ~= "table" then
return "No Upgrades Found"
end
-- Convert the upgrade number from a string to an integer
local upgrade_num = tonumber(upgrade_num_str)
if not upgrade_num then
return "Invalid Upgrade Number"
end
-- Get the specific upgrade table from the list (Lua tables are 1-indexed)
local upgrade = upgrades[upgrade_num]
if not upgrade or type(upgrade) ~= "table" then
return "Upgrade Not Found"
end
-- Get the desired property from the specific upgrade
local element = upgrade[prop]
if element == nil then
return "" -- The property doesn't exist in this specific upgrade, which is a valid case. Return empty.
end
-- If the property is a table (like a Damage object), return its Value field
if type(element) == "table" then
return element.Value or ""
end
-- Otherwise, return the direct value
return element
end
return p