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_cargo = require('Module:CargoUtil')
local util_form = require('Module:FormUtil')
local util_html = require('Module:HTMLUtil')
local util_time = require('Module:TimeUtil')
local m_team = require('Module:Team')
local settings = mw.loadData('Module:FrontpageMatchesSettings')

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

local h = {}
local p = {}
function p.main(frame)
	if frame == mw.getCurrentFrame() then
		args = require('Module:ProcessArgs').merge(true)
	else
		frame = mw.getCurrentFrame()
	end
	local limit = args.limit or 20
	local tbl = mw.html.create()
	for _, section in ipairs(settings.sections) do
		local result = h.doQuery(section, limit)
		tbl:tag('div')
			:addClass(settings[section].toggles.toggle)
			:node(h.makeTogglers(section))
			:wikitext(string.format(settings[section].sentence,limit))
			:node(h.makeSection(section, result))
		:done()
	end
	
	return tostring(tbl)
end

function h.doQuery(thissection, limit)
	local query = {
		tables = 'MatchSchedule',
		fields = {
			'Team1',
			'Team2',
			'Tab=Round',
			'DateTime_UTC=DateTime',
			'HasTime=TimeEntered',
			'OverviewPage=Tournament',
			'ShownName',
			'Winner',
			'Stream'
		},
		types = {
			TimeEntered = 'boolean'
		}
	}
	-- since you can't edit tables from mw.loadData
	for k, v in pairs(settings[thissection].cargo) do
		query[k] = v
	end
	query.limit = limit
	if thissection == 'Events' then
		query.where = util_cargo.makeMinMaxQuery(query, '_pageName', 'DateTime_UTC', 'MIN')
	end
	return util_cargo.queryAndCast(query, true)
end

function h.makeTogglers(thissection)
	-- creates the top row of the table where you can toggle between the three options
	-- this is called once per section
	local tbl = mw.html.create()
	div = tbl:tag('div')
		:addClass('titletabs-tabs')
	for _, section in ipairs(settings.sections) do
		div2 = div:tag('div')
		div2:addClass(settings[section].toggles.toggler)
			:addClass('titletabs-tab')
		if section == thissection then
			div2:addClass('titletabs-active')
		end
		div2:wikitext(settings[section].title):done()
	end
	return tbl
end

function h.makeSection(thissection, result)
	-- start table
	local tbl = mw.html.create()
	:tag('table')
		: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[thissection].sectiontitles[i])
	end
	-- rest of the table
	for n, row in ipairs(result) do
		row.N = n
		tbl:node(h.makeRow(row, thissection))
	end
	return tbl
end

function h.makeRow(row, thissection)
	local formatted = h.parseData(row)
	local tbl = mw.html.create('tr')
	for _, item in ipairs(settings[thissection].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]] [[File:Calendar Icon.png|12px|link=%s]]',
		tournament or '',
		shownname or '',
		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 team1 .. team2 == '' then
		tbl:wikitext(string.format("''%s''",round))
	else
		tbl:node(util_html.teamsVsAlign(team1, team2))
	end
	return tbl
end

function h.formatWinner(row)
	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 = '45px'}))
	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 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