Module:HeroData: Difference between revisions
Jump to navigation
Jump to search
m arg1 for ss/ls call renamed to 1 |
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 | --{{#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 | local hero_stat_key = frame.args[2] | ||
return p.get_hero_scalar_str(hero_name | 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 | function p.get_hero_scalar_str(hero_name, hero_stat_key) | ||
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 | ||
local scaling_type_full | |||
local | local scaling_data | ||
local scaling_value = "" | |||
scaling_types = {"Spirit", "Level"} | |||
scaling_abbrevs = {"Ss", "Ls"} | |||
-- | for index, scaling_type in ipairs(scaling_types) do | ||
scaling_type_full = scaling_type .. "Scaling" | |||
scaling_data = hero[scaling_type_full] | |||
--If the scaling data exists | |||
if (scaling_data ~= nil) then | |||
scaling_value = scaling_data[hero_stat_key] | |||
--If the stat scales | |||
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 | ||
-- Otherwise return empty string | |||
return scaling_value | |||
return | |||
end | end | ||
Revision as of 03:42, 18 September 2024
Documentation for this module may be created at Module:HeroData/doc
local p = {};
local data = mw.loadJsonData("Data:HeroData.json")
-- returns the table of a specific item
function get_json_item(name)
for i,v in pairs(data) do
if (v["Name"] == name) then
return v
end
end
return nil
end
-- returns the table of a specific item, used by external modules
function p.get_json_item(name)
for i,v in pairs(data) do
if (v["Name"] == name) then
return v
end
end
return nil
end
--{{#invoke:HeroData|get_hero_stat|HERO_NAME|STAT_NAME}}--
p.get_hero_stat = function(frame)
local hero_name = frame.args[1]
local hero_stat_key = frame.args[2]
local hero = get_json_item(hero_name)
if(hero == nil) then return "Hero Not Found" end
local var_value = hero[hero_stat_key]
if(var_value == nil) then return 0 end
return var_value
end
--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|STAT_NAME}}--
p.get_hero_scalar_str_invoke = function(frame)
local hero_name = frame.args[1]
local hero_stat_key = frame.args[2]
return p.get_hero_scalar_str(hero_name, hero_stat_key) --surely theres a better way
end
function p.get_hero_scalar_str(hero_name, hero_stat_key)
local hero = get_json_item(hero_name)
if(hero == nil) then return "Hero Not Found" end
local scaling_type_full
local scaling_data
local scaling_value = ""
scaling_types = {"Spirit", "Level"}
scaling_abbrevs = {"Ss", "Ls"}
for index, scaling_type in ipairs(scaling_types) do
scaling_type_full = scaling_type .. "Scaling"
scaling_data = hero[scaling_type_full]
--If the scaling data exists
if (scaling_data ~= nil) then
scaling_value = scaling_data[hero_stat_key]
--If the stat scales
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
-- Otherwise return empty string
return scaling_value
end
--{{#invoke:HeroData|write_hero_infobox|HERO_NAME}}--
p.write_hero_infobox = function(frame)
hero_name = frame.args[1]
hero = get_json_item(hero_name)
if(hero == nil) then return "Hero Not Found" end
-- Prepare the arguments for the template
local templateArgs = {}
for stat_name, stat_value in pairs(hero) do
if (type(stat_value) ~= "table") then -- skips complex nested tables
templateArgs[stat_name] = stat_value
end
end
-- Use expandTemplate to evaluate the Infobox_hero template
local templateTitle = mw.title.new("Template:Infobox_hero")
local expandedTemplate = mw.getCurrentFrame():expandTemplate{ title = templateTitle, args = templateArgs }
return expandedTemplate
end
return p