Call of Duty Esports Wiki
Advertisement

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

Automated creation of tournament tabs templates without the user needing to use if statements or anything. See {{TournamentTabs}} for usage.

Dependencies: {{GetEventPageInfo}}m, {{SubpageSettings}}m, {{TabsHeader}}m, {{AutomatedTabs}}m

Eventually {{GetEventPageInfo}}m should be merged into this but not until it's no longer needed standalone.


local util_args = require('Module:ArgsUtil')
local util_title = require('Module:TitleUtil')

local settings = require('Module:SubpageSettings').tournaments
local util = require('Module:Util')
local pageinfo = require('Module:GetEventPageInfo')
local tabs = require('Module:AutomatedTabs')

local p = {}
function p.makeArgsIntoTables(args)
	local args2 = mw.clone(args) -- since we might have to modify some keys if they are numbers
	for key, value in pairs(args2) do
		key_str = tostring(key) or key
		if not p.doWeSkip(key) then
			args[key_str] = mw.text.split(value,'%s*%f[,%%],%s*')
			for k, v in ipairs(args[key_str]) do
				args[key_str][k] = args[key_str][k]:gsub('%%,',','):gsub('_',' ')
			end
		end
	end
end

function p.doWeSkip(key)
	if settings.dontconvert[key] then
		return true
	end
	for k, v in pairs(settings.dontconvert_patterns) do
		if string.find(key, v, 1, true) then
			return true
		end
	end
	return false
end

-- making json
function p.jsonFromArgs(args)
	if args.oldMD == 'Yes' then
		p.oldMD(args)
	end
	local tbl = {}
	p.jsonRecursion(tbl, args, nil)
	return tbl
end

function p.oldMD(args)
	-- oldMD is an arg to make a shortcut for every Match Details artificially becoming named
	-- Match Details & VODs
	for _, event in ipairs(args.events) do
		args[event .. '_names_Match Details'] = 'Match Details'
	end
	args['names_Match Details'] = 'Match Details'
	args['events_names_Match Details'] = 'Match Details'
	return
end

function p.jsonRecursion(tbl, args, path)
	-- names are made ready for output now but links are kept in canonical form until output
	local thispath = path or 'events'
	local links = mw.clone(args[thispath])
	p.processLinks(links)
	local names = mw.clone(args[thispath .. '_names'] or links)
	p.processNames(args, thispath, names, links)
	tbl.links = links
	tbl.names = names
	if args[thispath .. '_nooverview'] ~= 'Yes' then tbl.links[1] = 'Overview' end
	for k, v in ipairs(links) do
		local newpath = tabs.newIndex(path, v)
		if args[newpath] and not tbl[v] then
			tbl[v] = {}
		 	p.jsonRecursion(tbl[v],args,newpath)
		end
	end
	-- if we don't specify if it's specificaly not an overview page
	-- then we probably don't want any subpage for the first tab
	if args[thispath .. '_nooverview'] ~= 'Yes' then tbl.links[1] = '' end
	tbl.path = thispath -- for maintenance cargo table to be used when automating things
	return tbl
end

function p.processLinks(links)
	for k, link in ipairs(links) do
		links[k] = settings.lookup[mw.ustring.lower(link)] or link
	end
	return
end

function p.processNames(args, path, names, links)
	for key, name in ipairs(names) do
		names[key] = args[path .. '_names_' .. links[key]] or settings.names[name] or name
	end
	return
end

-- make navbox args
function p.getNavboxData(args)
	local navboxdata = {}
	for i, navbox in ipairs(args.navboxes) do
		local tm = args['navbox' .. i .. '_titlematch'] and args['navbox' .. i .. '_titlematch'][1]
		navboxdata[i] = {
			title = navbox,
			args = {},
			events = args['navbox' .. i .. '_events'] or ((not tm) and { showAll = true } or {}),
			titlematch = tm
		}
		for k,v in pairs(args['navbox' .. i .. '_keys'] or {}) do
			navboxdata[i].args[v] = args['navbox' .. i .. '_values'][k]
		end
	end
	return navboxdata
end

-- link manipulation
function p.linkAdjustments(args)
	local tbl = {}
	tbl.fr = p.linkAdjustments_makeFR(args.titlefind, args.titlereplace)
	tbl.cd = p.linkAdjustments_makeCD(args.currentdata_pages or {}, args.currentdata_values or {})
	return tbl
end

function p.linkAdjustments_makeFR(find, replace)
	-- in case the tournament has different base urls for different sections
	return find and replace and { find = find, replace = replace } or { find = {}, replace = {} }
end

function p.linkAdjustments_makeCD(pages, values)
	-- current data makes a different subpage from the overview focused
	local data = {}
	for k, v in ipairs(pages) do
		data[v] = util_title.concatSubpage(v,values[k])
	end
	return data
end

function p.getCategories()
	local title = mw.title.getCurrentTitle()
	if title.nsText == 'Template' then
		return '[[Category:Tournament Tabs]]'
	end
	return
end

-- main
function p.fromArgs(frame)
	local args = util_args.merge(true)
	if not args.basepage then
		error('Missing basepage parameter')
	end
	-- process data
	if not args.events then args.events = args.Overview end
	p.makeArgsIntoTables(args)
	local datadiv = pageinfo.getEventPageInfo(frame) -- for vardefines
	
	local data = {
		linkadjustments = p.linkAdjustments(args),
		navboxdata = args.navboxes and p.getNavboxData(args),
		corrdata = args.showCorr and args,
		after = p.getCategories(),
		basepage = args.basepage,
		tabstype = args.tabstype or 'header'
	}
	local json = args.events and p.jsonFromArgs(args)
	return tabs.main(json, data)
end

return p
Advertisement