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

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

local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_map = require("Module:MapUtil")
local util_table = require('Module:TableUtil')
local util_title = require('Module:TitleUtil')
local util_vars = require('Module:VarsUtil')
local i18n = require('Module:i18nUtil')
local Stats = require('Module:ScoreboardPlayerStats')

local h = {}
local p = {}
function p.main(frame)
	local args = util_args.merge()
	args.link = args.player or mw.title.getCurrentTitle().rootText
	local tournaments = h.getTournaments(args)
	return h.makeOutput(tournaments, args)
end

-- get data
function h.getTournaments(args)
	local query = h.getQuery(args)
	local data = util_cargo.queryAndCast(query)
	return h.groupDataByRegion(data)
end

function h.getQuery(args)
	local ret = {
		tables = { 'ScoreboardPlayers=SP', 'PlayerRedirects=PR', 'Tournaments=IT' },
		join = { 'SP.OverviewPage=IT.OverviewPage', 'SP.Link=PR.AllName' },
		where = h.getWhere(args),
		fields = {
			'IT.StandardName=Event',
			'IT.Region=Region'
		},
		groupBy = 'SP.OverviewPage',
		orderBy = 'SP.DateTime_UTC',
	}
	return ret
end

function h.getWhere(args)
	local tbl = {
		('PR._pageName="%s"'):format(args.link),
		('IT.Year="%s"'):format(args.year or util_title.titleparts(nil, 1, 3))
	}
	return util_cargo.concatWhere(tbl)
end

-- process data
function h.groupDataByRegion(data)
	local tournaments = {}
	for _, row in ipairs(data) do
		h.processRow(tournaments, row)
	end
	return tournaments
end

function h.processRow(tournaments, row)
	local key = h.pickKey(row.Region)
	if not tournaments[key] then tournaments[key] = {} end
	tournaments[key][#tournaments[key] + 1] = row
end

function h.pickKey(region)
	return region == 'International' and 'International' or 'Domestic'
end

-- make output
function h.makeOutput(tournaments, args)
	local output = mw.html.create()
	if util_table.generalLength(tournaments) == 2 then
		for _, key in ipairs({ 'Domestic', 'International' }) do
			h.printHeading(output, key)
			h.printTournaments(output, tournaments[key], args)
		end
	elseif next(tournaments) then
		h.printTournaments(output, tournaments[next(tournaments)], args)
	else
		output:wikitext('No results found[[Category:Empty ScoreboardPlayerStats]]')
	end
	return output
end

function h.printHeading(output, key)
	output:tag('h2'):wikitext(key)
end

function h.printTournaments(output, tournaments, args)
	for _, row in ipairs(tournaments) do
		local statsArgs = mw.clone(args)
		statsArgs.preload = 'PlayerByChampion'
		statsArgs.tournament = row.Event
		util_map.inPlace({Stats:_main(statsArgs)}, h.printStatsNodes, output)
	end
end

function h.printStatsNodes(node, output)
	output:node(node)
end

return p
Advertisement