Module:PortalCurrentRosters

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

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

local util_args = require('Module:ArgsUtil')
local util_esports = require('Module:EsportsUtil')
local util_footnote = require('Module:FootnoteUtil')
local util_html = require('Module:HTMLUtil')
local util_text = require('Module:TextUtil')
local util_vars = require('Module:VarsUtil')

local m_team = require('Module:Team')
local m_role = require('Module:Role')

local ROLES = { 't', 'j', 'm', 'a', 's' }

local COL_CLASSES = { 'old', 'new' }

local p = {}
local h = {}
function p.main(frame)
	local args = util_args.merge(true)
	h.setRoles(args)
	h.addLinksToArgs(args)
	local dataByType = h.getData(args)
	h.normalizeData(dataByType)
	h.copyOldDataToNewIfIsOver(args.isover, dataByType)
	local dataByColumns = h.organizeData(dataByType, args)
	local processed = h.processData(dataByColumns), args.isover
	h.ensureOneRowPerRole(processed)
	return h.makeOutput(args, processed)
end

function h.setRoles(args)
	if util_args.castAsBool(args.coach) then
		ROLES[#ROLES+1] = 'c'
	end
end

function h.addLinksToArgs(args)
	for _, v in ipairs(ROLES) do
		args[v .. '_old_links'] = args[v .. '_old_links'] or args[v .. '_old']
		args[v .. '_new_links'] = args[v .. '_new_links'] or args[v .. '_new']
	end
end

function h.getData(args)
	return {
		old = h.getOneColumnData(args, 'old'),
		new = h.getOneColumnData(args, 'new')
	}
end

function h.getOneColumnData(args, column)
	local tbl = { names = {}, links = {}, classes = {} }
	for _, v in ipairs(ROLES) do
		tbl.names[v] = util_text.split(args[v .. '_' .. column])
		tbl.links[v] = util_text.split(args[v .. '_' .. column .. '_links'])
		tbl.classes[v] = util_text.split(args[v .. '_' .. column .. '_classes'])
	end
	return tbl
end

function h.normalizeData(dataByType)
	h.normalizeDataColumn(dataByType.new.links)
	h.normalizeDataColumn(dataByType.new.names)
end

function h.normalizeDataColumn(dataByTypeColumn)
	for _, row in pairs(dataByTypeColumn) do
		for k, v in pairs(row) do
			if v == '' then
				row[k] = nil
			end
		end
	end
end

function h.copyOldDataToNewIfIsOver(isover, dataByType)
	if not util_args.castAsBool(isover) then return end
	for _, role in ipairs(ROLES) do
		if not next(dataByType.new.names[role]) then
			-- only clone links if we had to clone the args, otherwise we might unintentionally clone links when actually just the first column needed a disambig
			dataByType.new.names[role] = mw.clone(dataByType.old.names[role])
			if not next(dataByType.new.links[role]) then
				dataByType.new.links[role] = mw.clone(dataByType.old.links[role])
			end
		end
	end
end

function h.organizeData(data, args)
	local tbl = {}
	for _, role in ipairs(ROLES) do
		tbl[role] = {
			old = {
				names = data.old.names[role],
				links = data.old.links[role],
				classes = h.processClasses(data.old.classes[role])
			},
			new = {
				names = data.new.names[role],
				links = data.new.links[role],
				classes = h.processClasses(data.new.classes[role])
			},
			footnoten = args[role .. '_footnoten']
		}
	end
	return tbl
end

function h.processClasses(tbl)
	for k, v in ipairs(tbl) do
		if v and v ~= '' then
			tbl[k] = h.processClass(v)
		end
	end
	return tbl
end

function h.processClass(str)
	return 'rosterswap-current-' .. str
end

function h.processData(data, isover)
	-- organized data
	local processed = {}
	for _, role in ipairs(ROLES) do
		processed[role] = h.processRole(data[role], util_args.castAsBool(isover))
	end
	return processed
end

function h.processRole(data, isover)
	local dataByRow = data
	for i, v in ipairs(data.old.links) do
		if v == '' then
			dataByRow[i] = h.newPlayerJoined(data, i)
		elseif data.new.links[i] == v then
			dataByRow[i] = h.playerStayed(data, i)
		elseif data.new.links[i] then
			dataByRow[i] = h.playerSwitched(data, i)
		elseif not data.new.links[i] and isover then
			dataByRow[i] = h.playerLeft(data, i)
		elseif not isover then
			dataByRow[i] = h.playerIncomplete(data, i)
		end
	end
	return dataByRow
end

function h.playerLinked(data, column, i)
	return util_text.intLink(data[column].links[i], data[column].names[i])
end

function h.newPlayerJoined(data, i)
	return {
		'',
		h.playerLinked(data, 'new', i),
		classes = {
			data.old.classes[i],
			data.new.classes[i] or 'rosterswap-current-join',
		}
	}
end

function h.playerStayed(data, i)
	return {
		h.playerLinked(data, 'old', i),
		classes = { data.old.classes[i] or 'rosterswap-current-stay' }
	}
end

function h.playerSwitched(data, i)
	return {
		h.playerLinked(data, 'old', i),
		h.playerLinked(data, 'new', i),
		classes = {
			data.old.classes[i] or 'rosterswap-current-leave',
			data.new.classes[i] or 'rosterswap-current-join',
		}
	}
end

function h.playerLeft(data, i)
	return {
		h.playerLinked(data, 'old', i),
		'',
		classes = {
			data.old.classes[i] or 'rosterswap-current-leave',
			data.new.classes[i],
		}
	}
end

function h.playerIncomplete(data, i)
	return {
		h.playerLinked(data, 'old', i),
		'',
		classes = {
			data.old.classes[i],
			data.new.classes[i],
		}
	}
end

function h.ensureOneRowPerRole(processed)
	for _, role in ipairs(ROLES) do
		if not processed[role][1] then
			processed[role] = { { '', '', classes = {} } }
		end
	end
end

function h.makeOutput(args, processed)
	local tbl = mw.html.create('table')
		:addClass('wikitable2')
		:addClass('rosterswap-current')
	h.printHeading(tbl, args)
	h.printPlayers(tbl, processed)
	return tbl
end

function h.printHeading(tbl, args)
	util_html.makeEmptyWidthRow(tbl,{'5em','8em','8em'})
	if args.team2 then
		h.printHeadingTwoTeams(tbl, args)
	else
		h.printHeadingOneTeam(tbl, args)
	end
	h.printSeasons(tbl)
end

function h.printHeadingTwoTeams(tbl, args)
	local tr = tbl:tag('tr')
	tr:tag('th'):attr('rowspan',2)
	tr:tag('th')
		:wikitext(m_team.onlyimagelinked(args.team1))
		:addClass(h.processClass(COL_CLASSES[1]))
	tr:tag('th')
		:wikitext(m_team.onlyimagelinked(args.team2))
		:addClass(h.processClass(COL_CLASSES[2]))
end

function h.printHeadingOneTeam(tbl, args)
	local tr = tbl:tag('tr')
	tr:tag('th'):attr('rowspan',2):wikitext(m_team.onlyimagelinked(args.team1))
	tr:tag('th'):attr('colspan',2):wikitext(m_team.mediumplainlinked(args.team1))
end

function h.printSeasons(tbl)
	local tr = tbl:tag('tr')
	tr:tag('th')
		:addClass('rosterswap-current-season')
		:wikitext(util_vars.getVar('season1'))
	tr:tag('th')
		:addClass('rosterswap-current-season')
		:addClass('rosterswap-current-new')
		:wikitext(util_vars.getVar('season2'))
end
	
function h.printPlayers(tbl, processed)
	for _, role in ipairs(ROLES) do
		h.printPlayer(tbl, processed[role], role)
	end
end

function h.printPlayer(tbl, processed, role)
	local tr = tbl:tag('tr')
		:addClass('rosterswap-current-' .. role)
		:addClass('rosterswap-current-firstline')
	local td = tr:tag('td')
		:wikitext(m_role.mediumname(role))
		:attr('rowspan', h.getRowspan(processed))
	if processed.footnoten then
		util_footnote.tagFootnotePlain(td,processed.footnoten)
	end
	for i, row in ipairs(processed) do
		if i > 1 then
			tr = tbl:tag('tr')
				:addClass('rosterswap-current-' .. role)
		end
		for j, player in ipairs(row) do
			tr:tag('td')
				:attr('colspan',2 / #row)
				:addClass(row.classes[j])
				:wikitext(row[j])
				:addClass(h.processClass(COL_CLASSES[j]))
		end
	end
end

function h.getRowspan(processed)
	if #processed == 0 then
		return 1
	else
		return #processed
	end
end
return p