Hello there! We are conducting a survey to better understand the user experience in making a first edit. If you have ever made an edit on Gamepedia, please fill out the survey. Thank you!

Module:PickBanScore

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search
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 CONCAT_GAME = 'CONCAT(PB.OverviewPage,"_",PB.Tab,"_",PB.N_MatchInTab,"_",PB.N_GameInMatch)'
local CONCAT_MATCH = 'CONCAT(MSG.OverviewPage,"_",MS.Tab,"_",MSG.N_MatchInTab)'

local h = {}

local p = {}
function p.main(frame)
	local args = util_args.merge(true)
	local overviewPage = args.page
	if not overviewPage then
		error('An overview page is required')
	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.UniqueMatch=MSG.UniqueMatch',
		fields = {
			'PB._pageName=_pageName',
			CONCAT_GAME .. '=GameID',
			CONCAT_MATCH .. '=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.UniqueMatch=MSG.UniqueMatch',
		fields = {
			'MSG.Blue=Team1',
			'MSG.Red=Team2',
			'MSG.Winner=Winner'
		},
		types = {
			Winner = 'number'
		},
	}
	return query
end

function h.getSeriesScoreWhere(matchID, N)
	local tbl = {
		('%s="%s"'):format(CONCAT_MATCH, 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 win_team = game['Team' .. game.Winner]
		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.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