[checked revision] | [checked revision] |
(replace with the contents of Module:Role2) |
([ST] fulllength support) |
||
(22 intermediate revisions by the same user not shown) | |||
Line 9: | Line 9: | ||
local lang = mw.getLanguage('en') |
local lang = mw.getLanguage('en') |
||
− | local p = require('Module:EntityAbstract'): |
+ | local p = require('Module:EntityAbstract'):finalExtends() |
local h = {} |
local h = {} |
||
Line 33: | Line 33: | ||
function p:init(str, opts) |
function p:init(str, opts) |
||
if not opts then opts = {} end |
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) |
||
⚫ | |||
self:super('init', str, 'Role') |
self:super('init', str, 'Role') |
||
self.modifier = h.determineModifierFromInput(opts) |
self.modifier = h.determineModifierFromInput(opts) |
||
− | self.defaultlength = ' |
+ | self.defaultlength = 'name' |
if self.modifier and self.is_nil then |
if self.modifier and self.is_nil then |
||
self.is_nil = false |
self.is_nil = false |
||
Line 54: | Line 48: | ||
role = str, |
role = str, |
||
store = str, |
store = str, |
||
− | + | sentence = (str or ''):lower(), |
|
− | + | prep = 'for', |
|
article = 'a', |
article = 'a', |
||
sortnumber = 70, |
sortnumber = 70, |
||
Line 69: | Line 63: | ||
if self.is_nil then return end |
if self.is_nil then return end |
||
if opts.skip_prefix then return self:super('name', opts) end |
if opts.skip_prefix then return self:super('name', opts) end |
||
⚫ | |||
if not self:hasPrefix() 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 len = opts.len or self.defaultlength |
||
Line 76: | Line 71: | ||
-- always return short when printing a modifier first unless we override |
-- always return short when printing a modifier first unless we override |
||
-- (eg if sentence) |
-- (eg if sentence) |
||
− | self:get(MODIFIER_OVERRIDES[len] and len or 'short') |
+ | self:get(((opts.fulllength or MODIFIER_OVERRIDES[len]) and len) or 'short') |
} |
} |
||
return util_table.concat(tbl, '') |
return util_table.concat(tbl, '') |
||
Line 85: | Line 80: | ||
local modSortNumber = self:hasPrefix() and MODIFIERS[self.modifier].sortnumber or 10 |
local modSortNumber = self:hasPrefix() and MODIFIERS[self.modifier].sortnumber or 10 |
||
return util_math.padleft(self:get('sortnumber') + modSortNumber, 2) |
return util_math.padleft(self:get('sortnumber') + modSortNumber, 2) |
||
⚫ | |||
+ | |||
⚫ | |||
+ | if self.is_nil then return nil end |
||
+ | if not opts then opts = {} end |
||
+ | local opts2 = mw.clone(opts) |
||
+ | opts2.len = 'sentence' |
||
⚫ | |||
end |
end |
||
Line 108: | Line 111: | ||
end |
end |
||
− | function p: |
+ | function p:image(opts) |
+ | -- override for wikis that don't actually use roles |
||
⚫ | |||
+ | if self.is_nil then return nil end |
||
− | -- it's desired to have a role stored as a single field in cargo |
||
+ | if self.vars.name == 'Player' and not self.modifier then return nil end |
||
− | -- 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 |
||
⚫ | |||
− | end |
||
− | |||
− | -- @staticmethod |
||
⚫ | |||
− | if not str:find('::') then |
||
− | return str, opts |
||
− | end |
||
− | modifier, str = str:match('(.*)::(.*)') |
||
− | opts.modifier = modifier |
||
⚫ | |||
end |
end |
||
Line 133: | Line 125: | ||
end |
end |
||
− | function p: |
+ | function p:getSpriteFallback(opts) |
if self:isStaff() and self.unknown then |
if self:isStaff() and self.unknown then |
||
− | -- we'll overwrite the display so we can send any sprite we want for display |
||
return 'Staff' |
return 'Staff' |
||
end |
end |
||
− | + | return self:super('getSpriteFallback', opts) |
|
− | return spriteKey |
||
end |
end |
||
Latest revision as of 09:57, 23 October 2020
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'):finalExtends()
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(((opts.fulllength or 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:getSpriteFallback(opts)
if self:isStaff() and self.unknown then
return 'Staff'
end
return self:super('getSpriteFallback', opts)
end
return p