Module:Sandbox/MoonOverMira/class attributes and Module:Sandbox/MoonOverMira/class attributes/sandbox: Difference between pages

(Difference between pages)
Jump to navigation Jump to search
Page 1
Page 2
m (Vinifera7 moved page Module:Class attributes to Module:Sandbox/MoonOverMira/class attributes without leaving a redirect)
 
m (Vinifera7 moved page Module:Class attributes/sandbox to Module:Sandbox/MoonOverMira/class attributes/sandbox without leaving a redirect)
 
Line 13: Line 13:
     errors = {
     errors = {
         invalid_cols = 'Invalid columns. Only str, dex, and int are allowed.',
         invalid_cols = 'Invalid columns. Only str, dex, and int are allowed.',
        invalid_sort_col = 'Invalid sort_col. One of str, dex, or int is allowed.',
     },
     },
     messages = {},
     messages = {},
     char_class = {
     char_class = {
         class = 'Class',
        attr = 'Attribute →',
         class = 'Class ',
         total = 'Total',
         total = 'Total',
     },
     },
Line 34: Line 34:
int = 'intelligence',
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)
function h.link(page_name)
-- Returns a wikitext internal link for the specified page name.
-- Returns a wikitext internal link for the specified page name.
return string.format('[[%s]]', 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
end


Line 61: Line 45:
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 class, each specified attribute, and the
Creates a table with a header for each specified attribute and the total,
total.
if desired.
]]
]]


Line 70: Line 54:
      
      
     tr:tag('th')
     tr:tag('th')
:wikitext(i18n.char_class.class)
:wikitext(i18n.char_class.attr)


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


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


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


function p.class_attributes_table(frame)
function p.character_class_table(frame)
     --[[
     --[[
     Displays a table of character classes and their attributes.
     Displays a table of all character classes and their attributes.
     cols is a comma-separated list of attributes to include as columns in the
     cols is a comma-separated list of attributes to include as columns in the
     table.
     table.
Line 112: Line 101:
     Examples
     Examples
     --------
     --------
     = p.class_attributes_table{cols='str,dex,int', show_total=true, sort_col='str', reverse_sort=true}
     = p.character_class_table{cols='str,dex,int', show_total=True}
     ]]
     ]]


Line 120: Line 109:
     })
     })
frame = mw.getCurrentFrame()
frame = mw.getCurrentFrame()
local show_total = tpl_args.show_total
local show_table = tpl_args.show_table
local sort_col = tpl_args.sort_col
local reverse_sort = tpl_args.reverse_sort
      
      
     -- Validate cols arg
     -- Validate cols arg
     if #(tpl_args.cols) == 0 then
     local cols = mw.text.split(tpl_args.cols, ",")
    if #cols == 0 then
     error(i18n.errors.invalid_cols, tpl_args.cols)
     error(i18n.errors.invalid_cols, tpl_args.cols)
     end
     end
    local cols = mw.text.split(tpl_args.cols, ",")
     for _, col in ipairs(cols) do
     for _, col in ipairs(cols) do
     if h.col_to_attr[col] == nil then
     if h.col_to_attrs[col] == nil then
error(i18n.errors.invalid_cols, tpl_args.cols)
error(i18n.errors.invalid_cols, tpl_args.cols)
end
end
    end
   
    -- Validate sort_col
    if sort_col and h.col_to_attr[sort_col] == nil then
error(i18n.errors.invalid_sort_col, sort_col)
     end
     end


Line 143: Line 125:
     -- Build output table
     -- Build output table
     -- ------------------------------------------------------------------------
     -- ------------------------------------------------------------------------
     local tbl = p._shared.class_tbl_head(cols, show_total)
     local tbl = p._shared.class_tbl_head()
 
-- The default order is the game order.
local characters = m_game.constants.characters_order
if sort_col and sort_col ~= "" then
-- The sort will first compare by the sort_col, then by class name.
function cmp(a, b)
a_attr = m_game.constants.characters[a][sort_col]
b_attr = m_game.constants.characters[b][sort_col]
if a_attr ~= b_attr then
if reverse_sort then
return a_attr > b_attr
else
return a_attr < b_attr
end
end
return a < b
end
table.sort(characters, cmp)
end


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


Line 178: Line 141:


return p
return p
-- Console testing:
-- mw.logObject(p.class_attributes_table({cols='str,dex,int'}))