Module:Map variants/sandbox

From Path of Exile Wiki
Jump to navigation Jump to search

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

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


Implements {{map variants}}.


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

require('Module:No globals')
local m_util = require('Module:Util')
local m_item_util = require('Module:Item 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 f_item_table -- require('Module:Item table').item_table

-- 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

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)
    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,
    }
    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=name'
        },
        {
            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].name
end

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

local p = {}

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

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

return p