Module:Role

From NBA 2K Esports Wiki
Jump to: navigation, search

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


For names see {{Rolenames}}m

Allowed settings:

  • |sub= (makes it a sub)
  • |sorted= (adds a display:none; sort number before the rest of the output)
  • |allowdefault= (allow the input to be displayed even if it's not recognized as a valid input, use for staff etc)

Call p.main() from MediaWiki via #invoke; do NOT call this from Lua though, instead call a style function directly.


local util_args = require('Module:ArgsUtil')
local util_math = require('Module:MathUtil')
local util_table = require('Module:TableUtil')

local LOOKUP = mw.loadData('Module:Rolenames')

local PREPEND = {
	sub = {
		long = 'substitute ',
		short = 'Sub/'
	},
	trainee = {
		long = 'trainee ',
		short = 'TRN/'
	},
	[''] = {
		long = '',
		short = ''
	}
}

local h = {}

function h.getInfo(str, settings)
	if str == '' then str = nil end
	settings.namelength = 'role'
	if settings.sub then
		settings.div = true
		if str then
			settings.sortnumber = 20
			settings.prepend = 'sub'
			settings.namelength = 'short'
		else
			settings.sortnumber = 10
			settings.prepend = ''
			str = 'substitute'
		end
	elseif settings.trainee then
		settings.div = true
		if str then
			settings.sortnumber = 40
			settings.prepend = 'trainee'
			settings.namelength = 'short'
		else
			settings.sortnumber = 30
			settings.prepend = ''
			str = 'trainee'
		end
	else
		if str then
			settings.sortnumber = 10
			settings.prepend = ''
		else
			str = ''
			settings.prepend = ''
		end
	end
	local default = {
		adjective = str,
		short = str,
		role = str,
		rolelc = string.lower(str or ''),
		sentence = (str or '') .. ' for',
		article = 'a',
		sortnumber = 70
	}
	settings.vars = util_args.lookupVars(str, LOOKUP, settings.allowdefault) or default
	settings.name = settings.vars[settings.namelength]
	return
end

function h.addImage(tbl, settings)
	
	tbl:attr('title',settings.vars.role)
		:addClass('role-icon')
	
	if settings.sub then
		local img = tbl:tag('div')
			:addClass('role-icon-sub-image')
		h.printIcon(img, settings.vars.role, settings.size)
		h.printLetter(tbl, 'S')
	elseif settings.trainee then
		local img = tbl:tag('div')
			:addClass('role-icon-sub-image')
		h.printIcon(img, settings.vars.role, settings.size)
		h.printLetter(tbl, 'T')
	else
		h.printIcon(tbl, settings.vars.role, settings.size)
	end
	return tbl
end

function h.printIcon(tbl, role, size)
	tbl:wikitext(h.iconText(role, size))
end

function h.iconText(role, size)
	return ('[[File:%srole icon.png|%s|link=]]'):format(role, size or '15px')
end

function h.printLetter(tbl, letter)
	if not letter then return end
	tbl:tag('div'):addClass('role-icon-sub-s'):wikitext(letter)
end

local p = {}

function p.main(frame)
	-- this should NEVER be called from Lua, only invoked from MW
	local args = util_args.merge(true)
	
	if not args[1] and not args.sub then
		return ''
	end
	
	local style = args[2] or 'rolename'
	
	return p[style](args[1], args)
end

function p.isplayer(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	return not settings.vars.notaplayer
end

function p.onlyimage(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	local tbl = mw.html.create('div')
	return tostring(h.addImage(tbl, settings))
end

function p.sentencemultiple(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	local tbl = {
		settings.vars.article,
		PREPEND[settings.prepend].long,
		settings.vars.sentence
	}
	return util_table.concat(tbl, ' ')
end

function p.rolelc(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	return settings.vars.rolelc
end

function p.article(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	return settings.vars.article
end

function p.sentence(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	local tbl = {
		PREPEND[settings.prepend].long,
		settings.vars.sentence
	}
	return util_table.concat(tbl, ' ')
end

function p.sortnumber(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	return util_math.padleft(settings.vars.sortnumber + settings.sortnumber, 2)
end

function p.roleonly(str, settings)
	-- Ignores whether it's a sub or not
	if not settings then settings = {} end
	h.getInfo(str, settings)
	local tbl = mw.html.create()
	tbl:wikitext(settings.vars.role)
	return tostring(tbl)
end

function p.shortname(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	local tbl = mw.html.create()
	tbl:wikitext(settings.vars.short)
	return tostring(tbl)
end

function p.mediumname(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	local tbl = mw.html.create()
	tbl:wikitext(settings.vars.medium)
	return tostring(tbl)
end

function p.rolename(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	local tbl = mw.html.create()
	tbl:wikitext(('%s%s'):format(PREPEND[settings.prepend].short, settings.name))
	return tostring(tbl)
end

function p.storename(str, settings)
	if not settings then settings = {} end
	h.getInfo(str, settings)
	return settings.vars.store
end

function p.default(str, settings)
	-- deprecated, do not use pls ty
	return p.rolename(str, settings)
end

return p