Leaguepedia | League of Legends Esports Wiki
Advertisement

Edit the documentation or categories for this module.


local util_args = require('Module:ArgsUtil')
local util_html = require("Module:HtmlUtil")
local util_map = require('Module:MapUtil')
local util_math = require("Module:MathUtil")
local util_table = require("Module:TableUtil")
local util_text = require("Module:TextUtil")
local util_vars = require("Module:VarsUtil")
local i18n = require("Module:I18nUtil")
local lang = mw.getLanguage('en')

local p = require('Module:EntityAbstract'):extends()
local h = {}

p.objectType = 'Role'
p.imagelength = 'role'
p.imagesizes = {
	default = 15
}
local MODIFIERS = {
	sub = {
		sortnumber = 20,
		default = 'Sub/',
		sentence = 'substitute ',
	},
	trainee = {
		sortnumber = 30,
		default = 'Trn/',
		sentence = 'trainee ',
	},
}

function p:init(str, opts)
	if not opts then opts = {} end
	if opts.serialized then
		-- this should only be required to happen from Cargo
		-- and in that case, we would be casting in CargoUtil/Wiki
		-- therefore let's not bother doing the string matching unless we have to
		str, opts = self:deserialize(str, opts)
	end
	self:super('init', str, 'Role')
	self.modifier = h.determineModifierFromInput(opts)
	self.defaultlength = 'store'
	if self.modifier and self.is_nil then
		self.is_nil = false
		self:super('init', self.modifier, 'Role')
		self.modifier = nil
	end
	if self.is_nil then return end
	if self.unknown then
		self.vars = {
			adjective = str,
			medium = str,
			short = str,
			role = str,
			store = str,
			rolelc = (str or ''):lower(),
			sentence = (str or '') .. ' for',
			article = 'a',
			sortnumber = 70,
			name = '',
			notaplayer = true,
		}
	end
	self.vars.notaplayer = util_args.nilToFalse(self.vars.notaplayer)
end

function p:name(length, opts)
	if not opts then opts = {} end
	if self.is_nil then return end
	if opts.skip_prefix then return self:super('name', length, opts) end
	if not self:hasPrefix() then return self:super('name', length, opts) end
	local tbl = {
		self.modifier and MODIFIERS[self.modifier][length] or MODIFIERS[self.modifier].default or '',
		
		-- always return short when printing a modifier first, no matter the context
		self:get('short')
	}
	return util_table.concat(tbl, '')
end

function p:sortnumber(opts)
	if self.is_nil then return end
	local modSortNumber = self:hasPrefix() and MODIFIERS[self.modifier].sortnumber or 10
	return util_math.padleft(self:get('sortnumber') + modSortNumber, 2)
end

function h.determineModifierFromInput(opts)
	if opts.modifier then return opts.modifier:lower() end
	
	-- legacy support when we said sub = true, trainee = true
	if opts.Sub or opts.sub then return 'sub' end
	if opts.Trainee or opts.trainee then return 'trainee' end
	return nil
end

function p:hasPrefix()
	return self.modifier
end

function p:isIngame()
	return not self:get('notaplayer')
end

function p:isStaff()
	return self:get('notaplayer')
end

function p:serialize()
	if self.is_nil then return nil, {} end
	-- it's desired to have a role stored as a single field in cargo
	-- but because roles have more than one piece of information (i.e. role + modifier)
	-- we need to be able to serialize and then deserialize later
	return ('%s::%s'):format(self.modifier, self:name())
end

-- @staticmethod
function p.deserialize(str, opts)
	if not str:find('::') then
		return str, opts
	end
	modifier, str = str:match('(.*)::(.*)')
	opts.modifier = modifier
	return str, opts
end

function p:getImageClass(opts)
	if self.modifier == 'sub' then return 'sub' end
	if self.modifier == 'trainee' then return 'trainee' end
	return nil
end

function p:getSpriteKey(opts)
	if self:isStaff() and self.unknown then
		-- we'll overwrite the display so we can send any sprite we want for display
		return 'Staff'
	end
	return self.vars[self.imagelength]
end

return p
Advertisement