Module:Sandbox/MoonOverMira/class attributes

From Path of Exile Wiki
Jump to navigation Jump to search
Module documentation[view] [edit] [history] [purge]


See Template:Sandbox/MoonOverMira/class attributes for documentation on this module, since it transparently uses this module's class_attributes_table.

Usage

{{#invoke:Class attributes|class_attributes_table}}


local m_game = require('Module:Game')
local getArgs = require('Module:Arguments').getArgs

-- Return table
local p = {}
p._shared = {}

-- ----------------------------------------------------------------------------
-- Strings
-- ----------------------------------------------------------------------------

local i18n = {
    errors = {
        invalid_cols = 'Invalid columns. Only str, dex, and int are allowed.',
    },
    messages = {},
    char_class = {
        attr = 'Attribute →',
        class = 'Class ↓',
        total = 'Total',
    },
}

-- ----------------------------------------------------------------------------
-- Helper functions and globals
-- ----------------------------------------------------------------------------


local h = {}

h.col_to_attr = {
	str = 'strength',
	dex = 'dexterity',
	int = 'intelligence',
}

function h.pretty_attr(attr)
	-- Returns the pretty name of the attribute from the canonical name.
	-- eg. strength -> Strength
	return m_game.constants.attributes[attr].long_upper
end

function h.link(page_name)
	-- Returns a wikitext internal link for the specified page name.
	return string.format('[[%s]]', page_name)
end

function h.attribute_header(attr)
	-- Returns a wikitext snippet for the attribute that includes a link to
	-- its page and an image of its icon.

	local link = h.link(h.pretty_attr(attr))
	local icon = m_game.constants.attributes[attr].icon
	
	return link .. " " .. icon
end

-- ----------------------------------------------------------------------------
-- Page functions
-- ----------------------------------------------------------------------------
function p._shared.class_tbl_head(cols, show_total)
	--[[
	Creates a table with a header for each specified attribute and the total,
	if desired.
	]]

    local tbl = mw.html.create('table')
    tbl:attr('class', 'wikitable sortable')
    local tr = tbl:tag('tr')
    
    tr:tag('th')
		:wikitext(i18n.char_class.attr)

	for _, col in ipairs(cols) do
		attr = h.col_to_attr[col]
		tr:tag('th')
			:attr('rowspan', 2)
			:wikitext(h.attribute_header(attr))
	end

	if show_total then
		tr:tag('th')
			:attr('rowspan', 2)
			:wikitext(i18n.char_class.total)
	end

    tbl
        :tag('tr')
            :tag('th')
                :wikitext(i18n.char_class.class)
    return tbl
end

function p._shared.class_row(tr, class_name, cols, show_total)
	-- Populates a tr for the specified class with columns for attrs and a total.

	tr:tag('th')
		:wikitext(h.link(class_name))

	local class = m_game.constants.characters[class_name]
	for _, col in ipairs(cols) do
		tr:tag('td')
			:wikitext(class[col])
	end

	if show_total then
		tr:tag('td')
			:wikitext(class.str + class.dex + class.int)
	end
end

function p.class_attributes_table(frame)
    --[[
    Displays a table of character classes and their attributes.
    cols is a comma-separated list of attributes to include as columns in the
    table.

    Examples
    --------
    = p.class_attributes_table{cols='str,dex,int', show_total=True}
    ]]

    -- Get args
    local tpl_args = getArgs(frame, {
        parentFirst = true
    })
	frame = mw.getCurrentFrame()
	local show_total = tpl_args.show_total
    
    -- Validate cols arg
    if #(tpl_args.cols) == 0 then
    	error(i18n.errors.invalid_cols, tpl_args.cols)
    end
    local cols = mw.text.split(tpl_args.cols, ",")
    for _, col in ipairs(cols) do
    	if h.col_to_attr[col] == nil then
			error(i18n.errors.invalid_cols, tpl_args.cols)
		end
    end

    -- ------------------------------------------------------------------------
    -- Build output table
    -- ------------------------------------------------------------------------
    local tbl = p._shared.class_tbl_head(cols, show_total)

	-- Each class gets a row
    for _, class in ipairs(m_game.constants.characters_order) do
		local tr = tbl:tag('tr')
		p._shared.class_row(tr, class, cols, show_total)
    end

    return tostring(tbl)
end

-- ----------------------------------------------------------------------------
-- End
-- ----------------------------------------------------------------------------

return p