Leaguepedia | League of Legends Esports Wiki
Advertisement
Leaguepedia | League of Legends Esports Wiki
To edit the documentation or categories for this module, click here.
local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_vars = require('Module:VarsUtil')

local h = {}

local p = {}
function p.main(frame)
	local args = util_args.merge()
	local overviewPage = args.page
	if not overviewPage then
		error('An overview page is required (args.page)')
	end
	local missingGames = h.doMissingGamesQuery(overviewPage)
	h.getSeriesScores(missingGames)
	return h.makeOutput(missingGames)
end

function h.doMissingGamesQuery(page)
	local query = h.getMissingGamesQuery(page)
	return util_cargo.queryAndCast(query)
end

function h.getMissingGamesQuery(page)
	local query = {
		tables = 'MatchSchedule=MS,MatchScheduleGame=MSG,PicksAndBansS7=PB',
		join = 'MSG.OverviewPage = PB.OverviewPage, MS.MatchId=MSG.MatchId',
		fields = {
			'PB._pageName=_pageName',
			'PB.GameId=GameID',
			'PB.MatchId=MatchID',
			'MSG.N_GameInMatch=N_GameInMatch',
			'PB.Team1=Team1',
			'PB.Team2=Team2',
			'PB.N_GameInPage=N',
			'MSG.Winner=Winner',
			'MS.BestOf=BestOf',
			'MS.Winner=SeriesWinner',
		},
		where = h.getMissingGamesWhere(page),
		orderBy = 'MSG.N_Page, MSG.N_TabInPage, MSG.N_MatchInTab, MSG.N_GameInMatch'
	}
	return query
end

function h.getMissingGamesWhere(page)
	local tbl = {
		('PB.OverviewPage="%s"'):format(page),
		'MSG.OverviewPage=PB.OverviewPage',
		'MS.Tab=PB.Tab',
		'MS.N_MatchInTab=PB.N_MatchInTab',
		'MSG.N_GameInMatch=PB.N_GameInMatch',
		-- null teams sometimes store 'TBD' and sometimes NULL so just exclude both
		'(PB.Team1 IS NOT NULL AND PB.Team2 IS NOT NULL)',
		'(PB.Team1 != "TBD" AND PB.Team2 != "TBD")',
		'PB.Winner IS NULL',
		'MSG.Winner IS NOT NULL',
		'PB.Team1Pick1 IS NOT NULL' -- leave unfilled games blank still
	}
	return util_cargo.concatWhere(tbl)
end

function h.getSeriesScores(missingGames)
	for _, row in ipairs(missingGames) do
		results = h.doSeriesScoreQuery(row.MatchID, row.N_GameInMatch)
		h.processResults(row, results)
	end
end

function h.doSeriesScoreQuery(matchID, N)
	local query = h.getSeriesScoreQuery(matchID, N)
	return util_cargo.queryAndCast(query)
end

function h.getSeriesScoreQuery(matchID, N)
	local query = {
		tables = 'MatchScheduleGame=MSG,MatchSchedule=MS',
		where = h.getSeriesScoreWhere(matchID, N),
		join = 'MS.MatchId=MSG.MatchId',
		fields = {
			'MSG.Blue=Team1',
			'MSG.Red=Team2',
			'MSG.Winner=Winner [number]'
		},
	}
	return query
end

function h.getSeriesScoreWhere(matchID, N)
	local tbl = {
		('MSG.MatchId="%s"'):format(matchID),
		('MSG.N_GameInMatch <= "%s"'):format(N)
	}
	return util_cargo.concatWhere(tbl)
end

function h.processResults(row, results)
	local scores = { [row.Team1] = 0, [row.Team2] = 0 }
	for _, game in ipairs(results) do
		local res, win_team = pcall(h.getWinner, game)
		if not res then
			error(('Error in match order detected. MatchSchedule: %s vs %s. PickBan: %s vs %s'):format(
				row.Team1 or '???',
				row.Team2 or '???',
				game.Team1,
				game.Team2
			))
		end
		-- util_vars.log('!!!!!!!!!!!!')
		-- util_vars.log(scores)
		-- util_vars.log('Game: ')
		-- util_vars.log(game)
		scores[win_team] = scores[win_team] + 1
	end
	row.Team1Score = scores[row.Team1]
	row.Team2Score = scores[row.Team2]
	row.Blue = results[1].Team1
	row.Red = results[1].Team2
	row.BestOf = row.BestOf or 'undefined'
	row.SeriesWinner = row.SeriesWinner or 'undefined'
end

function h.getWinner(game)
	return game['Team' .. game.Winner]
end

function h.makeOutput(missingGames)
	local tbl = {}
	for _, row in ipairs(missingGames) do
		thisgame = {
			row._pageName,
			row.N,
			row.Team1Score,
			row.Team2Score,
			row.Winner,
			row.BestOf,
			row.SeriesWinner,
			row.Team1,
			row.Team2,
			row.Blue or '',
			row.Red or ''
		}
		tbl[#tbl+1] = table.concat(thisgame,',')
	end
	local output = table.concat(tbl,';')
	return '!*****' .. output .. '*****'
end
return p
Advertisement