Module:TeamSchedule/RegionH2H

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

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_html = require('Module:HTMLUtil')
local util_time = require('Module:TimeUtil')
local util_vars = require('Module:VarsUtil')
local m_team = require('Module:Team')
local m_region = require('Module:Region')

local COLUMNS = { 'Date', 'Event', 'Round', 'Team 1', 'Team 2', 'Score' }
local SIDES = { [0] = 'Blue', [1] = 'Red' }

local h = {}
local p = {}
function p.main(frame)
	local args = util_args.merge(true)
	local region1 = m_region.long(args[1])
	local region2 = m_region.long(args[2])
	local query = h.query(region1, region2, args)
	local result = util_cargo.queryAndCast(query)
	local processed = h.processData(result, region1, region2, util_args.castAsBool(args.errorincomplete))
	return h.makeOutput(processed, region1, region2)
end

function h.query(region1, region2, args)
	local tbl = {
		tables = { 'MatchSchedule=MS', 'InfoboxTeam=IT1', 'InfoboxTeam=IT2' },
		join = {
			'MS.Team1=IT1._pageName',
			'MS.Team2=IT2._pageName'
		},
		fields = {
			'MS._pageName=_pageNameMS',
			'MS.Team1=Team1',
			'MS.Team2=Team2',
			'MS.DateTime_UTC=UTC',
			'MS.OverviewPage=OverviewPage',
			'MS.Winner=Winner',
			'MS.Tab=Tab',
			'MS.Round=Round',
			'MS.Team1Score=Team1Score',
			'MS.Team2Score=Team2Score',
			'MS.ShownName=ShownName',
			'IT1.Region=Region1',
			'IT2.Region=Region2'
		},
		where = h.getWhere(region1, region2, args),
		orderBy = 'MS.DateTime_UTC DESC',
		limit = args.limit or 200,
		types = {
			Team1Score = 'number',
			Team2Score = 'number'
		}
	}
	return tbl
end

function h.getWhere(region1, region2, args)
	local tbl = {
		h.getWhereRegionCondition(region1, region2),
		util_cargo.whereFromArg('MS.OverviewPage RLIKE "%s"', args.forceinclude),
		util_cargo.whereFromArg('MS.OverviewPage NOT RLIKE "%s"', args.exclude)
	}
	return util_cargo.concatWhere(tbl)
end

function h.getWhereRegionCondition(region1, region2)
	local tbl = {
		('(IT1.Region="%s" AND IT2.Region="%s")'):format(region1, region2),
		('(IT1.Region="%s" AND IT2.Region="%s")'):format(region2, region1),
	}
	return util_cargo.concatWhereOr(tbl)
end

function h.processData(result, region1, region2, errorincomplete)
	local processed = h.initializeProcessedTable(region1, region2)
	for i, row in ipairs(result) do
		if h.validateRow(row, errorincomplete) then
			processed[#processed+1] = h.processRow(row, region1, region2)
			h.getTotals(processed.totals, row, processed[#processed].Blue, processed[#processed].Red)
		end
	end
	return processed
end

function h.initializeProcessedTable(region1, region2)
	return {
		totals = {
			[region1] = { games = 0, series = 0 },
			[region2] = { games = 0, series = 0 }
		}
	}
end

function h.validateRow(row, errorincomplete)
	if not errorincomplete then
		return row.Team1Score and row.Team2Score
	end
	if not row.Team1Score then
		h.errorMissingScore(row, 1)
	end
	if not row.Team2Score then
		h.errorMissingScore(row, 2)
	end
	return true
end

function h.errorMissingScore(row, teamNumber)
	error(('Missing score on page %s %s, %s vs %s, team %s'):format(
		row._pageNameMS,
		row.Tab or 'unknown tab',
		row.Team1 or 'unknown Team 1',
		row.Team2 or 'unknown Team 2',
		teamNumber
	))
end

function h.processRow(row, region1, region2)
	local blue_color = row.Region1 == region1 and 'th2h-team0win' or 'th2h-team1win'
	local red_color = row.Region2 == region2 and 'th2h-team0win' or 'th2h-team1win'
	local tbl = {
		Blue = row.Region1,
		Red = row.Region2,
		Date = util_time.dateInLocal(row.UTC),
		Event = ('[[%s|%s]]'):format(row.OverviewPage, row.ShownName or ''),
		Round = row.Tab or row.Round or '',
		['Team 1'] = m_region.onlyimage(row.Region1) .. m_team.rightmediumlinked(row.Team1),
		['Team 2'] = m_region.onlyimage(row.Region2) .. m_team.rightmediumlinked(row.Team2),
		Score = ('%s - %s'):format(row.Team1Score, row.Team2Score),
		classes = {
			['Team 1'] = row.Winner == '1' and blue_color or '',
			['Team 2'] = row.Winner == '2' and red_color or ''
		}
	}
	return tbl
end

function h.getTotals(totals, row, blue, red)
	totals[blue].games = totals[blue].games + row.Team1Score
	totals[red].games = totals[red].games + row.Team2Score
	totals[blue].series = totals[blue].series + (row.Winner == '1' and 1 or 0)
	totals[red].series = totals[red].series + (row.Winner == '2' and 1 or 0)
end

function h.makeOutput(processed, team1, team2)
	local output = mw.html.create()
	h.printTable(output, processed, team1, team2)
	return output
end

function h.printTable(output, processed, team1, team2)
	local tbl = output:tag('table')
		:addClass('wikitable')
		:attr('id', 'teamschedule-history-table')
	h.addTotals(tbl, processed.totals, team1, team2)
	util_html.makeColspanHeader(tbl, 'Head-to-Head History', #COLUMNS)
	h.addHeading(tbl)
	for _, row in ipairs(processed) do
		local tr = tbl:tag('tr')
		for _, class in ipairs(row.classes) do
			tr:addClass(class)
		end
		for _, col in ipairs(COLUMNS) do
			tr:tag('td')
				:wikitext(row[col])
				:addClass(row.classes[col])
		end
	end
end

function h.addTotals(tbl, totals, team1, team2)
	util_html.makeColspanHeader(tbl, 'Series Totals', #COLUMNS, 'small-heading')
	h.addtotalRecord(tbl, team1, team2, totals[team1].series, totals[team2].series)
	util_html.makeColspanHeader(tbl, 'Game Totals', #COLUMNS, 'small-heading')
	h.addtotalRecord(tbl, team1, team2, totals[team1].games, totals[team2].games)
end

function h.addtotalRecord(tbl, team1, team2, score1, score2)
	local tr = tbl:tag('tr')
	local td = tbl:tag('td'):attr('colspan', #COLUMNS)
	td:node(util_html.vsAlign(m_region.leftmedium(team1) .. ' ', ' ' .. m_region.rightmedium(team2), h.formatTotalScore(score1, score2)))
end

function h.formatTotalScore(score1, score2)
	return ('%s - %s'):format(score1, score2)
end

function h.addHeading(tbl)
	local tr = tbl:tag('tr')
	for _, col in ipairs(COLUMNS) do
		tbl:tag('th'):wikitext(col)
	end
	return
end

return p