Module:CircuitPoints

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_esports = require('Module:EsportsUtil')
local util_footnote = require('Module:FootnoteUtil')
local util_text = require('Module:TextUtil')
local util_vars = require('Module:VarsUtil')

local m_team = require('Module:Team')

local legend = require('Module:Legend')._main

local h = {}
local p = {}

function p.start(frame)
	local args = util_args.merge(true)
	util_vars.resetGlobalIndex('cp_place_counter')
	util_vars.resetGlobalIndex('cp_place')
	util_vars.setVar('standings-outof', args.netplaces)
	util_vars.setVar('hasRegionals', args.regionals)
	local tbl = mw.html.create('table')
		:addClass('wikitable circuitpoints')
	if util_args.castAsBool(args.sortable) then tbl:addClass('sortable') end
	h.printLegend(tbl, args)
	local tr = tbl:tag('tr')
	tr:tag('th'):wikitext('Seed')
	if util_args.castAsBool(args.path) then
		util_vars.setVar('path', 'Yes')
		tr:tag('th'):wikitext('Path')
	end
	tr:tag('th'):wikitext('Team')
	h.printPhaseColumns(tr, args.headings)
	tr:tag('th'):wikitext('Total')
	--h.printPhaseColumns(tr, args.regionals)
	local str = tostring(tbl)
	str = str:gsub('</table>$','')
	return str
end

function p.line(frame)
	local args = util_args.merge(true)
	local tr = mw.html.create('tr')
	if args.class then
		tr:addClass(util_esports.standingsClasses(args.class))
	end
	h.addPlace(tr, args)
	h.addTeam(tr, args.team, args.footnoteteamn)
	local total = h.addPoints(tr, args)
	--h.addRegionals(tr, args.regionals)
	return tr
end

function p.endtable(frame)
	return '</table>'
end

function h.printLegend(tbl, args)
	local tr = tbl:tag('tr')
	local colspan = (args.headings and #args.headings or 0) + 3
	local th = tr:tag('th'):attr('colspan', colspan)
	legend(th, args)
end

function h.printPhaseColumns(tr, headings)
	for _, v in ipairs(util_text.split(headings)) do
		tr:tag('th'):wikitext(v)
	end
end

function h.printRegionalsHeader(tr, regionals)
	if not regionals then return end
	tr:tag('td')
		:wikitext(regionals)
		:attr('colspan', 2)
end

function h.addPlace(tr, args)
	local place_default = util_vars.setGlobalIndex('cp_place_counter')
	if util_args.castAsBool(args.sameplaces) then
		place_default = util_vars.getGlobalIndex('cp_place')
	else
		util_vars.resetGlobalIndex('cp_place', place_default)
	end
	local p = args.p or place_default
	local seedIndex = util_vars.getObject('seeds') or {}
	tr:tag('td'):addClass(util_esports.standingsClasses(args.pclass)):wikitext(args.seed or seedIndex[p] or p)
	
	if util_vars.getBool('path') then
		local pathIndex = util_vars.getObject('paths') or {}
		tr:tag('td'):addClass(util_esports.standingsClasses(args.pclass)):wikitext(args.path or pathIndex[p])
	end
end

function h.addTeam(tr, team, footnote)
	local td = tr:tag('td'):addClass('circuitpoints-team'):wikitext(m_team.rightmediumlinked(team))
	if footnote then
		util_footnote.tagFootnotePlain(td, footnote)
	end
end

function h.addPoints(tr, args)
	local total = 0
	local points = util_text.split(args.points)
	local places = args.places and util_text.split(args.places) or {}
	local netplaces = args.netplaces and util_text.split(args.netplaces) or {}
	local classes = args.classes and util_text.split(args.classes) or {}
	for i, p in ipairs(points) do
		local td = tr:tag('td'):wikitext(p)
		if classes[i] then
			td:addClass(util_esports.standingsClass(classes[i]))
		end
		local gradient = h.gradient(places[i], netplaces[i])
		if gradient then
			td:css('background-image', gradient)
		end
		total = total + (tonumber(p) or 0)
	end
	if args.total then
		total = args.total
	end
	tr:tag('td'):wikitext(total, h.regionalsSeed(args.regionals))
end

function h.gradient(str, outof)
	local num = tonumber(str or '')
	if not num then return nil end
	local total = outof and tonumber(outof) or tonumber(util_vars.getVar('standings-outof'))
	if not total then
		error('Attempted to give places without a total number of places')
	end
	local css = "linear-gradient(rgba(0,0,0,%s),rgba(0,0,0,%s)"
	local percent = (1 - (num / total)) * .3
	return css:format(percent, percent)
end

function h.regionalsSeed(regionals)
	if true then return nil end
	if not regionals then return nil end
	local ret = mw.html.create('abbr')
		:wikitext(('(#%s)'):format(regionals))
		:attr('title', 'Regionals Seed')
	return ' ', tostring(ret)
end

-- function h.addRegionals(tr, regionals)
-- 	if not util_vars.getBool('hasRegionals') then return end
-- 	for _, n in ipairs(util_text.split(regionals or '')) do
-- 		tr:tag('td'):wikitext(regionals)
-- 	end
-- end

return p