Module:RosterTooltip

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

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_html = require('Module:HTMLUtil')
local util_table = require('Module:TableUtil')
local m_country = require('Module:Country')
local m_region = require('Module:Region')
local m_role = require('Module:Role')
local m_team = require('Module:Team')

local HEADINGS = { 'R', 'C', 'R', 'ID', 'Name' }
local FIELDS = { 'Residency', 'Country', 'Role', 'ID', 'Name' }

local h = {}

function h.doQuery(pagename)
	local query = h.makeQuery(pagename)
	local result = util_cargo.queryAndCast(query)
	return result
end

function h.makeQuery(pagename)
	local query = {
		tables = 'ListplayerCurrent',
		fields = {
			'ID',
			'Name',
			'Country',
			'Residency',
			'Role',
			'IsSubstitute'
		},
		where = ('Team="%s" AND ID IS NOT NULL'):format(pagename),
		orderBy = 'N ASC'
	}
	return query
end

function h.processRow(row)
	row.Residency = m_region.onlyimage(row.Residency)
	row.Country = row.Country and m_country.onlyimage(row.Country) or ''
	row.Role = m_role.onlyimage(row.Role, {sub = util_args.castAsBool(row.IsSubstitute)})
	return
end

function h.printTable(data, team)
	local tbl = h.initializeTableHeadings(team)
	util_html.printRowsByList(tbl, data, FIELDS)
	return tbl
end

function h.initializeTableHeadings(team)
	local tbl = h.initializeTable()
	tbl:tag('tr'):tag('th'):attr('colspan',#HEADINGS):wikitext(('%s Current Roster'):format(team))
	util_html.makeHeader(tbl, HEADINGS, 'roster-tooltip-heading')
	return tbl
end

function h.initializeTable()
	local tbl = mw.html.create('table')
		:addClass('wikitable')
		:addClass('tippingover-tooltip')
		:addClass('tooltip-table')
	return tbl
end

function h.makeEmptyRosterTable(pagename, team)
	local query = {
		tables = 'InfoboxTeam',
		fields = 'IsDisbanded',
		where = ('_pageName="%s"'):format(pagename),
		types = { IsDisbanded = 'boolean' }
	}
	local isDisbanded = util_cargo.getOneResult(query)
	if isDisbanded then
		return h.printDisbandedTeam(team)
	else
		return h.printEmptyTable(team)
	end
end

function h.printDisbandedTeam(team)
	local tbl = h.initializeTable()
	tbl:tag('tr'):tag('th')
		:wikitext(('%s - Disbanded Team'):format(team))
	return tbl
end

function h.printEmptyTable(team)
	local tbl = h.initializeTableHeadings(team)
	tbl:tag('tr'):tag('td')
		:attr('colspan',#HEADINGS)
		:wikitext("''Team has no active members.''")
	return tbl
end

local p = {}
function p.main(frame)
	local args = util_args.merge(true)
	local pagename = args.page or mw.title.getCurrentTitle().text
	if not mw.title.new(pagename).exists then
		return '[[Category:Tooltip Pages Without Overview Pages]]'
	end
	local team = m_team.teammediumname(pagename)
	local data = h.doQuery(pagename)
	if not next(data) then
		return h.makeEmptyRosterTable(pagename, team)
	end
	util_table.mapRowsInPlace(data, h.processRow)
	return h.printTable(data, team)
end
return p