Module:PicksAndBans

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

To edit the documentation or categories for this module, click here.

For usage information & copyable code, please see {{PicksAndBans}} or {{PicksAndBansS7}}.


local util_args = require('Module:ArgsUtil')
local util_cargo = require('Module:CargoUtil')
local util_html = require('Module:HTMLUtil')
local util_vars = require('Module:VarsUtil')
local util_table = require('Module:TableUtil')
local util_toggle = require('Module:ToggleUtil')

local m_role = require('Module:Role')
local m_team = require('Module:Team')
local m_champion = require('Module:Champion')
local Sprite = require('Module:Sprite').sprite

local SETTINGS = mw.loadData('Module:PicksAndBans/Settings')

local lang = mw.getLanguage('en')

local NOROLES = mw.html.create()
	:tag("td"):attr("colspan","2")
	:wikitext("''Pick''"):done()

local TOGGLES = {
	one = {
		show_attr = '.pb-w%s-g%s',
		hide_attr = '.pb-showbutton-w%s-g%s',
		show_class = 'pb-allw pb-w%s-allg pb-w%s-g%s',
		hide_class = 'pb-showbutton-allw pb-showbutton-w%s-allg pb-showbutton-w%s-g%s',
	},
	tab = {
		show_attr = '.pb-w%s-allg',
		hide_attr = '.pb-showbutton-w%s-allg',
		show_class = 'pb-allw pb-w%s-allg',
		hide_class = 'pb-showbutton-allw pb-showbutton-w%s-allg',
		cssclass = 'expand-contract-button-no-margin',
	},
	all = {
		show_attr = '.pb-allw',
		hide_attr = '.pb-showbutton-allw',
		show_class = 'pb-allw',
		hide_class = 'pb-showbutton-allw',
		show_text = 'Show Entire Page',
		hide_text = 'Hide Entire Page',
	},
	row = 'pb-allw pb-w%s-allg pb-w%s-g%s toggle-section-hidden'
}

local h = {}

function h.ChampionSprite(id, isBackwards)
	return Sprite{
		id,
		backwards = isBackwards,
		size = '20',
		type = 'Champion',
		lengthkey = 'display',
		nolink = 'Yes',
	}
end

function h.RoleSprite(id)
	return Sprite{
		id,
		size = '15',
		type = 'Role',
		notext = 'Yes',
		nolink = 'Yes',
	}
end

------------------------------------------------------------------
-- Start Helper Functions
------------------------------------------------------------------

function h.makeToggles(frame)
	local n = util_vars.setGlobalIndex('pbn', frame)	
	local section = util_vars.getGlobalIndex('pb_N_TabInPage_display', frame)
	TOGGLES.one.show_attr = TOGGLES.one.show_attr:format(section, n)
	TOGGLES.one.hide_attr = TOGGLES.one.hide_attr:format(section, n)
	TOGGLES.one.show_class = TOGGLES.one.show_class:format(section, section, n)
	TOGGLES.one.hide_class = TOGGLES.one.hide_class:format(section, section, n)
	TOGGLES.row = TOGGLES.row:format(section, section, n)
end

function h.getThisGame(args)
	if util_args.castAsBool(args.game1) then
		return 1
	end
	if args.game then
		return tonumber(args.game)
	else
		local total = (tonumber(args.team1score) or 0) + (tonumber(args.team2score) or 0)
		return total
	end
end

function h.setGame1Vars(frame)
	util_vars.setGlobalIndex('pb_N_MatchInPage', frame)
	util_vars.resetGlobalIndex('pb_N_GameInMatch')
	util_vars.setGlobalIndex('pb_N_MatchInTab', frame)
end

function h.parse(args, frame)
	local vars = h.getIndexingVars()
	local data = {
		'',
		_table = 'PicksAndBansS7',
		Team1 = m_team.teamlinkname(args.team1),
		Team2 = m_team.teamlinkname(args.team2),
		Winner = args.winner,
		Team1Score = args.team1score,
		Team2Score = args.team2score,
		OverviewPage = util_vars.getVar('overviewpage'),
		UniqueLine = mw.title.getCurrentTitle().text .. '_' .. vars.N_GameInPage,
	}
	util_table.merge(data, vars)
	for k, v in pairs(SETTINGS.roles) do
		data[k] = args[v] and m_role.rolename(args[v])
	end
	for k, v in pairs(SETTINGS.champions) do
		data[k] = args[v] and m_champion.championname(args[v])
	end
	return data
end

function h.getIndexingVars()
	local tbl = {
		Tab = util_vars.getVar('pbTabName'),
		N_Page = util_vars.getVar('tabThis'),
		N_TabInPage = util_vars.getGlobalIndex('pb_N_TabInPage'),
		N_GameInPage = util_vars.setGlobalIndex('OverallPickBan'),
		N_GameInTab = util_vars.setGlobalIndex('pb_N_GameInTab'),
		N_MatchInPage = util_vars.getGlobalIndex('pb_N_MatchInPage'),
		N_MatchInTab = util_vars.getGlobalIndex('pb_N_MatchInTab'),
		N_GameInMatch = util_vars.setGlobalIndex('pb_N_GameInMatch'),
	}
	return tbl
end

function h.formatRows(data, lookup, noroles)
	formatted = {}
	for k, v in ipairs(lookup.picks) do
		formatted['pick' .. k] = h.printPickRow(
			data['Team1Pick' .. k],
			data['Team2Pick' .. k],
			noroles and NOROLES or h.roles(data['Team1Role' .. k], data['Team2Role' .. k]),
			v
		)
	end
	for k, v in ipairs(lookup.bans) do
		formatted['ban' .. k] = h.printBanRow(
			data['Team1Ban' .. k],
			data['Team2Ban' .. k],
			v
		)
	end
	return formatted
end

function h.roles(blue, red)
	return mw.html.create()
		:tag("td")
			:addClass('pb-role')
			:wikitext(h.RoleSprite(blue or 'Unknown'))
	:done()
		:tag("td")
		:addClass('pb-role')
		:wikitext(h.RoleSprite(red or 'Unknown'))
	:done()
end

function h.makeWidthRow()
	local tbl = mw.html.create("tr"):addClass('set-widths')
		:tag("td"):css('width','calc(50% - 50px)'):done()
		:tag("td"):css('width','25px'):done()
		:tag("td"):css('width','25px'):done()
		:tag("td"):css('width','calc(50% - 50px)'):done()
	return tbl
end

function h.makeTopRow(team1, team2, footnotes)
	team1 = "'''" .. m_team.leftshortlinked(team1) .."'''" .. footnotes[1]
	team2 = "'''" .. m_team.rightshortlinked(team2) .."'''" .. footnotes[2]
	local tbl = mw.html.create("tr")
	tbl:tag("td")
		:attr("colspan","4")
		:css({ ["padding-left"] = "0", ["padding-right"] = "0" })
		:node(util_html.vsAlign(team1, team2))
	:done()
	return tbl
end

function h.makeScoreRow(team1score, team2score, winner)
	local tbl = mw.html.create("tr")
	local td1 = tbl:tag("td")
		:wikitext(team1score or '')
	local td_toggle = tbl:tag('td')
		:attr('colspan',2)
	util_toggle.printToggleButton(td_toggle, TOGGLES.one)
	local td2 = tbl:tag("td")
		:wikitext(team2score or '')
	if winner == 1 then
		td1:addClass('pb-winner')
	elseif winner == 2 then
		td2:addClass('pb-winner')
	end
	return tbl
end

function h.printBanRow(champ1, champ2, borderclass)
	tr = mw.html.create('tr')
		:addClass(TOGGLES.row)
		:addClass(borderclass)
		:tag("td"):addClass("pb-ban"):addClass("pb-blue")
			:wikitext(h.ChampionSprite(champ1, true)):done()
		:tag("td"):attr("colspan","2")
			:wikitext("''Ban''"):done()
		:tag("td"):addClass("pb-ban"):addClass("pb-red")
			:wikitext(h.ChampionSprite(champ2)):done()
	:done()
	return tr
end

function h.printPickRow(champ1, champ2, rolecells, borderclass)
	tr = mw.html.create('tr')
		:addClass(TOGGLES.row)
		:addClass(borderclass.row or '')
		:tag("td"):addClass("pb-pick"):addClass("pb-blue"):addClass(borderclass.blue or '')
			:wikitext(h.ChampionSprite(champ1, true)):done()
		:node(rolecells)
		:tag("td"):addClass("pb-pick"):addClass("pb-red"):addClass(borderclass.red or '')
			:wikitext(h.ChampionSprite(champ2)):done()
	:done()
	return tr
end

function h.storeCargo(data, nocargo)
	if util_args.castAsBool(nocargo) then return end
	if mw.title.getCurrentTitle():inNamespace(0) then
		util_cargo.store(data)
	end
end

function h.makeErrors(text, re, ce, ignore, frame)
	if util_args.castAsBool(ignore) then return end
	if re == 'Yes' then
		 text[#text+1] = frame:expandTemplate{ title = "Important", args = { "Role Error ([[Help:Role Error|?]])[[Category:Picks And Bans Pages With Role Errors]]" } }
	end
	if ce == 'Yes' then
		text[#text+1] = frame:expandTemplate{ title = "Important", args = {"Champ Error ([[Help:Champion Error|?]])[[Category:Picks And Bans Pages With Champion Errors]]"} }
	end
	return
end

------------------------------------------------------------------
-- Start Main Functions
------------------------------------------------------------------

local p = {}

function p.printButton(frame)
	local args = util_args.merge(true)
	if util_args.castAsBool(args.name) then
		local section = util_vars.setGlobalIndex('pb_N_TabInPage_display', frame)
		if not util_args.castAsBool(args.notab) then
			util_vars.setGlobalIndex('pb_N_TabInPage', frame)
			util_vars.resetGlobalIndex('pb_N_GameInTab')
			util_vars.resetGlobalIndex('pb_N_MatchInTab')
			util_vars.setVar('pbTabName', args.name or section, frame)
		end
		util_toggle.prepDataByWeek(TOGGLES.tab, section)
		TOGGLES.tab.show_text = 'Show ' .. (args.display or args.name)
		TOGGLES.tab.hide_text = 'Hide ' .. (args.display or args.name)
		return util_toggle.printSectionToggler(nil, TOGGLES.tab), util_html.clear()
	else
		return util_toggle.printSectionToggler(nil, TOGGLES.all), util_html.clear()
	end
end

function p.picksAndBansForfeit(frame)
	local args = util_args.merge(true)
	local thisgame = h.getThisGame(args)
	if thisgame == 1 then h.setGame1Vars(frame) end
	h.makeToggles(frame)
	local footnotes = { 
		util_html.makeFootnoteN(args.team1footnoten),
		util_html.makeFootnoteN(args.team2footnoten)
	}
	local tbl = mw.html.create('table')
		:addClass("wikitable")
		:addClass('pb')
		:css('width', args.width)
	if thisgame > 1 then tbl:addClass('pb-game-2') end
	tbl:node(h.makeWidthRow())
	tbl:node(h.makeTopRow(args.team1, args.team2, footnotes))
	tbl:node(h.makeScoreRow(args.team1score, args.team2score, tonumber(args.winner) or '', toggles))
	tbl:tag('tr')
		:tag('td')
			:attr('colspan','4')
			:addClass(TOGGLES.row)
			:wikitext("Game was forfeited.")
	local vars = h.getIndexingVars() -- so that we don't mess up the flow of the entire page
	return tbl
end

function p.picksAndBansBlind(frame)
	local args = util_args.merge(true)
	local thisgame = h.getThisGame(args)
	if thisgame == 1 then h.setGame1Vars(frame) end
	h.makeToggles(frame)
	local footnotes = { 
		util_html.makeFootnoteN(args.team1footnoten),
		util_html.makeFootnoteN(args.team2footnoten)
	}	
	rolecells = {}
	for i = 1, 5 do
		rolecells[i] = mw.html.create()
			:tag("td"):wikitext(h.RoleSprite("Scoreboard" .. i)):done()
			:tag("td"):wikitext(h.RoleSprite("Scoreboard" .. i)):done()
	end
	
	local tbl = mw.html.create('table')
		:addClass("wikitable")
		:addClass('pb')
		:css('width', args.width)
	if thisgame > 1 then tbl:addClass('pb-game-2') end
	tbl:node(h.makeWidthRow())
	tbl:node(h.makeTopRow(args.team1, args.team2, footnotes))
	tbl:node(h.makeScoreRow(args.team1score, args.team2score, tonumber(args.winner) or ''))
	
	for i = 1, 5 do
		tbl:node(h.printPickRow(
			m_champion.championname(args["bluepick" .. i]),
			m_champion.championname(args["red_pick" .. i]),
			rolecells[i],
			{}
		))
	end
	
	return tbl
end

function p.picksAndBansS7(frame)
	local args = util_args.merge(true)
	local thisgame = h.getThisGame(args)
	if thisgame == 1 then h.setGame1Vars(frame) end
	local data = h.parse(args, frame)
	local noroles = util_args.castAsBool(args.noroles)
	h.makeToggles(frame)
	local lookup = SETTINGS.cells.S7
	local formatted = h.formatRows(data, lookup, noroles)
	
	local footnotes = { 
		util_html.makeFootnoteN(args.team1footnoten),
		util_html.makeFootnoteN(args.team2footnoten)
	}
	
	local tbl = mw.html.create('table')
		:addClass("wikitable")
		:addClass('pb')
		:css('width', args.width)
	if thisgame > 1 then tbl:addClass('pb-game-2') end
	
	tbl:node(h.makeWidthRow())
	tbl:node(h.makeTopRow(args.team1, args.team2, footnotes))
	tbl:node(h.makeScoreRow(args.team1score, args.team2score, tonumber(args.winner) or ''))
	
	for _, v in ipairs(lookup.order) do
		tbl:node(formatted[v])
	end
	
	h.storeCargo(data, args.nocargo)
	local text = { tostring(tbl) }
	h.makeErrors(text, not noroles and args.hasroleerror, args.haschampionerror, args.ignorewarnings, frame)
	return table.concat(text,"\n")
end

function p.picksAndBans(frame)
	local args = util_args.merge(true)
	local thisgame = h.getThisGame(args)
	if thisgame == 1 then h.setGame1Vars(frame) end
	local data = h.parse(args, frame)
	local noroles = lang:lc(args.noroles or '') == 'yes'
	h.makeToggles(frame)
	local lookup = SETTINGS.cells.S3
	
	local footnotes = { 
		util_html.makeFootnoteN(args.team1footnoten),
		util_html.makeFootnoteN(args.team2footnoten)
	}
	
	local tbl = mw.html.create('table')
		:addClass("wikitable")
		:addClass('pb')
		:css('width', args.width)
	if thisgame > 1 then tbl:addClass('pb-game-2') end
	
	tbl:node(h.makeWidthRow())
	tbl:node(h.makeTopRow(args.team1, args.team2, footnotes))
	tbl:node(h.makeScoreRow(args.team1score, args.team2score, tonumber(args.winner) or ''))
	
	local nBans = args.nBans or 3
	for i = 1, nBans do
		local style = (i == nBans) and "pb-border" or ""
		tbl:node(h.printBanRow(data['Team1Ban' .. i], data['Team2Ban' .. i], style))
	end
	for i = 1, 5 do
		tbl:node(h.printPickRow(
			data['Team1Pick' .. i],
			data['Team2Pick' .. i],
			noroles and NOROLES or h.roles(data['Team1Role' .. i], data['Team2Role' .. i]),
			lookup.picks[i]
		))
	end

	local text = { tostring(tbl) }
	h.makeErrors(text, not noroles and args.hasroleerror, args.haschampionerror, args.ignorewarnings, frame)
	return table.concat(text,"\n")
end

return p