Module:Control settings table

From Liquipedia Rocket League Wiki

local p = {} -- p stands for package

local getArgs = require('Module:Arguments').getArgs
local args

function p.create(frame)
	args = getArgs(frame)
	
	local tableClass = args['class']
	local tableWidth = args['width']
	local tableCellWidth = true
	local footer = args['footer']
	local bgcolor = args['bgcolor']
	local header
	if args['header'] then
		header = args['header']
	else
		header = "'''[[Control settings]]"
		if args['ref'] then
			header = header .. frame:callParserFunction{ name = '#tag', args = { 'ref', args['ref'] } }
		elseif args['insidesource'] then
			header = header .. frame:callParserFunction{ name = '#tag', args = { 'ref', frame:expandTemplate{ title = 'inside source' } } }
		end
		header = header .. " <small>([[List of player control settings|list of]])</small>'''"
	end
	
	local controller = string.lower(args['controller'])
	local powerslide = args['powerslide']
	local air_roll = args['air_roll']
	local air_roll_left = args['air_roll_left']
	local air_roll_right = args['air_roll_right']
	local boost = args['boost']
	local jump = args['jump']
	local ballcam = args['ballcam']
	local brake = args['brake']
	local throttle = args['throttle']
	
	local air_roll_title = 'Air roll'
	if air_roll_left or air_roll_right then
		air_roll_title = 'Air roll&nbsp;(left/right)'
	end

	local columns = {
		{title='Powerslide', value=powerslide},
		{title=air_roll_title, func=p.air_roll, value={air_roll=air_roll, air_roll_left=air_roll_left, air_roll_right=air_roll_right}},
		{title='Boost', value=boost},
		{title='Jump', value=jump},
		{title='Ball cam', value=ballcam},
		{title='Brake', value=brake},
		{title='Throttle', value=throttle},
	}

	local wrapper = mw.html.create('div')
	if controller or powerslide or air_roll or air_roll_left or air_roll_right or boost or jump or ballcam or brake or throttle then
		local t = wrapper:tag('table')
			:addClass('rl-responsive-table')
		if tableClass then
			t:addClass(tableClass)
		end
		if tableWidth then
			t:css('width', tableWidth)
		end
		if tableCellWidth then
			t:css('table-layout', 'auto')
		end
		if header then
			local captionHeader = t:tag('caption')
				:css('caption-side', 'top')
				:wikitext(header)
		end
		if footer then
			local captionFooter = t:tag('caption')
				:css('caption-side', 'bottom')
				:wikitext(footer)
		end

		local tr = t:tag('tr')
		for k,v in ipairs(columns) do
			if v.value then
				local th = tr:tag('th')
				if args['width' .. tostring(k)] then -- This will never be fulfilled based on how {{Controls_settings_table}} is used
					th:css('width', args['width' .. tostring(k)])
				end
				th:attr('scope', 'col')
				th:wikitext(v.title)
			end
		end
		tr = t:tag('tr')
		if bgcolor then
			tr:css('background-color', bgcolor)
		end
		for k,v in ipairs(columns) do
			if v.value then
				local td = tr:tag('td')
				if args['leftalign' .. tostring(k)] then -- This will never be fulfilled based on how {{Controls_settings_table}} is used
					td:addClass('rl-responsive-table-left-align')
				end
				td:attr('data-label', v.title)
				if not v.func then
					v.func = p.buttons
				end
				td:wikitext(v.func(controller, v.value, v.title))
			end
		end
	end

	local lpdb_controls = mw.ext.LiquipediaDB.lpdb_settings('settings_' .. mw.title.getCurrentTitle().text, {
		name = mw.title.getCurrentTitle().text,
		keys = mw.ext.LiquipediaDB.lpdb_create_json( {
			lastupdated = args['date'],
			reference = args['ref'],
			powerslide = args['powerslide'],
			air_roll = args['air_roll'],
			air_roll_left = args['air_roll_left'],
			air_roll_right = args['air_roll_right'],
			jump = args['jump'],
			ballcam = args['ballcam'],
			brake = args['brake'],
			throttle = args['throttle'],
			boost = args['boost']
		} ),
		type = string.lower(args['controller'])
	} )
	
	return tostring(wrapper:done())
end

function p.air_roll(controller, keys, name)
	local ret = ''
	if keys.air_roll_left or keys.air_roll_right then
		if keys.air_roll then
			ret = ret .. p.buttons(controller, keys.air_roll, name)
		else
			ret = ret .. '&nbsp;-&nbsp;'
		end
		ret = ret .. '&nbsp;('
		if keys.air_roll_left then
			ret = ret .. p.buttons(controller, keys.air_roll_left, name)
		else
			ret = ret .. '&nbsp;-'
		end
		ret = ret .. '&nbsp;/&nbsp;'
		if keys.air_roll_right then
			ret = ret .. p.buttons(controller, keys.air_roll_right, name)
		else
			ret = ret .. '&nbsp;-'
		end
		ret = ret .. ')'
	else 
		ret = ret .. p.buttons(controller, keys.air_roll, name)
	end
	return ret
end

function p.buttons(controller, button, name)
	if controller == 'kbm' then
		return '<kbd>' .. button .. '</kbd>'
	else
		return '[[File:' .. mw.getCurrentFrame():expandTemplate{ title = 'Button translation', args = { controller, string.lower(button) } } .. '.svg|' .. button .. '|link=]]'
	end
end

return p