Module:HeroData

From Deadlock Wiki
Revision as of 04:00, 18 September 2024 by Sur (talk | contribs) (local prefix on types and abbrevs vars)
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_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]
	local hero_data = get_json_item(hero_name)
	if(hero == nil) then return "Hero Not Found" end
	return p.get_hero_scalar_str(hero_data, hero_stat_key) --surely theres a better way
end

function p.get_hero_scalar_str(hero, hero_stat_key)
	local scaling_type_full
	local scaling_data
	local scaling_value
	local scaling_types = {"Spirit", "Level"}
	local 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"] = scaling_value --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 ""
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