Module:HeroData

From Deadlock Wiki
Revision as of 03:24, 18 September 2024 by Sur (talk | contribs) (get_hero_scalar_str slightly reworked)
Jump to navigation Jump to search

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|HERO_NAME|SPIRIT_OR_LEVEL|STAT_NAME}}--
p.get_hero_scalar_str_invoke = function(frame)
	local hero_name = frame.args[1]
	local spirit_or_level = 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
end

function p.get_hero_scalar_str(hero_name, spirit_or_level, 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)
	if(hero == nil) then return "Hero Not Found" end
	
	--Retrieve scaling data
	local hero_scaling_data = hero[spirit_or_level_full]
	if(hero_scaling_data == nil) then return "" end
	
	--Retrieve scaling value
	local hero_scalar = hero_scaling_data[hero_stat_key]
	if(hero_scalar == nil) then return "" end
	
	--The hero has a scaling value with this stat
	local template_title = "Template:"
	if (spirit_or_level == "Spirit") then 
		template_title = template_title .. "Ss" 
	else 
		template_title = template_title .. "Ls"
	end
	
	local template_args = {}
	template_args["arg1"] = hero_scalar
	
	local template_call = mw.getCurrentFrame():expandTemplate{ title = template_title, args = template_args }
	
	return template_call
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