Module:RoleAbstract

From Call of Duty Esports Wiki
Jump to: navigation, search

Documentation for this module may be created at Module:RoleAbstract/doc

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 ',
	},
}
local MODIFIER_OVERRIDES = { sentence = true }

function p:init(str, opts)
	if not opts then opts = {} end
	self:super('init', str, 'Role')
	self.modifier = h.determineModifierFromInput(opts)
	self.defaultlength = 'name'
	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,
			short = str,
			role = str,
			store = str,
			sentence = (str or ''):lower(),
			prep = 'for',
			article = 'a',
			sortnumber = 70,
			name = str,
			notaplayer = true,
		}
	end
	self.vars.notaplayer = util_args.nilToFalse(self.vars.notaplayer)
end

function p:name(opts)
	if not opts then opts = {} end
	if self.is_nil then return end
	if opts.skip_prefix then return self:super('name', opts) end
	if self:isStaff() then return self:super('name', opts) end
	if not self:hasPrefix() then return self:super('name', opts) end
	local len = opts.len or self.defaultlength
	local tbl = {
		self.modifier and MODIFIERS[self.modifier][len] or MODIFIERS[self.modifier].default or '',
		
		-- always return short when printing a modifier first unless we override
		-- (eg if sentence)
		self:get(MODIFIER_OVERRIDES[len] and len or '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 p:sentence(opts)
	if self.is_nil then return nil end
	if not opts then opts = {} end
	local opts2 = mw.clone(opts)
	opts2.len = 'sentence'
	return ('%s %s'):format(self:name(opts2), self:get('prep'))
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:image(opts)
	-- override for wikis that don't actually use roles
	if self.is_nil then return nil end
	if self.vars.name == 'Player' and not self.modifier then return nil end
	return self:super('image', opts)
end

function p:getImageClass(opts)
	if not self:isIngame() then return nil end
	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
	local spriteKey = self:super('getSpriteKey', opts)
	return spriteKey
end

return p