Module:Passive skill link: Difference between revisions
Jump to navigation
Jump to search
(Fix for Cargo bug) |
Mefisto1029 (talk | contribs) (Moved cfg to seperate page, fixed query by page always throwing error) |
||
| Line 1: | Line 1: | ||
------------------------------------------------------------------------------- | |||
-- | |||
-- Module:Passive skill link | |||
-- | |||
-- This module implements [[Template:Passive skill link]] | |||
-- | -- | ||
-- | ------------------------------------------------------------------------------- | ||
-- | |||
require('strict') | |||
local m_util = require('Module:Util') | 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 | 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 | |||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
| Line 55: | Line 32: | ||
'keystone', | 'keystone', | ||
'ascendancy_basic', | 'ascendancy_basic', | ||
'ascendancy_notable' | 'ascendancy_notable', | ||
'atlas_basic', | |||
'atlas_notable', | |||
'atlas_keystone', | |||
} | } | ||
function h.get_type(passive) | function h.get_type(passive) | ||
| Line 72: | Line 52: | ||
if passive['passive_skills.ascendancy_class'] ~= nil then | if passive['passive_skills.ascendancy_class'] ~= nil then | ||
key = 'ascendancy_' .. key | key = 'ascendancy_' .. key | ||
elseif tonumber(passive['passive_skills.is_atlas_passive']) == 1 then | |||
key = 'atlas_' .. key | |||
end | end | ||
| Line 126: | Line 108: | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
-- | -- Main functions | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
local | local function _passive_skill_link(tpl_args) | ||
function | |||
--[[ | --[[ | ||
Links a passive skill. | Links a passive skill. | ||
| Line 152: | Line 132: | ||
]] | ]] | ||
-- Hande first and second arguments: | -- Hande first and second arguments: | ||
| Line 164: | Line 138: | ||
-- Check if the correct parameters have been set: | -- Check if the correct parameters have been set: | ||
if m_util.table.has_all_value(tpl_args, | 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. | return m_util.html.error{msg=i18n.errors.invalid_args .. m_util.misc.add_category(i18n.errors.category)} | ||
end | end | ||
-- | -- | ||
local passive = {} | local passive = {} | ||
if m_util.table.has_one_value(tpl_args, | 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: | -- Create q_where depending on the input: | ||
if tpl_args.passive_name then | if tpl_args.passive_name then | ||
| Line 181: | Line 155: | ||
'passive_skills.id="%s"', | 'passive_skills.id="%s"', | ||
tpl_args.id | 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 | elseif tpl_args.q_where then | ||
| Line 186: | Line 165: | ||
else | else | ||
return m_util.html.error{ | return m_util.html.error{ | ||
msg=i18n.errors.invalid_args .. m_util.misc.add_category(i18n.errors. | msg=i18n.errors.invalid_args .. m_util.misc.add_category(i18n.errors.category) | ||
} | } | ||
end | end | ||
| Line 223: | Line 202: | ||
msg=string.format( | msg=string.format( | ||
i18n.errors.too_many_passives_found, | i18n.errors.too_many_passives_found, | ||
q_where .. m_util.misc.add_category(i18n.errors. | q_where .. m_util.misc.add_category(i18n.errors.category) | ||
) | ) | ||
} | } | ||
| Line 231: | Line 210: | ||
msg=string.format( | msg=string.format( | ||
i18n.errors.no_passives_found, | i18n.errors.no_passives_found, | ||
q_where .. m_util.misc.add_category(i18n.errors. | q_where .. m_util.misc.add_category(i18n.errors.category) | ||
) | ) | ||
} | } | ||
| Line 246: | Line 225: | ||
-- Add allowed override parameters: | -- Add allowed override parameters: | ||
for k, prop in pairs( | for k, prop in pairs(cfg.parameters) do | ||
if tpl_args[k] ~= nil then | if tpl_args[k] ~= nil then | ||
passive[prop] = tpl_args[k] | passive[prop] = tpl_args[k] | ||
| Line 318: | Line 297: | ||
else | else | ||
return m_util.html.error{ | return m_util.html.error{ | ||
msg=string.format(i18n.error.invalid_format, tpl_args.format .. m_util.misc.add_category(i18n.errors. | msg=string.format(i18n.error.invalid_format, tpl_args.format .. m_util.misc.add_category(i18n.errors.category)) | ||
} | } | ||
end | end | ||
| Line 324: | Line 303: | ||
-- ---------------------------------------------------------------------------- | -- ---------------------------------------------------------------------------- | ||
-- | -- 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', | |||
} | |||
end | |||
function p.debug.test() | |||
return mw.log(p.passive_skill_link(p.debug.preset())) | |||
end | |||
return p | return p | ||
Revision as of 10:58, 22 December 2025
The above documentation is transcluded from Module:Passive skill link/doc.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Subpages of this module.
-------------------------------------------------------------------------------
--
-- 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',
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('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',
}
end
function p.debug.test()
return mw.log(p.passive_skill_link(p.debug.preset()))
end
return p