Leaguepedia | League of Legends Esports Wiki
Advertisement
Leaguepedia | League of Legends Esports Wiki

To edit the documentation or categories for this module, click here. This module has an i18n file. Click here to edit it.


local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_esports = require('Module:EsportsUtil')
local util_html = require('Module:HtmlUtil')
local util_map = require('Module:MapUtil')
local util_sort = require('Module:SortUtil')
local util_table = require('Module:TableUtil')
local util_text = require('Module:TextUtil')
local util_vars = require('Module:VarsUtil')
local i18n = require('Module:i18nUtil')

local m_team = require('Module:Team')

local DEBUG = false -- meh this doesn't do anything rn but maybe later

local h = {}

local p = {}
function p.main(frame)
	local args = util_args.merge()
	i18n.init('TeamPLHQuery')
	local teamArg = args[1] and m_team.teamlinkname(args[1])
	local team = args[1] and m_team.teamlinkname(args[1]) or mw.title.getCurrentTitle().text
	local leagues = h.getListOfLeagues(teamArg, team)
	local playerData = h.getPlayerData(team)
	h.sortPlayers(leagues, playerData, team)
	return h.makeOutput(leagues, playerData)
end

function h.setDebug(args)
	if util_args.castAsBool(args.debug) then
		DEBUG = true
	end
end

-- get leagues data
function h.getListOfLeagues(teamArg, team)
	local leaguesResult = util_cargo.getOrderedList(h.getTeamLeaguesQuery(teamArg, team), 'Long')
	local allLeagueEvents = util_cargo.queryAndCast(h.getAllLeaguesQuery(leaguesResult))
	return h.groupLeagueResult(allLeagueEvents)
end

function h.getTeamLeaguesQuery(teamArg, team)
	local ret = {
		tables = {
			'TeamRedirects=TRed',
			'TournamentRosters=TR',
			'Tournaments=IT',
			'LeagueGroups__Leagues=L',
			'LeagueGroups=LG',
		},
		join = {
			'TRed.AllName=TR.Team',
			'TR.OverviewPage=IT.OverviewPage',
			'IT.League=L._value',
			'L._rowID=LG._ID',
		},
		fields = {
			'LG.LongName=Long',
		},
		where = h.getTeamLeaguesWhere(teamArg, team),
		groupBy = 'LG.LongName',
	}
	return ret
end

function h.getTeamLeaguesWhere(teamArg, team)
	local tbl = {
		'LG.Leagues__full IS NOT NULL',
		h.getTeamWhereCondition(teamArg, team),
		'IT.IsPlayoffs="0"',
		'IT.IsQualifier="0"',
	}
	return util_cargo.concatWhere(tbl)
end

function h.getTeamWhereCondition(teamArg, team)
	if teamArg then
		return ('TR.Team="%s"'):format(teamArg)
	end
	return ('TRed._pageName="%s"'):format(team)
end

function h.getAllLeaguesQuery(leaguesResult)
	local ret = {
		tables = { 'LeagueGroups=LG', 'Tournaments=IT' },
		join = 'LG.Leagues HOLDS IT.League',
		fields = {
			'LG.LongName=Long',
			'LG.ShortName=Short',
			'IT.Year=Year',
			'IT.SplitNumber=SplitNumber',
			'IT.Split=Split',
			'IT.OverviewPage=Tournament'
		},
		where = h.getAllLeaguesWhere(leaguesResult),
		orderBy = 'IT.Year ASC, IT.SplitNumber ASC',
		groupBy = 'IT.OverviewPage'
	}
	return ret
end

function h.getAllLeaguesWhere(leaguesResult)
	if #leaguesResult == 0 then
		error(i18n.print('error_noLeagues'))
	end
	local tbl = {
		'LG.Leagues__full IS NOT NULL',
		util_cargo.concatWhereOr(util_map.inPlace(leaguesResult, h.formatLeaguesResultWhere)),
		'IT.IsPlayoffs="0"',
		'IT.IsQualifier="0"',
		'IT.Split IS NOT NULL', -- temporary until this is resolved
	}
	return util_cargo.concatWhere(tbl)
end

function h.formatLeaguesResultWhere(str)
	return ('LG.LongName="%s"'):format(str)
end

function h.groupLeagueResult(result)
	local byLeagues = {}
	for _, row in ipairs(result) do
		h.parseLeagueRow(byLeagues, row)
	end
	return byLeagues
end

function h.parseLeagueRow(byLeagues, row)
	h.initLeagueGroup(byLeagues, row)
	util_table.initDict(byLeagues[row.Long], row.Tournament)
	byLeagues[row.Long][row.Tournament] = row
	h.parseLeagueGroupFromRow(byLeagues[row.Long].byYear, row)
end

function h.initLeagueGroup(byLeagues, row)
	if byLeagues[row.Long] then return end
	byLeagues[#byLeagues+1] = row.Long
	byLeagues[row.Long] = { byYear = {}, Long = row.Long, Short = row.Short }
end

function h.parseLeagueGroupFromRow(leagueGroupObj, row)
	util_table.initDict(leagueGroupObj, row.Year)
	h.parseYearFromRow(leagueGroupObj[row.Year], row)
	leagueGroupObj.Long = row.Long
	leagueGroupObj.Short = row.Short
end

function h.parseYearFromRow(yearObj, row)
	util_table.initDict(yearObj, row.Split)
	yearObj[row.Split] = row
end

-- get player data
function h.getPlayerData(team)
	local result = util_cargo.queryAndCast(h.getPlayerQuery(team))
	util_map.rowsInPlace(result, h.parsePlayerRow)
	return h.parsePlayersByLeague(result)
end

function h.getPlayerQuery(team)
	-- note that this depends on players being blank edited
	local ret = {
		tables = {
			'PlayerLeagueHistory=PLH',
			'PlayerLeagueHistory__Teams=PLHTeams',
			'TeamRedirects=TRed',
		},
		join = {
			'PLH._ID=PLHTeams._rowID',
			'PLHTeams._value=TRed.AllName',
		},
		where = ('TRed._pageName="%s"'):format(team),
		fields = {
			'PLH.Player',
			'PLH.LeagueHistory',
			'PLH.TotalGames',
			'PLH.League',
			
		}
	}
	return ret
end

function h.parsePlayerRow(row)
	local tournaments = util_text.split(row.LeagueHistory, ';;;')
	row.league = h.splitTournaments(tournaments)
end

function h.splitTournaments(tournaments)
	local ret = {}
	for _, tournament in ipairs(tournaments) do
		h.addOneTournamentToPlayerData(ret, tournament)
	end
	return ret
end

function h.addOneTournamentToPlayerData(data, tournament)
	local name, teams = tournament:match('(.-)::(.*)')
	data[name] = util_text.split(teams, ';;')
end

function h.parsePlayersByLeague(result)
	local byLeague = {}
	for _, row in ipairs(result) do
		h.parsePlayersByTournament(byLeague,row)
	end
	return byLeague
end

function h.parsePlayersByTournament(byLeague, row)
	util_table.initDict(byLeague, row.League)
	h.parseOneLeagueData(byLeague[row.League], row)
end

function h.parseOneLeagueData(oneLeague, row)
	util_table.initDict(oneLeague, row.Player)
	h.parseOnePlayerLeagueData(oneLeague[row.Player], row)
end

function h.parseOnePlayerLeagueData(playerData, row)
	util_table.merge(playerData, row, row.league)
	return playerData
end

-- sort players
function h.sortPlayers(leagueData, playerData, team)
	for _, league in ipairs(playerData) do
		h.sortPlayerLeague(leagueData[league], playerData[league], team)
	end
end

function h.sortPlayerLeague(leagueData, playerLeagueData, team)
	for _, player in ipairs(playerLeagueData) do
		h.getMinYearAndSplit(leagueData, playerLeagueData[player], team)
	end
	util_sort.dictByKeys(playerLeagueData, {'minYear', 'minSplit', 'TotalGames'}, { true, true })
end

function h.getMinYearAndSplit(leagueData, player, team)
	for _, tournament in ipairs(leagueData or {}) do
		if player[tournament] and util_table.keyOf(player[tournament], team) then
			player.minYear = leagueData[tournament].Year
			player.minSplit = leagueData[tournament].SplitNumber
			return
		end
	end
end

-- output
function h.makeOutput(leagues, playerData)
	local output = mw.html.create('div')
		:addClass('wide-content-scroll')
		:wikitext(i18n.print('tiebreakerNotice'))
	for _, league in ipairs(leagues) do
		if playerData[league] and next(playerData[league]) then
			h.printLeagueOutput(output, leagues[league], playerData[league])
		end
	end
	return output
end

function h.printLeagueOutput(output, leagueData, playerData)
	h.printIntro(output, leagueData)
	local tbl = output:tag('table')
		:addClass('wikitable hoverable-rows')
	h.printTable(tbl, leagueData, playerData)
end

function h.printIntro(output, leagueData)
	output:tag('h3'):wikitext(leagueData.Short)
end

function h.printTable(tbl, leagueData, playerData)
	h.printHeading(tbl, leagueData.byYear)
	h.printPlayers(tbl, leagueData, playerData)
end

function h.printHeading(tbl, leagueDataByYear)
	h.printYearsHeading(tbl, leagueDataByYear)
	h.printSplitsHeading(tbl, leagueDataByYear)
end

function h.printYearsHeading(tbl, leagueDataByYear)
	local tr = tbl:tag('tr')
		:addClass('league-timeline-heading-row')
	h.printRowspanHeadingItem(tr, 'Player')
	for _, year in ipairs(leagueDataByYear) do
		tr:tag('th')
			:attr('colspan', #leagueDataByYear[year])
			:wikitext(year)
	end
	h.printRowspanHeadingItem(tr, 'Total')
end

function h.printRowspanHeadingItem(tr, key)
	tr:tag('th')
		:attr('rowspan', 2)
		:wikitext(i18n.print(key))
end

function h.printSplitsHeading(tbl, leagueDataByYear)
	local tr = tbl:tag('tr')
		:addClass('league-timeline-heading-row')
	for _, year in ipairs(leagueDataByYear) do
		for _, split in ipairs(leagueDataByYear[year]) do
			tr:tag('th'):wikitext(util_text.intLink(leagueDataByYear[year][split].Tournament, split))
		end
	end
end

function h.printPlayers(tbl, leagueData, playerData)
	for _, player in ipairs(playerData or {}) do
		local tr = tbl:tag('tr')
		h.printPlayerName(tr, playerData[player])
		h.printPlayerRow(tr, leagueData, playerData[player])
		tr:tag('td'):wikitext(playerData[player].TotalGames)
	end
end

function h.printPlayerName(tr, playerData)
	tr:tag('td'):wikitext(util_esports.playerLinked(playerData.Player))
end

function h.printPlayerRow(tr, leagueData, playerLeagueData)
	if not playerLeagueData then
		h.printEmptyRow(tr, leagueData)
		return
	end
	for _, tournament in ipairs(leagueData) do
		h.printPlayerTeamCell(tr, playerLeagueData[tournament])
	end
end

function h.printEmptyRow(tr, leagueData)
	error(i18n.print('error_emptyRow'))
end

function h.printPlayerTeamCell(tr, playerLeagueTournamentData)
	local td = tr:tag('td')
	td:wikitext(util_table.concat(util_table.guaranteeTable(playerLeagueTournamentData), '<br>', m_team.onlyimagelinkedmedium))
end
return p
Advertisement