Module:Role2

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'

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') if self.unknown then self.vars = { adjective = str, medium = str, short = str, role = str, rolelc = (str or ''):lower, sentence = (str or '') .. ' for', article = 'a', sortnumber = 70, name = '' }	end self.defaultlength = 'store' self.modifier = h.determineModifierFromInput(opts) end

function p:name(length, opts) if not opts then opts = {} 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 = { MODIFIERS[self.modifier][length] or MODIFIERS[self.modifier].default or '', self:get(length) }	return util_table.concat(tbl, '') end

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

function h.determineModifierFromInput(opts) if opts.modifier then return opts.modifier 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 -- 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

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

return p