Module:MatchHistoryCaster

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 util_esports = require('Module:EsportsUtil') local m_team = require('Module:Team') local lang = mw.getLanguage('en')

local COLUMNS = { 'Date', 'Event', 'Round', 'Team 1', 'Team 2', 'Score', 'VODs', 'PBP', 'Color' }

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

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

local h = {}

local p = {} function p.main(frame) local args = util_args.merge(true) local caster = args[1] or mw.title.getCurrentTitle.baseText local query = h.getQuery(caster ) local result = util_cargo.queryAndCast(query) local processed = h.processData(result, caster) return h.printTable(processed) end

function h.getQuery(caster, args) local ret = { tables = h.tables, join = h.join, fields = h.fields, where = h.getWhere(caster, args), orderBy = 'MS.DateTime_UTC DESC', limit = 1000, types = { Team1Score = 'number', Team2Score = 'number', }	}	return ret end

h.tables = { 'MatchSchedule=MS', 'PlayerRedirects=PR', }

h.join = { 'MS.Casters HOLDS PR.AllName', }

h.fields = { 'MS.Team1=Team1', 'MS.Team2=Team2', 'MS.DateTime_UTC=Date', 'MS.OverviewPage=OverviewPage', 'MS.Winner=Winner', 'MS.Tab=Tab', 'MS.ShownRound=ShownRound', 'MS.Team1Score=Team1Score', 'MS.Team2Score=Team2Score', 'MS.ShownName=ShownName', 'MS.CastersPBP=PBP', 'MS.CastersColor=Color', 'MS.Vod=Vod', 'MS.Vod2=Vod2', 'MS.Vod3=Vod3', 'MS.Vod4=Vod4', 'MS.Vod5=Vod5', 'MS.Vod6=Vod6', 'MS.Vod7=Vod7', }

function h.getWhere(caster, args) local tbl = { ('PR._pageName="%s"'):format(caster), ('MS.OverviewPage IS NOT NULL'), }	return util_cargo.concatWhere(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, caster) local winner = h.getWinner(row.Winner, side) local tbl = { Date = util_time.dateInLocal(row.Date), Event = ('%s'):format(row.OverviewPage, row.ShownName or ''), Round = row.ShownRound or row.Tab or '', PBP = util_esports.playersLinked(row.PBP), Color = util_esports.playersLinked(row.Color), ['Team 1'] = m_team.rightshortlinked(row.Team1), ['Team 2'] = m_team.rightshortlinked(row.Team2), Score = ('%s - %s'):format(row.Team1Score or 'Missing Data', row.Team2Score or 'Missing Data'), VODs = h.makeMatchVodList(row), classes = { ['Team 1'] = row.Winner == '1' and 'match-history-win' or '', ['Team 2'] = row.Winner == '2' and 'match-history-win' 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.getWinner(winner, side) if winner == '0' then return 'Draw' elseif side == 0 then return winner == '1' else return winner == '2' end 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