Module:AbilityData: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Saag (talk | contribs)
added tech damage icon
Saag (talk | contribs)
m Fixed get_ability_name
 
(62 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local lang = require "Module:Lang"
local data = mw.loadJsonData("Data:AbilityCards.json")
 
local p = {}
local p = {}
local data = mw.loadJsonData("Data:AbilityCard.json")


-- returns the table of a specific item
-- returns the table of a specific item
function get_ability(hero_name, ability_num)
function get_ability(hero_key, ability_num)
local hero_key = get_hero_key(hero_name)
local ui_data = data[hero_key]
if(hero_key == nil) then return "Hero Not Found" end
if(ui_data == nil) then return "Hero Not Found" end
return data[hero_key][tonumber(ability_num)]
return ui_data[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|ADD_LINK}}--
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 data not found for hero ' ..hero_name.. ' and num ' .. ability_num
end
 
local name_link = string.format("%s#(%s)_%s", hero_name, ability_num, ability.Name)
return frame:expandTemplate{
title = "User:Saag/Sandbox/AbilityCard",
args = {
hero_name = hero_name,
ability_num = ability_num,
name = ability.Name,
name_link = name_link,
description = format_value_with_prepost("Description", ability.Description),
radius = format_value_with_prepost("Radius", ability.Radius and ability.Radius.Value),
range = format_value_with_prepost("AbilityCastRange", ability.AbilityCastRange and ability.AbilityCastRange.Value),
-- ability_width = format_value_with_prepost(width_key, ability[width_key]),
cooldown = format_value_with_prepost("AbilityCooldown", ability.AbilityCooldown and ability.AbilityCooldown.Value),
charge_cooldown = format_value_with_prepost("AbilityDuration", ability.AbilityCooldownBetweenCharge and ability.AbilityCooldownBetweenCharge.Value),
num_of_charges = format_value_with_prepost("AbilityCharges", ability.AbilityCharges and ability.AbilityCharges.Value)
}
}
end
 
local ATTR_TYPE_ICON_MAP = {
tech_damage = 'AttributeIconTechShieldHealth.png',
distance = 'AttributeIconTechRange.png',
duration = 'AttributeIconTechDuration.png',
slow = 'MoveSlow.png'
}
--{{#invoke:AbilityData|get_info_main|HERO_NAME|ABILITY_NUM|INFO_SECTION_INDEX}}--
p.get_info_main = function(frame)
local hero_name = frame.args[1]
local ability_num = frame.args[2]
local info_section_num = frame.args[3]
local ability = get_ability(hero_name, ability_num)
if(ability == nil) then return "Ability Not Found" end
local info_section = ability['Info'..info_section_num]
-- some abilities have no info sections
if info_section == nil then return '' end
local props = info_section.Main.Props
-- Concatenate multiple section boxes into a single output template
local info_box_template = ''
for k, prop in pairs(props) do
section_box = frame:expandTemplate{
title = "User:Saag/Sandbox/AbilityCard/MainBox",
args = {
  key = prop.Key,
  value = format_value_with_prepost(prop.Key, prop.Value),
  icon = ATTR_TYPE_ICON_MAP[prop.Type] or 'GenericProperty.png',
  scale_value = prop.Scale and format_value_with_prepost(prop.Key, prop.Scale.Value),
  scale_type =  prop.Scale and prop.Scale.Type
}
}
info_box_template = info_box_template .. section_box .. '\n'
end
return info_box_template
end
 
--{{#invoke:AbilityData|get_info_alt|HERO_NAME|ABILITY_NUM|INFO_SECTION_INDEX}}--
p.get_info_alt = function(frame)
local hero_name = frame.args[1]
local ability_num = frame.args[2]
local info_section_num = frame.args[3]
local ability = get_ability(hero_name, ability_num)
if(ability == nil) then return "Ability Not Found" end
local info_section = ability['Info'..info_section_num]
-- some abilities have no info sections
if info_section == nil then return '' end
local props = info_section.Alt
-- Concatenate multiple section boxes into a single output template
local info_box_template = ''
for k, prop in pairs(props) do
-- Some props don't have values, as those come from upgrades
-- For now, we will ignore these and only show data for the base ability
if prop.Value then
section_box = frame:expandTemplate{
title = "User:Saag/Sandbox/AbilityCard/AltBox",
args = {
  key = prop.Key,
  value = format_value_with_prepost(prop.Key, prop.Value),
  icon = ATTR_TYPE_ICON_MAP[prop.Type] or 'GenericProperty.png',
  scale_value = prop.Scale and prop.Scale.Value,
  scale_type =  prop.Scale and prop.Scale.Type
}
}
info_box_template = info_box_template .. section_box .. '\n'
end
end
return info_box_template
end
 
local UPGRADE_COST_MAP = {1, 2, 5}
 
--{{#invoke:AbilityData|get_upgrades|HERO_NAME|ABILITY_NUM}}--
p.get_upgrades = 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
local props = ability.Upgrades
-- Concatenate multiple section boxes into a single output template
local upgrades_template = ''
for k, prop in pairs(props) do
box = frame:expandTemplate{
title = "User:Saag/Sandbox/AbilityCard/UpgradeBox",
args = {
  cost = UPGRADE_COST_MAP[k],
  description = prop.Description,
  scale_value = prop.Scale and prop.Scale.Value,
  scale_type =  prop.Scale and prop.Scale.Type
}
}
upgrades_template = upgrades_template .. box .. '\n'
end
return upgrades_template
end
end


Line 168: Line 20:
end
end


function get_effects(ability, used_keys)
--Write's an ability link for a given ability name in english
local effects = {}
--Siphon Life --> Abrams#(1)_Siphon_Life
for key, value in pairs(ability) do
function ability_to_hyperlink(ability_name_to_search)
if not string_in_list(key, used_keys) then
if (ability_name_to_search == nil) then return "No ability name provided" end
local label = lang._get_string(string.format("%s_label", key))
local hero_name
 
local ability_name
if (label == nil) then  
table.insert(effects, string.format("Missing label for key '%s'", key))
-- Iterate heros
else
for hero_key, hero_data in pairs(data) do
local effect_label= string.format("'''%s''': %s", label, format_value_with_prepost(key, value))
-- Iterate abilities
table.insert(effects, effect_label)
hero_name = hero_data["Name"]
for ability_num, ability_data in pairs(hero_data) do
if (ability_num ~= "Name") then
ability_name = ability_data["Name"]
if (ability_name == ability_name_to_search) then
-- Ability number and hero found
-- "Siphon Life" > "Siphon_Life"
ability_name = ability_name:gsub(" ", "_")
return hero_name .. "#(" .. ability_num .. ")_" .. ability_name
end
end
end
end
end
end
end
return effects
return nil
end
end


-- Add prefix and postfix labels to a value. Eg. "32" -> "32s"
--Writes an ability's icon from its name
function format_value_with_prepost(key, value)
--Shoulder Charge --> [[File:Shoulder Charge.png|link=Abrams#(1)_Shoulder_Charge|size=20px]] [[Abrams...|Shoulder Charge]]
if (value == nil) then return nil end
p.write_ability_icon = function(frame)
local ability_name = frame.args[1]
local prefix = lang._get_string(string.format("%s_prefix",key))
local custom_display_text = frame.args[2]
local postfix = lang._get_string(string.format("%s_postfix",key))
if (custom_display_text == nil or custom_display_text == "") then custom_display_text = ability_name end
local size = 20
local hyperlink = ability_to_hyperlink(ability_name)
if (hyperlink == nil) then return "Ability " .. ability_name .. " not found" end
-- Default pre/post fix to empty string, as they may not exist
local icon = "[[File:" .. ability_name .. ".png|link=" .. hyperlink .. "|" .. size .. "px]]"
if (prefix == nil) then prefix = '' end
icon = '<span style="position: relative; bottom: 2px; filter: brightness(0) saturate(100%);">' .. icon .. '</span>'
if (postfix == nil) then postfix = '' end
if (prefix == '{s:sign}') then
local link = "[[" .. hyperlink .. "|" .. custom_display_text .. "]]"
if value < 0 then
prefix = '-'
else
prefix = '+'
end
end
return string.format("%s%s%s", prefix, value, postfix)
return icon .. " " .. link
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
end


return p
return p

Latest revision as of 17:25, 26 October 2024

Deprecated[edit source]

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


local data = mw.loadJsonData("Data:AbilityCards.json")

local p = {}

-- returns the table of a specific item
function get_ability(hero_key, ability_num)
	local ui_data = data[hero_key]
	if(ui_data == nil) then return "Hero Not Found" end
	return ui_data[tonumber(ability_num)]
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

--Write's an ability link for a given ability name in english
--Siphon Life --> Abrams#(1)_Siphon_Life
function ability_to_hyperlink(ability_name_to_search)
	if (ability_name_to_search == nil) then return "No ability name provided" end
	local hero_name
	local ability_name
	
	-- Iterate heros
	for hero_key, hero_data in pairs(data) do
		-- Iterate abilities
		hero_name = hero_data["Name"]
		for ability_num, ability_data in pairs(hero_data) do
			if (ability_num ~= "Name") then
				ability_name = ability_data["Name"]
				if (ability_name == ability_name_to_search) then
					-- Ability number and hero found
					-- "Siphon Life" > "Siphon_Life"
					ability_name = ability_name:gsub(" ", "_")
					return hero_name .. "#(" .. ability_num .. ")_" .. ability_name
				end
			end
		end
	end
	
	return nil
end

--Writes an ability's icon from its name
--Shoulder Charge --> [[File:Shoulder Charge.png|link=Abrams#(1)_Shoulder_Charge|size=20px]] [[Abrams...|Shoulder Charge]]
p.write_ability_icon = function(frame)
	local ability_name = frame.args[1]
	local custom_display_text = frame.args[2]
	if (custom_display_text == nil or custom_display_text == "") then custom_display_text = ability_name end
	
	local size = 20
	
	local hyperlink = ability_to_hyperlink(ability_name)
	if (hyperlink == nil) then return "Ability " .. ability_name .. " not found" end
	
	local icon = "[[File:" .. ability_name .. ".png|link=" .. hyperlink .. "|" .. size .. "px]]"
	icon = '<span style="position: relative; bottom: 2px; filter: brightness(0) saturate(100%);">' .. icon .. '</span>'
	
	local link = "[[" .. hyperlink .. "|" .. custom_display_text .. "]]"
	
	return icon .. " " .. link
end

return p