Module:HeroData: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
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



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