Module:MatchDetails

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

To edit the documentation or categories for this module, click here. This module is a bit of a mess and should probably be reworked or completely rewritten sometime. Because it needs to make one single table, but it's invoked separate times per series, it requires a <table> tag in wikitext instead of from the mw.html construction.

The reason it's written this way is to avoid forcing editors to re-index when series don't go to 3 games.

See {{MatchDetails/Series}} for usage information.


local SETTINGS = mw.loadData('Module:MatchDetailsSettings')
local m_team = require('Module:Team')
local lang = mw.getLanguage('en')
local p = {}

-- to be called on its own, prints the top row of the table
function p.matchDetailsStart(frame)
	local args = frame
	if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
	end
	
	toggles = {
		show = "_toggler_show-allPhasesshow _toggler_hide-allPhaseshide",
		hide = "_toggler_show-allPhaseshide _toggler_hide-allPhasesshow",
		shown = "_toggle allPhasesshow",
		hidden = "_toggle allPhaseshide",
		init = "_toggle_inithide",
		init2 = "_toggle_initshow"
	}
	
	if args['currentphase'] then
		frame:callParserFunction('#vardefine','currentphase',args['currentphase'])
	end
	
	tbl = mw.html.create('table'):addClass('wikitable'):css('width','100%')
	:tag('tr')
		:tag('th'):attr('colspan','30')
			:tag('div')
				:css({ width = "100%", position = "relative" })
				:wikitext("Game Information & Links")
				:tag('div')
					:css({ position = "absolute", top = "0", right = "5px" })
					:wikitext('[')
					:tag('span')
						:addClass(toggles.init2)
						:addClass(toggles.show)
						:addClass(toggles.hidden)
						:wikitext('Show All')
					:done()
					:tag('span')
						:addClass(toggles.init)
						:addClass(toggles.hide)
						:addClass(toggles.shown)
						:wikitext('Hide All')
					:done()
					:wikitext(']')
				:done()
			:done()
		:done()
	:done()
	
	output = tostring(tbl)
	
	return output
end
	

-- helper functions for the main function
function p.phaseHeader(phase, toggles)
	tbl = mw.html.create()
	:tag('tr')
		:tag('th')
			:attr('colspan','30')
			:css('font-size','90%')
			:tag('div')
				:css({ position = 'relative', width = '100%' })
				:tag('div')
					:css({ position = 'absolute', top = '0', right = '5px' })
					:wikitext('[')
					:tag('span')
						:addClass(toggles.init2):addClass(toggles.hidden):addClass(toggles.show)
						:wikitext('Show')
					:done()
					:tag('span')
						:addClass(toggles.init):addClass(toggles.shown):addClass(toggles.hide)
						:wikitext('Hide')
					:done()
					:wikitext(']')
				:done()
				:wikitext(phase)
			:done()
		:done()
	:done()
	:tag('tr')
		:addClass(toggles.init)
		:addClass(toggles.shown)
		:css('font-size','70%')
		:tag('th'):wikitext("Round"):done()
		:tag('th'):wikitext("Team 1"):done()
		:tag('th'):wikitext("Team 2"):css({ ["border-right-width"] = "medium" }):done()
		:tag('th'):wikitext("PBP"):done()
		:tag('th'):wikitext("Color"):css({ ["border-right-width"] = "medium" }):done()
		:tag('th'):wikitext("PB"):done()
		:tag('th'):wikitext("Game"):done()
		-- :tag('th'):wikitext("Post"):done()
		:tag('th'):wikitext("Interview"):done()
		:tag('th'):wikitext("With"):done()
		:tag('th'):wikitext("HL"):done()
		:tag('th'):wikitext("MVP"):css({ ["border-right-width"] = "medium" }):done()
		:tag('th'):wikitext("MH"):done()
		:tag('th'):wikitext("Summary"):done()
		:tag('th'):wikitext("Reddit"):done()
	:done()
	return tbl
end

function p.concatLinks(names, links)
	local linkednames = {}
	for index, name in ipairs(names) do
		if links[index] == '' or links[index] == nil then -- allow people to not specify links for everyone if one is defined
			links[index] = name
		end
		linkednames[index] = '[[' .. links[index] .. '|' .. name .. ']]'
	end
	return { names = names, links = links, linkednames = linkednames }
end
-- end helper functions

-- main function
function p.matchDetailsSeries(frame)
	local args = frame
	if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
	end
	
	local title = mw.title.getCurrentTitle()
	local titlestring = tostring(title)
	
	-- variables
	local phase = frame:callParserFunction('#var','phase')
	local currentphase = frame:callParserFunction('#var', 'currentphase')
	local seriesN = (tonumber(frame:callParserFunction('#var','seriesN')) or 0) + 1
	frame:callParserFunction('#vardefine','seriesN',seriesN)
	local overviewpage = frame:callParserFunction('#var', 'overviewpage')
	
	local printPhase = false
	if args['phase'] and phase ~= args['phase'] then
		frame:callParserFunction('#vardefine','phase',args['phase'])
		phase = args['phase']
		printPhase = true
	end
	
	local phasetoggle = string.gsub(phase, ' ', '')
	
	toggles = {
		show = "_toggler_show-" .. phasetoggle .. 'show _toggler_hide-' .. phasetoggle .. 'hide',
		hide = "_toggler_show-" .. phasetoggle .. 'hide _toggler_hide-' .. phasetoggle .. 'show',
		shown = "_toggle " .. phasetoggle .. "show" .. ' allPhasesshow',
		hidden = "_toggle " .. phasetoggle .. "hide" .. ' allPhaseshide',
	}
	
	if phase == currentphase then
		toggles.init = "_toggle_initshow"
		toggles.init2 = "_toggle_inithide"
	else
		toggles.init = "_toggle_inithide"
		toggles.init2 = "_toggle_initshow"
	end
	
	-- define things about how we will format what things
	
	local exlinkargs = {
		pb = {},
		gamestart = {},
		-- post = {},
		interview = {},
		mh = {},
		hl = {},
		summary = {},
		reddit = {},
	}
	
	-- these are displays, if there are links different from display they will be at |pbplink= etc
	-- note these can all be comma-separated lists given in inputs, both for displays & for links. the code assumes the arrays are the same size if the link array exists.
	local intlinkargs = {
		pbp = {},
		color = {},
		mvp = {},
		subject = {},
	}
		
	local cargofieldnames = SETTINGS.cargofieldnames
	
	-- vertical lines for breaking up visually
	local printstyles = { [2] = { ["border-right-width"] = "medium" }, [8] = { ["border-right-width"] = "medium" } }
	
	local seriesLength = 1
	while args['g' .. seriesLength .. 'team1'] or args['g' .. seriesLength .. 'team2'] do
		seriesLength = seriesLength + 1
	end
	seriesLength = seriesLength - 1
	
	-- unpack args	
	local args_formatted = {}
	local seriesargs = {}
	for k, v in pairs(exlinkargs) do
		if args[k] then
			seriesargs[k] = args[k]
		else
			i = 1
			while i <= seriesLength do
				v[i] = args['g' .. i .. k]
				i = i + 1
			end
		end
	end
	
	for k, v in pairs(intlinkargs) do
		if args[k] then
			seriesargs[k] = p.concatLinks(mw.text.split(args[k],','), mw.text.split(args[k .. 'link'] or '',','))
		else
			i = 1
			while i <= seriesLength do
				arg = args['g' .. i .. k]
				if arg then
					v[i] = p.concatLinks(mw.text.split(arg,','), mw.text.split(args['g' .. i .. k .. 'link'] or '',','))
				end
				i = i + 1
			end
		end
	end

	
	-- format args
	-- seriesargs, exlinkargs, and intlinkargs will contain unformatted raw data to store in cargo; key is data type so we can table.concat to store for a series table
	-- args_formatted and contain formatteds data to print to chart
	local i = 1
	local winner = '' -- we want to use the last winner later on so initialize this here
	while i <= seriesLength do
		-- first set up constants
		
		team1 = args['g' .. i .. 'team1'] or ''
		team2 = args['g' .. i .. 'team2'] or ''
		winner = args['g' .. i .. 'winner'] or ''
		
		data = { 
			Team1 = m_team.teamlinkname(team1),
			Team2 = m_team.teamlinkname(team2),
			Winner = winner,
			Round = args['round'],
			Phase = phase,
			Series = seriesN,
			Game = i,
			UniqueLine = titlestring .. "_" .. seriesN .. "_" .. i,
			OverviewPage = overviewpage,
		}
		
		args_formatted[i] = {
			team1 = m_team.rightshort(team1),
			team2 = m_team.rightshort(team2),
			winner = winner
		}
		
		args_formatted[i]['style' .. winner] = { ['background-color'] = 'var(--color-up)' }
		
		-- now format external link args
		for k, v in pairs(exlinkargs) do
			if seriesargs[k] then
				data[cargofieldnames[k]] = seriesargs[k]
				args_formatted[i][k] = '[' .. seriesargs[k] .. ' Link]'
			elseif v[i] then
				data[cargofieldnames[k]] = v[i]
				args_formatted[i][k] = '[' .. v[i] .. ' Link]'
			else
				args_formatted[i][k] = '-'
			end
		end
		
		-- now format internal link args
		for k, v in pairs(intlinkargs) do
			if seriesargs[k] then
				data[cargofieldnames[k]] = table.concat(seriesargs[k].names,',')
				data[cargofieldnames[k.. 'links']] = table.concat(seriesargs[k].links,',')
				args_formatted[i][k] = table.concat(seriesargs[k].linkednames, ' &#8226; ')
			elseif v[i] then
				data[cargofieldnames[k]] = table.concat(v[i].names,',')
				data[cargofieldnames[k.. 'links']] = table.concat(v[i].links,',')
				args_formatted[i][k] = table.concat(v[i].linkednames, ' &#8226; ')
			else
				args_formatted[i][k] = '-'
			end
		end
		
		i = i + 1
	end
	
	-- game-specific styles within the series
	if seriesLength > 1 and lang:lc(args.draw or '') ~= 'yes' then
		args_formatted[seriesLength]['style' .. winner]['font-weight'] = '700'
	end
	
	local trstyle = {}
	if not printPhase and (seriesLength > 1 or lang:lc(args.forcenewseries or '') == 'yes') then
		trstyle = { ["border-top"] = "3px solid #aaa;" }
	end
	
	-- make the table
	local tbl = (seriesLength > 0 or printPhase) and mw.html.create() or nil
	
	if printPhase then
		tbl:node(p.phaseHeader(phase, toggles))
	end
	
	local j = 1
	while j <= seriesLength do
		tr = tbl:tag('tr'):addClass(toggles.init):addClass(toggles.shown)
		-- first print the specially-formatted stuff, i.e. round & teams
		if j == 1 then
			tr:css(trstyle):tag('td'):attr('rowspan',seriesLength):wikitext(args['round'] or ''):done()
		end
		-- teams now
		tr:tag('td'):css(args_formatted[j].style1 or {}):wikitext(args_formatted[j].team1):done()
		:tag('td'):css(args_formatted[j].style2 or {})
			:css({ ["border-right-width"] = "medium" })
			:wikitext(args_formatted[j].team2):done()
		-- now do the rest of the cells with data, which are all the same
		for key , value in ipairs(SETTINGS.printthese) do
			-- first attempt to make a rowspan value if there's a full series arg & we are in the first row
			if seriesargs[value] and j == 1 then
				tr:tag('td')
					:attr('rowspan',seriesLength)
					:css(printstyles[key] or {})
					:wikitext(args_formatted[j][value] or '-')
				:done()
			-- otherwise we have no rowspan so we will print a cell definitely, either the content we actually have or a -
			elseif seriesargs[value] == nil then
				tr:tag('td')
					:css(printstyles[key] or {})
					:wikitext(args_formatted[j][value] or '-')
				:done()
			end
		end
	j = j + 1
	end
	
	output = tostring(tbl or '<!-- -->')
	return output

end

return p