Module:PickBanHistory

From Leaguepedia | League of Legends Esports Wiki
Jump to: navigation, search

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

local lang = mw.getLanguage('en')
local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_form = require('Module:FormUtil')
local util_esports = require('Module:EsportsUtil')
local util_table = require('Module:TableUtil')
local m_team = require('Module:Team')
local Sprite = require('Module:Sprite').spriteImage
local FIELDS = {
	'Team1Role1',
	'Team1Role2',
	'Team1Role3',
	'Team1Role4',
	'Team1Role5',
	'Team2Role1',
	'Team2Role2',
	'Team2Role3',
	'Team2Role4',
	'Team2Role5',
	'Team1Ban1',
	'Team1Ban2',
	'Team1Ban3',
	'Team1Ban4',
	'Team1Ban5',
	'Team1Pick1',
	'Team1Pick2',
	'Team1Pick3',
	'Team1Pick4',
	'Team1Pick5',
	'Team2Ban1',
	'Team2Ban2',
	'Team2Ban3',
	'Team2Ban4',
	'Team2Ban5',
	'Team2Pick1',
	'Team2Pick2',
	'Team2Pick3',
	'Team2Pick4',
	'Team2Pick5',
	'Team1',
	'Team2',
	'Winner',
	'Team1Score',
	'Team2Score',
	'OverviewPage',
	'Tab',
	'_pageName'
}
local CELLORDER = {
	{ 'Team1Ban1', classes = { 'pbh-ban', 'pbh-blue', 'pbh-cell' } },
	{ 'Team2Ban1', classes = { 'pbh-ban', 'pbh-red', 'pbh-cell' } },
	{ 'Team1Ban2', classes = { 'pbh-ban', 'pbh-blue', 'pbh-cell' } },
	{ 'Team2Ban2', classes = { 'pbh-ban', 'pbh-red', 'pbh-cell' } },
	{ 'Team1Ban3', classes = { 'pbh-ban', 'pbh-blue', 'pbh-cell' } },
	{ 'Team2Ban3', classes = { 'pbh-ban', 'pbh-red', 'pbh-cell' } },
	{ 'Team1Pick1', classes = { 'pbh-blue', 'pbh-cell' }, mandatory = true },
	{ 'Team2Pick1', 'Team2Pick2', classes = { 'pbh-red', 'pbh-cell' }, mandatory = true },
	{ 'Team1Pick2', 'Team1Pick3', classes = { 'pbh-blue', 'pbh-cell' }, mandatory = true },
	{ 'Team2Pick3', classes = { 'pbh-red', 'pbh-cell' }, mandatory = true },
	{ 'Team2Ban4', classes = { 'pbh-ban', 'pbh-red', 'pbh-cell' } },
	{ 'Team1Ban4', classes = { 'pbh-ban', 'pbh-blue', 'pbh-cell' } },
	{ 'Team2Ban5', classes = { 'pbh-ban', 'pbh-red', 'pbh-cell' } },
	{ 'Team1Ban5', classes = { 'pbh-ban', 'pbh-blue', 'pbh-cell' } },
	{ 'Team2Pick4', classes = { 'pbh-red', 'pbh-cell' }, mandatory = true },
	{ 'Team1Pick4', 'Team1Pick5', classes = { 'pbh-blue', 'pbh-cell' }, mandatory = true },
	{ 'Team2Pick5', classes = { 'pbh-red', 'pbh-cell' }, mandatory = true }
}
local HEADERS = { 'Phase', 'Blue', 'Red', 'Score', 'BB1', 'RB1', 'BB2', 'RB2', 'BB3', 'RB3', 'BP1', 'RP1-2', 'BP2-3', 'RP3', 'RB4', 'BB4', 'RB5', 'BB5', 'RP4', 'BP4-5', 'RP5' }
local ROLE_HEADERS = { 'BR1', 'BR2', 'BR3', 'BR4', 'BR5', 'RR1', 'RR2', 'RR3', 'RR4', 'RR5' }
local COLORS = { 'red', 'blue', 'green', 'yellow' }
local MESSAGE = [[:''Unlike some other charts, this page is based on data that was manually entered to the wiki, and as such it is possible that there are some mistakes. If you notice any, please report it to wiki staff!''
:''Select a highlighting color to the right and then click a champion to toggle highlighting on-off.''
]]

local FORM_INFO = { form = 'PickBanHistory', template = 'PBH' }

local h = {}
local p = {}

function p.main(frame)
	local args = util_args.merge(true)
	local overviewPage = util_esports.getOverviewPage(args.page, frame)
	if not overviewPage then return '' end
	local alias = h.getDisplayText(overviewPage)
	local pretty = lang:lc(args.textonly or '') ~= 'yes'
	local event = args.display or alias or args.overviewPage
	local result = util_cargo.queryAndCast(h.makeQuery(overviewPage, args))
	if not next(result) then return '' end
	local formatted = h.formatResults(result, pretty)
	local output = mw.html.create()
	output:wikitext(MESSAGE)
	h.printTable(output,formatted, event, pretty)
	if pretty then output:node(h.printColorPicker()) end
	return output
end

function h.ChampionSprite(id)
	return Sprite{
		id,
		size = '25',
		type = 'Champion',
		notext = true,
		nolink = true,
		nosize = true
	}
end

function h.getDisplayText(page)
	local query = {
		tables = 'InfoboxTournament',
		fields = 'Name',
		where = string.format('_pageName="%s"', page)
	}
	return util_cargo.getOneResult(query)
end

function h.makeQuery(page, args)
	return {
		tables = 'PicksAndBansS7',
		fields = FIELDS,
		where = h.makeWhere(page, args),
		orderBy = 'N_Page DESC,N_GameInPage DESC',
		limit = 9999,
		groupBy = 'UniqueLine'
	}
end

function h.makeWhere(page, args)
	local team = util_args.nilToFalse(args.team) and m_team.teamlinkname(args.team)
	local tbl = {
		string.format('OverviewPage="%s"', page),
		'Winner IS NOT NULL',
		util_cargo.whereFromArg('N_Page >= "%s"', args.pagestart),
		util_cargo.whereFromArg('N_Page <= "%s"', args.pageend),
		team and ('(Team1="%s" OR Team2="%s")'):format(team, team)
	}
	return util_table.concat(tbl, ' AND ')
end

function h.formatResults(result, pretty)
	local formatted = {}
	for i, row in ipairs(result) do
		formatted[i] = h.formatRow(row, pretty, i)
	end
	return formatted
end

function h.formatRow(row, pretty, rowNumber)
	local tbl = {
		data = {
			row.Tab and ('[[%s#%s|%s]]'):format(row._pageName,row.Tab,row.Tab) or '',
			pretty and m_team.onlyimage(row.Team1) or row.Team1,
			pretty and m_team.onlyimage(row.Team2) or row.Team2,
			pretty and ((row.Team1Score  or '?') .. ' - ' .. (row.Team2Score or '?')) or string.format(
				'%s - %s, %s',
				row.Team1Score,
				row.Team2Score,
				row.Winner
			),
		},
		classes = {
			[2] = row.Winner == '1' and { 'pbh-winner' } or {},
			[3] = row.Winner == '2' and { 'pbh-winner' } or {},
		},
		attrs = {}
	}
	for i, v in ipairs(CELLORDER) do
		local n = #tbl.data+1
		local c1 = v[1] and row[v[1]]
		local c2 = v[2] and row[v[2]]
		if not c1 and v.mandatory and not pretty then
			error(('Missing data in game from %s vs %s, line %s column %s'):format(row.Team1, row.Team2, rowNumber, i))
		end
		tbl.data[n] = pretty and h.printChampions(c1, c2) or ((c1 or '') .. (c2 and string.format(', %s',c2) or ''))
		tbl.classes[n] = mw.clone(v.classes) or {}
		h.printClasses(tbl.classes[n], c1, c2)
		tbl.attrs[n] = {}
		if c1 then tbl.attrs[n]['data-c1'] = h.champToClass(c1) end
		if c2 then tbl.attrs[n]['data-c2'] = h.champToClass(c2) end
	end
	if not pretty then
		for i = 1,2 do
			for j = 1,5 do
				tbl.data[#tbl.data+1] = row[('Team%sRole%s'):format(i,j)]
			end
		end
	end
	return tbl
end

function h.printChampions(a, b)
	return h.printChampion(a) .. h.printChampion(b)
end

function h.printChampion(champ)
	if not champ then return '' end
	local span = mw.html.create('span')
		:addClass('pbh-champion')
		:attr('data-champion',h.champToClass(champ))
		:wikitext(h.ChampionSprite(champ))
	return tostring(span)
end

function h.champToClass(champ)
	return lang:lc(champ:gsub("[ '.]",''))
end

function h.printClasses(tbl, a, b)
	a = a and h.champToClass(a)
	b = b and h.champToClass(b)
	tbl[#tbl+1] = b and string.format('pbh-%s pbh-%s',a,b) or (a and 'pbh-' .. a) or ''
	return
end

function h.printTable(outer, formatted, event, pretty)
	local div = outer:tag('div'):attr('id','pbh-outer'):addClass('wide-content-scroll')
	local tbl = div:tag('table')
		:addClass('wikitable')
		:attr('id','pbh-table')
	h.addHeader(tbl, event, pretty)
	for _, row in ipairs(formatted) do
		local tr = tbl:tag('tr')
		for i, v in ipairs(row.data) do
			local td = tr:tag('td')
			td:wikitext(v)
			for attr, val in pairs(row.attrs[i] or {}) do
				td:attr(attr,val)
			end
			for _, class in ipairs(row.classes[i] or {}) do
				td:addClass(class or '')
			end
		end
	end
	return tbl
end

function h.addHeader(tbl, event, pretty)
	local headers = h.getHeaders(pretty)
	local th = tbl:tag('tr'):tag('th'):attr('colspan',#headers)
	th:wikitext(string.format(
		'Pick and Ban Order - [[%s]]',
		event
	))
	if pretty then
		th:wikitext(string.format(
			' <span class="plainlinks">([%s View Text-Only Version])</span>',
			util_form.fullURL(FORM_INFO, { textonly = 'Yes', page = event })
		))
	end
	local tr = tbl:tag('tr')
	for k, v in ipairs(headers) do
		tr:tag('th'):wikitext(v)
	end
	return tr
end

function h.getHeaders(pretty)
	if pretty then
		return HEADERS
	else
		return util_table.mergeArrays(HEADERS, ROLE_HEADERS)
	end
end

function h.printColorPicker()
	local tbl = mw.html.create('div')
	tbl:attr('id','pbh-setcolor-box')
	for k, v in ipairs(COLORS) do
		tbl:tag('div')
			:addClass('pbh-setcolor')
			:addClass(k == 1 and 'pbh-current-tool' or '')
			:attr('data-pbhcolor', v)
			:css('background-color',v)
	end
	tbl:tag('div')
		:attr('id','pbh-remove')
		:attr('title','Clear')
	tbl:tag('div')
		:attr('id','pbh-removeall')
		:attr('title','Clear All')
	tbl:tag('div')
		:attr('id','pbh-undo')
		:attr('title','Undo Clear All')
		:css('display','none')
	return tbl
end
return p