Module:Experience/table: Difference between revisions

From Brighter Shores Wiki
Jump to navigation Jump to search
Content added Content deleted
mNo edit summary
(Show xp per label in captions)
 
(2 intermediate revisions by the same user not shown)
Line 40: Line 40:
:addClass(collapsed[label.name] and 'mw-collapsed' or nil)
:addClass(collapsed[label.name] and 'mw-collapsed' or nil)
:tag('caption')
:tag('caption')
:wikitext(('%s (%s-%s)'):format(label.name, label.min, label.max))
:wikitext((label.min == label.max and '%s (%s)' or '%s (%s-%s; %s XP)'):format(label.name, label.min, label.max, lang:formatNum((data.level[label.max + 1] or 0) - data.level[label.min])))
:done()
:done()
:tag('tr')
:tag('tr')
Line 77: Line 77:
:done()
:done()
:tag('td')
:tag('td')
:wikitext(data.level[level])
:wikitext(lang:formatNum(data.level[level]))
:done()
:done()
:node(next_level_node)
:node(next_level_node)

Latest revision as of 09:01, 27 November 2024

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 t = mw.html.create('table')
			:addClass('wikitable mw-collapsible')
			:addClass(collapsed[label.name] and 'mw-collapsed' or nil)
			:tag('caption')
				:wikitext((label.min == label.max and '%s (%s)' or '%s (%s-%s; %s XP)'):format(label.name, label.min, label.max, lang:formatNum((data.level[label.max + 1] or 0) - data.level[label.min])))
			: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 next_level_node
			if data.level[level+1] then
				next_level_node = mw.html.create('td')
					:wikitext(lang:formatNum(data.level[level+1] - data.level[level]))
				:done()
			else
				-- Max level
				next_level_node = mw.html.create('td')
					:addClass('table-na')
					:wikitext('N/A')
				:done()
			end
			t:tag('tr')
				:tag('td')
					:wikitext(level)
				:done()
				:tag('td')
					:wikitext(lang:formatNum(data.level[level]))
				:done()
				:node(next_level_node)
				: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