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', 'Result', 'Score' } local SIDES = { [0] = 'Blue', [1] = 'Red' }

local h = {}

function h.query(team) local tbl = { tables = 'MatchSchedule', fields = { 'Team1', 'Team2', 'DateTime_UTC=UTC', 'OverviewPage', 'Winner', 'Tab', 'ShownRound', 'Team1Score', 'Team2Score', 'ShownName' },		where = ('(Team1="%s" OR Team2="%s") AND Winner IS NOT NULL'):format(team, team), orderBy = 'DateTime_UTC DESC', limit = 1000, types = { Team1Score = 'number', Team2Score = '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.ShownRound or row.Tab or '', ['Team 1'] = m_team.rightmediumlinked(row.Team1), ['Team 2'] = m_team.rightmediumlinked(row.Team2), Result = result, Score = ('%s - %s'):format(row.Team1Score or 'Missing Data', row.Team2Score or 'Missing Data'), classes = { ('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 0 or 1 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.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 sortable 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

local p = {} function p.main(frame) local args = util_args.merge(true) 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 return p