Module:AbilityData: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Saag (talk | contribs)
switch to new ability card
Saag (talk | contribs)
Initial generation of dynamic info sections
Line 1: Line 1:
local lang = require "Module:Lang"
local lang = require "Module:Lang"
local p = {}
local p = {}
local data = mw.loadJsonData("Data:HeroData.json")
local data = mw.loadJsonData("Data:AbilityCard.json")


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


Line 23: Line 23:
local ability_num = frame.args[2]
local ability_num = frame.args[2]
local info_section_num = frame.args[3]
local info_section_num = frame.args[3]
local ability = get_ability(hero_name, ability_num)
if(ability == nil) then return "Ability Not Found" end
return 'Info section'
local info_section = ability['Info'..info_section_num]
local main_props = info_section.Main.Props
-- Concatenate multiple section boxes into a single output template
local info_box_template = ''
for k, props in pairs(main_props) do
section_box = frame:expandTemplate{
title = "User:Saag/Sandbox/AbilityCard/InfoSection/Box",
args = {
  key = props.Key,
  value = props.Value,
  type = props.Type,
  scale_value = props.Scale and props.Scale.Value or nil,
  scale_type =  props.Scale and props.Scale.Type or nil
}
}
info_box_template = info_box_template .. section_box .. '\n'
end
return info_box_template
end
end


Line 30: Line 52:
local hero_name = frame.args[1]
local hero_name = frame.args[1]
local ability_num = frame.args[2]
local ability_num = frame.args[2]
 
-- local ability = get_ability(hero_name, ability_num)
-- if(ability == nil) then return "Ability Not Found" end
return 'Upgrades'
return 'Upgrades'
end
end
Line 41: Line 66:


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


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


end
-- end


local name_link = string.format("%s#(%s)_%s", hero_name, ability_num, ability.Name)
local name_link = string.format("%s#(%s)_%s", hero_name, ability_num, ability.Name)
return frame:expandTemplate{
return frame:expandTemplate{
-- TODO - remove when testing is done
-- TODO - remove when testing is done
title = "User:Saag/Sandbox/AbilityCard",
title = "User:Saag/Sandbox/AbilityCard",
args = {
args = {
name = ability.Name,
hero_name = hero_name,
name_link = name_link,
ability_num = ability_num,
description = format_value_with_prepost("Description", ability.Description),
name = ability.Name,
radius = format_value_with_prepost("Radius", ability.Radius),
name_link = name_link,
range = format_value_with_prepost("AbilityCastRange", ability.AbilityCastRange),
description = format_value_with_prepost("Description", ability.Description),
ability_width = format_value_with_prepost(width_key, ability[width_key]),
radius = format_value_with_prepost("Radius", ability.Radius),
duration = format_value_with_prepost("AbilityDuration", ability.AbilityDuration),
range = format_value_with_prepost("AbilityCastRange", ability.AbilityCastRange),
cooldown = format_value_with_prepost("AbilityCooldown", ability.AbilityCooldown),
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),
}
}
else
}
return "Ability data not found for hero " .. hero_name .. " and num " .. ability_num
end
end
end


Line 89: Line 115:
end
end


-- function get_effects(ability, used_keys)
function get_effects(ability, used_keys)
-- local effects = {}
local effects = {}
-- for key, value in pairs(ability) do
for key, value in pairs(ability) do
-- if not string_in_list(key, used_keys) then
if not string_in_list(key, used_keys) then
-- local label = lang._get_string(string.format("%s_label", key))
local label = lang._get_string(string.format("%s_label", key))


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


-- Add prefix and postfix labels to a value. Eg. "32" -> "32s"
-- Add prefix and postfix labels to a value. Eg. "32" -> "32s"

Revision as of 15:05, 22 September 2024

Deprecated

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


local lang = require "Module:Lang"
local p = {}
local data = mw.loadJsonData("Data:AbilityCard.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][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

p.get_info_section = 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]
	local main_props = info_section.Main.Props
	
	-- Concatenate multiple section boxes into a single output template 
	local info_box_template = ''
	for k, props in pairs(main_props) do
		section_box = frame:expandTemplate{
			title = "User:Saag/Sandbox/AbilityCard/InfoSection/Box",
			args = {
			  key = props.Key,
			  value = props.Value,
			  type = props.Type,
			  scale_value = props.Scale and props.Scale.Value or nil,
			  scale_type =  props.Scale and props.Scale.Type or nil
			}
		}
		info_box_template = info_box_template .. section_box .. '\n'
	end
	
	return info_box_template
end

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
	
	return 'Upgrades'
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
	
	-- 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/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),
			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),
		}
	}
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