Module:Skill: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
>OmegaK2
(Force execution order, skill_icon should now be working properly)
(The skill_icon parameter can be used to specify an override for the default icon based on the name of the skill; The skill_screenshot_file parameter is now deprecated.)
 
(94 intermediate revisions by 6 users not shown)
Line 1: Line 1:
local p = {}
-------------------------------------------------------------------------------
--
--                              Module:Skill
--
-- This module implements Template:Skill and Template:Skill progression
-------------------------------------------------------------------------------


local getArgs = require('Module:Arguments').getArgs
require('Module:No globals')
local m_util = require('Module:Util')
local m_util = require('Module:Util')
local m_game = require('Module:Game')
local m_cargo = require('Module:Cargo')
 
-- Should we use the sandbox version of our submodules?
local use_sandbox = m_util.misc.maybe_sandbox('Skill')
 
local m_game = use_sandbox and mw.loadData('Module:Game/sandbox') or mw.loadData('Module:Game')
 
-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
local cfg = use_sandbox and mw.loadData('Module:Skill/config/sandbox') or mw.loadData('Module:Skill/config')


local mwlanguage = mw.language.getContentLanguage()
local mwlanguage = mw.language.getContentLanguage()


--
local i18n = cfg.i18n
-- Data
local tables = {}
--
local data = {}
 
-- ----------------------------------------------------------------------------
-- Helper functions
-- ----------------------------------------------------------------------------
local h = {}
 
function h.map_to_arg(tpl_args, properties, prefix_in, map, level, set_name, set_id)
    if map.fields then
        for key, row in pairs(map.fields) do
            if row.name then
                local val = tpl_args[prefix_in .. row.name]
                if row.func ~= nil then
                    val = row.func(tpl_args, val)
                end
                if val == nil and row.default ~= nil then
                    val = row.default
                end
                if val ~= nil then
                    if level ~= nil then
                        if set_name then
                            tpl_args.skill_levels[level][set_name] = tpl_args.skill_levels[level][set_name] or {}
                            tpl_args.skill_levels[level][set_name][set_id] = tpl_args.skill_levels[level][set_name][set_id] or {}
                            tpl_args.skill_levels[level][set_name][set_id][key] = val
                        else
                            tpl_args.skill_levels[level][key] = val
                        end
 
                        -- Nuke variables since they're remapped to skill_levels
                        tpl_args[prefix_in .. row.name] = nil
                    else
                        if set_name then
                            tpl_args[set_name] = tpl_args[set_name] or {}
                            tpl_args[set_name][set_id] = tpl_args[set_name][set_id] or {}
                            tpl_args[set_name][set_id][key] = val
 
                            -- Nuke variables since they're remapped to [set_name]
                            tpl_args[prefix_in .. row.name] = nil
                        else
                            tpl_args[key] = val
                        end
                    end
                    properties[row.field] = val
 
                    -- Deprecated parameters
                    if val and row.deprecated then
                        tpl_args._flags.has_deprecated_skill_parameters = true
                        if tpl_args.test then -- Log when testing
                            tpl_args.deprecated_parameters = tpl_args.deprecated_parameters or {}
                            tpl_args.deprecated_parameters[#tpl_args.deprecated_parameters+1] = {row.name, val}
                        end
                    end
                end
            end
        end
    end
end


local i18n = {
function h.expand_costs_data(tpl_args, skill_levels)
     skill_icon = 'File:%s skill icon.png',
     --[[
      
     Expand costs data so that each cost type has its own column with amounts
     args = {
     Assumptions:
        -- skills
      Cost types are always static
         skill_id = 'skill_id',
      Cost amounts can either be static or leveled, but not both
         is_support_gem = 'is_support_gem',
    --]]
        support_gem_letter = 'support_gem_letter',
    if skill_levels[0] then
        cast_time = 'cast_time',
         local cost_types = m_util.cast.table(skill_levels[0].cost_types)
        gem_description = 'gem_description',
         if #cost_types > 0 then
        active_skill_name = 'active_skill_name',
            for _, level_data in pairs(skill_levels) do
        skill_icon = 'skill_icon',
                if type(level_data) == 'table' and level_data.cost_amounts then
        item_class_restriction = 'item_class_restriction',
                    local cost_amounts = m_util.cast.table(level_data.cost_amounts, {callback = m_util.cast.number})
        projectile_speed = 'projectile_speed',
                    for i=1, #cost_types do
        stat_text = 'stat_text',
                        local type = cost_types[i]
        quality_stat_text = 'quality_stat_text',
                        local amount = cost_amounts[i]
        has_percentage_mana_cost = 'has_percentage_mana_cost',
                        if amount then
        has_reservation_mana_cost = 'has_reservation_mana_cost',
                            level_data['cost_' .. type] = amount
        radius = 'radius',
                        end
        radius_description = 'radius_description',
                    end
        radius_secondary = 'radius_secondary',
                end
         radius_secondary_description = 'radius_secondary_description',
            end
        radius_tertiary = 'radius_tertiary',
         end
        radius_tertiary_description = 'radius_tertiary_description',
    end
end


        -- skill_levels
function h.stats(tpl_args, prefix_in, level)
        level_requirement = 'level_requirement',
    for i=1, cfg.max_stats_per_level do
         dexterity_requirement = 'dexterity_requirement',
         local stat_prefix = string.format('%s%s%d_', prefix_in, i18n.parameters.skill.stat, i) -- level<level>_stat<i>_
        intelligence_requirement = 'intelligence_requirement',
         local stat = {
         strength_requirement = 'strength_requirement',
            id = tpl_args[stat_prefix .. tables.skill_stats_per_level.fields.id.name], --level<level>_stat<i>_id
        mana_multiplier = 'mana_multiplier',
            value = tpl_args[stat_prefix .. tables.skill_stats_per_level.fields.value.name], --level<level>_stat<i>_value
        critical_strike_chance = 'critical_strike_chance',
         }
         mana_cost = 'mana_cost',
         if stat.id ~= nil and stat.value ~= nil then
         damage_effectiveness = 'damage_effectiveness',
            local properties = {
        stored_uses = 'stored_uses',
                _table = tables.skill_stats_per_level.table,
        cooldown = 'cooldown',
                [tables.skill_stats_per_level.fields.level.field] = level,
        vaal_souls_requirement = 'vaal_souls_requirement',
            }
        vaal_stored_uses = 'vaal_stored_uses',
            h.map_to_arg(tpl_args, properties, stat_prefix, tables.skill_stats_per_level, level, 'stats', i)
        damage_multiplier = 'damage_multiplier',
            tpl_args.skill_levels.has_stats = true
        experience = 'experience',
            if not tpl_args.test then
        stat_text = 'stat_text',
                m_cargo.store(properties)
        quality_stat_text = 'quality_stat_text',
            end
       
         end
        -- skill_stats_per_level
     end
        id = 'id',
end
        value = 'value',
       
        -- prefixes
        prefix_level = 'level',
        prefix_static = 'static_',
        prefix_quality_stat = 'quality_',
         infix_stat = 'stat'
     },
}


--
function h.int_value_or_na(tpl_args, tblrow, value, tmap)
-- Data
    value = tonumber(value)
--
    if value == nil then
        tblrow:node(m_util.html.td.na())
    else
        -- value = mwlanguage:formatNum(value) -- Removed for now. lang:formatNum() returns a string, which causes issues for formatting
        if tmap.fmt ~= nil then
            if type(tmap.fmt) == 'string' then
                value = string.format(tmap.fmt, value)
            elseif type(tmap.fmt) == 'function' then
                value = string.format(tmap.fmt(tpl_args) or '%s', value)
            end
        end
        tblrow
            :tag('td')
                :wikitext(value)
                :done()
    end
end


local data = {}
h.cast = {}
data.cast = {}
function h.cast.wrap(func)
data.cast.wrap = function(f)
     return function(tpl_args, value)
     return function(tpl_args, frame, value)
         if value == nil then
         if value == nil then
             return nil
             return nil
        end
        return func(value)
    end
end
h.display = {}
h.display.factory = {}
function h.display.factory.value(args)
    return function (tpl_args)
        args.fmt = args.fmt or tables.static.fields[args.key].fmt
        local value = tpl_args[args.key]
        if args.fmt and value then
            return string.format(args.fmt, value)
        else
            return value
        end
    end
end
function h.display.factory.range_value(args)
    return function (tpl_args)
        local value = {}
        if args.set_name and args.set_id then
            -- Guard against index errors
            tpl_args.skill_levels[0][args.set_name] = tpl_args.skill_levels[0][args.set_name] or {}
            tpl_args.skill_levels[0][args.set_name][args.set_id] = tpl_args.skill_levels[0][args.set_name][args.set_id] or {}
            tpl_args.skill_levels[1][args.set_name] = tpl_args.skill_levels[1][args.set_name] or {}
            tpl_args.skill_levels[1][args.set_name][args.set_id] = tpl_args.skill_levels[1][args.set_name][args.set_id] or {}
            tpl_args.skill_levels[tpl_args.max_level][args.set_name] = tpl_args.skill_levels[tpl_args.max_level][args.set_name] or {}
            tpl_args.skill_levels[tpl_args.max_level][args.set_name][args.set_id] = tpl_args.skill_levels[tpl_args.max_level][args.set_name][args.set_id] or {}
            value.min = tpl_args.skill_levels[0][args.set_name][args.set_id][args.key] or tpl_args.skill_levels[1][args.set_name][args.set_id][args.key]
            value.max = tpl_args.skill_levels[0][args.set_name][args.set_id][args.key] or tpl_args.skill_levels[tpl_args.max_level][args.set_name][args.set_id][args.key]
        else
            value.min = tpl_args.skill_levels[0][args.key]
            if value.min == nil or type(value.min) == 'table' and #value.min == 0 then
                value.min = tpl_args.skill_levels[1][args.key]
            end
            value.max = tpl_args.skill_levels[0][args.key]
            if value.max == nil or type(value.max) == 'table' and #value.max == 0 then
                value.max = tpl_args.skill_levels[tpl_args.max_level][args.key]
            end
            if type(value.min) == 'table' and type(value.max) == 'table' and args.key_index then
                value.min = value.min[args.key_index]
                value.max = value.max[args.key_index]
            end
        end
        if value.min == nil or value.max == nil then
            -- property not set for this skill
            return nil
        end
        local map = args.map or tables.progression
        local options = {
            fmt=args.fmt or map.fields[args.key] and map.fields[args.key].fmt,
            color=false,
        }
        if type(value.min) == 'table' and type(value.max) == 'table' then
            local formatted_values = {}
            for i=1, #value.min do
                formatted_values[i] = m_util.html.format_value(tpl_args, {min = value.min[i], max = value.max[i]}, options)
            end
            return formatted_values
        end
        return m_util.html.format_value(tpl_args, value, options)
    end
end
function h.display.factory.radius(args)
    return function (tpl_args)
        local radius = tpl_args['radius' .. args.key]
        if radius == nil then
            return
        end
        local description = tpl_args[string.format('radius%s_description', args.key)]
        if description then
            return m_util.html.abbr(radius, description)
         else
         else
             return f(value)
             return radius
         end
         end
     end
     end
end
end


local map = {}
function h.query_skill(tpl_args)
map.stats = {
    local fields = {
     {
        'skill._pageID=_pageID',
         prefix_in = '',
    }
         out = 'stats',
    local query = {
         quality = false,
        groupBy = 'skill._pageID',
     },
    }
     {
    local results = {}
         prefix_in = i18n.args.prefix_quality_stat,
    local search_param
         out = 'quality_stats',
     if tpl_args.skill_id then -- Query by skill id
         quality = true,
         query.where = string.format('skill_id="%s"', tpl_args.skill_id)
     },
        search_param = 'skill_id'
}
    else -- Query by page name
        local page = tpl_args.page or mw.title.getCurrentTitle().prefixedText
         query.where = string.format('_pageName="%s"', page)
         search_param = 'page'
    end
     results = m_cargo.query({tables.static.table}, fields, query)
     if #results == 0 then
        -- No results found
         error(string.format(i18n.errors.validate_skill.no_results_found, search_param, tpl_args[search_param]))
    elseif #results > 1 then
         -- More than one result found
         error(string.format(i18n.errors.validate_skill.many_results_found, search_param, tpl_args[search_param]))
    end
     return results[1]
end
 
-- ----------------------------------------------------------------------------
-- Cargo tables
-- ----------------------------------------------------------------------------


map.static = {
tables.static = {
     table = 'skill',
     table = 'skill',
    order = {'skill_id', 'cast_time', 'gem_description', 'active_skill_name', 'skill_icon', 'item_class_restriction', 'projectile_speed', 'stat_text', 'quality_stat_text', 'has_percentage_mana_cost', 'has_reservation_mana_cost', 'radius', 'radius_secondary', 'radius_tertiary', 'radius_description', 'radius_secondary_description', 'radius_tertiary_description'},
     fields = {
     fields = {
         -- GrantedEffects.dat
         -- GrantedEffects.dat
         skill_id = {
         skill_id = {
             name = i18n.args.skill_id,
             name = i18n.parameters.skill.skill_id,
             field = 'skill_id',
             field = 'skill_id',
             type = 'String',
             type = 'String',
             func = nil,
             func = nil,
         },
         },
        --[[is_support_gem = {
            name = i18n.args.is_support_gem,
            field = 'is_support_gem',
            type = 'Boolean',
            func = data.cast.wrap(m_util.cast.boolean),
        },
        support_gem_letter = {
            name = i18n.args.support_gem_letter,
            field = 'support_gem_letter',
            type = 'String (size=2)',
            func = nil,
        },-]]--
         -- Active Skills.dat
         -- Active Skills.dat
         cast_time = {
         cast_time = {
             name = i18n.args.cast_time,
             name = i18n.parameters.skill.cast_time,
             field = 'cast_time',
             field = 'cast_time',
             type = 'Float',
             type = 'Float',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            fmt = '%.2f ' .. m_game.units.seconds.short_lower,
         },
         },
         gem_description = {
         gem_description = {
             name = i18n.args.gem_description,
             name = i18n.parameters.skill.gem_description,
             field = 'description',
             field = 'description',
             type = 'Text',
             type = 'Text',
Line 133: Line 292:
         },
         },
         active_skill_name = {
         active_skill_name = {
             name = i18n.args.active_skill_name,
             name = i18n.parameters.skill.active_skill_name,
             field = 'active_skill_name',
             field = 'active_skill_name',
             type = 'String',
             type = 'String',
Line 139: Line 298:
         },
         },
         skill_icon = {
         skill_icon = {
             name = i18n.args.skill_icon,
             name = i18n.parameters.skill.skill_icon,
             field = 'skill_icon',
             field = 'skill_icon',
             type = 'Page',
             type = 'Page',
             func = function(tpl_args, frame)
             func = function(tpl_args, value)
                 return string.format(i18n.skill_icon, tpl_args.active_skill_name)
                 if value then
                    value = string.format(i18n.files.skill_icon, value)
                elseif tpl_args.active_skill_name then
                    value = string.format(i18n.files.skill_icon, tpl_args.active_skill_name)
                else
                    value = nil
                end
                return value
             end,
             end,
         },
         },
         item_class_restriction = {
         item_class_id_restriction = {
             name = i18n.args.item_class_restriction,
             name = i18n.parameters.skill.item_class_id_restriction,
             field = 'item_class_restriction',
             field = 'item_class_id_restriction',
             type = 'List (,) of String',
             type = 'List (,) of String',
             func = function(tpl_args, frame, value)
             func = function(tpl_args, value)
                 if value == nil then  
                 if value == nil then  
                     return nil
                     return nil
Line 156: Line 322:
                 value = m_util.string.split(value, ', ')
                 value = m_util.string.split(value, ', ')
                 for _, v in ipairs(value) do
                 for _, v in ipairs(value) do
                     local result = m_util.table.find_in_nested_array{value=v,key='full', tbl=m_game.constants.item.class}
                     if m_game.constants.item.classes[v] == nil then
                    if result == nil then
                         error(string.format(i18n.errors.skill.invalid_item_class_id, v))
                         error(string.format('Invalid item class: %s', v))
                     end
                     end
                 end
                 end
                 return value
                 return value
            end,
        },
        item_class_restriction = {
            name = i18n.parameters.skill.item_class_restriction,
            field = 'item_class_restriction',
            type = 'List (,) of String',
            func = function(tpl_args, value)
                if tpl_args.item_class_id_restriction == nil then
                    return
                end
                -- This function makes a localized list based on ids
                local item_classes = {}
                for _, v in ipairs(tpl_args.item_class_id_restriction) do
                    item_classes[#item_classes+1] = m_game.constants.item.classes[v].full
                end
               
                return item_classes
             end,
             end,
         },
         },
         -- Projectiles.dat - manually mapped to the skills
         -- Projectiles.dat - manually mapped to the skills
         projectile_speed = {
         projectile_speed = {
             name = i18n.args.projectile_speed,
             name = i18n.parameters.skill.projectile_speed,
             field = 'projectile_speed',
             field = 'projectile_speed',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
         },
         },
         -- Misc data derieved from stats
         -- Misc data derieved from stats
         stat_text = {
         stat_text = {
             name = i18n.args.stat_text,
             name = i18n.parameters.skill.stat_text,
             field = 'stat_text',
             field = 'stat_text',
            type = 'Text',
            func = nil,
        },
        quality_stat_text = {
            name = i18n.args.quality_stat_text,
            field = 'quality_stat_text',
             type = 'Text',
             type = 'Text',
             func = nil,
             func = nil,
         },
         },
         -- Misc data currently not from game data
         -- Misc data currently not from game data
        has_percentage_mana_cost = {
            name = i18n.args.has_percentage_mana_cost,
            field = 'has_percentage_mana_cost',
            type = 'Boolean',
            func = data.cast.wrap(m_util.cast.boolean),
            default = false,
        },
        has_reservation_mana_cost = {
            name = i18n.args.has_reservation_mana_cost,
            field = 'has_reservation_mana_cost',
            type = 'Boolean',
            func = data.cast.wrap(m_util.cast.boolean),
            default = false,
        },
         radius = {
         radius = {
             name = i18n.args.radius,
             name = i18n.parameters.skill.radius,
             field = 'radius',
             field = 'radius',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
         },
         },
         radius_description = {
         radius_description = {
             name = i18n.args.radius_description,
             name = i18n.parameters.skill.radius_description,
             field = 'radius_description',
             field = 'radius_description',
             type = 'Text',
             type = 'Text',
             func = nil,
             func = h.cast.wrap(m_util.cast.text),
         },
         },
         radius_secondary = {
         radius_secondary = {
             name = i18n.args.radius_secondary,
             name = i18n.parameters.skill.radius_secondary,
             field = 'radius_secondary',
             field = 'radius_secondary',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
         },
         },
         radius_secondary_description = {
         radius_secondary_description = {
             name = i18n.args.radius_secondary_description,
             name = i18n.parameters.skill.radius_secondary_description,
             field = 'radius_secondary_description',
             field = 'radius_secondary_description',
             type = 'Text',
             type = 'Text',
             func = nil,
             func = h.cast.wrap(m_util.cast.text),
         },
         },
         -- not sure if any skill actually has 3 radius componets
         radius_tertiary = { -- not sure if any skill actually has 3 radius componets
        radius_tertiary = {
             name = i18n.parameters.skill.radius_tertiary,
             name = i18n.args.radius_tertiary,
             field = 'radius_tertiary',
             field = 'radius_tertiary',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
         },
         },
         radius_tertiary_description = {
         radius_tertiary_description = {
             name = i18n.args.radius_tertiary_description,
             name = i18n.parameters.skill.radius_tertiary_description,
             field = 'radius_tertiary_description',
             field = 'radius_tertiary_description',
             type = 'Text',
             type = 'Text',
             func = nil,
             func = h.cast.wrap(m_util.cast.text),
        },
        skill_screenshot = {
            name = i18n.parameters.skill.skill_screenshot,
            field = 'skill_screenshot',
            type = 'Page',
            func = function(tpl_args, value)
                if tpl_args.skill_screenshot_file then
                    tpl_args._flags.has_deprecated_skill_parameters = true
                    value = string.format('File:%s', tpl_args.skill_screenshot_file)
                elseif value then
                    value = string.format(i18n.files.skill_screenshot, value)
                elseif tpl_args.active_skill_name then
                    -- When this parameter is set manually, we assume/expect it to exist, but otherwise it probably doesn't and we don't need dead links in that case
                    value = string.format(i18n.files.skill_screenshot, tpl_args.active_skill_name)
                    local page = mw.title.new(value)
                    if page == nil or not page.exists then
                        value = nil
                    end
                else
                    value = nil
                end
                return value
            end,
         },
         },
         -- Set manually
         -- Set programmatically
         max_level = {
         max_level = {
            name = nil,
             field = 'max_level',
             field = 'max_level',
             type = 'Integer',
             type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        html = {
            name = nil,
            field = 'html',
            type = 'Text',
            func = nil,
         },
         },
     },
     },
}
}


map.progression = {
tables.progression = {
     table = 'skill_levels',
     table = 'skill_levels',
    order = {'level', 'level_requirement', 'dexterity_requirement', 'intelligence_requirement', 'strength_requirement', 'mana_multiplier', 'critical_strike_chance', 'mana_cost', 'damage_effectiveness', 'stored_uses', 'cooldown', 'vaal_souls_requirement', 'vaal_stored_uses', 'damage_multiplier', 'experience', 'stat_text', 'quality_stat_text'},
     fields = {
     fields = {
         level = {
         level = {
Line 253: Line 444:
             type = 'Integer',
             type = 'Integer',
             func = nil,
             func = nil,
            header = nil,
         },
         },
         level_requirement = {
         level_requirement = {
             name = i18n.args.level_requirement,
             name = i18n.parameters.skill.level_requirement,
             field = 'level_requirement',
             field = 'level_requirement',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = m_util.html.abbr('[[Image:Level_up_icon_small.png|link=|Lvl.]]', 'Required Level', 'nounderline'),
         },
         },
         dexterity_requirement = {
         dexterity_requirement = {
             name = i18n.args.dexterity_requirement,
             name = i18n.parameters.skill.dexterity_requirement,
             field = 'dexterity_requirement',
             field = 'dexterity_requirement',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = m_util.html.abbr('[[Image:DexterityIcon_small.png|link=|dexterity]]', 'Required Dexterity', 'nounderline'),
         },
         },
         strength_requirement = {
         strength_requirement = {
             name = i18n.args.strength_requirement,
             name = i18n.parameters.skill.strength_requirement,
             field = 'strength_requirement',
             field = 'strength_requirement',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = m_util.html.abbr('[[Image:StrengthIcon_small.png|link=|strength]]', 'Required Strength', 'nounderline'),
         },
         },
         intelligence_requirement = {
         intelligence_requirement = {
             name = i18n.args.intelligence_requirement,
             name = i18n.parameters.skill.intelligence_requirement,
             field = 'intelligence_requirement',
             field = 'intelligence_requirement',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = m_util.html.abbr('[[Image:IntelligenceIcon_small.png|link=|intelligence]]', 'Required Intelligence', 'nounderline'),
         },
         },
         mana_multiplier = {
         cost_multiplier = {
             name = i18n.args.mana_multiplier,
             name = i18n.parameters.skill.cost_multiplier,
             field = 'mana_multiplier',
             field = 'cost_multiplier',
             type = 'Float',
             type = 'Float',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = 'Mana<br>Multiplier',
             fmt = '%s%%',
             fmt = '%s%%',
        },
        attack_time = {
            name = i18n.parameters.skill.attack_time,
            field = 'attack_time',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%.2f ' .. m_game.units.seconds.short_lower,
         },
         },
         critical_strike_chance = {
         critical_strike_chance = {
             name = i18n.args.critical_strike_chance,
             name = i18n.parameters.skill.critical_strike_chance,
             field = 'critical_strike_chance',
             field = 'critical_strike_chance',
             type = 'Float',
             type = 'Float',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = 'Critical<br>Strike<br>Chance',
             fmt = '%s%%',
             fmt = '%s%%',
        },
        mana_cost = {
            name = i18n.args.mana_cost,
            field = 'mana_cost',
            type = 'Integer',
            func = data.cast.wrap(m_util.cast.number),
            header = function (skill_data)
                if skill_data["skill.has_reservation_mana_cost"] then
                    return 'Mana<br>Reserved'
                else
                    return 'Mana<br>Cost'
                end
            end,
            fmt = function (tpl_args, frame, value)
                local str
                if tpl_args.has_percentage_mana_cost then
                    str = '%s%%'
                else
                    str = '%s'
                end
           
                return string.format(str, value)
            end,
         },
         },
         damage_effectiveness = {
         damage_effectiveness = {
             name = i18n.args.damage_effectiveness,
             name = i18n.parameters.skill.damage_effectiveness,
             field = 'damage_effectiveness',
             field = 'damage_effectiveness',
             type = 'Float',
             type = 'Float',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = 'Damage<br>Effectiveness',
             fmt = '%s%%',
             fmt = '%s%%',
         },
         },
         stored_uses = {
         stored_uses = {
             name = i18n.args.stored_uses,
             name = i18n.parameters.skill.stored_uses,
             field = 'stored_uses',
             field = 'stored_uses',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = 'Stored<br>Uses',
         },
         },
         cooldown = {
         cooldown = {
             name = i18n.args.cooldown,
             name = i18n.parameters.skill.cooldown,
             field = 'cooldown',
             field = 'cooldown',
             type = 'Float',
             type = 'Float',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = 'Cooldown',
             fmt = '%.2f ' .. m_game.units.seconds.short_lower,
             fmt = '%ss',
         },
         },
         vaal_souls_requirement = {
         vaal_souls_requirement = {
             name = i18n.args.vaal_souls_requirement,
             name = i18n.parameters.skill.vaal_souls_requirement,
             field = 'vaal_souls_requirement',
             field = 'vaal_souls_requirement',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            header = 'Vaal<br>souls',
         },
         },
         vaal_stored_uses = {
         vaal_stored_uses = {
             name = i18n.args.vaal_stored_uses,
             name = i18n.parameters.skill.vaal_stored_uses,
             field = 'vaal_stored_uses',
             field = 'vaal_stored_uses',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
             header = 'Stored<br>Uses',
             header = i18n.progression.vaal_stored_uses,
        },
        vaal_soul_gain_prevention_time = {
            name = i18n.parameters.skill.vaal_soul_gain_prevention_time,
            field = 'vaal_soul_gain_prevention_time',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%i ' .. m_game.units.seconds.short_lower,
         },
         },
         damage_multiplier = {
         damage_multiplier = {
             name = i18n.args.damage_multiplier,
             name = i18n.parameters.skill.damage_multiplier,
             field = 'damage_multiplier',
             field = 'damage_multiplier',
             type = 'Float',
             type = 'Float',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
             header = m_util.html.abbr('Damage<br>Multiplier', 'Deals x% of Base Damage'),
             fmt = '%s%%',
        },
        attack_speed_multiplier = {
            name = i18n.parameters.skill.attack_speed_multiplier,
            field = 'attack_speed_multiplier',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
             fmt = '%s%%',
             fmt = '%s%%',
        },
        duration = {
            name = i18n.parameters.skill.duration,
            field = 'duration',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%.2f ' .. m_game.units.seconds.short_lower,
        },
        cost_types = {
            name = i18n.parameters.skill.cost_types,
            field = 'cost_types',
            type = 'List (,) of String',
            func = function(tpl_args, value)
                if value == nil then
                    return nil
                end
                value = m_util.cast.table(value)
                for _, v in ipairs(value) do
                    if m_game.constants.skill.cost_types[v] == nil then
                        error(string.format(i18n.errors.skill.invalid_cost_type, v))
                    end
                end
                return value
            end,
        },
        cost_amounts = {
            name = i18n.parameters.skill.cost_amounts,
            field = 'cost_amounts',
            type = 'List (,) of Integer',
            func = function(tpl_args, value)
                if value == nil then
                    return nil
                end
                value = m_util.cast.table(value, {callback = m_util.cast.number})
                return value
            end,
        },
        mana_reservation_flat = {
            name = i18n.parameters.skill.mana_reservation_flat,
            field = 'mana_reservation_flat',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        mana_reservation_percent = {
            name = i18n.parameters.skill.mana_reservation_percent,
            field = 'mana_reservation_percent',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        life_reservation_flat = {
            name = i18n.parameters.skill.life_reservation_flat,
            field = 'life_reservation_flat',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        life_reservation_percent = {
            name = i18n.parameters.skill.life_reservation_percent,
            field = 'life_reservation_percent',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
         },
         },
         -- from gem experience, optional
         -- from gem experience, optional
         experience = {
         experience = {
             name = i18n.args.experience,
             name = i18n.parameters.skill.experience,
             field = 'experience',
             field = 'experience',
             type = 'Integer',
             type = 'Integer',
             func = data.cast.wrap(m_util.cast.number),
             func = h.cast.wrap(m_util.cast.number),
            hide = true,
         },
         },
         stat_text = {
         stat_text = {
             name = i18n.args.stat_text,
             name = i18n.parameters.skill.stat_text,
             field = 'stat_text',
             field = 'stat_text',
             type = 'Text',
             type = 'Text',
             func = nil,
             func = h.cast.wrap(m_util.cast.text),
            hide = true,
        },
        quality_stat_text = {
            name = i18n.args.quality_stat_text,
            field = 'quality_stat_text',
            type = 'Text',
            func = nil,
            hide = true,
         },
         },
     }
     }
}
}


data.progression_display_order = {'level_requirement', 'dexterity_requirement', 'strength_requirement', 'intelligence_requirement', 'mana_multiplier', 'critical_strike_chance', 'mana_cost', 'damage_effectiveness', 'stored_uses', 'cooldown', 'vaal_souls_requirement', 'vaal_stored_uses', 'damage_multiplier'}
tables.skill_stats_per_level = {
 
map.skill_stats_per_level = {
     table = 'skill_stats_per_level',
     table = 'skill_stats_per_level',
     fields = {
     fields = {
         level = {
         level = {
            name = nil,
             field = 'level',
             field = 'level',
             type = 'Integer',
             type = 'Integer',
            func = nil,
         },
         },
         id = {
         id = {
            name = i18n.parameters.skill.stat_id,
             field = 'id',
             field = 'id',
             type = 'String',
             type = 'String',
            func = h.cast.wrap(m_util.cast.text),
         },
         },
         value = {
         value = {
            name = i18n.parameters.skill.stat_value,
             field = 'value',
             field = 'value',
             type = 'Integer',
             type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
    },
}
tables.skill_quality = {
    table = 'skill_quality',
    fields = {
        set_id = {
            field = 'set_id',
            type = 'Integer',
        },
        weight = {
            field = 'weight',
            type = 'Integer',
        },
        stat_text = {
            field = 'stat_text',
            type = 'String',
        },
    },
}
tables.skill_quality_stats = {
    table = 'skill_quality_stats',
    fields = {
        set_id = {
            field = 'set_id',
            type = 'Integer',
        },
        id = {
            field = 'id',
            type = 'String',
         },
         },
         is_quality_stat = {
         value = {
             field = 'is_quality_stat',
             field = 'value',
             type = 'Boolean',
             type = 'Integer',
         },
         },
     },
     },
}
}


--  
-- ----------------------------------------------------------------------------
-- Helper functions
-- Data
--
-- ----------------------------------------------------------------------------
local h = {}
 
data.skill_progression_table = {
    {
        field = 'level',
        header = i18n.progression.level,
    },
    {
        field = 'level_requirement',
        header = i18n.progression.level_requirement,
    },
    {
        field = 'dexterity_requirement',
        header = i18n.progression.dexterity_requirement,
    },
    {
        field = 'strength_requirement',
        header = i18n.progression.strength_requirement,
    },
    {
        field = 'intelligence_requirement',
        header = i18n.progression.intelligence_requirement,
    },
    {
        field = 'cost_multiplier',
        header = i18n.progression.cost_multiplier,
        fmt = '%s%%',
    },
    {
        field = 'attack_time',
        header = i18n.progression.attack_time,
        fmt = '%.2f ' .. m_game.units.seconds.short_lower,
    },
    {
        field = 'critical_strike_chance',
        header = i18n.progression.critical_strike_chance,
        fmt = '%s%%',
    },
    {
        field = 'cost_Mana',
        header = i18n.progression.mana_cost,
    },
    {
        field = 'cost_Life',
        header = i18n.progression.life_cost,
    },
    {
        field = 'cost_ES',
        header = i18n.progression.energy_shield_cost,
    },
    {
        field = 'cost_Rage',
        header = i18n.progression.rage_cost,
    },
    {
        field = 'cost_ManaPercent',
        header = i18n.progression.mana_cost,
        fmt = '%s%%',
    },
    {
        field = 'cost_LifePercent',
        header = i18n.progression.life_cost,
        fmt = '%s%%',
    },
    {
        field = 'mana_reservation_flat',
        header = i18n.progression.mana_reserved,
    },
    {
        field = 'mana_reservation_percent',
        header = i18n.progression.mana_reserved,
        fmt = '%s%%',
    },
    {
        field = 'life_reservation_flat',
        header = i18n.progression.life_reserved,
    },
    {
        field = 'life_reservation_percent',
        header = i18n.progression.life_reserved,
        fmt = '%s%%',
    },
    {
        field = 'damage_effectiveness',
        header = i18n.progression.damage_effectiveness,
        fmt = '%s%%',
    },
    {
        field = 'stored_uses',
        header = i18n.progression.stored_uses,
    },
    {
        field = 'cooldown',
        header = i18n.progression.cooldown,
        fmt = '%.2f ' .. m_game.units.seconds.short_lower,
    },
    {
        field = 'vaal_souls_requirement',
        header = i18n.progression.vaal_souls_requirement,
    },
    {
        field = 'vaal_stored_uses',
        header = i18n.progression.vaal_stored_uses,
    },
    {
        field = 'vaal_soul_gain_prevention_time',
        header = i18n.progression.vaal_soul_gain_prevention_time,
        fmt = '%i ' .. m_game.units.seconds.short_lower,
    },
    {
        field = 'damage_multiplier',
        header = i18n.progression.damage_multiplier,
        fmt = '%s%%',
    },
    {
        field = 'duration',
        header = i18n.progression.duration,
        fmt = '%.2f ' .. m_game.units.seconds.short_lower,
    },
    {
        field = 'attack_speed_multiplier',
        header = i18n.progression.attack_speed_multiplier,
        fmt = '%s%%',
    },
}


function h.map_to_arg(tpl_args, frame, properties, prefix_in, map, level)
data.infobox_table = {
     if map.order then
    {
         for _, key in ipairs(map.order) do
        header = i18n.infobox.active_skill_name,
             row = map.fields[key]
        func = h.display.factory.value{key='active_skill_name'},
             if row.name then
    },
                local val = tpl_args[prefix_in .. row.name]
    {
                 if row.func ~= nil then
        header = i18n.infobox.skill_id,
                     val = row.func(tpl_args, frame, val)
        func = function (tpl_args)
            return string.format('[[%s|%s]]', mw.title.getCurrentTitle().fullText, tpl_args.skill_id)
        end
    },
     {
        header = i18n.infobox.skill_icon,
         func = function (tpl_args)
             if tpl_args.skill_icon then
                return string.format('[[%s]]', tpl_args.skill_icon)
             end
        end,
    },
    {
        header = i18n.infobox.cast_time,
        func = function (tpl_args)
            local value = tpl_args.cast_time
            if value then
                 if value == 0 then
                     return i18n.infobox.instant_cast_time
                 end
                 end
                 if val == nil and row.default ~= nil then
                 return string.format('%.2f %s', value, m_game.units.seconds.short_lower)
                     val = row.default
            end
            return value
        end,
    },
    {
        header = i18n.infobox.item_class_restrictions,
        func = function (tpl_args)
            if tpl_args.item_class_restriction == nil then
                return
            end
            local out = {}
            for _, class in ipairs(tpl_args.item_class_restriction) do
                out[#out+1] = string.format('[[%s]]', class)
            end
            return table.concat(out, '<br>')
        end,
    },
    {
        header = i18n.infobox.projectile_speed,
        func = h.display.factory.value{key='projectile_speed'},
    },
    {
        header = i18n.infobox.radius,
        func = h.display.factory.radius{key=''},
    },
    {
        header = i18n.infobox.radius_secondary,
        func = h.display.factory.radius{key='_secondary'},
    },
    {
        header = i18n.infobox.radius_tertiary,
        func = h.display.factory.radius{key='_tertiary'},
    },
    {
        header = i18n.infobox.level_requirement,
        func = h.display.factory.range_value{key='level_requirement'},
    },
    -- ignore attrbiutes?
    {
        header = i18n.infobox.cost_multiplier,
        func = h.display.factory.range_value{key='cost_multiplier'},
    },
    {
        header = i18n.infobox.attack_time,
        func = h.display.factory.range_value{key='attack_time'},
    },
    {
        header = i18n.infobox.critical_strike_chance,
        func = h.display.factory.range_value{key='critical_strike_chance'},
    },
    {
        header = i18n.infobox.cost,
        func = function (tpl_args)
            local parts = {}
            for k, v in pairs(m_game.constants.skill.cost_types) do
                local key = 'cost_' .. k
                local fmt
                if string.find(k, 'Percent', 1, true) then
                     fmt = '%s%% %s'
                else
                    fmt = '%s %s'
                 end
                 end
                 if val ~= nil then   
                 local range = h.display.factory.range_value{key=key}(tpl_args)
                    if level ~= nil then
                if range then
                        tpl_args.skill_levels[level][key] = val
                     parts[#parts+1] = string.format(fmt, range, v.long_lower)
                        -- Nuke variables since they're remapped to skill_levels
                        tpl_args[prefix_in .. row.name] = nil
                    else
                        tpl_args[row.name] = val
                    end
                     properties[row.field] = val
                 end
                 end
             end
             end
         end
            return table.concat(parts, ', ')
     end
         end,
end
     },
 
    {
function h.stats(tpl_args, frame, prefix_in, level)
        header = i18n.infobox.reservation,
    for _, stat_type in ipairs(map.stats) do
        func = function (tpl_args)
        local type_prefix = string.format('%s%s%s', prefix_in, stat_type.prefix_in, i18n.args.infix_stat)
            local parts = {}
        tpl_args.skill_levels[level][stat_type.out] = {}
            local keys = {
        for i=1, 8 do
                {
            local stat_id_key = string.format('%s%s_%s', type_prefix, i, i18n.args.id)
                    key = 'mana_reservation_flat',
            local stat_val_key = string.format('%s%s_%s', type_prefix, i, i18n.args.value)
                    fmt = '%s ' .. m_game.constants.skill.cost_types['Mana'].long_lower,
            local stat = {
                },
                id = tpl_args[stat_id_key],
                {
                 value = tonumber(tpl_args[stat_val_key]),
                    key = 'mana_reservation_percent',
                    fmt = '%s%% ' .. m_game.constants.skill.cost_types['Mana'].long_lower,
                },
                {
                    key = 'life_reservation_flat',
                    fmt = '%s ' .. m_game.constants.skill.cost_types['Life'].long_lower,
                },
                {
                    key = 'life_reservation_percent',
                    fmt = '%s%% ' .. m_game.constants.skill.cost_types['Life'].long_lower,
                 },
             }
             }
             if stat.id ~= nil and stat.value ~= nil then
             for _, v in ipairs(keys) do
                 tpl_args.skill_levels[level][stat_type.out][#tpl_args.skill_levels[level][stat_type.out]+1] = stat
                 local range = h.display.factory.range_value{key=v.key}(tpl_args)
               
                if range then
                m_util.cargo.store(frame, {
                     parts[#parts+1] = string.format(v.fmt, range)
                    _table = map.skill_stats_per_level.table,
                 end
                     [map.skill_stats_per_level.fields.level.field] = level,
                    [map.skill_stats_per_level.fields.id.field] = stat.id,
                    [map.skill_stats_per_level.fields.value.field] = stat.value,
                    [map.skill_stats_per_level.fields.is_quality_stat.field] = stat_type.quality,
                })
                  
                -- Nuke variables since they're remapped to skill levels
                tpl_args[stat_id_key] = nil
                tpl_args[stat_val_key] = nil
             end
             end
         end
            return table.concat(parts, ', ')
     end
         end,
end
     },
 
    {
function h.na(tr)
        header = i18n.infobox.attack_speed_multiplier,
     tr
        func = h.display.factory.range_value{key='attack_speed_multiplier'},
         :tag('td')
        fmt = '%s ' .. i18n.infobox.of_base_stat,
            :attr('class', 'table-na')
    },
            :wikitext('N/A')
     {
            :done()
         header = i18n.infobox.damage_multiplier,
end
        func = h.display.factory.range_value{key='damage_multiplier'},
 
        fmt = '%s ' .. i18n.infobox.of_base_stat,
function h.int_value_or_na(tpl_args, frame, tr, value, pdata)
    },
     value = tonumber(value)
    {
     if value == nil then
        header = i18n.infobox.damage_effectiveness,
         h.na(tr)
        func = h.display.factory.range_value{key='damage_effectiveness'},
     else
    },
         value = mwlanguage:formatNum(value)
    {
         if pdata.fmt ~= nil then
        header = i18n.infobox.stored_uses,
            if type(pdata.fmt) == 'string' then
        func = h.display.factory.range_value{key='stored_uses'},
                value = string.format(pdata.fmt, value)
     },
            elseif type(pdata.fmt) == 'function' then
     {
                value = pdata.fmt(tpl_args, frame, value)
        header = i18n.infobox.cooldown,
            end
         func = h.display.factory.range_value{key='cooldown'},
         end
    },
         tr
     {
            :tag('td')
         header = i18n.infobox.vaal_souls_requirement,
                :wikitext(value)
         func = h.display.factory.range_value{key='vaal_souls_requirement'},
                :done()
    },
     end
    {
end
        header = i18n.infobox.vaal_stored_uses,
        func = h.display.factory.range_value{key='vaal_stored_uses'},
    },
    {
        header = i18n.infobox.vaal_soul_gain_prevention_time,
        func = h.display.factory.range_value{key='vaal_soul_gain_prevention_time'},
    },
    {
        header = i18n.infobox.duration,
        func = h.display.factory.range_value{key='duration'},
    },
    {
         header = nil,
         func = h.display.factory.value{key='gem_description'},
        class = 'tc -gemdesc',
    },
    {
        header = nil,
        func = h.display.factory.value{key='stat_text'},
        class = 'tc -mod',
     },
}


--
-- ----------------------------------------------------------------------------
-- For Template:Skill
-- Main functions
--
-- ----------------------------------------------------------------------------
p.table_skills = m_util.cargo.declare_factory{data=map.static}
p.table_skill_levels = m_util.cargo.declare_factory{data=map.progression}
p.table_skill_stats_per_level = m_util.cargo.declare_factory{data=map.skill_stats_per_level}


function p.skill(frame, tpl_args)
local function _process_skill_data(tpl_args)
     if tpl_args == nil then
     --[[
        tpl_args = getArgs(frame, {
    Processes skill data from tpl_args.
            parentFirst = true
     Stores skill data in cargo tables.
        })
     Attaches page to cargo tables.
     end
     --]]
     frame = m_util.misc.get_frame(frame)
   
    --
     -- Args
    --
    local properties
      
      
    tpl_args = tpl_args or {}
    tpl_args._flags = tpl_args._flags or {}
     tpl_args.skill_levels = {
     tpl_args.skill_levels = {
         [0] = {},
         [0] = {},
     }
     }
   
    -- Quality
    tpl_args.skill_quality = {}
    local i = 0
    repeat
        i = i + 1
        local prefix = string.format('quality_type%s', i)
        local q = {
            _table = tables.skill_quality.table,
            set_id = i,
            weight = tonumber(tpl_args[string.format('%s_weight', prefix)]),
            stat_text = tpl_args[string.format('%s_stat_text', prefix)],
        }
        if q.stat_text then
            tpl_args.skill_quality[#tpl_args.skill_quality+1] = q
            m_cargo.store(q)
           
            q.stats = {}
            q._table = nil
            local j = 0
            repeat
                j = j + 1
                local stat_prefix = string.format('%s_stat%s', prefix, j)
                local s = {
                    _table = tables.skill_quality_stats.table,
                    set_id = i,
                    id = tpl_args[string.format('%s_id', stat_prefix)],
                    value = tonumber(tpl_args[string.format('%s_value', stat_prefix)]),
                }
                if s.id and s.value then
                    q.stats[#q.stats+1] = s
                    m_cargo.store(s)
                end
               
                s._table = nil
            until s.id == nil or s.value == nil
        end
    until q.stat_text == nil
    if #tpl_args.skill_quality > 1 then
        -- Gem has alternative qualtiy
        tpl_args._flags.is_alt_quality_gem = true
    end
      
      
     -- Handle level progression
     -- Handle level progression
     for i=1, 30 do
    local level_count = 0
         local prefix = i18n.args.prefix_level .. i  
     for i=1, math.huge do -- repeat until no more levels are found
         if m_util.cast.boolean(tpl_args[prefix]) == true then
         local prefix = i18n.parameters.skill.level .. i
             -- Don't need this anymore
         local level = m_util.cast.boolean(tpl_args[prefix])
            tpl_args[prefix] = nil
        if not level then
            tpl_args.skill_levels[i] = {}
             break
            prefix = prefix .. '_'
        end
          
        tpl_args.skill_levels[i] = {}
            if tpl_args[prefix .. i18n.args.experience] ~= nil then
        prefix = prefix .. '_'
                tpl_args.max_level = i
         level_count = i
            end
        if tpl_args[prefix .. i18n.parameters.skill.experience] ~= nil then
           
            -- For skill gems, max level is the highest level with experience.
            properties = {
            tpl_args.max_level = i
                _table = map.progression.table,
        end
                [map.progression.fields.level.field] = i
        local properties = {
            }
            _table = tables.progression.table,
            h.map_to_arg(tpl_args, frame, properties, prefix, map.progression, i)
            [tables.progression.fields.level.field] = i
             m_util.cargo.store(frame, properties)
        }
           
        h.map_to_arg(tpl_args, properties, prefix, tables.progression, i)
            h.stats(tpl_args, frame, prefix, i)
        if not tpl_args.test then
             m_cargo.store(properties)
         end
         end
        h.stats(tpl_args, prefix, i)
     end
     end
    tpl_args.max_level = tpl_args.max_level or level_count
     -- handle static progression
     -- handle static progression
     properties = {
     local prefix = i18n.parameters.skill.static .. '_'
        _table = map.progression.table,
    do
        [map.progression.fields.level.field] = 0
        local properties = {
    }
            _table = tables.progression.table,
    h.map_to_arg(tpl_args, frame, properties, i18n.args.prefix_static, map.progression, 0)
            [tables.progression.fields.level.field] = 0
    m_util.cargo.store(frame, properties)
        }
     mw.logObject(properties)
        h.map_to_arg(tpl_args, properties, prefix, tables.progression, 0)
        if not tpl_args.test then
            m_cargo.store(properties)
        end
     end
 
    -- Expand costs data
    h.expand_costs_data(tpl_args, tpl_args.skill_levels)
      
      
     -- Handle static arguments
     -- Handle static arguments
     properties = {
     local properties = {
         _table = map.static.table,
         _table = tables.static.table,
         [map.static.fields.max_level.field] = tpl_args.max_level
         [tables.static.fields.max_level.field] = tpl_args.max_level
     }
     }
    h.map_to_arg(tpl_args, properties, '', tables.static)
    h.stats(tpl_args, prefix, 0)
    -- Build infobox
    local infobox = mw.html.create('span')
    infobox:addClass('skill-box')
    local tbl = infobox:tag('table')
    tbl:addClass('wikitable skill-box-table')
    for _, infobox_data in ipairs(data.infobox_table) do
        local display = infobox_data.func(tpl_args)
        if type(display) == 'string' and string.len(display) > 0 then
            if infobox_data.fmt ~= nil then
                if type(infobox_data.fmt) == 'string' then
                    display = string.format(infobox_data.fmt, display)
                elseif type(infobox_data.fmt) == 'function' then
                    display = string.format(infobox_data.fmt(tpl_args) or '%s', display)
                end
            end
            local tr = tbl:tag('tr')
            if infobox_data.header then
                local header_text
                if type(infobox_data.header) == 'function' then
                    header_text = infobox_data.header(tpl_args)
                else
                    header_text = infobox_data.header
                end
                tr
                    :tag('th')
                        :wikitext(header_text)
                        :done()
            end
            local td = tr:tag('td')
            td:wikitext(display)
            td:addClass(infobox_data.class or 'tc -value')
            if infobox_data.header == nil then
                td:attr('colspan', 2)
            end
        end
    end
    infobox = tostring(infobox)
    -- Store data
    properties[tables.static.fields.html.field] = infobox
    if not tpl_args.test then
        m_cargo.store(properties)
    end
    -- Attach tables
    if not tpl_args.test then
        local attach_tables = {
            tables.static.table,
            tables.progression.table,
        }
        if #tpl_args.skill_quality > 0 then
            attach_tables[#attach_tables+1] = tables.skill_quality.table
            attach_tables[#attach_tables+1] = tables.skill_quality_stats.table
        end
        if tpl_args.skill_levels.has_stats then
            attach_tables[#attach_tables+1] = tables.skill_stats_per_level.table
        end
        for _, table_name in ipairs(attach_tables) do
            mw.getCurrentFrame():expandTemplate{
                title = string.format(i18n.templates.cargo_attach, table_name),
                args = {}
            }
        end
    end
    -- Log when testing
    if tpl_args.test then
        mw.logObject(tpl_args)
    end
    return infobox
end
local function _skill(tpl_args)
    --[[
    Display skill infobox
      
      
     h.map_to_arg(tpl_args, frame, properties, '', map.static)
     Examples
     h.stats(tpl_args, frame, i18n.args.prefix_static, 0)
    --------
    =p.skill{gem_description='Icy bolts rain down over the targeted area.', active_skill_name='Icestorm', skill_id='IcestormUniqueStaff12', cast_time=0.75, required_level=1, static_mana_cost=22, static_critical_strike_chance=6, static_damage_effectiveness=30, static_damage_multiplier=100, static_stat1_id='spell_minimum_base_cold_damage_+_per_10_intelligence', static_stat1_value=1, static_stat2_id='spell_maximum_base_cold_damage_+_per_10_intelligence', static_stat2_value=3, static_stat3_id='base_skill_effect_duration', static_stat3_value=1500, static_stat4_id='fire_storm_fireball_delay_ms', static_stat4_value=100, static_stat5_id='skill_effect_duration_per_100_int', static_stat5_value=150, static_stat6_id='skill_override_pvp_scaling_time_ms', static_stat6_value=450, static_stat7_id='firestorm_drop_ground_ice_duration_ms', static_stat7_value=500, static_stat8_id='skill_art_variation', static_stat8_value=4, static_stat9_id='base_skill_show_average_damage_instead_of_dps', static_stat9_value=1, static_stat10_id='is_area_damage', static_stat10_value=1, stat_text='Deals 1 to 3 base Cold Damage per 10 Intelligence<br>Base duration is 1.5 seconds<br>One impact every 0.1 seconds<br>0.15 seconds additional Base Duration per 100 Intelligence', quality_stat_text = nil, level1=true, level1_level_requirement=1}
 
    ]]
 
    -- Handle skill data and get infobox
    local infobox = _process_skill_data(tpl_args)
 
    -- Container
     local container = mw.html.create('span')
    container
        :addClass('skill-box-page-container')
        :wikitext(infobox)
    if tpl_args.skill_screenshot then
        container
            :wikitext(string.format('[[%s]]', tpl_args.skill_screenshot))
    end
 
    -- Generic messages on the page:
    local out = {}
    if mw.ustring.find(tpl_args.skill_id, '_') then
        out[#out+1] = mw.getCurrentFrame():expandTemplate{
            title = i18n.templates.incorrect_title,
            args = {title=tpl_args.skill_id}
        } .. '\n\n\n'
    end
    if tpl_args.active_skill_name then
        out[#out+1] = string.format(
            i18n.messages.intro_named_id,  
            tpl_args.skill_id,
            tpl_args.active_skill_name
        )
    else
        out[#out+1] = string.format(
            i18n.messages.intro_unnamed_id,
            tpl_args.skill_id
        )
    end
 
    -- Categories
    local cats = {i18n.categories.skill_data}
    if tpl_args._flags.has_deprecated_skill_parameters then
        cats[#cats+1] = i18n.categories.deprecated_parameters
    end
      
      
     m_util.cargo.store(frame, properties)
     return tostring(container) .. m_util.misc.add_category(cats) .. '\n' .. table.concat(out)
end
end


function p.progression(frame)
local function _progression(tpl_args)
     local tpl_args = getArgs(frame, {
     --[[
        parentFirst = true
        Displays the level progression for the skill gem.
    })
       
     frame = m_util.misc.get_frame(frame)
        Examples
        --------
        = p.progression{page='Reave'}
     ]]
      
      
     --
     -- Parse column arguments:
     tpl_args.stat_format = {}
     tpl_args.stat_format = {}
     local prefix
     local param_keys = {
    for i=1, 9 do
         i18n.parameters.progression.header,
         prefix = 'c' .. i .. '_'
         i18n.parameters.progression.abbr,
         local statfmt = {
        i18n.parameters.progression.pattern_extract,
            header = tpl_args[prefix .. 'header'],
        i18n.parameters.progression.pattern_value,
            abbr = tpl_args[prefix .. 'abbr'],
    }
            pattern_extract = tpl_args[prefix .. 'pattern_extract'],
    for i=1, math.huge do -- repeat until no more columns are found
            pattern_value = tpl_args[prefix .. 'pattern_value'],
         local prefix = string.format('%s%d_', i18n.parameters.progression.column, i)
            counter = 0,
        if tpl_args[prefix .. param_keys[1]] == nil then
         }
        if m_util.table.has_all_value(statfmt, {'header', 'abbr', 'pattern_extract', 'pattern_value'}) then
             break
             break
         end
         end
          
         local statfmt = {counter = 0}
         if m_util.table.has_one_value(statfmt, {'header', 'abbr', 'pattern_extract', 'pattern_value'}) then
         for _, key in ipairs(param_keys) do
            error(string.format('All formatting keys must be specified for index "%s"', i))
            local arg = prefix .. key
            if tpl_args[arg] == nil then
                error(string.format(i18n.errors.progression.argument_unspecified, arg))
            end
            statfmt[key] = tpl_args[arg]
         end
         end
       
         statfmt.header = m_util.html.abbr(statfmt.abbr, statfmt.header)
         statfmt.header = m_util.html.abbr(statfmt.abbr, statfmt.header)
         statfmt.abbr = nil
         statfmt.abbr = nil
Line 605: Line 1,258:
     end
     end
      
      
      
     -- Query skill data
     local result
     local skill_data = h.query_skill(tpl_args)
 
    -- Query progression data
    local fields = {}
    for _, fmap in pairs(tables.progression.fields) do
        fields[#fields+1] = fmap.field
    end
     local query = {
     local query = {
         groupBy = 'skill._pageID',
         where = string.format(
    }
            '_pageID="%s"',
    local skill_data
            skill_data._pageID
   
        ),
    local fields = {
        groupBy = string.format(
        'skill._pageName',
            '_pageID, %s',
         'skill.has_reservation_mana_cost',
            tables.progression.fields.level.field
         ),
     }
     }
    local results = m_cargo.query({tables.progression.table}, fields, query)
      
      
     if tpl_args.skill_id then
     -- Re-index by level
        query.where = string.format('skill.skill_id="%s"', tpl_args.skill_id)
     skill_data.levels = {}
        result = m_util.cargo.query({'skill'}, fields, query)
     for _, v in ipairs(results) do
        if #result == 0 or #result[1] == 0 then
         skill_data.levels[tonumber(v.level)] = v
            error('Couldn\'t find a page for the specified skill id')
        end
        skill_data = result[1]
    else
        if tpl_args.page then
            page = tpl_args.page
        else
            page = mw.title.getCurrentTitle().prefixedText
        end
        query.where = string.format('skill._pageName="%s"', page)
       
        result = m_util.cargo.query({'skill'}, fields, query)
        if #result == 0 then
            error('Couldn\'t find the queried data on the skill page')
        end
       
        skill_data = result[1]
    end
   
     skill_data["skill.has_reservation_mana_cost"] = data.cast.wrap(m_util.cast.boolean)(skill_data["skill.has_reservation_mana_cost"])
   
    query.where=string.format('skill_levels._pageName="%s"', skill_data['skill._pageName'])
    fields = {}
     for _, pdata in pairs(map.progression.fields) do
         fields[#fields+1] = string.format('skill_levels.%s', pdata.field)
     end
     end
      
     if #skill_data.levels == 0 then
    result = m_util.cargo.query(
         error(i18n.errors.progression.missing_level_data)
        {'skill_levels'},
        fields,
        {
            where=string.format('skill_levels._pageName="%s" AND skill_levels.level > 0', skill_data['skill._pageName']),
            groupBy='skill_levels._pageID, skill_levels.level',
            orderBy='skill_levels.level ASC',
        }
    )
   
    if #result == 0 then
         error('No gem level progression data found')
     end
     end
    -- Expand costs data
    h.expand_costs_data(tpl_args, skill_data.levels)
      
      
     headers = {}
     -- Set up html table headers
     for i, row in ipairs(result) do
    local headers = {}
     for _, row in ipairs(skill_data.levels) do
         for k, v in pairs(row) do
         for k, v in pairs(row) do
             headers[k] = true
             headers[k] = true
         end
         end
     end
     end
   
     local tbl = mw.html.create('table')
     local tbl = mw.html.create('table')
     tbl:attr('class', 'wikitable skill-progression-table')
     tbl
   
        :addClass('wikitable responsive-table skill-progression-table')
     local head = tbl:tag('tr')
     local head = tbl:tag('tr')
    head
     for _, tmap in pairs(data.skill_progression_table) do
        :tag('th')
         if headers[tmap.field] then
            :wikitext('Level')
             local text = type(tmap.header) == 'function' and tmap.header(tpl_args) or tmap.header
            :done()
   
     for _, key in ipairs(data.progression_display_order) do
        local pdata = map.progression.fields[key]
        -- TODO should be nil?
         if pdata.hide == nil and headers['skill_levels.' .. pdata.field] then
             local text
            if type(pdata.header) == 'function' then
                text = pdata.header(skill_data)
            else
                text = pdata.header
            end
             head
             head
                 :tag('th')
                 :tag('th')
Line 694: Line 1,310:
         end
         end
     end
     end
   
     for _, statfmt in ipairs(tpl_args.stat_format) do
     for _, statfmt in ipairs(tpl_args.stat_format) do
         head
         head
Line 701: Line 1,316:
                 :done()
                 :done()
     end
     end
   
     if headers[tables.progression.fields.experience.field] then
     if headers['skill_levels.experience'] then
         head
         head
             :tag('th')
             :tag('th')
                 :wikitext(m_util.html.abbr('Exp.', 'Experience Needed to Level Up'))
                 :wikitext(i18n.progression.experience)
                 :done()
                 :done()
             :tag('th')
             :tag('th')
                 :wikitext(m_util.html.abbr('Total Exp.', 'Total experience needed'))
                 :wikitext(i18n.progression.total_experience)
                 :done()
                 :done()
     end
     end
      
 
     -- Table rows
     local tblrow
     local tblrow
     local lastexp = 0
     local lastexp = 0
     local experience
     local experience
   
     for _, row in ipairs(skill_data.levels) do
     for i, row in ipairs(result) do
         tblrow = tbl:tag('tr')
         tblrow = tbl:tag('tr')
        tblrow
         for _, tmap in pairs(data.skill_progression_table) do
            :tag('th')
             if headers[tmap.field] then
                :wikitext(row['skill_levels.level'])
                 h.int_value_or_na(tpl_args, tblrow, row[tmap.field], tmap)
                :done()
       
         for _, key in ipairs(data.progression_display_order) do
            local pdata = map.progression.fields[key]
             if pdata.hide == nil and headers['skill_levels.' .. pdata.field] then
                 h.int_value_or_na(tpl_args, frame, tblrow, row['skill_levels.' .. pdata.field], pdata)
             end
             end
         end
         end
          
          
         -- stats
         -- stats
         if row['skill_levels.stat_text'] then
        local stats = {}
             stats = m_util.string.split(row['skill_levels.stat_text'], '<br>')
         if row[tables.progression.fields.stat_text.field] then
        else
             stats = m_util.string.split(
             stats = {}
                row[tables.progression.fields.stat_text.field],
                '<br>'
             )
         end
         end
         for _, statfmt in ipairs(tpl_args.stat_format) do
         for _, statfmt in ipairs(tpl_args.stat_format) do
Line 741: Line 1,351:
                 match = {string.match(stat, statfmt.pattern_extract)}
                 match = {string.match(stat, statfmt.pattern_extract)}
                 if #match > 0 then
                 if #match > 0 then
                     -- TODO maybe remove stat here to avoid testing against in future loops
                     -- TODO maybe remove stat here to avoid testing  
                    -- against in future loops
                     break
                     break
                 end
                 end
             end
             end
             if #match == 0 then
             if #match == 0 then
                 h.na(tblrow)
                 tblrow:node(m_util.html.td.na())
             else
             else
                 -- used to find broken progression (i.e. due to game updates)
                 -- used to find broken progression due to game updates
                -- for example:
                 statfmt.counter = statfmt.counter + 1
                 statfmt.counter = statfmt.counter + 1
                 tblrow
                 tblrow
                     :tag('td')
                     :tag('td')
                         :wikitext(string.format(statfmt.pattern_value, match[1], match[2], match[3], match[4], match[5]))
                         :wikitext(string.format(
                            statfmt.pattern_value,  
                            match[1],  
                            match[2],  
                            match[3],  
                            match[4],  
                            match[5]
                            )
                        )
                         :done()
                         :done()
             end
             end
         end
         end
          
          
        -- TODO: Quality stats, afaik no gems use this atm
         if headers[tables.progression.fields.experience.field] then
       
             experience = tonumber(row[tables.progression.fields.experience.field])
         if headers['skill_levels.experience'] then
             experience = tonumber(row['skill_levels.experience'])
             if experience ~= nil then
             if experience ~= nil then
                 h.int_value_or_na(tpl_args, frame, tblrow, experience - lastexp, {})
                 h.int_value_or_na(tpl_args, tblrow, experience - lastexp, {})
               
                 lastexp = experience
                 lastexp = experience
             else
             else
                 h.na(tblrow)
                 tblrow:node(m_util.html.td.na())
             end
             end
             h.int_value_or_na(tpl_args, frame, tblrow, experience, {})
             h.int_value_or_na(tpl_args, tblrow, experience, {})
         end
         end
     end
     end
      
      
     local empty = ''
     local cats = {}
   
     for _, statfmt in ipairs(tpl_args.stat_format) do
     for _, statfmt in ipairs(tpl_args.stat_format) do
         if statfmt.counter == 0 then
         if statfmt.counter == 0 then
             empty = '[[Category:Pages with broken skill progression tables]]'
             cats = i18n.categories.broken_progression_table
             break
             break
         end
         end
     end
     end
      
      
     return empty .. tostring(tbl)
     return tostring(tbl) .. m_util.misc.add_category(cats)
end
end


function p.map(arg)
local function _quality(tpl_args)
     for key, data in pairs(map[arg].fields) do
    --[[
         mw.logObject(key)
    Displays a table comparing the stats of superior gem quality with
    alternative quality types.
    --]]
 
    -- Query skill data
    local skill_data = h.query_skill(tpl_args)
 
    -- Query progression data
    local fields = {}
     for _, fmap in pairs(tables.skill_quality.fields) do
        fields[#fields+1] = fmap.field
    end
    local query = {
        where = string.format(
            '_pageID="%s"',
            skill_data._pageID
        ),
        groupBy = string.format(
            '_pageID, %s',
            tables.skill_quality.fields.set_id.field
        ),
        orderBy = string.format(
            '%s ASC',
            tables.skill_quality.fields.set_id.field
         ),
    }
    skill_data.quality = m_cargo.query({tables.skill_quality.table}, fields, query)
    if #skill_data.quality == 0 then
        error(i18n.errors.quality.missing_quality_data)
     end
     end
    -- Build table
    local tbl = mw.html.create('table')
    tbl
        :addClass('wikitable skill-quality-table')
        :tag('tr')
            :tag('th')
                :wikitext(i18n.quality.type)
                :done()
            :tag('th')
                :wikitext(i18n.quality.stats)
                :done()
            :tag('th')
                :wikitext(i18n.quality.weight)
                :done()
    for k, row in ipairs(skill_data.quality) do
        tbl
            :tag('tr')
                :tag('td')
                    :wikitext(m_game.constants.item.gem_quality_types[k].long_upper)
                    :done()
                :tag('td')
                    :addClass('tc -mod')
                    :wikitext(skill_data.quality[k].stat_text)
                    :done()
                :tag('td')
                    :wikitext(skill_data.quality[k].weight)
                    :done()
    end
   
    return tostring(tbl)
end
end


p._debug = {}
-- ----------------------------------------------------------------------------
function p._debug.order(frame)
-- Exported functions
    for _, mapping in ipairs({'static', 'progression'}) do
-- ----------------------------------------------------------------------------
        for _, key in ipairs(map[mapping].order) do
local p = {}
            if map[mapping].fields[key] == nil then
 
                mw.logObject(string.format('Missing key in %s.fields: %s', mapping, key))
p.table_skills = m_cargo.declare_factory{data=tables.static}
            end
p.table_skill_levels = m_cargo.declare_factory{data=tables.progression}
        end
p.table_skill_stats_per_level = m_cargo.declare_factory{data=tables.skill_stats_per_level}
        for key, _ in pairs(map[mapping].fields) do
p.table_skill_quality = m_cargo.declare_factory{data=tables.skill_quality}
            local missing = true
p.table_skill_quality_stats = m_cargo.declare_factory{data=tables.skill_quality_stats}
            for _, order_key in ipairs(map[mapping].order) do
 
                if order_key == key then
function p.process_skill_data(tpl_args)
                    missing = false
     _process_skill_data(tpl_args)
                    break
                end
            end
            if missing then
                mw.logObject(string.format('Missing key in %s.order: %s', mapping, key))
            end
        end
     end
end
end
p._skill = p.process_skill_data
--
-- Template:Skill
--
p.skill = m_util.misc.invoker_factory(_skill, {
    wrappers = cfg.wrappers.skill,
})
--
-- Template:Skill progression
--
p.progression = m_util.misc.invoker_factory(_progression, {
    wrappers = cfg.wrappers.progression,
})
--
-- Template:Skill quality
--
p.quality = m_util.misc.invoker_factory(_quality, {
    wrappers = cfg.wrappers.quality,
})


return p
return p

Latest revision as of 16:44, 16 December 2023

Module documentation[view] [edit] [history] [purge]


Lua logo

This module depends on the following other modules:

Overview

Module for handling skills with semantic media wiki support

Skill templates

Module:Item2

All templates defined in Module:Skill:

Module:Skill link

All templates defined in Module:Skill link:

ru:Модуль:Skill

-------------------------------------------------------------------------------
-- 
--                              Module:Skill
-- 
-- This module implements Template:Skill and Template:Skill progression
-------------------------------------------------------------------------------

require('Module:No globals')
local m_util = require('Module:Util')
local m_cargo = require('Module:Cargo')

-- Should we use the sandbox version of our submodules?
local use_sandbox = m_util.misc.maybe_sandbox('Skill')

local m_game = use_sandbox and mw.loadData('Module:Game/sandbox') or mw.loadData('Module:Game')

-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
local cfg = use_sandbox and mw.loadData('Module:Skill/config/sandbox') or mw.loadData('Module:Skill/config')

local mwlanguage = mw.language.getContentLanguage()

local i18n = cfg.i18n
local tables = {}
local data = {}

-- ----------------------------------------------------------------------------
-- Helper functions 
-- ----------------------------------------------------------------------------
local h = {}

function h.map_to_arg(tpl_args, properties, prefix_in, map, level, set_name, set_id)
    if map.fields then
        for key, row in pairs(map.fields) do
            if row.name then
                local val = tpl_args[prefix_in .. row.name]
                if row.func ~= nil then
                    val = row.func(tpl_args, val)
                end
                if val == nil and row.default ~= nil then
                    val = row.default
                end
                if val ~= nil then
                    if level ~= nil then
                        if set_name then
                            tpl_args.skill_levels[level][set_name] = tpl_args.skill_levels[level][set_name] or {}
                            tpl_args.skill_levels[level][set_name][set_id] = tpl_args.skill_levels[level][set_name][set_id] or {}
                            tpl_args.skill_levels[level][set_name][set_id][key] = val
                        else
                            tpl_args.skill_levels[level][key] = val
                        end

                        -- Nuke variables since they're remapped to skill_levels
                        tpl_args[prefix_in .. row.name] = nil
                    else
                        if set_name then
                            tpl_args[set_name] = tpl_args[set_name] or {}
                            tpl_args[set_name][set_id] = tpl_args[set_name][set_id] or {}
                            tpl_args[set_name][set_id][key] = val

                            -- Nuke variables since they're remapped to [set_name]
                            tpl_args[prefix_in .. row.name] = nil
                        else
                            tpl_args[key] = val
                        end
                    end
                    properties[row.field] = val

                    -- Deprecated parameters
                    if val and row.deprecated then
                        tpl_args._flags.has_deprecated_skill_parameters = true
                        if tpl_args.test then -- Log when testing
                            tpl_args.deprecated_parameters = tpl_args.deprecated_parameters or {}
                            tpl_args.deprecated_parameters[#tpl_args.deprecated_parameters+1] = {row.name, val}
                        end
                    end
                end
            end
        end
    end
end

function h.expand_costs_data(tpl_args, skill_levels)
    --[[
    Expand costs data so that each cost type has its own column with amounts
    Assumptions:
      Cost types are always static
      Cost amounts can either be static or leveled, but not both
    --]]
    if skill_levels[0] then
        local cost_types = m_util.cast.table(skill_levels[0].cost_types)
        if #cost_types > 0 then
            for _, level_data in pairs(skill_levels) do
                if type(level_data) == 'table' and level_data.cost_amounts then
                    local cost_amounts = m_util.cast.table(level_data.cost_amounts, {callback = m_util.cast.number})
                    for i=1, #cost_types do
                        local type = cost_types[i]
                        local amount = cost_amounts[i]
                        if amount then
                            level_data['cost_' .. type] = amount
                        end
                    end
                end
            end
        end
    end
end

function h.stats(tpl_args, prefix_in, level)
    for i=1, cfg.max_stats_per_level do
        local stat_prefix = string.format('%s%s%d_', prefix_in, i18n.parameters.skill.stat, i) -- level<level>_stat<i>_
        local stat = {
            id = tpl_args[stat_prefix .. tables.skill_stats_per_level.fields.id.name], --level<level>_stat<i>_id
            value = tpl_args[stat_prefix .. tables.skill_stats_per_level.fields.value.name], --level<level>_stat<i>_value
        }
        if stat.id ~= nil and stat.value ~= nil then
            local properties = {
                _table = tables.skill_stats_per_level.table,
                [tables.skill_stats_per_level.fields.level.field] = level,
            }
            h.map_to_arg(tpl_args, properties, stat_prefix, tables.skill_stats_per_level, level, 'stats', i)
            tpl_args.skill_levels.has_stats = true
            if not tpl_args.test then
                m_cargo.store(properties)
            end
        end
    end
end

function h.int_value_or_na(tpl_args, tblrow, value, tmap)
    value = tonumber(value)
    if value == nil then
        tblrow:node(m_util.html.td.na())
    else
        -- value = mwlanguage:formatNum(value) -- Removed for now. lang:formatNum() returns a string, which causes issues for formatting
        if tmap.fmt ~= nil then
            if type(tmap.fmt) == 'string' then
                value = string.format(tmap.fmt, value)
            elseif type(tmap.fmt) == 'function' then
                value = string.format(tmap.fmt(tpl_args) or '%s', value)
            end
        end
        tblrow
            :tag('td')
                :wikitext(value)
                :done()
    end
end

h.cast = {}
function h.cast.wrap(func)
    return function(tpl_args, value)
        if value == nil then
            return nil
        end
        return func(value)
    end
end

h.display = {}
h.display.factory = {}
function h.display.factory.value(args)
    return function (tpl_args)
        args.fmt = args.fmt or tables.static.fields[args.key].fmt
        local value = tpl_args[args.key]
        if args.fmt and value then
            return string.format(args.fmt, value)
        else
            return value
        end
    end
end

function h.display.factory.range_value(args)
    return function (tpl_args)
        local value = {}
        if args.set_name and args.set_id then
            -- Guard against index errors
            tpl_args.skill_levels[0][args.set_name] = tpl_args.skill_levels[0][args.set_name] or {}
            tpl_args.skill_levels[0][args.set_name][args.set_id] = tpl_args.skill_levels[0][args.set_name][args.set_id] or {}
            tpl_args.skill_levels[1][args.set_name] = tpl_args.skill_levels[1][args.set_name] or {}
            tpl_args.skill_levels[1][args.set_name][args.set_id] = tpl_args.skill_levels[1][args.set_name][args.set_id] or {}
            tpl_args.skill_levels[tpl_args.max_level][args.set_name] = tpl_args.skill_levels[tpl_args.max_level][args.set_name] or {}
            tpl_args.skill_levels[tpl_args.max_level][args.set_name][args.set_id] = tpl_args.skill_levels[tpl_args.max_level][args.set_name][args.set_id] or {}

            value.min = tpl_args.skill_levels[0][args.set_name][args.set_id][args.key] or tpl_args.skill_levels[1][args.set_name][args.set_id][args.key]
            value.max = tpl_args.skill_levels[0][args.set_name][args.set_id][args.key] or tpl_args.skill_levels[tpl_args.max_level][args.set_name][args.set_id][args.key]
        else
            value.min = tpl_args.skill_levels[0][args.key]
            if value.min == nil or type(value.min) == 'table' and #value.min == 0 then
                value.min = tpl_args.skill_levels[1][args.key]
            end
            value.max = tpl_args.skill_levels[0][args.key]
            if value.max == nil or type(value.max) == 'table' and #value.max == 0 then
                value.max = tpl_args.skill_levels[tpl_args.max_level][args.key]
            end
            if type(value.min) == 'table' and type(value.max) == 'table' and args.key_index then
                value.min = value.min[args.key_index]
                value.max = value.max[args.key_index]
            end
        end
        if value.min == nil or value.max == nil then
            -- property not set for this skill
            return nil
        end
        local map = args.map or tables.progression
        local options = {
            fmt=args.fmt or map.fields[args.key] and map.fields[args.key].fmt,
            color=false,
        }
        if type(value.min) == 'table' and type(value.max) == 'table' then
            local formatted_values = {}
            for i=1, #value.min do
                formatted_values[i] = m_util.html.format_value(tpl_args, {min = value.min[i], max = value.max[i]}, options)
            end
            return formatted_values
        end
        return m_util.html.format_value(tpl_args, value, options)
    end
end

function h.display.factory.radius(args)
    return function (tpl_args)
        local radius = tpl_args['radius' .. args.key]
        if radius == nil then
            return
        end
        local description = tpl_args[string.format('radius%s_description', args.key)]
        if description then
            return m_util.html.abbr(radius, description)
        else
            return radius
        end
    end
end

function h.query_skill(tpl_args)
    local fields = {
        'skill._pageID=_pageID',
    }
    local query = {
        groupBy = 'skill._pageID',
    }
    local results = {}
    local search_param
    if tpl_args.skill_id then -- Query by skill id
        query.where = string.format('skill_id="%s"', tpl_args.skill_id)
        search_param = 'skill_id'
    else -- Query by page name
        local page = tpl_args.page or mw.title.getCurrentTitle().prefixedText
        query.where = string.format('_pageName="%s"', page)
        search_param = 'page'
    end
    results = m_cargo.query({tables.static.table}, fields, query)
    if #results == 0 then
        -- No results found
        error(string.format(i18n.errors.validate_skill.no_results_found, search_param, tpl_args[search_param]))
    elseif #results > 1 then
        -- More than one result found
        error(string.format(i18n.errors.validate_skill.many_results_found, search_param, tpl_args[search_param]))
    end
    return results[1]
end

-- ----------------------------------------------------------------------------
-- Cargo tables
-- ----------------------------------------------------------------------------

tables.static = {
    table = 'skill',
    fields = {
        -- GrantedEffects.dat
        skill_id = {
            name = i18n.parameters.skill.skill_id,
            field = 'skill_id',
            type = 'String',
            func = nil,
        },
        -- Active Skills.dat
        cast_time = {
            name = i18n.parameters.skill.cast_time,
            field = 'cast_time',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%.2f ' .. m_game.units.seconds.short_lower,
        },
        gem_description = {
            name = i18n.parameters.skill.gem_description,
            field = 'description',
            type = 'Text',
            func = nil,
        },
        active_skill_name = {
            name = i18n.parameters.skill.active_skill_name,
            field = 'active_skill_name',
            type = 'String',
            func = nil,
        },
        skill_icon = {
            name = i18n.parameters.skill.skill_icon,
            field = 'skill_icon',
            type = 'Page',
            func = function(tpl_args, value)
                if value then
                    value = string.format(i18n.files.skill_icon, value)
                elseif tpl_args.active_skill_name then
                    value = string.format(i18n.files.skill_icon, tpl_args.active_skill_name)
                else
                    value = nil
                end
                return value
            end,
        },
        item_class_id_restriction = {
            name = i18n.parameters.skill.item_class_id_restriction,
            field = 'item_class_id_restriction',
            type = 'List (,) of String',
            func = function(tpl_args, value)
                if value == nil then 
                    return nil
                end
                value = m_util.string.split(value, ', ')
                for _, v in ipairs(value) do
                    if m_game.constants.item.classes[v] == nil then
                        error(string.format(i18n.errors.skill.invalid_item_class_id, v))
                    end
                end
                return value
            end,
        },
        item_class_restriction = {
            name = i18n.parameters.skill.item_class_restriction,
            field = 'item_class_restriction',
            type = 'List (,) of String',
            func = function(tpl_args, value)
                if tpl_args.item_class_id_restriction == nil then
                    return
                end
                -- This function makes a localized list based on ids
                local item_classes = {}
                for _, v in ipairs(tpl_args.item_class_id_restriction) do
                    item_classes[#item_classes+1] = m_game.constants.item.classes[v].full
                end
                
                return item_classes
            end,
        },
        -- Projectiles.dat - manually mapped to the skills
        projectile_speed = {
            name = i18n.parameters.skill.projectile_speed,
            field = 'projectile_speed',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        -- Misc data derieved from stats
        stat_text = {
            name = i18n.parameters.skill.stat_text,
            field = 'stat_text',
            type = 'Text',
            func = nil,
        },
        -- Misc data currently not from game data
        radius = {
            name = i18n.parameters.skill.radius,
            field = 'radius',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        radius_description = {
            name = i18n.parameters.skill.radius_description,
            field = 'radius_description',
            type = 'Text',
            func = h.cast.wrap(m_util.cast.text),
        },
        radius_secondary = {
            name = i18n.parameters.skill.radius_secondary,
            field = 'radius_secondary',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        radius_secondary_description = {
            name = i18n.parameters.skill.radius_secondary_description,
            field = 'radius_secondary_description',
            type = 'Text',
            func = h.cast.wrap(m_util.cast.text),
        },
        radius_tertiary = { -- not sure if any skill actually has 3 radius componets
            name = i18n.parameters.skill.radius_tertiary,
            field = 'radius_tertiary',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        radius_tertiary_description = {
            name = i18n.parameters.skill.radius_tertiary_description,
            field = 'radius_tertiary_description',
            type = 'Text',
            func = h.cast.wrap(m_util.cast.text),
        },
        skill_screenshot = {
            name = i18n.parameters.skill.skill_screenshot,
            field = 'skill_screenshot',
            type = 'Page',
            func = function(tpl_args, value)
                if tpl_args.skill_screenshot_file then
                    tpl_args._flags.has_deprecated_skill_parameters = true
                    value = string.format('File:%s', tpl_args.skill_screenshot_file)
                elseif value then
                    value = string.format(i18n.files.skill_screenshot, value)
                elseif tpl_args.active_skill_name then
                    -- When this parameter is set manually, we assume/expect it to exist, but otherwise it probably doesn't and we don't need dead links in that case
                    value = string.format(i18n.files.skill_screenshot, tpl_args.active_skill_name)
                    local page = mw.title.new(value)
                    if page == nil or not page.exists then
                        value = nil
                    end
                else
                    value = nil
                end
                return value
            end,
        },
        -- Set programmatically
        max_level = {
            name = nil,
            field = 'max_level',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        html = {
            name = nil,
            field = 'html',
            type = 'Text',
            func = nil,
        },
    },
}

tables.progression = {
    table = 'skill_levels',
    fields = {
        level = {
            name = nil,
            field = 'level',
            type = 'Integer',
            func = nil,
        },
        level_requirement = {
            name = i18n.parameters.skill.level_requirement,
            field = 'level_requirement',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        dexterity_requirement = {
            name = i18n.parameters.skill.dexterity_requirement,
            field = 'dexterity_requirement',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        strength_requirement = {
            name = i18n.parameters.skill.strength_requirement,
            field = 'strength_requirement',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        intelligence_requirement = {
            name = i18n.parameters.skill.intelligence_requirement,
            field = 'intelligence_requirement',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        cost_multiplier = {
            name = i18n.parameters.skill.cost_multiplier,
            field = 'cost_multiplier',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%s%%',
        },
        attack_time = {
            name = i18n.parameters.skill.attack_time,
            field = 'attack_time',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%.2f ' .. m_game.units.seconds.short_lower,
        },
        critical_strike_chance = {
            name = i18n.parameters.skill.critical_strike_chance,
            field = 'critical_strike_chance',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%s%%',
        },
        damage_effectiveness = {
            name = i18n.parameters.skill.damage_effectiveness,
            field = 'damage_effectiveness',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%s%%',
        },
        stored_uses = {
            name = i18n.parameters.skill.stored_uses,
            field = 'stored_uses',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        cooldown = {
            name = i18n.parameters.skill.cooldown,
            field = 'cooldown',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%.2f ' .. m_game.units.seconds.short_lower,
        },
        vaal_souls_requirement = {
            name = i18n.parameters.skill.vaal_souls_requirement,
            field = 'vaal_souls_requirement',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        vaal_stored_uses = {
            name = i18n.parameters.skill.vaal_stored_uses,
            field = 'vaal_stored_uses',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
            header = i18n.progression.vaal_stored_uses,
        },
        vaal_soul_gain_prevention_time = {
            name = i18n.parameters.skill.vaal_soul_gain_prevention_time,
            field = 'vaal_soul_gain_prevention_time',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%i ' .. m_game.units.seconds.short_lower,
        },
        damage_multiplier = {
            name = i18n.parameters.skill.damage_multiplier,
            field = 'damage_multiplier',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%s%%',
        },
        attack_speed_multiplier = {
            name = i18n.parameters.skill.attack_speed_multiplier,
            field = 'attack_speed_multiplier',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%s%%',
        },
        duration = {
            name = i18n.parameters.skill.duration,
            field = 'duration',
            type = 'Float',
            func = h.cast.wrap(m_util.cast.number),
            fmt = '%.2f ' .. m_game.units.seconds.short_lower,
        },
        cost_types = {
            name = i18n.parameters.skill.cost_types,
            field = 'cost_types',
            type = 'List (,) of String',
            func = function(tpl_args, value)
                if value == nil then 
                    return nil
                end
                value = m_util.cast.table(value)
                for _, v in ipairs(value) do
                    if m_game.constants.skill.cost_types[v] == nil then
                        error(string.format(i18n.errors.skill.invalid_cost_type, v))
                    end
                end
                return value
            end,
        },
        cost_amounts = {
            name = i18n.parameters.skill.cost_amounts,
            field = 'cost_amounts',
            type = 'List (,) of Integer',
            func = function(tpl_args, value)
                if value == nil then 
                    return nil
                end
                value = m_util.cast.table(value, {callback = m_util.cast.number})
                return value
            end,
        },
        mana_reservation_flat = {
            name = i18n.parameters.skill.mana_reservation_flat,
            field = 'mana_reservation_flat',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        mana_reservation_percent = {
            name = i18n.parameters.skill.mana_reservation_percent,
            field = 'mana_reservation_percent',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        life_reservation_flat = {
            name = i18n.parameters.skill.life_reservation_flat,
            field = 'life_reservation_flat',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        life_reservation_percent = {
            name = i18n.parameters.skill.life_reservation_percent,
            field = 'life_reservation_percent',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        -- from gem experience, optional
        experience = {
            name = i18n.parameters.skill.experience,
            field = 'experience',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
        stat_text = {
            name = i18n.parameters.skill.stat_text,
            field = 'stat_text',
            type = 'Text',
            func = h.cast.wrap(m_util.cast.text),
        },
    }
}

tables.skill_stats_per_level = {
    table = 'skill_stats_per_level',
    fields = {
        level = {
            name = nil,
            field = 'level',
            type = 'Integer',
            func = nil,
        },
        id = {
            name = i18n.parameters.skill.stat_id,
            field = 'id',
            type = 'String',
            func = h.cast.wrap(m_util.cast.text),
        },
        value = {
            name = i18n.parameters.skill.stat_value,
            field = 'value',
            type = 'Integer',
            func = h.cast.wrap(m_util.cast.number),
        },
    },
}

tables.skill_quality = {
    table = 'skill_quality',
    fields = {
        set_id = {
            field = 'set_id',
            type = 'Integer',
        },
        weight = {
            field = 'weight',
            type = 'Integer',
        },
        stat_text = {
            field = 'stat_text',
            type = 'String',
        },
    },
}

tables.skill_quality_stats = {
    table = 'skill_quality_stats',
    fields = {
        set_id = {
            field = 'set_id',
            type = 'Integer',
        },
        id = {
            field = 'id',
            type = 'String',
        },
        value = {
            field = 'value',
            type = 'Integer',
        },
    },
}

-- ----------------------------------------------------------------------------
-- Data
-- ----------------------------------------------------------------------------

data.skill_progression_table = {
    {
        field = 'level',
        header = i18n.progression.level,
    },
    {
        field = 'level_requirement',
        header = i18n.progression.level_requirement,
    },
    {
        field = 'dexterity_requirement',
        header = i18n.progression.dexterity_requirement,
    },
    {
        field = 'strength_requirement',
        header = i18n.progression.strength_requirement,
    },
    {
        field = 'intelligence_requirement',
        header = i18n.progression.intelligence_requirement,
    },
    {
        field = 'cost_multiplier',
        header = i18n.progression.cost_multiplier,
        fmt = '%s%%',
    },
    {
        field = 'attack_time',
        header = i18n.progression.attack_time,
        fmt = '%.2f ' .. m_game.units.seconds.short_lower,
    },
    {
        field = 'critical_strike_chance',
        header = i18n.progression.critical_strike_chance,
        fmt = '%s%%',
    },
    {
        field = 'cost_Mana',
        header = i18n.progression.mana_cost,
    },
    {
        field = 'cost_Life',
        header = i18n.progression.life_cost,
    },
    {
        field = 'cost_ES',
        header = i18n.progression.energy_shield_cost,
    },
    {
        field = 'cost_Rage',
        header = i18n.progression.rage_cost,
    },
    {
        field = 'cost_ManaPercent',
        header = i18n.progression.mana_cost,
        fmt = '%s%%',
    },
    {
        field = 'cost_LifePercent',
        header = i18n.progression.life_cost,
        fmt = '%s%%',
    },
    {
        field = 'mana_reservation_flat',
        header = i18n.progression.mana_reserved,
    },
    {
        field = 'mana_reservation_percent',
        header = i18n.progression.mana_reserved,
        fmt = '%s%%',
    },
    {
        field = 'life_reservation_flat',
        header = i18n.progression.life_reserved,
    },
    {
        field = 'life_reservation_percent',
        header = i18n.progression.life_reserved,
        fmt = '%s%%',
    },
    {
        field = 'damage_effectiveness',
        header = i18n.progression.damage_effectiveness,
        fmt = '%s%%',
    },
    {
        field = 'stored_uses',
        header = i18n.progression.stored_uses,
    },
    {
        field = 'cooldown',
        header = i18n.progression.cooldown,
        fmt = '%.2f ' .. m_game.units.seconds.short_lower,
    },
    {
        field = 'vaal_souls_requirement',
        header = i18n.progression.vaal_souls_requirement,
    },
    {
        field = 'vaal_stored_uses',
        header = i18n.progression.vaal_stored_uses,
    },
    {
        field = 'vaal_soul_gain_prevention_time',
        header = i18n.progression.vaal_soul_gain_prevention_time,
        fmt = '%i ' .. m_game.units.seconds.short_lower,
    },
    {
        field = 'damage_multiplier',
        header = i18n.progression.damage_multiplier,
        fmt = '%s%%',
    },
    {
        field = 'duration',
        header = i18n.progression.duration,
        fmt = '%.2f ' .. m_game.units.seconds.short_lower,
    },
    {
        field = 'attack_speed_multiplier',
        header = i18n.progression.attack_speed_multiplier,
        fmt = '%s%%',
    },
}

data.infobox_table = {
    {
        header = i18n.infobox.active_skill_name,
        func = h.display.factory.value{key='active_skill_name'},
    },
    {
        header = i18n.infobox.skill_id,
        func = function (tpl_args)
            return string.format('[[%s|%s]]', mw.title.getCurrentTitle().fullText, tpl_args.skill_id)
        end 
    },
    {
        header = i18n.infobox.skill_icon,
        func = function (tpl_args)
            if tpl_args.skill_icon then 
                return string.format('[[%s]]', tpl_args.skill_icon)
            end
        end,
    },
    {
        header = i18n.infobox.cast_time,
        func = function (tpl_args)
            local value = tpl_args.cast_time
            if value then
                if value == 0 then
                    return i18n.infobox.instant_cast_time
                end
                return string.format('%.2f %s', value, m_game.units.seconds.short_lower)
            end
            return value
        end,
    },
    {
        header = i18n.infobox.item_class_restrictions,
        func = function (tpl_args)
            if tpl_args.item_class_restriction == nil then
                return
            end
            local out = {}
            for _, class in ipairs(tpl_args.item_class_restriction) do
                out[#out+1] = string.format('[[%s]]', class)
            end
            return table.concat(out, '<br>')
        end,
    },
    {
        header = i18n.infobox.projectile_speed,
        func = h.display.factory.value{key='projectile_speed'},
    },
    {
        header = i18n.infobox.radius,
        func = h.display.factory.radius{key=''},
    },
    {
        header = i18n.infobox.radius_secondary,
        func = h.display.factory.radius{key='_secondary'},
    },
    {
        header = i18n.infobox.radius_tertiary,
        func = h.display.factory.radius{key='_tertiary'},
    },
    {
        header = i18n.infobox.level_requirement,
        func = h.display.factory.range_value{key='level_requirement'},
    },
    -- ignore attrbiutes?
    {
        header = i18n.infobox.cost_multiplier,
        func = h.display.factory.range_value{key='cost_multiplier'},
    },
    {
        header = i18n.infobox.attack_time,
        func = h.display.factory.range_value{key='attack_time'},
    },
    {
        header = i18n.infobox.critical_strike_chance,
        func = h.display.factory.range_value{key='critical_strike_chance'},
    },
    {
        header = i18n.infobox.cost,
        func = function (tpl_args)
            local parts = {}
            for k, v in pairs(m_game.constants.skill.cost_types) do
                local key = 'cost_' .. k
                local fmt
                if string.find(k, 'Percent', 1, true) then
                    fmt = '%s%% %s'
                else
                    fmt = '%s %s'
                end
                local range = h.display.factory.range_value{key=key}(tpl_args)
                if range then
                    parts[#parts+1] = string.format(fmt, range, v.long_lower)
                end
            end
            return table.concat(parts, ', ')
        end,
    },
    {
        header = i18n.infobox.reservation,
        func = function (tpl_args)
            local parts = {}
            local keys = {
                {
                    key = 'mana_reservation_flat',
                    fmt = '%s ' .. m_game.constants.skill.cost_types['Mana'].long_lower,
                },
                {
                    key = 'mana_reservation_percent',
                    fmt = '%s%% ' .. m_game.constants.skill.cost_types['Mana'].long_lower,
                },
                {
                    key = 'life_reservation_flat',
                    fmt = '%s ' .. m_game.constants.skill.cost_types['Life'].long_lower,
                },
                {
                    key = 'life_reservation_percent',
                    fmt = '%s%% ' .. m_game.constants.skill.cost_types['Life'].long_lower,
                },
            }
            for _, v in ipairs(keys) do
                local range = h.display.factory.range_value{key=v.key}(tpl_args)
                if range then
                    parts[#parts+1] = string.format(v.fmt, range)
                end
            end
            return table.concat(parts, ', ')
        end,
    },
    {
        header = i18n.infobox.attack_speed_multiplier,
        func = h.display.factory.range_value{key='attack_speed_multiplier'},
        fmt = '%s ' .. i18n.infobox.of_base_stat,
    },
    {
        header = i18n.infobox.damage_multiplier,
        func = h.display.factory.range_value{key='damage_multiplier'},
        fmt = '%s ' .. i18n.infobox.of_base_stat,
    },
    {
        header = i18n.infobox.damage_effectiveness,
        func = h.display.factory.range_value{key='damage_effectiveness'},
    },
    {
        header = i18n.infobox.stored_uses,
        func = h.display.factory.range_value{key='stored_uses'},
    },
    {
        header = i18n.infobox.cooldown,
        func = h.display.factory.range_value{key='cooldown'},
    },
    {
        header = i18n.infobox.vaal_souls_requirement,
        func = h.display.factory.range_value{key='vaal_souls_requirement'},
    },
    {
        header = i18n.infobox.vaal_stored_uses,
        func = h.display.factory.range_value{key='vaal_stored_uses'},
    },
    {
        header = i18n.infobox.vaal_soul_gain_prevention_time,
        func = h.display.factory.range_value{key='vaal_soul_gain_prevention_time'},
    }, 
    {
        header = i18n.infobox.duration,
        func = h.display.factory.range_value{key='duration'},
    },
    {
        header = nil,
        func = h.display.factory.value{key='gem_description'},
        class = 'tc -gemdesc',
    },
    {
        header = nil,
        func = h.display.factory.value{key='stat_text'},
        class = 'tc -mod',
    },
}

-- ----------------------------------------------------------------------------
-- Main functions
-- ----------------------------------------------------------------------------

local function _process_skill_data(tpl_args)
    --[[
    Processes skill data from tpl_args.
    Stores skill data in cargo tables.
    Attaches page to cargo tables.
    --]]
    
    tpl_args = tpl_args or {}
    tpl_args._flags = tpl_args._flags or {}
    tpl_args.skill_levels = {
        [0] = {},
    }
    
    -- Quality
    tpl_args.skill_quality = {}
    local i = 0
    repeat
        i = i + 1
        local prefix = string.format('quality_type%s', i)
        local q = {
            _table = tables.skill_quality.table,
            set_id = i,
            weight = tonumber(tpl_args[string.format('%s_weight', prefix)]),
            stat_text = tpl_args[string.format('%s_stat_text', prefix)],
        }
        if q.stat_text then
            tpl_args.skill_quality[#tpl_args.skill_quality+1] = q
            m_cargo.store(q)
            
            q.stats = {}
            q._table = nil
            local j = 0
            repeat 
                j = j + 1
                local stat_prefix = string.format('%s_stat%s', prefix, j)
                local s = {
                    _table = tables.skill_quality_stats.table,
                    set_id = i,
                    id = tpl_args[string.format('%s_id', stat_prefix)],
                    value = tonumber(tpl_args[string.format('%s_value', stat_prefix)]),
                }
                if s.id and s.value then
                    q.stats[#q.stats+1] = s
                    m_cargo.store(s)
                end
                
                s._table = nil
            until s.id == nil or s.value == nil
        end
    until q.stat_text == nil
    if #tpl_args.skill_quality > 1 then
        -- Gem has alternative qualtiy
        tpl_args._flags.is_alt_quality_gem = true
    end
    
    -- Handle level progression
    local level_count = 0
    for i=1, math.huge do -- repeat until no more levels are found
        local prefix = i18n.parameters.skill.level .. i
        local level = m_util.cast.boolean(tpl_args[prefix])
        if not level then
            break
        end
        tpl_args.skill_levels[i] = {}
        prefix = prefix .. '_'
        level_count = i
        if tpl_args[prefix .. i18n.parameters.skill.experience] ~= nil then
            -- For skill gems, max level is the highest level with experience.
            tpl_args.max_level = i
        end
        local properties = {
            _table = tables.progression.table,
            [tables.progression.fields.level.field] = i
        }
        h.map_to_arg(tpl_args, properties, prefix, tables.progression, i)
        if not tpl_args.test then
            m_cargo.store(properties)
        end
        h.stats(tpl_args, prefix, i)
    end
    tpl_args.max_level = tpl_args.max_level or level_count

    -- handle static progression
    local prefix = i18n.parameters.skill.static .. '_'
    do
        local properties = {
            _table = tables.progression.table,
            [tables.progression.fields.level.field] = 0
        }
        h.map_to_arg(tpl_args, properties, prefix, tables.progression, 0)
        if not tpl_args.test then
            m_cargo.store(properties)
        end
    end

    -- Expand costs data
    h.expand_costs_data(tpl_args, tpl_args.skill_levels)
    
    -- Handle static arguments
    local properties = {
        _table = tables.static.table,
        [tables.static.fields.max_level.field] = tpl_args.max_level
    }
    h.map_to_arg(tpl_args, properties, '', tables.static)
    h.stats(tpl_args, prefix, 0)

    -- Build infobox
    local infobox = mw.html.create('span')
    infobox:addClass('skill-box')
    local tbl = infobox:tag('table')
    tbl:addClass('wikitable skill-box-table')
    for _, infobox_data in ipairs(data.infobox_table) do
        local display = infobox_data.func(tpl_args)
        if type(display) == 'string' and string.len(display) > 0 then
            if infobox_data.fmt ~= nil then
                if type(infobox_data.fmt) == 'string' then
                    display = string.format(infobox_data.fmt, display)
                elseif type(infobox_data.fmt) == 'function' then
                    display = string.format(infobox_data.fmt(tpl_args) or '%s', display)
                end
            end
            local tr = tbl:tag('tr')
            if infobox_data.header then
                local header_text
                if type(infobox_data.header) == 'function' then
                    header_text = infobox_data.header(tpl_args)
                else
                    header_text = infobox_data.header
                end
                tr
                    :tag('th')
                        :wikitext(header_text)
                        :done()
            end
            local td = tr:tag('td')
            td:wikitext(display)
            td:addClass(infobox_data.class or 'tc -value')
            if infobox_data.header == nil then
                td:attr('colspan', 2)
            end
        end
    end
    infobox = tostring(infobox)

    -- Store data
    properties[tables.static.fields.html.field] = infobox
    if not tpl_args.test then
        m_cargo.store(properties)
    end

    -- Attach tables
    if not tpl_args.test then
        local attach_tables = {
            tables.static.table,
            tables.progression.table,
        }
        if #tpl_args.skill_quality > 0 then
            attach_tables[#attach_tables+1] = tables.skill_quality.table
            attach_tables[#attach_tables+1] = tables.skill_quality_stats.table
        end
        if tpl_args.skill_levels.has_stats then
            attach_tables[#attach_tables+1] = tables.skill_stats_per_level.table
        end
        for _, table_name in ipairs(attach_tables) do
            mw.getCurrentFrame():expandTemplate{
                title = string.format(i18n.templates.cargo_attach, table_name),
                args = {}
            }
        end
    end

     -- Log when testing
    if tpl_args.test then
        mw.logObject(tpl_args)
    end

    return infobox
end

local function _skill(tpl_args)
    --[[
    Display skill infobox
    
    Examples
    --------
    =p.skill{gem_description='Icy bolts rain down over the targeted area.', active_skill_name='Icestorm', skill_id='IcestormUniqueStaff12', cast_time=0.75, required_level=1, static_mana_cost=22, static_critical_strike_chance=6, static_damage_effectiveness=30, static_damage_multiplier=100, static_stat1_id='spell_minimum_base_cold_damage_+_per_10_intelligence', static_stat1_value=1, static_stat2_id='spell_maximum_base_cold_damage_+_per_10_intelligence', static_stat2_value=3, static_stat3_id='base_skill_effect_duration', static_stat3_value=1500, static_stat4_id='fire_storm_fireball_delay_ms', static_stat4_value=100, static_stat5_id='skill_effect_duration_per_100_int', static_stat5_value=150, static_stat6_id='skill_override_pvp_scaling_time_ms', static_stat6_value=450, static_stat7_id='firestorm_drop_ground_ice_duration_ms', static_stat7_value=500, static_stat8_id='skill_art_variation', static_stat8_value=4, static_stat9_id='base_skill_show_average_damage_instead_of_dps', static_stat9_value=1, static_stat10_id='is_area_damage', static_stat10_value=1, stat_text='Deals 1 to 3 base Cold Damage per 10 Intelligence<br>Base duration is 1.5 seconds<br>One impact every 0.1 seconds<br>0.15 seconds additional Base Duration per 100 Intelligence', quality_stat_text = nil, level1=true, level1_level_requirement=1}

    ]]

    -- Handle skill data and get infobox
    local infobox = _process_skill_data(tpl_args)

    -- Container
    local container = mw.html.create('span')
    container
        :addClass('skill-box-page-container')
        :wikitext(infobox)
    if tpl_args.skill_screenshot then
        container
            :wikitext(string.format('[[%s]]', tpl_args.skill_screenshot))
    end

    -- Generic messages on the page:
    local out = {}
    if mw.ustring.find(tpl_args.skill_id, '_') then
        out[#out+1] = mw.getCurrentFrame():expandTemplate{
            title = i18n.templates.incorrect_title,
            args = {title=tpl_args.skill_id}
        } .. '\n\n\n'
    end
    if tpl_args.active_skill_name then
        out[#out+1] = string.format(
            i18n.messages.intro_named_id, 
            tpl_args.skill_id, 
            tpl_args.active_skill_name
        )
    else
        out[#out+1] = string.format(
            i18n.messages.intro_unnamed_id, 
            tpl_args.skill_id
        )
    end

    -- Categories
    local cats = {i18n.categories.skill_data}
    if tpl_args._flags.has_deprecated_skill_parameters then
        cats[#cats+1] = i18n.categories.deprecated_parameters
    end
    
    return tostring(container) .. m_util.misc.add_category(cats) .. '\n' .. table.concat(out)
end

local function _progression(tpl_args)
    --[[
        Displays the level progression for the skill gem. 
        
        Examples
        --------
        = p.progression{page='Reave'}
    ]]
    
    -- Parse column arguments:
    tpl_args.stat_format = {}
    local param_keys = {
        i18n.parameters.progression.header,
        i18n.parameters.progression.abbr,
        i18n.parameters.progression.pattern_extract,
        i18n.parameters.progression.pattern_value,
    }
    for i=1, math.huge do -- repeat until no more columns are found
        local prefix = string.format('%s%d_', i18n.parameters.progression.column, i)
        if tpl_args[prefix .. param_keys[1]] == nil then
            break
        end
        local statfmt = {counter = 0}
        for _, key in ipairs(param_keys) do
            local arg = prefix .. key
            if tpl_args[arg] == nil then
                error(string.format(i18n.errors.progression.argument_unspecified, arg))
            end
            statfmt[key] = tpl_args[arg]
        end
        statfmt.header = m_util.html.abbr(statfmt.abbr, statfmt.header)
        statfmt.abbr = nil
        tpl_args.stat_format[#tpl_args.stat_format+1] = statfmt
    end
    
    -- Query skill data
    local skill_data = h.query_skill(tpl_args)

    -- Query progression data
    local fields = {}
    for _, fmap in pairs(tables.progression.fields) do
        fields[#fields+1] = fmap.field
    end
    local query = {
        where = string.format(
            '_pageID="%s"',
            skill_data._pageID
        ),
        groupBy = string.format(
            '_pageID, %s',
            tables.progression.fields.level.field
        ),
    }
    local results = m_cargo.query({tables.progression.table}, fields, query)
    
    -- Re-index by level
    skill_data.levels = {}
    for _, v in ipairs(results) do
        skill_data.levels[tonumber(v.level)] = v
    end
    if #skill_data.levels == 0 then
        error(i18n.errors.progression.missing_level_data)
    end

    -- Expand costs data
    h.expand_costs_data(tpl_args, skill_data.levels)
    
    -- Set up html table headers
    local headers = {}
    for _, row in ipairs(skill_data.levels) do
        for k, v in pairs(row) do
            headers[k] = true
        end
    end
    local tbl = mw.html.create('table')
    tbl
        :addClass('wikitable responsive-table skill-progression-table')
    local head = tbl:tag('tr')
    for _, tmap in pairs(data.skill_progression_table) do
        if headers[tmap.field] then
            local text = type(tmap.header) == 'function' and tmap.header(tpl_args) or tmap.header
            head
                :tag('th')
                    :wikitext(text)
                    :done()
        end
    end
    for _, statfmt in ipairs(tpl_args.stat_format) do
        head
            :tag('th')
                :wikitext(statfmt.header)
                :done()
    end
    if headers[tables.progression.fields.experience.field] then
        head
            :tag('th')
                :wikitext(i18n.progression.experience)
                :done()
            :tag('th')
                :wikitext(i18n.progression.total_experience)
                :done()
    end

    -- Table rows
    local tblrow
    local lastexp = 0
    local experience
    for _, row in ipairs(skill_data.levels) do
        tblrow = tbl:tag('tr')
        for _, tmap in pairs(data.skill_progression_table) do
            if headers[tmap.field] then
                h.int_value_or_na(tpl_args, tblrow, row[tmap.field], tmap)
            end
        end
        
        -- stats
        local stats = {}
        if row[tables.progression.fields.stat_text.field] then
            stats = m_util.string.split(
                row[tables.progression.fields.stat_text.field],
                '<br>'
            )
        end
        for _, statfmt in ipairs(tpl_args.stat_format) do
            local match = {}
            for j, stat in ipairs(stats) do
                match = {string.match(stat, statfmt.pattern_extract)}
                if #match > 0 then
                    -- TODO maybe remove stat here to avoid testing 
                    -- against in future loops
                    break
                end
            end
            if #match == 0 then
                tblrow:node(m_util.html.td.na())
            else
                -- used to find broken progression due to game updates
                -- for example:
                statfmt.counter = statfmt.counter + 1
                tblrow
                    :tag('td')
                        :wikitext(string.format(
                            statfmt.pattern_value, 
                            match[1], 
                            match[2], 
                            match[3], 
                            match[4], 
                            match[5]
                            )
                        )
                        :done()
            end
        end
        
        if headers[tables.progression.fields.experience.field] then
            experience = tonumber(row[tables.progression.fields.experience.field])
            if experience ~= nil then
                h.int_value_or_na(tpl_args, tblrow, experience - lastexp, {})
                lastexp = experience
            else
                tblrow:node(m_util.html.td.na())
            end
            h.int_value_or_na(tpl_args, tblrow, experience, {})
        end
    end
    
    local cats = {}
    for _, statfmt in ipairs(tpl_args.stat_format) do
        if statfmt.counter == 0 then
            cats = i18n.categories.broken_progression_table
            break
        end
    end
    
    return tostring(tbl) .. m_util.misc.add_category(cats)
end

local function _quality(tpl_args)
    --[[
    Displays a table comparing the stats of superior gem quality with 
    alternative quality types.
    --]]

    -- Query skill data
    local skill_data = h.query_skill(tpl_args)

    -- Query progression data
    local fields = {}
    for _, fmap in pairs(tables.skill_quality.fields) do
        fields[#fields+1] = fmap.field
    end
    local query = {
        where = string.format(
            '_pageID="%s"',
            skill_data._pageID
        ),
        groupBy = string.format(
            '_pageID, %s',
            tables.skill_quality.fields.set_id.field
        ),
        orderBy = string.format(
            '%s ASC',
            tables.skill_quality.fields.set_id.field
        ),
    }
    skill_data.quality = m_cargo.query({tables.skill_quality.table}, fields, query)
    if #skill_data.quality == 0 then
        error(i18n.errors.quality.missing_quality_data)
    end

    -- Build table
    local tbl = mw.html.create('table')
    tbl
        :addClass('wikitable skill-quality-table')
        :tag('tr')
            :tag('th')
                :wikitext(i18n.quality.type)
                :done()
            :tag('th')
                :wikitext(i18n.quality.stats)
                :done()
            :tag('th')
                :wikitext(i18n.quality.weight)
                :done()
    for k, row in ipairs(skill_data.quality) do
        tbl
            :tag('tr')
                :tag('td')
                    :wikitext(m_game.constants.item.gem_quality_types[k].long_upper)
                    :done()
                :tag('td')
                    :addClass('tc -mod')
                    :wikitext(skill_data.quality[k].stat_text)
                    :done()
                :tag('td')
                    :wikitext(skill_data.quality[k].weight)
                    :done()
    end
    
    return tostring(tbl)
end

-- ----------------------------------------------------------------------------
-- Exported functions
-- ----------------------------------------------------------------------------
local p = {}

p.table_skills = m_cargo.declare_factory{data=tables.static}
p.table_skill_levels = m_cargo.declare_factory{data=tables.progression}
p.table_skill_stats_per_level = m_cargo.declare_factory{data=tables.skill_stats_per_level}
p.table_skill_quality = m_cargo.declare_factory{data=tables.skill_quality}
p.table_skill_quality_stats = m_cargo.declare_factory{data=tables.skill_quality_stats}

function p.process_skill_data(tpl_args)
    _process_skill_data(tpl_args)
end

p._skill = p.process_skill_data

-- 
-- Template:Skill
-- 
p.skill = m_util.misc.invoker_factory(_skill, {
    wrappers = cfg.wrappers.skill,
})

-- 
-- Template:Skill progression
-- 
p.progression = m_util.misc.invoker_factory(_progression, {
    wrappers = cfg.wrappers.progression,
})

-- 
-- Template:Skill quality
-- 
p.quality = m_util.misc.invoker_factory(_quality, {
    wrappers = cfg.wrappers.quality,
})

return p