Module:Sandbox/AB-me/Racing table

This is an old revision of this page, as edited by AB-me (talk | contribs) at 14:02, 5 July 2020. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
local p = {}
local getArgs = require('Module:Arguments').getArgs

function p.main(frame)
	local args = getArgs(frame)
	return p._main(args)
end

function p._main(args)
	local eventsArg = mw.text.split(args['events'], ',')
	local participantsArg = mw.text.split(args['participants'], ',')
	local pointPositionsArg = mw.text.split(args['points'], ',') or {}
	local specialArg = mw.text.split(args['special'], ',') or {}
	
	mw.logObject(points)
	
	local events = {}
	local participants = {}
	local pointPositions = {}
	local special = {}
	local results = {}
	local resultsSpecials = {}
	local points = {}
	
	for i,v in ipairs(eventsArg) do events[i] = {short = v, name = args['event_' .. v]} end
	for i,v in ipairs(participantsArg) do participants[i] = {short = v, name = args['participant_' .. v]} end
	for i,v in ipairs(pointPositionsArg) do pointPositions[i] = tonumber(v) or 0 end
	for i,v in ipairs(specialArg) do
		local adj = true
		local num = 0
		local arg = args['specialPoints_' .. v]
		
		if (arg ~= null) then
			local startsWith = mw.ustring.sub(arg, 1, 1)
			if (startsWith ~= '+' and startsWith ~= '-') then
				adj = false
			end
			
			num = tonumber(arg)
		end
		
		special[i] = {short = v, adj = adj, num = tonumber(args['specialPoints_' .. v] or 0)}
	end
	
	-- Point handling and calculations
	for iEvents,vEvents in ipairs(events) do
		if (args['result_' .. vEvents['short']] ~= nil) then
			local eventResult = mw.text.split(args['result_' .. vEvents['short']], ',') or nil
			
			results[iEvents] = {}
			
			for iEventResult,vEventResult in ipairs(eventResult) do
				--The position is put as the value, so that we can retrieve it from the participant in the table
				results[iEvents][vEventResult] = iEventResult
				
				--Add to points total
				points[vEventResult] = (points[vEventResult] or 0) + (pointPositions[iEventResult] or 0)
			end
			
			mw.logObject(points)
			
			-- Handling of special results
			for iSpecial,vSpecial in ipairs(special) do
				if (args['special_' .. vEvents['short'] .. '_' .. vSpecial['short']] ~= nil) then
					local eventSpecial = mw.text.split(args['special_' .. vEvents['short'] .. '_' .. vSpecial['short']], ',')
					
					for iEventSpecial,vEventSpecial in ipairs(eventSpecial) do
						if (results[iEvents][vEventSpecial] == nil) then
							results[iEvents][vEventSpecial] = vSpecial['short']
						else
							results[iEvents][vEventSpecial] = results[iEvents][vEventSpecial] .. '<span style="margin:0 0.1em 0 0.1em;font-size:110%;font-weight:bold;"><sup>' .. vSpecial['short'] .. '</sup></span>'
						end
						
						--if (vSpecial ~= nil and vSpecial['adj'] ~= nil and vSpecial['adj'] == true and vSpecial['num'] ~= nil) then
							--points[vEventResult] = 999
							--points[vEventResult] = (points[vEventResult] or 0) + (vSpecial['num'] or 0)
						--end
					end
					
				end
			end
			
		end
	end
	
	local stack = {}
	
	table.insert(stack, '{| class="wikitable" style="font-size: 85%; vertical-align:top; text-align:center""\n')
	table.insert(stack, '!style="vertical-align:middle;"|<abbr title="Position">Pos</abbr>\n')
	table.insert(stack, '!style="vertical-align:middle;"|'.. (args['text_participant'] or 'Participant') .. '\n')
	
	for i,v in ipairs(events) do
		table.insert(stack, '!' .. (v['name'] or v['short'] or i) .. '\n')
	end
	
	table.insert(stack, '!style="vertical-align:middle"|Points\n')
	
	for i,v in ipairs(participants) do
		table.insert(stack, '|-\n')
		table.insert(stack, '|' .. i .. '\n')
		table.insert(stack, '|style="text-align:left;"|' .. (v['name'] or v['short']) .. '\n')
		
		for j,w in ipairs(events) do
			table.insert(stack, '|')
			if (results[j] ~= nil and results[j][v['short']] ~= nil) then
				table.insert(stack, results[j][v['short']])
			end
			table.insert(stack, '\n')
		end
		
		table.insert(stack, '|' .. (points[v['short']] or '&ndash;') .. '\n')
	end
	
	table.insert(stack, '|}')
	
	return table.concat(stack)
end

	
return p;