Leaguepedia | League of Legends Esports Wiki
Advertisement
Leaguepedia | League of Legends Esports Wiki

Documentation for this module may be created at Module:ScheduleHistoryH2HAbstract/doc

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 LCS = require('Module:LuaClassSystem')

local p = LCS.class.abstract()

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

local h = {}

function p:init(arg1, arg2)
	self.entity1 = self:getEntity(arg1)
	self.entity2 = self:getEntity(arg2)
	self.entityLiteral1 = nil
	self.entityLiteral2 = nil
end

function p:run(args)
	local query = self:getQuery(args)
	local result = util_cargo.queryAndCast(query)
	local processed = self:processData(
		result,
		self.entityLiteral1,
		self.entityLiteral2,
		util_args.castAsBool(args.errorincomplete)
	)
	return self:makeOutput(processed, self.entityLiteral1, self.entityLiteral2)
end

function p:getEntity(arg) end

function p:getQuery(args)
	local tbl = {
		tables = self:getTables(),
		join = self:getJoin(),
		fields = self:getFields(),
		where = self:getWhere(args),
		orderBy = 'MS.DateTime_UTC DESC',
		limit = args.limit or 200,
	}
	return tbl
end
	
function p:getTables() end
function p:getJoin() end
function p:getFields() end
function p:getWhere(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)
			-- util_vars.log(entity1)
			-- util_vars.log(entity2)
			-- util_vars.log(processed[#processed].Blue)
			-- util_vars.log(processed[#processed].Red)
			h.getTotals(processed.totals, row, processed[#processed].Blue, processed[#processed].Red)
		end
	end
	return processed
end

function h.initializeProcessedTable(entity1, entity2)
	return {
		totals = {
			[util_text.ucfirst(entity1)] = { games = 0, series = 0 },
			[util_text.ucfirst(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 == util_text.ucfirst(entity1) and 'th2h-team0win' or 'th2h-team1win'
	local red_color = row.Entity2 == util_text.ucfirst(entity2) and 'th2h-team1win' or 'th2h-team0win'
	local tbl = {
		Blue = row.Entity1,
		Red = row.Entity2,
		Date = util_time.dateInLocal(row.UTC),
		Event = ('[[%s|%s]]'):format(row.OverviewPage, row.ShownName or ''),
		Round = 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),
		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)
	if not blue or not red then
		error(('Incomplete data for match %s - Page: %s'):format(row.MatchId, row.MSPage))
	end
	blue = util_text.ucfirst(blue)
	red = util_text.ucfirst(red)
	if not totals[blue] or not totals[red] then
		error(('Wrong data for match %s - Page: %s'):format(row.MatchId, row.MSPage))
	end
	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)
	local ucTeam1 = util_text.ucfirst(team1)
	local ucTeam2 = util_text.ucfirst(team2)
	util_html.printColspanHeader(tbl, 'Series Totals', #self.COLUMNS, 'small-heading')
	self:addtotalRecord(tbl, team1, team2, totals[ucTeam1].series, totals[ucTeam2].series)
	util_html.printColspanHeader(tbl, 'Game Totals', #self.COLUMNS, 'small-heading')
	self:addtotalRecord(tbl, team1, team2, totals[ucTeam1].games, totals[ucTeam2].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(),
			self:markupTotalEntity2(),
			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
Advertisement