Module:Abilities/utils: Difference between revisions

From Deadlock Wiki
Jump to navigation Jump to search
Saag (talk | contribs)
added format_value_with_prepost
Saag (talk | contribs)
m made ability_card_data accept hero_key
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
local p = {}
local p = {}
local commonutils = require "Module:Utilities"
local lang = require"Module:Lang"


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


-- returns the table of a specific item
-- returns the table of a specific item'
function p.get_ability_card_data(hero_name, ability_num)
function p.get_ability_card_data(hero_key, ability_num)
local hero_key = get_hero_key(hero_name)
if(hero_key == nil) then return nil end
if(hero_key == nil) then return nil end
return data[hero_key][tonumber(ability_num)]
return data[hero_key][tonumber(ability_num)]
end
end


function get_hero_key(hero_name)
function p.get_hero_key(hero_name)
for i, hero in pairs(data) do
for i, hero in pairs(data) do
if hero["Name"] == hero_name then
if hero["Name"] == hero_name then
Line 20: Line 21:


-- Add prefix and postfix labels to a value. Eg. "32" -> "32s"
-- Add prefix and postfix labels to a value. Eg. "32" -> "32s"
function format_value_with_prepost(key, value, frame)
function p.format_value_with_prepost(key, value, frame, exclude_plus)
if (value == nil) then return nil end
if (value == nil) then return nil end
local prefix = lang._get_string(string.format("%s_prefix",key))
local prefix = lang.get_string(string.format("%s_prefix",key))
local postfix = lang._get_string(string.format("%s_postfix",key))
local postfix = lang.get_string(string.format("%s_postfix",key))
-- Default pre/post fix to empty string, as they may not exist
-- Default pre/post fix to empty string, as they may not exist
Line 31: Line 32:
-- If string ends with 'm', then set the postfix to 'm' and remove it
-- If string ends with 'm', then set the postfix to 'm' and remove it
if utils.string_endswith(tostring(value), 'm') then
if commonutils.string_endswith(tostring(value), 'm') then
value = tonumber(value:sub(1, -2))
value = tonumber(value:sub(1, -2))
postfix = 'm'
postfix = 'm'
Line 39: Line 40:
if value < 0 then  
if value < 0 then  
prefix = '-'
prefix = '-'
else  
else
prefix = '+'
prefix = '+'
end
end
Line 47: Line 48:
prefix = '+'
prefix = '+'
end
end
end
-- some use cases don't require the '+' symbol prefix
if exclude_plus == true and prefix == '+' then
prefix = ''
end
end

Latest revision as of 17:38, 26 October 2024

Overview[edit source]

Common internal functions that are shared amongst any Abilities/ modules

Submodules[edit source]

Abilities - Simple functions. Eg. getting ability name

Abilities/utils - Common internal functions that are shared amongst any Abilities/ modules

Abilities/card - Generates hero ability cards

Abilities/icon - Searches for an icon based on the (English) name of the ability

Abilities/details table (WIP) - Generates details table to show raw ability data


local p = {}
local commonutils = require "Module:Utilities"
local lang = require"Module:Lang"

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

-- returns the table of a specific item'
function p.get_ability_card_data(hero_key, ability_num)
	if(hero_key == nil) then return nil end
	return data[hero_key][tonumber(ability_num)]
end

function p.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

-- Add prefix and postfix labels to a value. Eg. "32" -> "32s"
function p.format_value_with_prepost(key, value, frame, exclude_plus)
	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 string ends with 'm', then set the postfix to 'm' and remove it
	if commonutils.string_endswith(tostring(value), 'm') then
		value = tonumber(value:sub(1, -2))	
		postfix = 'm'
	end
	
	if (prefix == '{s:sign}') then
		if value < 0 then 
			prefix = '-'
		else
			prefix = '+'
		end
	-- if no prefix on positive value, add a '+'
	else
		if value > 0 then
			prefix = '+'
		end
	end
	
	-- some use cases don't require the '+' symbol prefix
	if exclude_plus == true and prefix == '+' then
		prefix = ''	
	end
	
	value_and_uom = frame:expandTemplate{
		title = 'Template:ValueAndUom',
		args = {
			prefix..value, 
			postfix,
			uom_style = 'font-size: 12px;'
		}
	}

	return value_and_uom
end

return p