Module:Map variants/sandbox: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(2 intermediate revisions by the same user not shown)
Line 156: Line 156:
             'items__name_list',
             'items__name_list',
             'maps',
             'maps',
             'map_series'
             'map_series',
         },
         },
         {
         {
Line 164: Line 164:
             join = 'items._pageID = maps._pageID, maps.series = map_series.name, items._ID = items__name_list._rowID',
             join = 'items._pageID = maps._pageID, maps.series = map_series.name, items._ID = items__name_list._rowID',
             where = string.format(
             where = string.format(
             'items._pageNamespace IN (%s) AND items.class_id = "Map" AND items__name_list._value IN (%s) AND map_series.name = "%s"',
             'items._pageNamespace IN (%s) AND items.class_id = "Map" AND items__name_list._value = "%s" AND map_series.name = "%s"',
                 m_item_util.get_item_namespaces{format = 'list'},
                 m_item_util.get_item_namespaces{format = 'list'},
                 m_cargo.addslashes(name),
                 m_cargo.addslashes(name),

Latest revision as of 23:04, 2 June 2024

This is the module sandbox page for Module:Map variants (diff).

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


Implemented templates


-------------------------------------------------------------------------------
-- 
--                             Module:Map variants
-- 
-- This module implements Template:Map variants.
-------------------------------------------------------------------------------

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('Map variants')

-- Lazy loading
local m_item_util -- require('Module:Item util')
local f_item_table -- require('Module:Item table').item_table
local f_current_map_series -- require('Module:Map series').current_map_series

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

local i18n = cfg.i18n

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

local h = {}

-- Lazy loading for Module:Item table
function h.item_table(args)
    if not f_item_table then
        f_item_table = require('Module:Item table').item_table
    end
    return f_item_table(args)
end

-- Lazy loading for Module:Map series
function h.current_map_series()
    if not f_current_map_series then
        f_current_map_series = require('Module:Map series').current_map_series
    end
    return f_current_map_series()
end

function h.get_map_name(tpl_args)
    if tpl_args.item_name then
        return tpl_args.item_name
    end
    -- Get the name of a map from a page title
    local page = tpl_args.page or mw.title.getCurrentTitle().prefixedText
    local results = m_cargo.query(
        {
            'items',
            '_pageData',
        },
        {
            'items.name=name'
        },
        {
            join = 'items._pageName = _pageData._pageNameOrRedirect',
            where = string.format(
                '_pageData._pageName = "%s" AND items.class_id = "Map"',
                m_cargo.addslashes(page)
            ),
            limit = 1,
        }
    )
    if #results == 0 then
        error(i18n.errors.no_map_on_page)
    end
    return results[1].name
end

function h.list_variant_names(name)
    -- Returns a list of map names to include map variant query
    local name_list = {
        m_cargo.addslashes(name),
    }
    for _, v in ipairs(i18n.named_variants) do
        name_list[#name_list+1] = m_cargo.addslashes( string.format(v, name) )
    end
    return table.concat({
        '"',
        table.concat(name_list, '", "'),
        '"',
    })
end

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

local function _map_variants(tpl_args)
    m_item_util = m_item_util or require('Module:Item util')

    tpl_args.item_name = tpl_args.item_name or tpl_args[1]
    local name = h.get_map_name(tpl_args)
    local html = h.item_table{
        q_tables = 'items, items__name_list, maps, map_series',
        q_join = 'items._pageID = maps._pageID, maps.series = map_series.name, items._ID = items__name_list._rowID',
        q_where = string.format(
            'items._pageNamespace IN (%s) AND items.class_id = "Map" AND items__name_list._value IN (%s)',
            m_item_util.get_item_namespaces{format = 'list'},
            h.list_variant_names(name)
        ),
        q_groupBy = 'items._pageID',
        q_orderBy = 'map_series.ordinal DESC, maps.tier ASC, items.name ASC',
        map_tier = true,
        map_level = true,
        map_series = true,
        large = tpl_args.large,
        responsive = tpl_args.responsive or true,
    }
    return html
end

local function _latest_variant(tpl_args)
    local name = tpl_args.item_name or tpl_args[1] or mw.title.getCurrentTitle().prefixedText
    local results = m_cargo.query(
        {
            'items',
            'maps',
            'map_series'
        },
        {
            'items._pageName=pagename'
        },
        {
            join = 'items._pageID = maps._pageID, maps.series = map_series.name',
            where = string.format(
                'items.name_list HOLDS "%s" AND items.class_id = "Map"',
                m_cargo.addslashes(name)
            ),
            orderBy = 'map_series.ordinal DESC',
            limit = 1,
        }
    )
    if #results == 0 then
        error(i18n.errors.no_map_on_page)
    end
    return results[1].pagename
end

local function _atlas_map(tpl_args)
    m_item_util = m_item_util or require('Module:Item util')

    tpl_args.item_name = tpl_args.item_name or tpl_args[1]
    local name = h.get_map_name(tpl_args)
    local current = h.current_map_series()
    local results = m_cargo.query(
        {
            'items',
            'items__name_list',
            'maps',
            'map_series',
        },
        {
            'items.name=name',
        },
        {
            join = 'items._pageID = maps._pageID, maps.series = map_series.name, items._ID = items__name_list._rowID',
            where = string.format(
            'items._pageNamespace IN (%s) AND items.class_id = "Map" AND items__name_list._value = "%s" AND map_series.name = "%s"',
                m_item_util.get_item_namespaces{format = 'list'},
                m_cargo.addslashes(name),
                current
            ),
            orderBy = 'map_series.ordinal DESC',
            limit = 1,
        }
    )
    local html = mw.html.create()
    local cats = {}
    if #results > 0 then
        html:wikitext(string.format(
            i18n.atlas_map.in_current_series,
            name,
            current
        ))
        cats[#cats+1] = i18n.categories.in_current_series
    else
        html:wikitext(string.format(
            i18n.atlas_map.not_in_current_series,
            name,
            current,
            name
        ))
        cats[#cats+1] = i18n.categories.not_in_current_series
    end
    return tostring(html) .. m_util.misc.add_category(cats)
end

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

local p = {}

-- 
-- Template:Map variants
-- 
p.map_variants = m_util.misc.invoker_factory(_map_variants, {
    wrappers = cfg.wrappers.map_variants,
})

-- 
-- Template:Latest map variant
-- 
p.latest_variant = m_util.misc.invoker_factory(_latest_variant, {
    wrappers = cfg.wrappers.latest_variant,
})

-- 
-- Template:Atlas map
-- 
p.atlas_map = m_util.misc.invoker_factory(_atlas_map, {
    wrappers = cfg.wrappers.atlas_map,
})

return p