Module:Cheerful carnival

From Sekaipedia

Documentation for this module may be created at Module:Cheerful carnival/doc

local getArgs = require('Module:Arguments').getArgs
local navbar  = require('Module:Navbar')._navbar
local VariablesLua = mw.ext.VariablesLua
local lang    = mw.getContentLanguage()

local p = {}

local function formatImage(image)
	if image == nil or image == '' then return end
	return string.format('[[File:%s|100px]]', image)
end

local function getThemeAndGroupsSection(theme, groupNames, groupImages)
	local tagTable = mw.html.create('table')
		:addClass('wikitable')
		:tag('tr')
			:tag('th')
				:wikitext('Theme')
				:done()
			:tag('td')
				:attr('colspan', 2)
				:wikitext(theme)
				:done()
			:done()
		
	local function getCell(groupName, groupImage)
		groupImage = formatImage(groupImage)
		
		local text = groupName
		if groupImage then
			text = string.format('%s<br>%s', groupImage, groupName)
		end
		
		local cell = mw.html.create('td')
				:wikitext(text)
				:done()
			
		return cell
	end
	
	local row = tagTable:tag('tr')
			:tag('th')
				:wikitext('Groups')
				:done()
	for i,_ in ipairs(groupNames) do
		local groupName = groupNames[i]
		local groupImage = groupImages[groupName]
		local cell = getCell(groupName, groupImage)
		row:node(cell)
	end
		
	return tagTable
end

local function getTermSection(termData, groupImages)
	local root = mw.html.create()
	local header = mw.html.create('h3')
		:wikitext('Midterms & final winners')
		:done()
	local winnersTable = mw.html.create('table')
		:addClass('wikitable')
		:tag('tr')
			:tag('th')
				:wikitext('Section')
				:done()
			:tag('th')
				:wikitext('Result announcement time')
				:done()
			:tag('th')
				:wikitext('Winning group')
				:done()
			:done()
		
	local function getRow(termName, termTime, groupImage)
		local row = mw.html.create('tr')
			:tag('th')
				:wikitext(termName)
				:done()
			:tag('td')
				:wikitext(termTime or 'TBA')
				:done()
			:tag('td')
				:wikitext(formatImage(groupImage) or 'TBA')
				:done()
			:done()
		
		return row
	end
	
	for _, data in ipairs(termData) do
		local row = getRow(
			data.name,
			data.announceTime,
			groupImages[data.winner]
		)
		winnersTable:node(row)
	end
	
	root:node(header):node(winnersTable)
	return root
end

local function getPointsSection(groupNames, pointsData)
	local root = mw.html.create()
	local header = mw.html.create('h3')
		:wikitext('Final points')
		:done()
	local pointsTable = mw.html.create('table')
		:addClass('wikitable')
		:tag('tr')
			:tag('th')
				:wikitext('Group')
				:done()
			:tag('th')
				:wikitext('Points')
				:done()
			:done()
			
	local function formatPoints(points)
		points = tonumber(points)
		if points == nil or points < 0 then return end
		
		return lang:formatNum(points) .. ' pts'
	end
	local function getRow(groupName, points)
		local row = mw.html.create('tr')
			:tag('th')
				:wikitext(groupName)
				:done()
			:tag('td')
				:wikitext(formatPoints(points) or 'TBA')
				:done()
			:done()
		
		return row
	end
	
	for i,_ in ipairs(groupNames) do
		local groupName = groupNames[i]
		local row = getRow(groupName, pointsData[groupName])
		pointsTable:node(row)
	end
	
	root:node(header):node(pointsTable)
	return root
end

function p.main(frame)
	local args = getArgs(frame)
	
	local theme = args['theme'] or 'TBA'
	local groupNames = {
		args['group 1'] or 'Group 1',
		args['group 2'] or 'Group 2'
	}
	local groupImages = {
		[groupNames[1]] = args['group 1 image'],
		[groupNames[2]] = args['group 2 image']
	}
	local groupPoints = {
		[groupNames[1]] = args['group 1 points'] or -1,
		[groupNames[2]] = args['group 2 points'] or -1
	}
	
	local terms = {
		{
			name = 'First midterm',
			announceTime = args['midterm 1 time'],
			winner = args['midterm 1 winner']  or 'TBA',
		},
		{
			name   = 'Second miterm',
			announceTime = args['midterm 2 time'],
			winner =  args['midterm 2 winner'] or 'TBA',
		},
		{
			name   = 'Final',
			announceTime = args['final time'] or VariablesLua.var('end date'),
			winner = args['final winner'] or 'TBA',
		},
	}
	
	local root = mw.html.create()
	root:node(getThemeAndGroupsSection(theme, groupNames, groupImages))
		:node(getTermSection(terms, groupImages))
		:node(getPointsSection(groupNames, groupPoints))
	
	return tostring(root)
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.