Module:TeamScheduleH2H

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 m_team = require('Module:Team')

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

local VODLIST = { 'Vod', 'Vod2', 'Vod3', 'Vod4', 'Vod5', 'Vod6', 'Vod7' }

local h = {}

local i18n = { Vod = 'Vod', Vod2 = 'Vod2', Vod3 = 'Vod3', Vod4 = 'Vod4', Vod5 = 'Vod5', Vod6 = 'Vod6', Vod7 = 'Vod7', Team1 = 'Team 1', Team2 = 'Team 2', Selection = 'Side Sel', header = 'VODs & Match Links', Highlights = 'HL', }

function h.query(team1, team2, limit) local tbl = { tables = 'MatchSchedule', fields = { 'Team1', 'Team2', 'DateTime_UTC=UTC', 'OverviewPage', 'Winner', 'Tab', 'ShownRound', 'Round', 'Team1Score', 'Team2Score', 'ShownName', 'Vod', 'Vod2', 'Vod3', 'Vod4', 'Vod5', 'Vod6', 'Vod7' },		where = ('((Team1="%s" AND Team2="%s") OR (Team1="%s" AND Team2="%s")) AND Winner IS NOT NULL'):format(			team1,			team2,			team2,			team1		), orderBy = 'DateTime_UTC DESC', limit = limit or 200, types = { Team1Score = 'number', Team2Score = 'number' }	}	return tbl end

function h.processData(result, team1, team2) local processed = { totals = { [team1] = { games = 0, series = 0 }, [team2] = { games = 0, series = 0 } }	}	for i, row in ipairs(result) do		processed[i] = h.processRow(row, team1, team2) h.getTotals(processed.totals, row, processed[i].Blue, processed[i].Red) end return processed end

function h.processRow(row, team1, team2) local blue_link = m_team.teamlinkname(row.Team1) local red_link = m_team.teamlinkname(row.Team2) local blue_color = blue_link == team1 and 'th2h-team0win' or 'th2h-team1win' local red_color = red_link == team1 and 'th2h-team0win' or 'th2h-team1win' local tbl = { Blue = blue_link, Red = red_link, Date = util_time.dateInLocal(row.UTC), Event = ('%s'):format(row.OverviewPage, row.ShownName or ''), Round = row.ShownRound or row.Tab or row.Round or '', ['Team 1'] = m_team.rightmediumlinked(row.Team1), ['Team 2'] = m_team.rightmediumlinked(row.Team2), Score = ('%s - %s'):format(row.Team1Score, row.Team2Score), VODs = h.makeMatchVodList(row), classes = { ['Team 1'] = row.Winner == '1' and blue_color or '', ['Team 2'] = row.Winner == '2' and red_color or '' }	}	return tbl 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, ' &#8226; ') 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 hoverable-rows') :attr('id', 'teamschedule-history-table') h.addTotals(tbl, processed.totals, team1, team2) util_html.printColspanHeader(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.printColspanHeader(tbl, 'Series Totals', #COLUMNS, 'small-heading') h.addtotalRecord(tbl, team1, team2, totals[team1].series, totals[team2].series) util_html.printColspanHeader(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_team.leftmedium(team1), m_team.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

local p = {} function p.main(frame) local args = util_args.merge(true) local team1, team2 = m_team.teamlinkname(args[1]), m_team.teamlinkname(args[2]) local query = h.query(team1, team2, args.limit) local result = util_cargo.queryAndCast(query) local processed = h.processData(result, team1, team2) return h.makeOutput(processed, team1, team2) end return p