Module:Map variants: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
(Created page with "------------------------------------------------------------------------------- -- -- Module:Map variants -- -- This module implements Template:M...")
 
No edit summary
 
(8 intermediate revisions by 2 users not shown)
Line 8: Line 8:
require('Module:No globals')
require('Module:No globals')
local m_util = require('Module:Util')
local m_util = require('Module:Util')
local m_item_util = require('Module:Item util')
local m_cargo = require('Module:Cargo')
local m_cargo = require('Module:Cargo')


Line 15: Line 14:


-- Lazy loading
-- Lazy loading
local m_item_util -- require('Module:Item util')
local f_item_table -- require('Module:Item table').item_table
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
-- The cfg table contains all localisable strings and configuration, to make it
Line 35: Line 36:
     end
     end
     return f_item_table(args)
     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
end


function h.get_map_name(tpl_args)
function h.get_map_name(tpl_args)
     if tpl_args.name then
     if tpl_args.item_name then
         return tpl_args.name
         return tpl_args.item_name
     end
     end
     -- Get the name of a map from a page title
     -- Get the name of a map from a page title
     local page = page or mw.title.getCurrentTitle().prefixedText
     local page = tpl_args.page or mw.title.getCurrentTitle().prefixedText
     local results = m_cargo.query(
     local results = m_cargo.query(
         {
         {
Line 63: Line 72:
         error(i18n.errors.no_map_on_page)
         error(i18n.errors.no_map_on_page)
     end
     end
     return results[1]
     return results[1].name
end
end


Line 69: Line 78:
     -- Returns a list of map names to include map variant query
     -- Returns a list of map names to include map variant query
     local name_list = {
     local name_list = {
         m_cargo.addslashes(name)
         m_cargo.addslashes(name),
     }
     }
     for _, v in ipairs(i18n.named_variants) do
     for _, v in ipairs(i18n.named_variants) do
Line 86: Line 95:


local function _map_variants(tpl_args)
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 name = h.get_map_name(tpl_args)
     local html = h.item_table{
     local html = h.item_table{
         q_tables = {
         q_tables = 'items, items__name_list, maps, map_series',
            '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_join = 'items._pageID = maps._pageID, maps.series = map_series.name, items._ID = items__name_list._rowID',
         q_where = string.format(
         q_where = string.format(
Line 105: Line 112:
         map_level = true,
         map_level = true,
         map_series = true,
         map_series = true,
         large = true,
         large = tpl_args.large,
         responsive = true,
         responsive = tpl_args.responsive or true,
     }
     }
     return html
     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
end


Line 122: Line 205:
p.map_variants = m_util.misc.invoker_factory(_map_variants, {
p.map_variants = m_util.misc.invoker_factory(_map_variants, {
     wrappers = cfg.wrappers.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
return p

Latest revision as of 23:05, 2 June 2024

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