Module:GenerateTeamRosterText

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

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

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 p = {}
local h = {}

function p.main(frame)
	local args = util_args.merge(true)
	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!<br>"}}
	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)
	local query = {
		tables = 'ListplayerCurrent',
		where = ('Team="%s"'):format(team),
		fields = {
			'ID',
			'Link',
			'Name',
			'Residency',
			'Country',
			'Role',
			'Name'
		},
		orderBy = 'N',
		groupBy = 'N'
	}
	return util_cargo.queryAndCast(query)
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] = '{{ExtendedRosterEnd}}\n'
	end
	local output = table.concat(tbl,'\n')
	return '<pre>' .. output .. '</pre>'
end

function h.makeHeader(team)
	return mw.text.nowiki(('==={{team|%s}}==='):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 '{{ExtendedRosterStart' .. str .. '}}'
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] = ('{{ExtendedRosterLine|flag=%s|res=%s|player=%s%s|name=%s|role=%s\n|r= }}'):format(
		player.Country or '',
		player.Residency or '',
		player.ID or '',
		h.printLink(player.Link, player.ID),
		player.Name or '',
		player.Role or ''
	)
end

function h.printLink(link, id)
	if link == id or not link then
		return ''
	else
		return ('|link=%s'):format(link)
	end
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, '<br>')
end

return p