Module:TeamSchedule

From Call of Duty Esports Wiki
Jump to: navigation, search

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_time = require('Module:TimeUtil')
local util_vars = require("Module:VarsUtil")
local m_team = require('Module:Team')
local lang = mw.getLanguage('en')

local COLUMNS = { 'Date', 'Event', 'Round', 'Team 1', 'Team 2', 'Result', 'Score', 'VODs' }
local SIDES = { [1] = 'Blue', [2] = 'Red' }
local VODLIST = { 'Vod', 'Vod2', 'Vod3', 'Vod4', 'Vod5', 'Vod6', 'Vod7' }

local h = {}

local i18n = {
	Vod = 'Vod',
	Vod2 = '2',
	Vod3 = '3',
	Vod4 = '4',
	Vod5 = '5',
	Vod6 = '6',
	Vod7 = '7',
}

local p = {}
function p.main(frame)
	local args = util_args.merge()
	local team = args.team and m_team.teamlinkname(args.team) or mw.title.getCurrentTitle().baseText
	local query = h.query(team)
	local result = util_cargo.queryAndCast(query)
	local processed = h.processData(result, team)
	return h.printTable(processed)
end

function h.query(team)
	local tbl = {
		tables = {
			'MatchSchedule=MS',
			'TeamRedirects=TR1',
			'TeamRedirects=TR2',
		},
		join = {
			'MS.Team1=TR1.AllName',
			'MS.Team2=TR2.AllName',
		},
		fields = {
			'MS.Team1',
			'MS.Team2',
			'TR1._pageName=Team1Page',
			'TR2._pageName=Team2Page',
			'MS.DateTime_UTC=UTC',
			'MS.OverviewPage',
			'MS.Winner',
			'MS.Tab',
			'MS.Round',
			'MS.ShownRound',
			'MS.Team1Score',
			'MS.Team2Score',
			'MS.ShownName',
			'MS.Vod',
			'MS.Vod2',
			'MS.Vod3',
			'MS.Vod4',
			'MS.Vod5',
			'MS.Vod6',
			'MS.Vod7',
			'MS.WinnerScoreUnknown',
		},
		where = ('(TR1._pageName="%s" OR TR2._pageName="%s") AND (WinnerScoreUnknown IS NOT NULL OR Winner IS NOT NULL)'):format(team, team),
		orderBy = 'DateTime_UTC DESC',
		limit = 1000,
		types = {
			Team1Score = 'number',
			Team2Score = 'number',
			Winner = 'number',
			WinnerScoreUnknown = 'number',
		}
	}
	return tbl
end

function h.processData(result, team)
	local processed = {}
	for i, row in ipairs(result) do
		processed[i] = h.processRow(row, team)
	end
	return processed
end

function h.processRow(row, team)
	local side = h.getSide(team, row.Team1Page, row.Team2Page)
	local winner = h.getWinner(row.Winner, side)
	local winnerScoreUnknown = h.getWinner(row.WinnerScoreUnknown, side)
	local result = winner == 'Draw' and 'Draw' or winner and 'Win' or winnerScoreUnknown and 'Win' or 'Loss'
	local isBO1 = h.isBO1(row.Team1Score, row.Team2Score)
	local tbl = {
		Date = util_time.dateInLocal(row.UTC),
		Event = ('[[%s|%s]]'):format(row.OverviewPage, row.ShownName or ''),
		Round = row.ShownRound or row.Tab or '',
		['Team 1'] = m_team.rightmediumlinked(row.Team1),
		['Team 2'] = m_team.rightmediumlinked(row.Team2),
		Side = isBO1 and SIDES[side] or 'Series',
		Result = result,
		Score = h.isScoreKnown(row),
	    VODs = h.makeMatchVodList(row),
		classes = {
			('teamschedule-side-%s'):format(isBO1 and side or 'Series'),
			('teamschedule-result-%s'):format(result),
			Side = 'standings-mhside' .. (isBO1 and side or 'Series'),
			Result = 'standings-mhwl' .. (winner == 'Draw' and '' or winner and 1 or winnerScoreUnknown and 1 or 0)
		}
	}
	return tbl
end

function h.getSide(team, team1, team2)
	if team1 and not team2 then return 1 end
	if team2 and not team1 then return 2 end
	if team1 and mw.ustring.lower(team1) == mw.ustring.lower(team) then
		return 1
	end
	if team2 and mw.ustring.lower(team2) == mw.ustring.lower(team) then
		return 2
	end
	error("team seems not to be in match")
end

function h.getWinner(winner, side)
	if winner == 0 then
		return 'Draw'
	end
	return winner == side
end

function h.getWinnerScoreUnknown(winnerScoreUnknown, side)
	if winnerScoreUnknown == 0 then
		return 'Draw'
	end
	return winnerScoreUnknown == side
end

function h.isBO1(score1, score2)
	score1 = score1 or 0
	score2 = score2 or 0
	local max = math.max(score1, score2)
	local total = score1 + score2
	return max == 1 and total == 1
end

function h.isScoreKnown(row)
	if not row.WinnerScoreUnknown then
		return ('%s - %s'):format(row.Team1Score or 'Missing Data', row.Team2Score or 'Missing Data')
	elseif row.WinnerScoreUnknown == 1 then
		return 'W - L'
	elseif row.WinnerScoreUnknown == 2 then
		return 'L - W'
	end
end

function h.makeMatchVodList(row)
	local tbl = {}
	for _, v in ipairs(VODLIST) do
		if row[v] then
			tbl[#tbl+1] = ('[%s %s]'):format(row[v], i18n[v])
		end
	end
	return table.concat(tbl, ' • ')
end

function h.printTable(processed)
	local tbl = mw.html.create('table')
		:addClass('wikitable hoverable-rows')
		:attr('id', 'teamschedule-history-table')
	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
	return tbl
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