Module:Sandbox/MoonOverMira/class attributes: Difference between revisions
Jump to navigation
Jump to search
MoonOverMira (talk | contribs) No edit summary Tag: Manual revert |
MoonOverMira (talk | contribs) No edit summary |
||
Line 16: | Line 16: | ||
messages = {}, | messages = {}, | ||
char_class = { | char_class = { | ||
class = 'Class ↓', | class = 'Class ↓', | ||
total = 'Total', | total = 'Total', | ||
Line 61: | Line 60: | ||
function p._shared.class_tbl_head(cols, show_total) | function p._shared.class_tbl_head(cols, show_total) | ||
--[[ | --[[ | ||
Creates a table with a header for each specified attribute and the | Creates a table with a header for class, each specified attribute, and the | ||
total. | |||
]] | ]] | ||
Line 70: | Line 69: | ||
tr:tag('th') | tr:tag('th') | ||
:wikitext(i18n.char_class. | :wikitext(i18n.char_class.class) | ||
for _, col in ipairs(cols) do | for _, col in ipairs(cols) do | ||
attr = h.col_to_attr[col] | attr = h.col_to_attr[col] | ||
tr:tag('th') | tr:tag('th') | ||
:wikitext(h.attribute_header(attr)) | :wikitext(h.attribute_header(attr)) | ||
end | end | ||
Line 85: | Line 83: | ||
end | end | ||
return tbl | return tbl | ||
end | end |
Revision as of 03:47, 4 March 2023
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}}
The above documentation is transcluded from Module:Sandbox/MoonOverMira/class attributes/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.
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 = {
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 class, each specified attribute, and the
total.
]]
local tbl = mw.html.create('table')
tbl:attr('class', 'wikitable sortable')
local tr = tbl:tag('tr')
tr:tag('th')
:wikitext(i18n.char_class.class)
for _, col in ipairs(cols) do
attr = h.col_to_attr[col]
tr:tag('th')
:wikitext(h.attribute_header(attr))
end
if show_total then
tr:tag('th')
:attr('rowspan', 2)
:wikitext(i18n.char_class.total)
end
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