Module:FrontpageMatches

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

To edit the documentation or categories for this module, click here.

Dependencies: {{FrontpageMatchesSettings}}m


local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_esports = require('Module:EsportsUtil')
local util_form = require('Module:FormUtil')
local util_html = require('Module:HTMLUtil')
local util_table = require('Module:TableUtil')
local util_time = require('Module:TimeUtil')
local util_vars = require('Module:VarsUtil')
local m_team = require('Module:Team')
local SETTINGS = require('Module:FrontpageMatches/Settings')
local SECTION
local DEFAULT_LIMIT = 20

local FORM_INFO = {
	form = 'SpoilerFreeSchedule',
	template = 'SFS'
}

local h = {}
local p = {}
function p.main(frame)
	local args = util_args.merge(true)
	SECTION = SETTINGS.sections[tonumber(args[1])]
	local data = h.getData(args)
	return h.togglers(args), h.makeFullOutput(data, args)
end

function p.section(frame)
	local args = util_args.merge(true)
	SECTION = SETTINGS.sections[tonumber(args[1])]
	local data = h.getData(args)
	return h.makeOutput(data, args)
end

-- togglers
function h.togglers(args)
	local output = mw.html.create('div')
		:addClass('titletabs-tabs')
	for i, section in ipairs(SETTINGS.sections) do
		h.printToggler(output, section, i == tonumber(args.default))
	end
	return output
end
function h.printToggler(output, section, isDefault)
	local div = output:tag('div')
		:addClass('titletabs-tab')
		:addClass('fpmatches-togglers')
		:attr('data-replaceid', 'fpmatches-section-' .. section)
		:attr('data-templatecode', ('Project:Matches Section/%s'):format(section))
		:attr('data-toggler-show', 'fpmatches-section-' .. section)
		:attr('data-toggler-hide', 'fpmatches-section')
		:attr('data-toggle-section', 'fpmatches-togglers')
		:wikitext(SETTINGS[section].title)
	if isDefault then
		div:addClass('active')
			:addClass('optionfromlist-toggler')
	else
		div:addClass('optionfromlist-toggler-lazy')
	end
end

function h.getData(args)
	local query = {}
	util_table.merge(query, SETTINGS.cargo, SETTINGS[SECTION].cargo)
	query.limit = h.getLimit(args)
	if SETTINGS[SECTION].min_max_query then
		query.where = util_cargo.makeMinMaxQuery(query, '_pageName', 'DateTime_UTC', 'MIN')
	end
	return util_cargo.queryAndCast(query)
end

function h.getLimit(args)
	return args.limit or DEFAULT_LIMIT
end

function h.makeFullOutput(data, args)
	local output = mw.html.create()
	for _, v in ipairs(SETTINGS.sections) do
		if v == SECTION then
			output:node(h.makeOutput(data, args))
		else
			output:node(h.makeEmptyOutput(v))
		end
	end
	return output
end

function h.makeEmptyOutput(section)
	local output = mw.html.create('div')
		:attr('id', 'fpmatches-section-' .. section)
		:addClass('fpmatches-section-' .. section)
		:addClass('fpmatches-section')
		:addClass('toggle-section-hidden')
	return output
end

function h.makeOutput(data, args)
	local output = mw.html.create('div')
		:addClass('fpmatches-section')
		:attr('id', 'fpmatches-section-' .. SECTION)
		:addClass('fpmatches-section-' .. SECTION)
	h.printSection(output, data, args)
	return output
end

function h.printSection(div, data, args)
	div:wikitext(string.format(SETTINGS[SECTION].sentence or '', h.getLimit(args)))
	local tbl = div:tag('table')
		:addClass('plainlinks')
		:css({ ['text-align'] = 'center', display = 'inline-table', margin = '0' })
	-- table headings
	tr = tbl:tag('tr')
	for i = 1, 3 do
		tr:tag('th'):css(SETTINGS.sectionstyles[i])
			:wikitext(SETTINGS[SECTION].sectiontitles[i])
	end
	-- rest of the table
	for n, row in ipairs(data) do
		row.N = n
		tbl:node(h.makeRow(row))
	end
end

function h.makeRow(row)
	local formatted = h.parseData(row)
	local tbl = mw.html.create('tr')
	for _, item in ipairs(SETTINGS[SECTION].fields) do
		tbl:tag('td')
			:node(formatted[item])
		:done()
	end
	return tbl
end

function h.parseData(row)
	local formatted = {
		tournament = h.formatTournament(row.Tournament, row.ShownName),
		teams = h.formatTeams(row.Team1, row.Team2, row.Round),
		winner = h.formatWinner(row),
		countdown = h.formatCountdown(row),
	}
	return formatted
end

function h.formatTournament(tournament, shownname)
	local link = string.format('[[%s|%s]] %s',
		tournament or '',
		shownname or '',
		util_esports.calendarExtLink(util_form.fullURL(FORM_INFO,{tournament}))
	)
	return util_html.makeNodeFromWikitext(link)
end

function h.formatTeams(team1, team2, round)
	local tbl = mw.html.create('table')
		:css({ margin = '-3px -1px', width = '100%', ['table-layout'] = 'fixed' })
	if not (team1 or team2) then
		tbl:wikitext(string.format("''%s''",round or ''))
	else
		tbl:node(util_html.teamsVsAlign(team1, team2))
	end
	return tbl
end

function h.formatWinner(row)
	if not row.Winner then return nil end
	if row.Winner == '0' then
		return'<span style="font-size:80%">DRAW</span>'
	elseif row.Winner == '-1' then
		return 'FFs'
	else
		return util_html.makeNodeFromWikitext(m_team.onlyimagelinked(row['Team' .. row.Winner], { size = '48px'}))
	end
end

function h.formatCountdown(row)
	local lang = mw.getLanguage('en')
	local tbl = mw.html.create()
	if not row.TimeEntered then
		tbl:wikitext(lang:formatDate('d M',row.DateTime))
	else
		tbl:wikitext(util_time.countdown(row.DateTime))
	end
	
	if not row.Stream then
		return tbl
	else
		link = ('[%s %s]'):format(row.Stream, tostring(tbl))
		local linktbl = mw.html.create('span'):addClass('plainlinks'):wikitext(link)
		return linktbl
	end
end

return p