Module:RoundRobin

From TwogPedia
Revision as of 21:57, 4 May 2023 by Couchor (talk | contribs) (Created page with "local getArgs = require('Module:Arguments').getArgs local cargo = mw.ext.cargo local RoundRobin = {} local VariablesLua = mw.ext.VariablesLua function RoundRobin.main(frame) local args = getArgs(frame) local tableContainer = mw.html.create('table'):addClass('rr-table') local header = mw.html.create('tr'):node(mw.html.create('th'):attr('colspan', 4):wikitext(args.title)) tableContainer:node(header) local teams = mw.text.split(args.participants, ',') local te...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

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

local RoundRobin = {}

local VariablesLua = mw.ext.VariablesLua

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

	local tableContainer = mw.html.create('table'):addClass('rr-table')
	
	local header = mw.html.create('tr'):node(mw.html.create('th'):attr('colspan', 4):wikitext(args.title))
	tableContainer:node(header)
	
	local teams = mw.text.split(args.participants, ',')
	local teamScores = {}
	for i = 1, #teams do
		local teamInfo = {}
		local team = mw.text.trim(teams[i])
		local tables = 'DotaMatches'
		local fields = 'p1, p2, p1score, p2score, winner'
		local tournament = args.tournament or mw.title.getCurrentTitle().text
		local cargoArgs = {
			where = '_pageName="' .. tournament .. '" AND (p1="' .. team .. '" OR p2="' .. team .. '")'
		}
		local 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]
				-- mw.log('TYPEEEEEEE')
				-- mw.log(type(result.winner))
				-- mw.logObject(result)
				-- 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)
		-- mw.log('RESULTTTTTTTTTTTTTTTT')
		-- mw.log('MAPWINS: ' .. mapWins)
		-- mw.log('mapLosses: ' .. mapLosses)
		-- mw.log('wins: ' .. wins)
		-- mw.logObject(results)
	end
	-- if tournament, title, promote, relegate ~= nil
	mw.log('TEAMSCORES')
	-- 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 row = mw.html.create('tr')
		local order = mw.html.create('td'):wikitext(i .. '.')
		local team = mw.html.create('td'):wikitext('[[' .. team.name .. ']]')
		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
	
	mw.logObject(teamScores)
	return tableContainer
end

return RoundRobin