Module:Experience/table

From Brighter Shores Wiki
Jump to navigation Jump to search
Module documentation
This documentation is transcluded from Module:Experience/table/doc. [edit] [history] [purge]
Module:Experience/table's function main is invoked by Template:Experience table.
Module:Experience/table requires Module:Mw.html extension.
Module:Experience/table requires Module:Yesno.
Module:Experience/table requires strict.
Module:Experience/table loads data from Module:Experience/data.

Implements {{Experience table}}. See there for documentation.


require('strict')
require('Module:Mw.html extension')
local data = mw.loadData('Module:Experience/data')
local yesno = require('Module:Yesno')
local lang = mw.language.getContentLanguage()

local p = {}

function p.main(frame)
	local args = frame:getParent().args
	local default_collapsed = yesno(args.collapsed)
	local collapsed = {}

	for _, label in ipairs(data.labels) do
		local this_collapsed = yesno(args['collapsed-' .. label.name:lower()])
		if this_collapsed == nil then
			this_collapsed = default_collapsed
		end
		collapsed[label.name] = this_collapsed
	end

	return p._main{ collapsed = collapsed }
end

--[[
Creates tables of expreience for {{Experience table}}

args.collapsed = { Novice = boolean, Apprentice = boolean, ... }: Whether the
corresponding table should automatically be collapsed
]]
function p._main(args)
	args = args or {}
	local collapsed = args.collapsed or {}

	local result = {}

	for _, label in ipairs(data.labels) do
		local caption = label.min == label.max and '%s (%s)' or '%s (%s-%s; %s XP)'
		local xpReq = data.level[label.max + 1] and data.level[label.max + 1] - data.level[label.min]
		
		local t = mw.html.create('table')
			:addClass('wikitable mw-collapsible align-right-1 align-right-2 align-right-3')
			:addClassIf(label.knowledge, 'align-right-4')
			:addClassIf(collapsed[label.name], 'mw-collapsed')
			:tag('caption')
				:wikitext(caption:format(label.name, label.min, label.max, lang:formatNum(xpReq or 0)))
			:done()
			:tag('tr')
				:tag('th')
					:wikitext('Level')
				:done()
				:tag('th')
					:wikitext('Experience')
				:done()
				:tag('th')
					:wikitext('XP to next level')
				:done()
				:IF(label.knowledge)
					:tag('th')
						:wikitext('Knowledge XP')
					:done()
				:END()
			:done()

		for level = label.min, label.max do
			local nextLevel = data.level[level + 1] and data.level[level + 1] - data.level[level]
			t:tag('tr')
				:tag('td')
					:wikitext(level)
				:done()
				:tag('td')
					:wikitext(lang:formatNum(data.level[level] or 0))
				:done()
				:IF(nextLevel)
					:tag('td')
						:wikitext(lang:formatNum(nextLevel or 0))
					:done()
				:ELSE() -- Max level
					:na()
				:END()
				:IF(label.knowledge)
					:tag('td')
						:wikitext(lang:formatNum(data.knowledge[level] or 0))
					:done()
				:END()
			:done()
		end

		table.insert(result, tostring(t))
	end

	return table.concat(result, '')
end

return p