Module:CurrentLeagueParticipantNavbox

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search
To edit the documentation or categories for this module, click here.
local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_navbox = require('Module:NavboxUtil')
local util_vars = require("Module:VarsUtil")
local m_team = require('Module:Team')
local UpcomingMatches = require('Module:ScheduleNavbox').addToArgs

local h = {}

local p = {}

function p.main(frame)
	local args = util_args.merge()	
	local thisteam = h.getThisTeam(args)
	if not thisteam then return end
	local result = h.getData(thisteam)
	local navbox_args = h.getNavboxArgs(thisteam)
	h.addDataToNavboxArgs(result, args, navbox_args)
	UpcomingMatches(navbox_args, 'Team', thisteam, #result)
	return util_navbox.makeNavbox(navbox_args)
end

function h.getThisTeam(args)
	if not args[1] then return nil end
	return m_team.teamlinkname(args[1])
end

function h.getData(team)
	return util_cargo.queryAndCast(h.makeQuery(team))
end

function h.makeQuery(team)
	return {
		tables = "CurrentLeagues=CL, TournamentRosters",
		fields = table.concat({
			"CONCAT(CL.Page)=Page",
			"CL.Event=Event",
		},','),
		where = string.format(
			'TournamentRosters.TeamLink="%s" AND CL.Page IS NOT NULL',
			team
		),
		groupBy = "CL.Page",
		join = "TournamentRosters._pageName = CL.Page",
		orderBy = 'CL.Priority ASC'
	}
end

function h.getNavboxArgs(thisteam)
	local ret = {
		title = ('Current Events & Schedule - %s'):format(m_team.mediumplainlinked(thisteam)),
		state = 'mw-collapsible',
		name = 'CurrentLeagueParticipantNavbox'
	}
	return ret
end

function h.addDataToNavboxArgs(result, args, navbox_args)
	for i, row in ipairs(result) do
		local teamsresult = h.getTeamsFromPage(row.Page)
		local teams = h.formatTeams(teamsresult, args.suffix)
		navbox_args['group' .. i] = "[[" .. row.Page .. "|" .. row.Event .. "]] Participants"
		navbox_args['list' .. i] = table.concat(teams, " ")
		navbox_args[('tr%sclass'):format(i)] = 'clpn-teamlist-row'
	end
end

function h.getTeamsFromPage(page)
	return util_cargo.queryAndCast({
		tables = 'TournamentRosters',
		fields = 'TeamLink',
		where = '_pageName = "' .. page .. '"',
		groupBy = "TeamLink"
	})
end

function h.formatTeams(result, useSuffix)
	local suffix = util_navbox.getSuffix(useSuffix)
	local tbl = {}
	for k, v in ipairs(result) do
		tbl[#tbl+1] = h.formatTeam(v.TeamLink, suffix)
	end
	return tbl
end

function h.formatTeam(team, suffix)
	if util_navbox.doesSuffixExist(team, suffix) then
		return m_team.rightmediumlinked(team, { suffix = suffix })
	else
		return string.format(
			'<span class="no-subpage">%s</span>',
			m_team.rightmediumlinked(team)
		)
	end
end

return p