Module:AbilityData

Revision as of 20:53, 15 September 2024 by Saag (talk | contribs)

Deprecated

This module is being replaced by multiple modules in Module:Abilities. Any new functions should be created there


local lang_module = require "Module:Lang"
local p = {}
local data = mw.loadJsonData("Data:HeroData.json")

-- returns the table of a specific item
function get_ability(hero_name, ability_num)
	local hero_key = get_hero_key(hero_name)
	if(hero_key == nil) then return "Hero Not Found" end
	return data[hero_key]["BoundAbilities"][tonumber(ability_num)]
end

function get_hero_key(hero_name)
	for i, hero in pairs(data) do
		if hero["Name"] == hero_name then
			return i
		end
	end
	return nil
end

--{{#invoke:AbilityData|get_ability_card|HERO_NAME|ABILITY_NUM}}--
p.get_ability_card = function(frame)
	local hero_name = frame.args[1]
	local ability_num = frame.args[2]
	local add_link = frame.args[3]

	local ability = get_ability(hero_name, ability_num)
	if(ability == nil) then return "Ability Not Found" end
	if ability then
		-- Pass the table fields to the template
		local width_key = find_width_key(ability)
		local used_keys = {
			'Name', 'Description', 'Radius', 'AbilityCastRange', 'AbilityDuration',
			'AbilityDuration', 'AbilityCooldown', 'Upgrades', width_key
		}

		local effects = get_effects(ability, used_keys)
		local name_link = string.format("%s#(%s)_%s", hero_name, ability_num, ability.Name)
		return frame:expandTemplate{
			-- TODO - remove when testing is done
			title = "User:Saag/Ability card",
			args = {
				name = ability.Name,
				name_link = name_link,
				description = ability.Description,
				radius = ability.Radius,
				range = ability.AbilityCastRange,
				ability_width = ability[width_key],
				duration = ability.AbilityDuration,
				cooldown = ability.AbilityCooldown,
				effect1 = effects[1],
				effect2 = effects[2],
				effect3 = effects[3],
				effect4 = effects[4],
				effect5 = effects[5],
				effect6 = effects[6],
				effect7 = effects[7],
				effect8 = effects[8],
				effect9 = effects[9],
				upgrade1 = ability.Upgrades[1].Description,
				upgrade2 = ability.Upgrades[2].Description,
				upgrade3 = ability.Upgrades[3].Description
			}
		}
	else
		return "Ability data not found for hero " .. hero_name .. " and num " .. ability_num
	end
end

--{{#invoke:AbilityData|get_ability_name|HERO_NAME|ABILITY_NUM}}--
p.get_ability_name = function(frame)
	local hero_name = frame.args[1]
	local ability_num = frame.args[2]
	
	local ability = get_ability(hero_name, ability_num)
	if(ability == nil) then return "Ability Not Found" end
	return ability.Name
end

function get_effects(ability, used_keys)
	effects = {}
	for key, value in pairs(ability) do
		if not string_in_list(key, used_keys) then
			local frame = {}
			frame.args = {string.format("%s_label", key), ""}
			local label = lang_module.get_string(frame)
			
			-- If response from lang_module includes "not found", it is an error.
			-- So we should ignore that attribute (until we have a valid label)
			if (not string.find(label, 'not found')) then
				local effect_value = string.format("'''%s''': %s", label, value)
				table.insert(effects, effect_value)
			end
		end
	end
	return effects
end

function find_width_key(ability)
	for key, value in pairs(ability) do
		if type(key) == "string" and key:sub(-5) == "Width" then
			return key
		end
	end
	return nil
end

function string_in_list(str, list)
	for _, value in ipairs(list) do
		if value == str then
			return true
		end
	end
	return false
end

return p