Module:Passive skill link: Difference between revisions

From Path of Exile 2 Wiki
Jump to navigation Jump to search
(Moved cfg to seperate page, fixed query by page always throwing error)
(fix tablerow)
 
Line 247: Line 247:
         return string.format(
         return string.format(
             '| %s\n| %s',
             '| %s\n| %s',
             p.passive_skill_link(tpl_args),
             _passive_skill_link(tpl_args),
             passive['passive_skills.stat_text']
             passive['passive_skills.stat_text']
         )
         )
Line 327: Line 327:
         --q_where = 'passive_skills.name = "Splinter Stack Size" AND passive_skills.atlas_sub_tree="Delirium"',
         --q_where = 'passive_skills.name = "Splinter Stack Size" AND passive_skills.atlas_sub_tree="Delirium"',
         page = 'Passive Skill:Passive~keystone~bulwark',
         page = 'Passive Skill:Passive~keystone~bulwark',
        format = 'tablerow',
     }
     }
end
end

Latest revision as of 11:00, 22 December 2025

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


Lua logo

This module depends on the following other modules:

Implements {{passive skill link}}.

-------------------------------------------------------------------------------
-- 
--                          Module:Passive skill link
-- 
-- This module implements [[Template:Passive skill link]]
--
-------------------------------------------------------------------------------

require('strict')
local m_util = require('Module:Util')

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

local m_cargo = use_sandbox and require('Module:Cargo/sandbox') or require('Module:Cargo')

-- 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:Passive skill link/config/sandbox') or mw.loadData('Module:Passive skill link/config')

local i18n = cfg.i18n

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

local h = {}

h.type_order = {
    'basic',
    'notable',
    'keystone',
    'ascendancy_basic',
    'ascendancy_notable',
    'atlas_basic',
    'atlas_notable',
    'atlas_keystone',
}
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
    elseif tonumber(passive['passive_skills.is_atlas_passive']) == 1 then
        key = 'atlas_' .. 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

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

local function _passive_skill_link(tpl_args)
    --[[
    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'}

    ]]

    -- 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, cfg.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.category)}
    end

    --
    local passive = {}
    if m_util.table.has_one_value(tpl_args, cfg.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.page then
            tpl_args.q_where = string.format(
                'passive_skills._pageName="%s"',
                tpl_args.page
            )
        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.category)
            }
        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
                ),
                groupBy='passive_skills._pageID',
                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.category)
                    )
                }
        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.category)
                )
            }
        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(cfg.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',
            _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('hoverbox c-passive-hoverbox')

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

        local activator = mw.html.create('span')
        activator:addClass('hoverbox__activator c-passive-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:addClass('hoverbox__display c-passive-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.category))
        }
    end
end

-- ----------------------------------------------------------------------------
-- Exported functions
-- ----------------------------------------------------------------------------

local p = {}

-- 
-- [[Template:Passive skill link]]
-- 
p.passive_skill_link = m_util.misc.invoker_factory(_passive_skill_link, {
    wrappers = cfg.wrappers.passive_skill_link,
})

-- ----------------------------------------------------------------------------
-- Debug
-- ----------------------------------------------------------------------------

p.debug = {}

function p.debug.preset()
    return {
        --id = 'passive_keystone_bulwark',
        --passive_name = 'Splinter Stack Size',
        --q_where = 'passive_skills.name = "Splinter Stack Size" AND passive_skills.atlas_sub_tree="Delirium"',
        page = 'Passive Skill:Passive~keystone~bulwark',
        format = 'tablerow',
    }
end
function p.debug.test()
    return mw.log(p.passive_skill_link(p.debug.preset()))
end

return p