Module:Sandbox/MoonOverMira/class attributes/sandbox
Jump to navigation
Jump to search
This is the module sandbox page for Module:Sandbox/MoonOverMira/class attributes (diff).
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.
Please add categories to the /doc subpage. Subpages of this module.
Editors can experiment in this module's sandbox and testcases pages.
Please add categories to the /doc subpage. 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 = {
attr = 'Attribute →',
class = 'Class ↓',
total = 'Total',
},
}
-- ----------------------------------------------------------------------------
-- Helper functions and globals
-- ----------------------------------------------------------------------------
local h = {}
h.col_to_attr = {
str = 'strength',
dex = 'dexterity',
int = 'intelligence',
}
function h.link(page_name)
-- Returns a wikitext internal link for the specified page name.
return string.format('[[%s]]', page_name)
end
-- ----------------------------------------------------------------------------
-- Page functions
-- ----------------------------------------------------------------------------
function p._shared.class_tbl_head(cols, show_total)
--[[
Create the 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
tr:tag('th')
:attr('rowspan', 2)
:wikitext(h.link(h.col_to_attr[col]))
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.character_class_table(frame)
--[[
Displays a table of all character classes and their attributes.
cols is a comma-separated list of attributes to include as columns in the
table.
Examples
--------
= p.character_class_table{cols='str,dex,int', show_total=True}
]]
-- Get args
local tpl_args = getArgs(frame, {
parentFirst = true
})
frame = mw.getCurrentFrame()
local show_table = tpl_args.show_table
-- Validate cols arg
local cols = mw.text.split(tpl_args.cols, ",")
if #cols == 0 then
error(i18n.errors.invalid_cols, tpl_args.cols)
end
for _, col in ipairs(cols) do
if h.col_to_attrs[col] == nil then
error(i18n.errors.invalid_cols, tpl_args.cols)
end
end
-- ------------------------------------------------------------------------
-- Build output table
-- ------------------------------------------------------------------------
local tbl = p._shared.class_tbl_head()
-- 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, cols)
end
return tostring(tbl)
end
-- ----------------------------------------------------------------------------
-- End
-- ----------------------------------------------------------------------------
return p