Module:HeroData: Difference between revisions

added in_development check
Added support for alt fire and new weapon data structure
Line 46: Line 46:
--sig_figs optional for rounding floats
--sig_figs optional for rounding floats
p.get_hero_var = function(frame)
p.get_hero_var = function(frame)
local hero_name = frame.args[1]
    local hero_name = frame.args[1]
local hero_stat_key = frame.args[2]
    local hero_stat_key = frame.args[2]
local sig_figs_or_localize = frame.args[3]
    local sig_figs_or_localize = frame.args[3]
   
local hero = heroes_data[hero_name] --check if hero key is passed instead
    local hero = heroes_data[hero_name]
if(hero == nil) then hero = p.get_json_item(hero_name) end --check if hero name is passed
    if(hero == nil) then hero = p.get_json_item(hero_name) end
if(hero == nil) then return "Hero Not Found" end --both invalid, error
    if(hero == nil) then return "Hero Not Found" end
   
local var_value = hero[hero_stat_key]
    -- Check top-level first, then Weapon, then AltFire
if(var_value == nil) then return 0 end
    local var_value = hero[hero_stat_key]
    if var_value == nil and hero.Weapon then
--round
        var_value = hero.Weapon[hero_stat_key]
if (sig_figs_or_localize ~= nil and tonumber(sig_figs_or_localize) ~= nil) then
    end
var_value = util_module.round_to_sig_fig(var_value, sig_figs_or_localize)
    if var_value == nil and hero.Weapon and hero.Weapon.AltFire then
if (var_value == nil) then return "get_hero_var() error with rounding" end
        var_value = hero.Weapon.AltFire[hero_stat_key]
end
    end
   
--localize
    if(var_value == nil) then return 0 end
if (sig_figs_or_localize == "true") then
   
return lang_module.get_string(var_value)
    --round
end
    if (sig_figs_or_localize ~= nil and tonumber(sig_figs_or_localize) ~= nil) then
        var_value = util_module.round_to_sig_fig(var_value, sig_figs_or_localize)
        if (var_value == nil) then return "get_hero_var() error with rounding" end
    end
   
    --localize
    if (sig_figs_or_localize == "true") then
        return lang_module.get_string(var_value)
    end
 
    return var_value
end
 
--{{#invoke:HeroData|get_alt_fire_var|HERO_NAME|STAT_NAME|sig_figs_or_localize}}--
p.get_alt_fire_var = function(frame)
    local hero_name = frame.args[1]
    local stat_key = frame.args[2]
    local sig_figs_or_localize = frame.args[3]
   
    local hero = heroes_data[hero_name]
    if(hero == nil) then hero = p.get_json_item(hero_name) end
    if(hero == nil) then return "Hero Not Found" end
   
    if not (hero.Weapon and hero.Weapon.AltFire) then
        return "No Alt Fire"
    end
   
    local var_value = hero.Weapon.AltFire[stat_key]
    if(var_value == nil) then return 0 end
   
    --round
    if (sig_figs_or_localize ~= nil and tonumber(sig_figs_or_localize) ~= nil) then
        var_value = util_module.round_to_sig_fig(var_value, sig_figs_or_localize)
    end
   
    --localize
    if (sig_figs_or_localize == "true") then
        return lang_module.get_string(var_value)
    end


return var_value
    return var_value
end
end


Line 78: Line 116:
local number_int = tonumber(number_str)
local number_int = tonumber(number_str)
local localize = frame.args[4]
local localize = frame.args[4]
local hero = heroes_data[hero_name] --check if hero key is passed instead
if(hero == nil) then hero = p.get_json_item(hero_name) end --check if hero name is passed
local hero = heroes_data[hero_name]
if(hero == nil) then return "Hero Not Found" end --both invalid, error
if(hero == nil) then hero = p.get_json_item(hero_name) end
if (hero == nil) then return "Hero " .. hero_name .. " not found" end
if(hero == nil) then return "Hero Not Found" end
-- Check top-level first, then Weapon, then AltFire
local list = hero[var]
local list = hero[var]
if list == nil and hero.Weapon then
    list = hero.Weapon[var]
end
if list == nil and hero.Weapon and hero.Weapon.AltFire then
    list = hero.Weapon.AltFire[var]
end
if (list == nil) then return "" end
if (list == nil) then return "" end
local element = list[number_int]
local element = list[number_int]
Line 265: Line 312:
stat_value = hero_data[stat_name]
stat_value = hero_data[stat_name]
if (stat_value == nil) then
if (stat_value == nil) then
stat_value = 0 --default to 0 if not present
    -- Check nested weapon stats
    if hero_data.Weapon and hero_data.Weapon[stat_name] ~= nil then
        stat_value = hero_data.Weapon[stat_name]
    elseif hero_data.Weapon and hero_data.Weapon.AltFire and hero_data.Weapon.AltFire[stat_name] ~= nil then
        stat_value = hero_data.Weapon.AltFire[stat_name]
    else
        stat_value = 0 --default to 0 if not present
    end
end
end
Line 363: Line 417:
stat_value = hero_data[stat_name]
stat_value = hero_data[stat_name]
if (stat_value == nil) then
if (stat_value == nil) then
stat_value = 0 --default to 0 if not present
    -- Check nested weapon stats
    if hero_data.Weapon and hero_data.Weapon[stat_name] ~= nil then
        stat_value = hero_data.Weapon[stat_name]
    elseif hero_data.Weapon and hero_data.Weapon.AltFire and hero_data.Weapon.AltFire[stat_name] ~= nil then
        stat_value = hero_data.Weapon.AltFire[stat_name]
    else
        stat_value = 0 --default to 0 if not present
    end
end
end
Line 434: Line 495:
return template_calls .. "</div>"
return template_calls .. "</div>"
end
local function get_nested_stat_value(hero_data, stat_key)
    -- Check top-level, then Weapon, then AltFire
    if hero_data[stat_key] ~= nil then
        return hero_data[stat_key]
    elseif hero_data.Weapon and hero_data.Weapon[stat_key] ~= nil then
        return hero_data.Weapon[stat_key]
    elseif hero_data.Weapon and hero_data.Weapon.AltFire and hero_data.Weapon.AltFire[stat_key] ~= nil then
        return hero_data.Weapon.AltFire[stat_key]
    end
    return 0
end
end


Line 539: Line 612:
--Retrieve the stats value from hero data
--Retrieve the stats value from hero data
stat_value = hero_data[attr_key]
stat_value = get_nested_stat_value(hero_data, attr_key)
if (stat_value == nil) then stat_value = 0 end
-- Retrieve scaling val and str if it scales
-- Retrieve scaling val and str if it scales