Module:GenerateTeamRosterText

local util_args = require('Module:ArgsUtil') local util_cargo = require('Module:CargoUtil') local util_table = require('Module:TableUtil') local util_text = require('Module:TextUtil') local util_title = require('Module:TitleUtil')

local QueryTeamMembers = require('Module:QueryTeamMembers').main

local p = {} local h = {}

function p.main(frame) local args = util_args.merge if not args.page then error('page is a required input') end local overviewPage = util_title.target(args.page) local teams = h.getTeams(overviewPage, args.from) local teamData = h.getTeamsData(teams, overviewPage) local warning = frame:expandTemplate{ title = 'Important', args = { "Coaches aren't included, please add! "}} local links = h.makeOutputLinks(overviewPage) return warning, links, h.makeOutput(teamData) end

function h.getTeams(page, querytype) if querytype == 'Schedule' then return h.getTeams_MatchSchedule(page) else return h.getTeams_TeamRosters(page) end end

function h.getTeams_MatchSchedule(page) local query = { tables = 'MatchSchedule', fields = { 'Team1', 'Team2' },		limit = 9999, where = ('OverviewPage="%s"'):format(page) }	local result = util_cargo.queryAndCast(query) return h.getTeamsFromResult_MatchSchedule(result) end

function h.getTeamsFromResult_MatchSchedule(result) local tbl = {} local hash = {} for _, row in ipairs(result) do		if not hash[row.Team1] then hash[row.Team1] = true tbl[#tbl+1] = row.Team1 end if not hash[row.Team2] then hash[row.Team2] = true tbl[#tbl+1] = row.Team2 end end util_table.alphabetize(tbl) return tbl end

function h.getTeams_TeamRosters(page) local query = { tables = 'TournamentRosters', where = ('_pageName="%s"'):format(page), fields = 'Team', orderBy = 'Team' }	return util_cargo.getOrderedList(query, 'Team') end

function h.getTeamsData(teams, page) local tbl = {} for i, team in ipairs(teams) do		tbl[i] = h.getTeamData(team, page) end return tbl end

function h.getTeamData(team, page) local tbl = { team = team, schedule = h.getSchedule(team, page), players = h.getPlayers(team) }	return tbl end

function h.getSchedule(team, page) local query = { tables = 'MatchSchedule', where = h.getScheduleWhere(team, page), fields = { 'Team1', 'Team2' },		limit = 9999, groupBy = 'UniqueMatch', orderBy = 'DateTime_UTC' }	local result = util_cargo.queryAndCast(query) return h.parseSchedule(team, result) end

function h.getScheduleWhere(team, page) local tbl = { ('(Team1="%s" OR Team2 = "%s")'):format(team, team), ('OverviewPage="%s"'):format(page) }	return util_cargo.concatWhere(tbl) end

function h.parseSchedule(team, result) local tbl = {} for i, row in ipairs(result) do		if row.Team1 == team then tbl[#tbl+1] = row.Team2 elseif row.Team2 == team then tbl[#tbl+1] = row.Team1 end end return tbl end

function h.getPlayers(team) return QueryTeamMembers(team) end

function h.makeOutput(data) local tbl = {} for _, row in ipairs(data) do		tbl[#tbl+1] = h.makeHeader(row.team or '') tbl[#tbl+1] = h.makeStart(row.schedule) h.addPlayers(tbl, row.players) tbl[#tbl+1] = '\n' end local output = table.concat(tbl,'\n') return ' ' .. output .. ' ' end

function h.makeHeader(team) return mw.text.nowiki(('======'):format(team)) end

function h.makeStart(schedule) local tbl = {} for i, team in ipairs(schedule) do		tbl[i] = ('|team%s=%s'):format(i, team) end local str = table.concat(tbl, '') return '' end

function h.addPlayers(tbl, players) for _, player in ipairs(players) do		h.addPlayer(tbl, player) end end

function h.addPlayer(tbl,player) tbl[#tbl+1] = ():format(		player.Country or ,		player.Residency or ,		player.Link or ,		player.NameFull or player.Name or ,		player.CompoundRole:get('name') or 	) end

function h.makeOutputLinks(overviewPage) local tbl = { ('Overview Page: %s'):format(util_text.intLinkOrText(overviewPage)), ('Data Page: %s'):format(util_text.intLinkOrText('Data:' .. overviewPage)) }	return table.concat(tbl, ' ') end

return p