Module:AbilityData: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Saag (talk | contribs)
mNo edit summary
Saag (talk | contribs)
added handling of {s:sign} in descriptions
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
local p = {};
local lang = require "Module:Lang"
local data = mw.loadJsonData("Data:ItemData.json")
local p = {}
local data = mw.loadJsonData("Data:HeroData.json")


-- returns the table of a specific item
-- returns the table of a specific item
function get_json_item(name)
function get_ability(hero_name, ability_num)
for i,v in pairs(data) do
local hero_key = get_hero_key(hero_name)
if (v["Name"] == name) then
if(hero_key == nil) then return "Hero Not Found" end
return v
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
end
end
Line 12: Line 19:
end
end


local abilities = {
--{{#invoke:AbilityData|get_ability_card|HERO_NAME|ABILITY_NUM}}--
    shoulderCharge = {
p.get_ability_card = function(frame)
        name = "Shoulder Charge",
local hero_name = frame.args[1]
        duration = "1.2s",
local ability_num = frame.args[2]
        cooldown = "35s",
local add_link = frame.args[3]
        description = "'''Charge forward''', colliding with enemies and dragging them along. Hitting a '''wall''' will '''Stun''' enemies caught by Abrams. Speed increased after colliding with enemy Heroes.",
 
        effect1 = "'''Damage:''' 40 {{ss|2.1}}",
local ability = get_ability(hero_name, ability_num)
        effect2 = "'''Stun Duration:''' 0.85s",
if(ability == nil) then return "Ability Not Found" end
        upgrade1 = "'''-20s''' Cooldown",
if ability then
        upgrade2 = "'''+0.5s''' Duration",
-- Pass the table fields to the template
        upgrade3 = "'''+5.5''' Weapon Damage for '''8s''' after colliding with an enemy"
local width_key = find_width_key(ability)
    }
local used_keys = {
}
'Key', 'Name', 'Description', 'Radius', 'AbilityCastRange', 'AbilityDuration',
'AbilityDuration', 'AbilityCooldown', 'Upgrades', width_key
}
 
local effects = get_effects(ability, used_keys)
local too_many_effects_warn = ''
if #effects > 9 and effects[9] ~= nil then
too_many_effects_warn = 'There are more effects than the ability card is able to display'
effects[9] = effects[9] .. too_many_effects_warn
 
end
 
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 = format_value_with_prepost("Description", ability.Description),
radius = format_value_with_prepost("Radius", ability.Radius),
range = format_value_with_prepost("AbilityCastRange", ability.AbilityCastRange),
ability_width = format_value_with_prepost(width_key, ability[width_key]),
duration = format_value_with_prepost("AbilityDuration", ability.AbilityDuration),
cooldown = format_value_with_prepost("AbilityCooldown", 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)
local effects = {}
for key, value in pairs(ability) do
if not string_in_list(key, used_keys) then
local label = lang._get_string(string.format("%s_label", key))
 
if (label == nil) then
table.insert(effects, string.format("Missing label for key '%s'", key))
else
local effect_label= string.format("'''%s''': %s", label, format_value_with_prepost(key, value))
table.insert(effects, effect_label)
end
end
end
return effects
end
 
-- Add prefix and postfix labels to a value. Eg. "32" -> "32s"
function format_value_with_prepost(key, value)
if (value == nil) then return nil end
local prefix = lang._get_string(string.format("%s_prefix",key))
local postfix = lang._get_string(string.format("%s_postfix",key))
-- Default pre/post fix to empty string, as they may not exist
if (prefix == nil) then prefix = '' end
if (postfix == nil) then postfix = '' end
if (prefix == '{s:sign}') then
if value < 0 then
prefix = '-'
else
prefix = '+'
end
end
return string.format("%s%s%s", prefix, value, postfix)
end


--{{#invoke:ItemData|get_cost|ITEM_NAME}}--
function find_width_key(ability)
p.test = function(frame)
for key, value in pairs(ability) do
    local abilityName = frame.args.abilityName
if type(key) == "string" and key:sub(-5) == "Width" then
    local ability = abilities[abilityName]
return key
end
end
return nil
end


    if ability then
function string_in_list(str, list)
        -- Pass the table fields to the template
for _, value in ipairs(list) do
        return frame:expandTemplate{
if value == str then
            title = "Ability card",
return true
            args = {
end
                name = ability.name,
end
                duration = ability.duration,
return false
                cooldown = ability.cooldown,
                description = ability.description,
                effect1 = ability.effect1,
                effect2 = ability.effect2,
                upgrade1 = ability.upgrade1,
                upgrade2 = ability.upgrade2,
                upgrade3 = ability.upgrade3
            }
        }
    else
        return "Ability data not found."
    end
end
end


return p
return p

Latest revision as of 20:28, 19 September 2024

Documentation for this module may be created at Module:AbilityData/doc

local lang = 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 = {
			'Key', 'Name', 'Description', 'Radius', 'AbilityCastRange', 'AbilityDuration',
			'AbilityDuration', 'AbilityCooldown', 'Upgrades', width_key
		}

		local effects = get_effects(ability, used_keys)
		
		local too_many_effects_warn = ''
		if #effects > 9 and effects[9] ~= nil then
			too_many_effects_warn = 'There are more effects than the ability card is able to display'
			effects[9] = effects[9] .. too_many_effects_warn

		end

		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 = format_value_with_prepost("Description", ability.Description),
				radius = format_value_with_prepost("Radius", ability.Radius),
				range = format_value_with_prepost("AbilityCastRange", ability.AbilityCastRange),
				ability_width = format_value_with_prepost(width_key, ability[width_key]),
				duration = format_value_with_prepost("AbilityDuration", ability.AbilityDuration),
				cooldown = format_value_with_prepost("AbilityCooldown", 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)
	local effects = {}
	for key, value in pairs(ability) do
		if not string_in_list(key, used_keys) then
			local label = lang._get_string(string.format("%s_label", key))

			if (label == nil) then 
				table.insert(effects, string.format("Missing label for key '%s'", key))
			else
				local effect_label= string.format("'''%s''': %s", label, format_value_with_prepost(key, value))
				table.insert(effects, effect_label)
			end
		end
	end
	return effects
end

-- Add prefix and postfix labels to a value. Eg. "32" -> "32s"
function format_value_with_prepost(key, value)
	if (value == nil) then return nil end
		
	local prefix = lang._get_string(string.format("%s_prefix",key))
	local postfix = lang._get_string(string.format("%s_postfix",key))
	
	-- Default pre/post fix to empty string, as they may not exist
	if (prefix == nil) then prefix = '' end
	if (postfix == nil) then postfix = '' end
	
	if (prefix == '{s:sign}') then
		if value < 0 then 
			prefix = '-'
		else 
			prefix = '+'
		end
	end
	
	return string.format("%s%s%s", prefix, value, postfix)
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