Module:ScheduleHistoryH2HAbstract

local util_args = require('Module:ArgsUtil') local util_cargo = require("Module:CargoUtil") local util_html = require("Module:HtmlUtil") local util_map = require('Module:MapUtil') local util_table = require("Module:TableUtil") local util_text = require("Module:TextUtil") local util_time = require('Module:TimeUtil') local util_vars = require("Module:VarsUtil") local i18n = require("Module:I18nUtil") local lang = mw.getLanguage('en') local LCS = require('Module:LuaClassSystem') local lang = mw.getLanguage('en')

local m_team = require('Module:Team')

local p = LCS.class.abstract

p.COLUMNS = {} p.VODLIST = {} p.SIDES = { [0] = 'Blue', [1] = 'Red' }

local h = {}

function p:init(args) local entity1 = self:getEntity1(args) local entity2 = self:getEntity2(args) local query = self:getQuery(entity1, entity2, args) local result = util_cargo.queryAndCast(query) local processed = self:processData(result, entity1, entity2, util_args.castAsBool(args.errorincomplete)) return self:makeOutput(processed, entity1, entity2) end

function p:getEntity1(args) end function p:getEntity2(args) end

function p:getQuery(entity1, entity2, args) local tbl = { tables = self:getTables, join = self:getJoin, fields = self:getFields, where = self:getWhere(entity1, entity2, args), orderBy = 'MS.DateTime_UTC DESC', limit = args.limit or 200, types = { Team1Score = 'number', Team2Score = 'number' }	}	return tbl end function p:getTables end function p:getJoin end function p:getFields end function p:getWhere(entity1, entity2, args) end

function p:processData(result, entity1, entity2, errorincomplete) local processed = h.initializeProcessedTable(entity1, entity2) for i, row in ipairs(result) do		if h.validateRow(row, errorincomplete) then processed[#processed+1] = self:processRow(row, entity1, entity2) h.getTotals(processed.totals, row, processed[#processed].Blue, processed[#processed].Red) end end return processed end

function h.initializeProcessedTable(entity1, entity2) return { totals = { [entity1] = { games = 0, series = 0 }, [entity2] = { games = 0, series = 0 } }	} end

function h.validateRow(row, errorincomplete) if not errorincomplete then return row.Team1Score and row.Team2Score end if not row.Team1Score then h.errorMissingScore(row, 1) end if not row.Team2Score then h.errorMissingScore(row, 2) end return true end

function h.errorMissingScore(row, teamNumber) error(('Missing score on page %s %s, %s vs %s, team %s'):format( row._pageNameMS, row.Tab or 'unknown tab', row.Team1 or 'unknown Team 1', row.Team2 or 'unknown Team 2', teamNumber )) end

function p:processRow(row, entity1, entity2) local blue_color = row.Entity1 == entity1 and 'th2h-team0win' or 'th2h-team1win' local red_color = row.Entity2 == entity2 and 'th2h-team1win' or 'th2h-team0win' local tbl = { Blue = row.Entity1, Red = row.Entity2, 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'] = self:getEntityMarkup(row, 1), ['Team 2'] = self:getEntityMarkup(row, 2), Score = ('%s - %s'):format(row.Team1Score, row.Team2Score), VODs = self: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 p:getEntityMarkup(row, i) 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 p:makeOutput(processed, team1, team2) local output = mw.html.create self:printTable(output, processed, team1, team2) return output end

function p:printTable(output, processed, team1, team2) local tbl = output:tag('table') :addClass('wikitable') :attr('id', 'teamschedule-history-table') self:printTotals(tbl, processed.totals, team1, team2) util_html.printColspanHeader(tbl, 'Head-to-Head History', #self.COLUMNS) self: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(self.COLUMNS) do			tr:tag('td') :wikitext(row[col]) :addClass(row.classes[col]) end end end

function p:printTotals(tbl, totals, team1, team2) util_html.printColspanHeader(tbl, 'Series Totals', #self.COLUMNS, 'small-heading') self:addtotalRecord(tbl, team1, team2, totals[team1].series, totals[team2].series) util_html.printColspanHeader(tbl, 'Map Totals', #self.COLUMNS, 'small-heading') self:addtotalRecord(tbl, team1, team2, totals[team1].games, totals[team2].games) end

function p:addtotalRecord(tbl, team1, team2, score1, score2) local tr = tbl:tag('tr') local td = tbl:tag('td'):attr('colspan', #self.COLUMNS) td:node(		util_html.vsAlign( self:markupTotalEntity1(team1), self:markupTotalEntity2(team2), h.formatTotalScore(score1, score2) )	) end

function p:markupTotalEntity1(entity) end function p:markupTotalEntity2(entity) end

function h.formatTotalScore(score1, score2) return ('%s - %s'):format(score1, score2) end

function p:addHeading(tbl) local tr = tbl:tag('tr') for _, col in ipairs(self.COLUMNS) do		tbl:tag('th'):wikitext(col) end return end

return p