(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...") |
mNo edit summary |
||
Line 1: | Line 1: | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
local cargo = mw.ext.cargo | local cargo = mw.ext.cargo | ||
local getTeamPage = require('Module:Functions').getTeamPage | |||
local RoundRobin = {} | local RoundRobin = {} | ||
Line 11: | Line 12: | ||
local tableContainer = mw.html.create('table'):addClass('rr-table') | 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)) | 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 = {} | |||
local multipleRounds = 1 | |||
if args.multiple then multipleRounds = tonumber(args.multiple) end | |||
local teams = mw.text.split(args.participants, ',') | local teams = mw.text.split(args.participants, ',') | ||
Line 18: | Line 26: | ||
for i = 1, #teams do | for i = 1, #teams do | ||
local teamInfo = {} | local teamInfo = {} | ||
local team = mw.text.trim(teams[i]) | local team = getTeamPage(mw.text.trim(teams[i])) | ||
local tables = ' | local tables = 'AllMatches' | ||
local fields = 'p1, p2, p1score, p2score, winner' | local fields = 'p1, p2, p1score, p2score, winner' | ||
local | 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 = { | local cargoArgs = { | ||
where = | where = whereStr | ||
} | } | ||
results = cargo.query(tables, fields, cargoArgs) | |||
local mapWins = 0 | local mapWins = 0 | ||
local mapLosses = 0 | local mapLosses = 0 | ||
Line 33: | Line 44: | ||
for j = 1, #results do | for j = 1, #results do | ||
local result = results[j] | local result = results[j] | ||
-- Check if the team in question is the first or second team in the db | -- Check if the team in question is the first or second team in the db | ||
if result.p1 == team then | if result.p1 == team then | ||
Line 57: | Line 65: | ||
table.insert(teamScores, teamInfo) | table.insert(teamScores, teamInfo) | ||
end | end | ||
-- Sort table by wins, then by mapwins and then by lower amount of maplosses | -- Sort table by wins, then by mapwins and then by lower amount of maplosses | ||
table.sort(teamScores, function(a,b) | table.sort(teamScores, function(a,b) | ||
Line 88: | Line 90: | ||
local mapWins = team.mapWins or 0 | local mapWins = team.mapWins or 0 | ||
local mapLosses = team.mapLosses 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') | local row = mw.html.create('tr') | ||
local order = mw.html.create('td'):wikitext(i .. '.') | |||
local team = mw.html.create('td'):wikitext('[[' .. team.name .. ']]') | -- 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 winsLosses = mw.html.create('td'):wikitext(wins .. '-' .. losses) | ||
local mapScores = mw.html.create('td'):wikitext(mapWins .. '-' .. mapLosses) | local mapScores = mw.html.create('td'):wikitext(mapWins .. '-' .. mapLosses) | ||
Line 96: | Line 110: | ||
end | end | ||
return tableContainer | return tableContainer | ||
end | end | ||
return RoundRobin | return RoundRobin |
Revision as of 11:22, 29 September 2023
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 = {}
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