Module:RoundRobin

From TwogPedia
Revision as of 10:04, 5 October 2023 by Couchor (talk | contribs)

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

local getArgs = require('Module:Arguments').getArgs
local cargo = mw.ext.cargo
local getTeamPage = require('Module:Functions').getTeamPage

local RoundRobin = {}

local VariablesLua = mw.ext.VariablesLua

function RoundRobin.main(frame)
	local args = getArgs(frame)

	local tableContainer = mw.html.create('table'):addClass('rr-table')
	
	if args.title then
		local header = mw.html.create('tr'):node(mw.html.create('th'):attr('colspan', 4):wikitext(args.title))
		tableContainer:node(header)
	end
	
	local results = {}
	
	-- If no participants, then create empty table
	if not args.participants and args.emptyRows then
		for i = 1, args.emptyRows do
			local colorClass = ''
			if args.promote and i <= tonumber(args.promote)  then colorClass = 'promote'
			elseif args.relegate and i > args.emptyRows - tonumber(args.relegate) then colorClass = 'relegate'
			else colorClass = 'stay' end
			local row = mw.html.create('tr')
			local order = mw.html.create('td'):addClass(colorClass):wikitext(i .. '.')
			local team = mw.html.create('td'):attr('style', 'min-width: 100px'):wikitext('TBD')
			local winsLosses = mw.html.create('td'):attr('style', 'min-width: 40px'):addClass('bold'):wikitext('0-0')
			local mapScores = mw.html.create('td'):attr('style', 'min-width: 40px'):wikitext('0-0')
			tableContainer:node(row:node(order):node(team):node(winsLosses):node(mapScores))
		end
		return tableContainer
	end
	
	local multipleRounds = 1
	if args.multiple then multipleRounds = tonumber(args.multiple) end
	
	local teams = mw.text.split(args.participants, ',')
	local teamScores = {}
	for i = 1, #teams do
		local teamInfo = {}
		local team = getTeamPage(mw.text.trim(teams[i]))
		local tables = 'AllMatches'
		local fields = 'p1, p2, p1score, p2score, winner'
		local page = args.page or mw.title.getCurrentTitle().text
		local whereStr = '_pageName="' .. page .. '" AND (p1="' .. team .. '" OR p2="' .. team .. '")'
		if args.stage then whereStr = whereStr .. ' AND stage="' .. args.stage .. '"' end
		local cargoArgs = {
			where = whereStr
		}
		
		results = cargo.query(tables, fields, cargoArgs)
		local mapWins = 0
		local mapLosses = 0
		local wins = 0
		local losses = 0
		if #results > 0 then
			for j = 1, #results do
				local result = results[j]
				-- Check if the team in question is the first or second team in the db
				if result.p1 == team then
					if result.winner == '1' then wins = wins + 1 else losses = losses + 1 end
					mapWins = mapWins + result.p1score
					mapLosses = mapLosses + result.p2score
				else 
					if result.winner == '2' then wins = wins + 1 else losses = losses + 1 end
					mapWins = mapWins + result.p2score
					mapLosses = mapLosses + result.p1score
				end
				-- determine if the team in question won
			end
		end
		
		teamInfo.name = team
		teamInfo.wins = wins
		teamInfo.mapWins = mapWins
		teamInfo.mapLosses = mapLosses
		teamInfo.losses = losses
		
		table.insert(teamScores, teamInfo)
	end

	-- Sort table by wins, then by mapwins and then by lower amount of maplosses
	table.sort(teamScores, function(a,b) 
		if a.wins == b.wins then
			if a.losses == b.losses then
				if a.mapWins == b.mapWins then
					if a.mapLosses < b.mapLosses then return true else return false end
				else 
					if a.mapWins > b.mapWins then return true else return false end
				end
			else 
				if a.losses < b.losses then return true else return false end
			end
		else
			if a.wins > b.wins then return true else return false end
		end
	end)
	
	for i = 1, #teamScores do
		local team = teamScores[i]
		local wins = team.wins or 0
		local losses = team.losses or 0
		local mapWins = team.mapWins or 0
		local mapLosses = team.mapLosses or 0
		local colorClass = ''
		
		if args.promote and i <= tonumber(args.promote)  then colorClass = 'promote'
		elseif args.relegate and i > #teamScores - tonumber(args.relegate) then colorClass = 'relegate'
		else colorClass = 'stay'end
		
		local row = mw.html.create('tr')
		
		-- If all matches in the group have been played
		if #results >= ((#teams * (#teams - 1)) / 2) * multipleRounds then 
			row:addClass(colorClass)
		end
		local order = mw.html.create('td'):wikitext(i .. '.'):addClass(colorClass)
		
		local team = mw.html.create('td'):wikitext('[[' .. team.name .. '|' .. mw.text.split(team.name, '/')[2] .. ']]')
		local winsLosses = mw.html.create('td'):wikitext(wins .. '-' .. losses)
		local mapScores = mw.html.create('td'):wikitext(mapWins .. '-' .. mapLosses)
		tableContainer:node(row:node(order):node(team):node(winsLosses):node(mapScores))
	end
	
	return tableContainer
end

return RoundRobin