Module:TeamSchedule

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

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

local h = {}

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', 'MS.DateTime_UTC=UTC', 'MS.OverviewPage', 'MS.Winner', 'MS.Tab', 'MS.Round', 'MS.Team1Score', 'MS.Team2Score', 'MS.ShownName' },		where = ('(TR1._pageName="%s" OR TR2._pageName="%s") AND Winner IS NOT NULL'):format(team, team), orderBy = 'DateTime_UTC DESC', limit = 200, types = { Team1Score = 'number', Team2Score = 'number', Winner = '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.Team1) local winner = h.getWinner(row.Winner, side) local result = winner == 'Draw' and 'Draw' or winner and 'Win' or 'Loss' local isBO1 = h.isBO1(row.Team1Score, row.Team2Score) local tbl = { Date = util_time.dateInLocal(row.UTC), Event = ('%s'):format(row.OverviewPage, row.ShownName or ''), Round = row.Tab or row.Round 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 = ('%s - %s'):format(row.Team1Score or 'Missing Data', row.Team2Score or 'Missing Data'), 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 0) }	}	return tbl end

function h.getSide(team, team1) return lang:lcfirst(team) == lang:lcfirst(team1) and 1 or 2 end

function h.getWinner(winner, side) if winner == 0 then return 'Draw' end return winner == 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.printTable(processed) local tbl = mw.html.create('table') :addClass('wikitable') :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