Module:HeroData: Difference between revisionsGive feedback
Jump to navigation
Jump to search
Gammaton32 (talk | contribs) 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_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 | |||
-- Check top-level first, then Weapon, then AltFire | |||
local var_value = hero[hero_stat_key] | |||
if var_value == nil and hero.Weapon then | |||
var_value = hero.Weapon[hero_stat_key] | |||
end | |||
if var_value == nil and hero.Weapon and hero.Weapon.AltFire then | |||
var_value = hero.Weapon.AltFire[hero_stat_key] | |||
end | |||
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) | |||
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 | |||
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] | |||
if(hero == nil) then hero = p.get_json_item(hero_name) end | local hero = heroes_data[hero_name] | ||
if(hero == nil) then return "Hero Not Found" end -- | if(hero == nil) then hero = p.get_json_item(hero_name) 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 | ||
-- 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 | ||
-- 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 | stat_value = get_nested_stat_value(hero_data, attr_key) | ||
-- Retrieve scaling val and str if it scales | -- Retrieve scaling val and str if it scales | ||