Module:Passive skill link

From Path of Exile Wiki
Jump to: navigation, search
Module documentation[create]
--
-- Module for passive skill links
--

local m_util = require('Module:Util')
local m_cargo = require('Module:Cargo')
local getArgs = require('Module:Arguments').getArgs


-- ----------------------------------------------------------------------------
-- Strings
-- ----------------------------------------------------------------------------

local i18n = {
    icon_name = 'File:%s passive skill icon.png',
    errors = {
        invalid_args = 'id, passive_name, page or q_where must be specified',
        too_many_passives_found = 'Too many passives found with q_where = %s',
        no_passives_found = 'No passives found with q_where = %s',
        invalid_format = 'Invalid return format specified: %s',
        cats = 'Pages with passive skill link errors'
    },
}


-- ----------------------------------------------------------------------------
-- Constants & Data
-- ----------------------------------------------------------------------------

local c = {}
c.image_size = 39
c.image_size_full = c.image_size * 2
c.parameters = {
    name = 'passive_skills.name',
    icon = 'passive_skills.icon',
    is_keystone = 'passive_skills.is_keystone',
    is_notable = 'passive_skills.is_notable',
    ascendancy_class = 'passive_skills.ascendancy_class',
    html = 'passive_skills.html',
    page = 'passive_skills.main_page',
}

c.selectors = {'id', 'page', 'passive_name', 'q_where'}


-- ----------------------------------------------------------------------------
-- Helper functions
-- ----------------------------------------------------------------------------

local h = {}

h.type_order = {
    'basic',
    'notable',
    'keystone',
    'ascendancy_basic',
    'ascendancy_notable'
}
function h.get_type(passive)
    --[[
    Determine what type of passive skill this passive is.
    ]]
    local key
    if tonumber(passive['passive_skills.is_keystone']) == 1 then
        key = 'keystone'
    elseif tonumber(passive['passive_skills.is_notable']) == 1 then
        key = 'notable'
    else
        key = 'basic'
    end

    if passive['passive_skills.ascendancy_class'] ~= nil then
        key = 'ascendancy_' .. key
    end

    return key
end

function h.format_passive_icon(passive, tpl_args)
    --[[
    Add a frame to the passive image.

    TODO: Add a inline size.
    ]]

    if passive['passive_skills.icon'] == nil then
        return ''
    end

    local div = mw.html.create('div')
    if tpl_args.large then
        -- Only use frames for large pictures, hard to see anything with it:
        local _type = h.get_type(passive)
        if _type ~= nil then
            local cls = string.format('passive-icon-type__%s', _type)
            div:addClass('passive-icon-container')
            div:addClass(cls)
        end

        div:tag('div')
            :addClass('passive-icon-frame')
            :done()
        div:wikitext(
            string.format(
                '[[%s|link=%s]]',
                passive['passive_skills.icon'],
                passive['passive_skills.main_page']
            )
        )
    else
        -- Inline size
        -- div:tag('div')
            -- :addClass('passive-icon-frame')
            -- :attr('style', 'height:16px;width:16px')
            -- :done()
        div = (
            string.format(
                '[[%s|14px|link=%s]]',
                passive['passive_skills.icon'],
                passive['passive_skills.main_page']
            )
        )
    end
    return tostring(div)
end

-- ----------------------------------------------------------------------------
-- Page functions
-- ----------------------------------------------------------------------------

local p = {}

function p.passive_skill_link(frame)
    --[[
    Links a passive skill.

    TODO:
    * Use own CSS.

    Examples
    --------
    = p.passive_skill_link{'Ghost Reaver', "Ghost Reaver's"}
    = p.passive_skill_link{id='AscendancyAscendant45'}
    = p.passive_skill_link{
        skip_query=true,
        page='Passive Skill:AscendancyAscendant45',
        name='test',
        icon='File:GLADSpeedAoE (Gladiator) passive skill icon.png',
        large=1,
    }
    = p.passive_skill_link{id='AscendancyAscendant45', format='tablerow'}

    ]]

    -- Get args
    local tpl_args = getArgs(frame, {
        parentFirst = true
    })
    frame = m_util.misc.get_frame(frame)

    -- Hande first and second arguments:
    tpl_args.passive_name = tpl_args.passive_name or tpl_args[1]
    tpl_args.name = tpl_args.name or tpl_args[2]

    -- Check if the correct parameters have been set:
    if m_util.table.has_all_value(tpl_args, c.selectors) and tpl_args.skip_query == nil then
        return m_util.html.error{msg=i18n.errors.invalid_args .. m_util.misc.add_category(i18n.errors.cats)}
    end

    --
    local passive = {}
    if m_util.table.has_one_value(tpl_args, c.selectors, nil) and tpl_args.skip_query == nil then
        -- Create q_where depending on the input:
        if tpl_args.passive_name then
            tpl_args.q_where = string.format(
                'passive_skills.name="%s"',
                tpl_args.passive_name
            )
        elseif tpl_args.id then
            tpl_args.q_where = string.format(
                'passive_skills.id="%s"',
                tpl_args.id
            )
        elseif tpl_args.q_where then
            -- Use tpl_args.q_where.
        else
            return m_util.html.error{
                msg=i18n.errors.invalid_args .. m_util.misc.add_category(i18n.errors.cats)
            }
        end

        -- Query cargo:
        local results = m_cargo.query(
            {'passive_skills', 'main_pages'},
            {
                'passive_skills._pageName',
                'passive_skills.stat_text',
                'passive_skills.main_page',
                'passive_skills.name',
                'passive_skills.icon',
                'passive_skills.is_keystone',
                'passive_skills.is_notable',
                'passive_skills.ascendancy_class',
                -- 'passive_skills.html',
                'main_pages._pageName',
            },
            {
                join='passive_skills.id=main_pages.id',
                where=string.format(
                    '(%s)',
                    tpl_args.q_where
                ),
                orderBy='passive_skills.stat_text',
                limit=2,
            }
        )

        -- Check number of results, there should only be one result:
        if #results > 1 then
            local q_where = tostring(tpl_args.q_where)
            return m_util.html.error{
                msg=string.format(
                    i18n.errors.too_many_passives_found,
                    q_where .. m_util.misc.add_category(i18n.errors.cats)
                    )
                }
        elseif #results < 1 then
            local q_where = tostring(tpl_args.q_where)
            return m_util.html.error{
                msg=string.format(
                    i18n.errors.no_passives_found,
                    q_where .. m_util.misc.add_category(i18n.errors.cats)
                )
            }
        end

        passive = results[1]
    end

    -- Set the link to the main page:
    passive['passive_skills.main_page'] = passive['passive_skills.main_page']
        or passive['main_pages._pageName']
        -- or passive['passive_skills.name']
        or passive['passive_skills._pageName']

    -- Add allowed override parameters:
    for k, prop in pairs(c.parameters) do
        if tpl_args[k] ~= nil then
            passive[prop] = tpl_args[k]
        end
    end

    -- Format the passive icon:
    local img = h.format_passive_icon(passive, tpl_args)

    ---------------------------------------------------------------------------
    -- Output
    ---------------------------------------------------------------------------

    -- Display in a table:
    if tpl_args.format == 'tablerow' then
        tpl_args.skip_query = true
        tpl_args.page = passive['passive_skills.main_page']
        tpl_args.name = passive['passive_skills.name'] or tpl_args.page
        tpl_args.icon = passive['passive_skills.icon']
        tpl_args.format = nil
        return string.format(
            '| %s\n| %s',
            p.passive_skill_link(tpl_args),
            passive['passive_skills.stat_text']
        )

    -- Normal inline link:
    elseif tpl_args.format == nil then
        local container = mw.html.create('span')
        container:addClass('c-item-hoverbox')

        if tpl_args.large then
            container:addClass('c-item-hoverbox--large')
        end

        local activator = mw.html.create('span')
        activator:addClass('c-item-hoverbox__activator')

        if img and not tpl_args.large then
            activator:wikitext(img)
        end

        activator:wikitext(string.format(
            '[[%s|%s]]',
            passive['passive_skills.main_page'],
            passive['passive_skills.name'] or passive['passive_skills.main_page']
            )
        )

        local display = mw.html.create('span')
        display:attr('class', 'c-item-hoverbox__display')

        if passive['passive_skills.html'] ~= nil then
            display:wikitext(passive['passive_skills.html'])

            if img then
                display:wikitext(img)
            end
        end

        if img and tpl_args.large then
            activator:wikitext(img)
        end

        container
            :node(activator)
            :node(display)
            :done()

        return tostring(container)
    else
        return m_util.html.error{
            msg=string.format(i18n.error.invalid_format, tpl_args.format .. m_util.misc.add_category(i18n.errors.cats))
        }
    end
end

-- ----------------------------------------------------------------------------
-- End
-- ----------------------------------------------------------------------------

return p