Module:DeadzoneSettingsList

From Liquipedia Rocket League Wiki

local SettingsList = {}

local Arguments = require('Module:Arguments')
local Flag = require('Module:Flags')
local Logic = require('Module:Logic')
local String = require('Module:StringUtils')
local Team = require('Module:Team')
local Template = require('Module:Template')

local EARNINGS_NOTABLE = 5000

SettingsList.backgroundColors = {
	inactive='sapphire-bg',
	retired='gray-bg',
	banned='cinnabar-bg',
}

function SettingsList.getSettings()
	return mw.ext.LiquipediaDB.lpdb('settings', {
		conditions = '[[gamesettings::!]]',
		order = 'pagename asc',
		limit = 5000,
	})
end

function SettingsList.getPlayers(onlyNotable)
	local condition
	if Logic.readBoolOrNil(onlyNotable) == true then
		condition = '[[earnings::>'.. EARNINGS_NOTABLE ..']]'
	elseif Logic.readBoolOrNil(onlyNotable) == false then
		condition = '[[earnings::<'.. EARNINGS_NOTABLE ..']]'
	end
    condition = condition .. ' AND [[extradata_role::Player]]'

	return mw.ext.LiquipediaDB.lpdb('player', {
		conditions = condition,
		query = 'pagename, id, nationality, status, team',
		order = 'pagename asc',
		limit = 5000,
	})
end

local function basicColumn(displayName, argName)
	local name = argName or displayName
	return {
		title = displayName, func = function(player, settings)
			return settings[name:lower()]
		end
	}
end

local columns = {
	{
		title = 'Player', dataSort=function(player, settings) return player.id end, columnStyle = {['text-align'] = 'left'},
		func = function(player, settings) return (Flag.Icon({flag = player.nationality or '', shouldLink = true})) .. " <b>[["..player.pagename.."|"..player.id.."]]</b>" end
	},
	{
		title = 'Team', dataSort=function(player, settings) return player.team end, columnStyle = {['text-align'] = 'left'},
		func = function(player, settings) return (player.team and mw.ext.TeamTemplate.teamexists(player.team) and Team.short(mw.getCurrentFrame(), player.team) or nil) end
	},
	basicColumn('Deadzone Shape', 'shape'),
	basicColumn('Deadzone'),
	basicColumn('Dodge Deadzone', 'dodge_deadzone'),
	basicColumn('Aerial Sensitivity', 'aerial_sens'),
	basicColumn('Steering Sensitivity', 'steering_sens'),
}

function SettingsList.create(frame)
	local args = Arguments.getArgs(frame)

	local limit = args.limit and tonumber(args.limit) or math.huge
	local offset = args.offset and tonumber(args.offset) or 0

	-- Fetch all settings
	local settingsData = SettingsList.getSettings()

	-- Fetching all players at once and caching it, is a lot faster than getting one at a time later with only those with settings setup
	local playerData = SettingsList.getPlayers(args.notable)
	local playerLookup = {}
	for _, player in pairs(playerData) do
		playerLookup[player.pagename] = player
	end

	local wrapper = mw.html.create('div')
		:addClass('table-responsive')
	local output = wrapper:tag('small'):tag('table')
		:addClass('wikitable'):addClass('rl-responsive-table-sortable'):css('width', '100%')

	-- Headers
	local tr = output:tag('tr')
	for _, column in ipairs(columns) do
		tr:tag('th'):css(column.headerStyle or {}):wikitext(column.title)
	end

	-- Player row
	-- Can't use the k of the for loop, or the lpdb query as limit should only be on notable players when notable is set.
	-- We can't filter 'lpdb_settings' on earnings so we do it this way.
	local count = 0
	limit = limit + offset
	for _, settings in ipairs(settingsData) do
		local player = playerLookup[settings.pagename]
		if player then
			if not settings.gamesettings then settings.gamesettings = {} end
			settings.gamesettings.type = settings.type
			if count >= offset then
				local tr = output:tag('tr'):css('text-align','center')
				if player.status then
					tr:addClass(SettingsList.backgroundColors[player.status:lower()])
				end
				for _, column in ipairs(columns) do
					local td = tr:tag('td'):css(column.columnStyle or {}):addClass(column.columnClass)
					local val = column.func(player, settings.gamesettings)
					td:wikitext(Logic.emptyOr(val))
					td:attr('data-sort-value', column.dataSort and column.dataSort(player, settings.gamesettings) or nil)
				end
			end
			count = count + 1
			if count == limit then
				break
			end
		end
	end
	
	return tostring(wrapper:done())
end

return SettingsList