Module:Abilities/utilsGive feedback
Jump to navigation
Jump to search
Overview
Common internal functions that are shared amongst any Abilities/ modules
Submodules
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 - Functions for retrieving ability and attribute icon data
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 dictionary_module = require("Module:Dictionary")
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 string ends with 's', then set the postfix to 's' and remove it
if commonutils.string_endswith(tostring(value), 's') then
value = tonumber(value:sub(1, -2))
postfix = 's'
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
function p.get_notes_source_page_name(ability_key)
local ability_name_en = data["Name"]
local ability_name_localized = lang.get_string(ability_key)
local notes_str_localized = dictionary_module.translate('Notes', nil, '')
--Determine notes link
local notes_source_page
local notes_untranslated_msg = ''
-- if either "Notes" string or ability_name is not able to be localized,
-- use an english link instead, as transcluded content relies on it
if (ability_name_localized == '') or (notes_str_localized == '') then
notes_source_page = ability_name_en .. '/' .. dictionary_module.translate('Notes', 'en', '')
if notes_str_localized=='' then
notes_untranslated_msg = '<br>' .. dictionary_module.translate('NotesNeedsTranslation') .. ' [[Template:Translate]]' .. "<br>"
end
else
notes_source_page = ability_name_localized .. '/' .. notes_str_localized
end
notes_source_page = "User:Sur/" .. notes_source_page --temp
return notes_source_page
end
-- Generic function to traverse the table
-- A parameter can be passed that is numerical, like 2; and the function will
-- first try to access the key "2", otherwise it will look for the value at index=2 (3rd value)
function p.get_value(frame)
local args = frame.args
local value = item_data
for i = 1, #args do
local key = args[i]
-- First try string key
if type(value) == "table" and value[key] ~= nil then
value = value[key]
-- Then try numeric key if string didn't work
elseif type(value) == "table" then
local num_key = tonumber(key)
if num_key and value[num_key] ~= nil then
value = value[num_key]
else
return ""
end
else
return ""
end
end
-- Return final value, convert to string if not nil
if value == nil then
return ""
else
return tostring(value)
end
end
return p