Module:HeroData: Difference between revisions

Sur (talk | contribs)
m arg1 for ss/ls call renamed to 1
Sur (talk | contribs)
m get_hero_scalar_str more dynamic to not require spirit/level input
Line 39: Line 39:


--If the hero scales with the stat, it returns {{Ss|value}} or {{Ls|value}}, else blank string
--If the hero scales with the stat, it returns {{Ss|value}} or {{Ls|value}}, else blank string
--{{#invoke:HeroData|get_hero_scalar_str_invoke|HERO_NAME|SPIRIT_OR_LEVEL|STAT_NAME}}--
--{{#invoke:HeroData|get_hero_scalar_str_invoke|HERO_NAME|STAT_NAME}}--
p.get_hero_scalar_str_invoke = function(frame)
p.get_hero_scalar_str_invoke = function(frame)
local hero_name = frame.args[1]
local hero_name = frame.args[1]
local spirit_or_level = frame.args[2]
local hero_stat_key = frame.args[2]
local hero_stat_key = frame.args[3]
return p.get_hero_scalar_str(hero_name, spirit_or_level, hero_stat_key) --surely theres a better way
return p.get_hero_scalar_str(hero_name, hero_stat_key) --surely theres a better way
end
end


function p.get_hero_scalar_str(hero_name, spirit_or_level, hero_stat_key)
function p.get_hero_scalar_str(hero_name, hero_stat_key)
--Ensure spirit or level is chosen
if(not (spirit_or_level == "Spirit" or spirit_or_level == "Level")) then return "SPIRIT_OR_LEVEL must be 'Spirit' or 'Level'" end
local spirit_or_level_full = spirit_or_level .. "Scaling" --i.e. SpiritScaling or LevelScaling
local hero = get_json_item(hero_name)
local hero = get_json_item(hero_name)
if(hero == nil) then return "Hero Not Found" end
if(hero == nil) then return "Hero Not Found" end
--Retrieve scaling data
local scaling_type_full
local hero_scaling_data = hero[spirit_or_level_full]
local scaling_data
if(hero_scaling_data == nil) then return "" end
local scaling_value = ""
scaling_types = {"Spirit", "Level"}
scaling_abbrevs = {"Ss", "Ls"}
--Retrieve scaling value
for index, scaling_type in ipairs(scaling_types) do
local hero_scalar = hero_scaling_data[hero_stat_key]
scaling_type_full = scaling_type .. "Scaling"
if(hero_scalar == nil) then return "" end
scaling_data = hero[scaling_type_full]
--The hero has a scaling value with this stat
--If the scaling data exists
local template_title = "Template:"
if (scaling_data ~= nil) then
if (spirit_or_level == "Spirit") then
scaling_value = scaling_data[hero_stat_key]
template_title = template_title .. "Ss"
else
--If the stat scales
template_title = template_title .. "Ls"
if (scaling_value ~= nil) then
--The hero has a scaling value with this stat
local template_title = "Template:" .. scaling_abbrevs[index] --scaling type's abbreviation
local template_args = {}
template_args["1"] = hero_scalar --store in 1st arg for {{{1}}} to grab it from {{SS}} or {{LS}} template
local template_call = mw.getCurrentFrame():expandTemplate{ title = template_title, args = template_args }
return template_call
end
end
end
end
local template_args = {}
-- Otherwise return empty string
template_args["1"] = hero_scalar
return scaling_value
local template_call = mw.getCurrentFrame():expandTemplate{ title = template_title, args = template_args }
return template_call
end
end