Module:HeroData: Difference between revisions

LVL (talk | contribs)
Made the hero attribute table hero column and stat row sticky.
LVL (talk | contribs)
Remove unused write_infobox and write_stat_infoboxes functions
Line 306: Line 306:
return scaling_data_returned
return scaling_data_returned
end
--HERO_NAME in english
--{{#invoke:HeroData|write_infobox|HERO_NAME}}--
p.write_infobox = function(frame)
-- Get hero data
    hero_key = frame.args[1]
    hero_data = heroes_data[hero_key]
    if(hero_data == nil) then return "Hero " .. hero_key .. " Not Found" end
   
    local infobox_attributes = {
        Weapon = {'DPS','ClipSize','RoundsPerSecond','ReloadTime'},
        Vitality = {'MaxHealth','BulletResist','TechResist','MaxMoveSpeed'}
    }
   
    -- Declarations and initializations
    local category_data = attribute_module.get_category_data()
    local stats
    local stat_data
    local stat_value
    local stat_text
    local label
    local postfix
    local should_display
    local image_file_name
    local image_file
    local icon_and_value_str
    local template_args = {}
    local stat_values = {Weapon = "", Vitality = ""}
   
    -- Add the main parameters
    template_args["name_english"] = lang_module.get_string(hero_key, 'en')
    template_args["name_localized"] = localize(hero_key, hero_key)
   
    -- Iterate attribute categories
    for category, stats in pairs(infobox_attributes) do
-- Iterate stats
for _, stat_name in ipairs(stats) do
-- Confirm its in attribute data and retrieve it
attribute_data = attributes_data[category]
if (attribute_data == nil) then return "Category " .. category .. " in infobox_attributes is not in Data:AttributeData" end
stat_data = attribute_data[stat_name]
if (stat_data == nil) then return "Attribute " .. stat_name .. " in infobox_attributes is not in Data:AttributeData" end
-- gets the stat's value
stat_value = hero_data[stat_name]
if (stat_value == nil) then
    -- Check nested weapon stats
    if hero_data.Weapon and hero_data.Weapon[stat_name] ~= nil then
        stat_value = hero_data.Weapon[stat_name]
    elseif hero_data.Weapon and hero_data.Weapon.AltFire and hero_data.Weapon.AltFire[stat_name] ~= nil then
        stat_value = hero_data.Weapon.AltFire[stat_name]
    else
        stat_value = 0 --default to 0 if not present
    end
end
--Round value to 3 significant figures
stat_value = util_module.round_to_sig_fig(stat_value, 3)
-- get label and postfix
label = localize(stat_data["label"], stat_name)
postfix = stat_data["postfix"]
if (postfix == nil) then
postfix = ""
else
-- light grey for postfixes
postfix = lang_module.get_string(postfix)
if (postfix == nil) then
postfix = ""
end
postfix = '<span style="color: #666666;">' .. postfix .. "</span>"
end
-- if a language is missing the postfix, use no postfix
-- Check if icon file exists, and if not, don't use any image
image_file_name = 'File:AttributeIcon' .. stat_name .. '.png'
image_file = util_module.get_image_file(image_file_name, 15, stat_name)
-- Create the template'd icon
local icon_color = attribute_module.get_attr_icon_color(stat_name)
icon_template_title = mw.title.new("Template:Icon/" .. icon_color)
icon_template_args = {}
icon_template_args[1] = image_file
icon_template_args[2] = stat_value .. postfix
icon_and_value_str = mw.getCurrentFrame():expandTemplate{ title = icon_template_title, args = icon_template_args }
label =  ',<span style="color: #999999; background-color: #2F2F2F; display: inline-flex; width: 100%; height: 100%; justify-content: center; align-items: center;">' .. label .. '</span>'
-- First add localized stat name for left column
stat_values[category] = stat_values[category] .. label
-- Then add icon and stat value for right column
stat_values[category] = stat_values[category] .. ',' .. icon_and_value_str
end
-- Add the stat values in the category to i.e weapon_values and vitality_values parameters
template_args[string.lower(category) .. "_values"] = stat_values[category]
end
   
    -- Use expandTemplate to evaluate the Infobox_hero template
    local template_title = mw.title.new("User:Sur")
    local expanded_template = mw.getCurrentFrame():expandTemplate{ title = template_title, args = template_args }
   
    return expanded_template
end
--{{#invoke:HeroData|write_stat_infoboxes|HERO_KEY}}
--Creates {{Infobox_stat}}'s' for the 3 categories Weapon, Vitality, Spirit
p.write_stat_infoboxes = function(frame)
local hero_key = frame.args[1]
if(hero_key == nil) then return "Hero parameter missing" end
-- Use expandTemplate to evaluate the Infobox_hero template
    local template_title = mw.title.new("Template:Infobox_stat") --name of the template
    local template_calls = '<div style="display: flex; flex-direction: column;">' --all template calls concatenated
    local template_call --current template call
local template_args = {} --current template arguments
local cell_values --current cell values
local cell_value --current cell value
local label --current stat's label
local postfix --current stat's postfix
local stat_value --current stat's numerical value in the hero data
local hero_data = heroes_data[hero_key]
local stats --stats of the current category
local image_file_name --name of the image_file to check
local image_file --mw returned image file
local extra_blank_cell --add a blank cell after certain stats
local stats_to_add_blank_after = {CritDamageReceivedPercent = true}
if (hero_data == nil) then return "Hero Not Found" end
local category_data = attribute_module.get_category_data()
local should_display
for _, category in ipairs(attribute_orders["category_order"]) do
if (category ~= "Spirit") then --hide Spirit section for now
stats = attributes_data[category]
local category_values = category_data[category]
template_args["box_name"] = category_values.unlocalized_name
template_args["box_rgb"] = category_values.rgb
template_args["num_cols"] = 2
cell_values = ""
-- Determine cell values
for _, stat_name in ipairs(attribute_orders[category]["attribute_order"]) do
local stat_data = stats[stat_name]
if stat_data == nil then return "Stat " .. stat_name .. " from StatInfoboxOrder has no data in AttributeData" end
-- gets the stat's value if it has that stat, default to 0 if not
stat_value = hero_data[stat_name]
if (stat_value == nil) then
    -- Check nested weapon stats
    if hero_data.Weapon and hero_data.Weapon[stat_name] ~= nil then
        stat_value = hero_data.Weapon[stat_name]
    elseif hero_data.Weapon and hero_data.Weapon.AltFire and hero_data.Weapon.AltFire[stat_name] ~= nil then
        stat_value = hero_data.Weapon.AltFire[stat_name]
    else
        stat_value = 0 --default to 0 if not present
    end
end
--Round value to 3 significant figures
stat_value = util_module.round_to_sig_fig(stat_value, 3)
-- get label and postfix
label = localize(stat_data["label"], stat_name)
postfix = stat_data["postfix"]
if (postfix == nil) then
postfix = ""
else
-- light grey for postfixes
postfix = '<span style="color: #666666;">' .. lang_module.get_string(postfix) .. "</span>"
end
-- if a language is missing the postfix, use no postfix
if (postfix == nil) then
postfix = ""
end
-- Check if icon file exists, and if not, don't use any image
image_file_name = 'File:AttributeIcon' .. stat_name .. '.png'
-- 15px and link to stat name (page name might not match perfectly yet)
image_file = util_module.get_image_file(image_file_name, 15, stat_name)
-- Create the template'd icon
local icon_color = attribute_module.get_attr_icon_color(stat_name)
if (icon_color == "Brown") then
image_file = '<span style="position: relative; bottom: 2px; filter: invert(42%) sepia(10%) saturate(2912%) hue-rotate(351deg) brightness(90%) contrast(87%);">' .. image_file .. '</span>'
end
-- use white instead of grey if grey is returned
-- Add an empty cell following some stats to align them correctly as seen in game
if (stats_to_add_blank_after[stat_name]) then
extra_blank_cell = " ," --prefixed space is needed
else
extra_blank_cell = ""
end
-- slightly lighter color for stat name
label = '<span style="color: #3d3d3d;">' .. label .. '</span>'
-- Add the scaling str if it scales
local scaling_data = p.get_hero_scaling_data(hero_data, stat_name)
local scaling_strs = ""
if (scaling_data ~= nil) then
for scaling_value, scaling_type in pairs(scaling_data) do
local scaling_str = p.write_scalar_str(scaling_value, scaling_type)
if (scaling_str ~= "") then scaling_str = " " .. scaling_str end
scaling_strs = scaling_strs .. scaling_str
end
end
-- Set cell value as "Icon StatvaluePostfix Statname Scaling,"
-- If icon file already exists, set it to #REDIRECT to the duplicate file page
cell_value = image_file .. " " .. stat_value  .. postfix .. " " .. label  .. scaling_strs .. "," .. extra_blank_cell
cell_values = cell_values .. cell_value --add value to values list
end
template_args["cell_values"] = cell_values
-- Write current call
template_call = mw.getCurrentFrame():expandTemplate{ title = template_title, args = template_args }
-- Add call to the set of calls
template_calls = template_calls .. "\n" .. template_call
end
end
return template_calls .. "</div>"
end
end